Let's say I have 3 features in my ols model as below.

model = sm.ols(formula='y ~ a + b + c', data=df)

Question 1. If I want to transform the feature 'a' as polynomial 3 degree, then should I just add I(a**2) and I(a**3)?

Question 2. What about if I want to transform the feature 'a' as 3 degree and the feature 'b' as 2 degree.


  • 1
    $\begingroup$ Your question 1 sounds correct, and that same idea would apply to question 2. Polynomials usually have an offset term, and you should have one in this case. $\endgroup$ Sep 19 '19 at 10:36
  • $\begingroup$ Thanks, James. Do you mean I should add one constant on exog as part of features? $\endgroup$ Sep 19 '19 at 22:33
  • 1
    $\begingroup$ Yes. The docs at statsmodels.org/dev/generated/… discuss "hasconst" to let the software know that the formula includes a constant (offset) term on the Right Hand Side (RHS in the docs). $\endgroup$ Sep 19 '19 at 23:23
  • $\begingroup$ Got it. Cheers! $\endgroup$ Sep 19 '19 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.