Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python stats models - quadratic term in regression

Tags:

I have the following linear regression:

import statsmodels.formula.api as sm  model = sm.ols(formula = 'a ~ b + c', data = data).fit() 

I want to add a quadratic term for b in this model.

Is there a simple way to do this with statsmodels.ols?
Is there a better package I should be using to achieve this?

like image 776
datavoredan Avatar asked Aug 13 '15 03:08

datavoredan


1 Answers

The simplest way is

model = sm.ols(formula = 'a ~ b + c + I(b**2)', data = data).fit() 

The I(...) basically says "patsy, please stop being clever here and just let Python handle everything inside kthx". (More detailed explanation)

like image 71
Nathaniel J. Smith Avatar answered Dec 27 '22 05:12

Nathaniel J. Smith