Search code examples
pythonpandasnumpylinear-regressionstatsmodels

Reading coef value from OLS regression results


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


Solution

  • 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)