2

I use pandas and statsmodels to do linear regression. However, i can't find any possible way to read the results. the results are displayed but i need to do some further calculations using coef values. is there any possible way to store coef values into a new variable?

import statsmodels.api as sm
import numpy
ones = numpy.ones(len(x[0]))
t = sm.add_constant(numpy.column_stack((x[0], ones)))
for m in x[1:]:
    t = sm.add_constant(numpy.column_stack((m, t)))
results = sm.OLS(y, t).fit()

This is the image of the results

HussainBiedouh
  • 99
  • 2
  • 11

2 Answers2

5

According to the docs, an instance of RegressionResults is returned.

You can see all the available attributes there.

Maybe you are interested in:

params

The linear coefficients that minimize the least squares criterion. This is usually called Beta for the classical linear model.

Example:

model = sm.OLS(Y,X)
results = model.fit()
print(results.params)
sascha
  • 32,238
  • 6
  • 68
  • 110
  • 1
    ```It didn't help``` is almost useless. Maybe you want to explain what the problem is. – sascha Jul 25 '17 at 21:59
  • 1
    there was a problem with numpy as i forgot to reverse all (np's) to numpy. sorry. Your answer is correct and it solved the problem. – HussainBiedouh Jul 25 '17 at 22:11
  • 1
    results.params is the answer, here are further examples http://www.statsmodels.org/dev/examples/notebooks/generated/ols.html – HussainBiedouh Jul 25 '17 at 22:11
-1

Try this:

B0, B1, B2, B3 = modelo.params
ah bon
  • 9,293
  • 12
  • 65
  • 148
  • 1
    Code-only answers may resolve the problem, but do not help people with this problem in understanding why this resolves the issue. Could you please edit your answer and include details on why this code resolves the problem? – Marc Sances Oct 31 '22 at 09:09