1

As I understood, Regression equation can be calculated by this functions:

import statsmodels.formula.api as smf
fg = smf.ols(formula='X ~ Y', data=data).fit()

we can also calculate from numpy polyfit function.

numpy.polyfit(x, y, degree)

as we can change the degree in numpy polyfit.

In ols function we can also add other independent variables as given below:

fg = smf.ols(formula='X ~ Y+Y1+Y2', data=data).fit()

So my question can we change the order/degree of fit in ols function ? or can we add another independent variables in numpy polyfit function?

lucasnadalutti
  • 5,818
  • 1
  • 28
  • 48
bikuser
  • 2,013
  • 4
  • 33
  • 57

1 Answers1

2

In the case of the statsmodels ability that you mention, formulae are specified using the patsy language (see http://patsy.readthedocs.io/en/latest/). Thus, for instance, that first invocation that you used could instead have been the following.

fg = smf.ols(formula='X ~ Y + Y**2', data=data).fit()

or

fg = smf.ols(formula='X ~ log(Y)', data=data).fit()
Bill Bell
  • 21,021
  • 5
  • 43
  • 58
  • So we cant add another independent variables in ols? am I correct?. If I want to change order from 1 to 2 or 3. – bikuser Nov 22 '16 at 16:16
  • Sure, you can. See http://statsmodels.sourceforge.net/devel/example_formulas.html. I intended to ask, what do you mean by order? – Bill Bell Nov 22 '16 at 16:23
  • I mean order (or degree) 1 is for linear, order 2 is for Quadratic, order 3 is Cubic and so on.. – bikuser Nov 22 '16 at 16:27
  • Yes, you could have, for example: `X ~ Y1**2 + Y2 + Y3**2`. Is that what you mean? – Bill Bell Nov 22 '16 at 16:31
  • Sorry I did not understand..so here: X ~ Y1**2 + Y2 + Y3**2 means Y1, Y2 and Y3 are three independent variables with 2degree polynomial function with X? – bikuser Nov 22 '16 at 16:36
  • Y1, Y2, Y3 are the independent variables. X is a linear function of Y2, and a quadratic function of each of the other variables. – Bill Bell Nov 22 '16 at 16:40
  • Thank you :) Now I understand. – bikuser Nov 22 '16 at 16:42