python stats models - quadratic term in regression

datavoredan picture datavoredan · Aug 13, 2015 · Viewed 15k times · Source

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?

Answer

Nathaniel J. Smith picture Nathaniel J. Smith · Apr 11, 2016

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)