Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a particular value from the OLS-summary in Pandas?

is it possible to get other values (currently I know only a way to get beta and intercept) from the summary of linear regression in pandas? I need to get R-squared. Here is an extraction from manual:

In [244]: model = ols(y=rets['AAPL'], x=rets.ix[:, ['GOOG']])

In [245]: model
Out[245]: 
-------------------------Summary of Regression Analysis---------------------   ----
Formula: Y ~ <GOOG> + <intercept>
Number of Observations:         756
Number of Degrees of Freedom:   2
R-squared:         0.2814
Adj R-squared:     0.2805
Rmse:              0.0147
F-stat (1, 754):   295.2873, p-value:     0.0000
Degrees of Freedom: model 1, resid 754
-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
      GOOG     0.5442     0.0317      17.18     0.0000     0.4822     0.6063
 intercept     0.0011     0.0005       2.14     0.0327     0.0001     0.0022
---------------------------------End of Summary---------------------------------

Thanks

like image 408
Joe Avatar asked May 29 '16 09:05

Joe


People also ask

How do you extract the coefficients from a model in python?

The coefficients can be obtained using the params attribute of a fitted model. It will print a numpy array with the values [0.89516052 2.00334187] for the intercept and slope, respectively. If you need more information, utilize the output. summary() object, which has three full tables with model descriptions.

How do you get adjusted R squared in Statsmodels?

Adjusted R-squared. This is defined here as 1 - ( nobs -1)/ df_resid * (1- rsquared ) if a constant is included and 1 - nobs / df_resid * (1- rsquared ) if no constant is included.

What is OLS function in Python?

Linear regression, also called Ordinary Least-Squares (OLS) Regression, is probably the most commonly used technique in Statistical Learning. It is also the oldest, dating back to the eighteenth century and the work of Carl Friedrich Gauss and Adrien-Marie Legendre.


1 Answers

Docs handling the results of the regression - this will allow you to extract a number of values from your regression results:

# Given
model = ols(y=rets['AAPL'], x=rets.ix[:, ['GOOG']]).fit()

In the case of r-squared use:

# retrieving model's r-squared value
model.rsquared

and in the case of p-values use:

# return p-values and corresponding coefficients in model
model.pvalues

For more parameters (fvalues ess) please refer to the doc

like image 73
foxyblue Avatar answered Oct 19 '22 18:10

foxyblue