Accessing individual parameters in statsmodels
Asked Answered
V

1

6

I'm using statsmodels.api to inspect the statistical parameters from different combinations of variables. You can use print(results.summary()) to get

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.454
Model:                            OLS   Adj. R-squared:                  0.454
Method:                 Least Squares   F-statistic:                     9694.
Date:                Mon, 30 Jul 2018   Prob (F-statistic):               0.00
Time:                        10:14:47   Log-Likelihood:                -9844.7
No. Observations:               11663   AIC:                         1.969e+04
Df Residuals:                   11662   BIC:                         1.970e+04
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
x1            -1.4477      0.015    -98.460      0.000      -1.477      -1.419
==============================================================================
Omnibus:                     1469.705   Durbin-Watson:                   1.053
Prob(Omnibus):                  0.000   Jarque-Bera (JB):             2504.774
Skew:                           0.855   Prob(JB):                         0.00
Kurtosis:                       4.493   Cond. No.                         1.00
==============================================================================

but say I was just interested in a couple of these parameters, say, No. observations and R-squared. How can I print just certain parameters such as these? Using print(results) just gives a pointer to the results object:

print(results)
<statsmodels.regression.linear_model.RegressionResultsWrapper object at 0x0000020DAB8028D0>
Valona answered 29/7, 2018 at 22:23 Comment(0)
B
5

Fitting a model with OLS returns a RegressionResults object - and from the docs, there are plenty of attributes on that class which give you particular information like number of observations (nobs) and the R squared value (rsquared).

Taking a look at the source code for summary, it is really just formatting all of the separately available attributes into a nice table for you.

Demo

>>> Y = [1, 3, 4, 5, 2, 3, 4]; X = range(1, 8)

>>> model = sm.OLS(Y, X)

>>> results = model.fit()

>>> results.nobs, results.rsquared
(7.0, 0.16118421052631615)
Baptistry answered 29/7, 2018 at 23:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.