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

2

12

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

Breeching answered 29/5, 2016 at 9:17 Comment(0)
C
1

try:

print model.r2

for example:

import pandas as pd
from pandas import Panel
from pandas.io.data import DataReader
import scikits.statsmodels.api as sm

symbols = ['MSFT', 'GOOG', 'AAPL']

data = dict((sym, DataReader(sym, "yahoo")) for sym in symbols)

panel = Panel(data).swapaxes('items', 'minor')

close_px = panel['Close']

# convert closing prices to returns
rets = close_px / close_px.shift(1) - 1
model = pd.ols(y=rets['AAPL'], x=rets.ix[:, ['GOOG']])
print model.r2

Docs: http://statsmodels.sourceforge.net/stable/index.html

Cloakanddagger answered 29/5, 2016 at 9:47 Comment(3)
What if I want to extract p-value?Stuffed
it should be model.rsquared instead of model.r2.Chatav
Here is the list of properties : statsmodels.org/stable/generated/…Fragonard
G
17

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

Guerrilla answered 18/12, 2016 at 20:24 Comment(0)
C
1

try:

print model.r2

for example:

import pandas as pd
from pandas import Panel
from pandas.io.data import DataReader
import scikits.statsmodels.api as sm

symbols = ['MSFT', 'GOOG', 'AAPL']

data = dict((sym, DataReader(sym, "yahoo")) for sym in symbols)

panel = Panel(data).swapaxes('items', 'minor')

close_px = panel['Close']

# convert closing prices to returns
rets = close_px / close_px.shift(1) - 1
model = pd.ols(y=rets['AAPL'], x=rets.ix[:, ['GOOG']])
print model.r2

Docs: http://statsmodels.sourceforge.net/stable/index.html

Cloakanddagger answered 29/5, 2016 at 9:47 Comment(3)
What if I want to extract p-value?Stuffed
it should be model.rsquared instead of model.r2.Chatav
Here is the list of properties : statsmodels.org/stable/generated/…Fragonard

© 2022 - 2024 — McMap. All rights reserved.