如何从Pandas中的OLS-summary中提取特定值?

时间:2016-05-29 09:17:14

标签: pandas linear-regression

是否可以从熊猫线性回归的摘要中获取其他值(目前我只知道获得beta和拦截的方法)?我需要得到R平方。 以下是手册摘录:

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

由于

2 个答案:

答案 0 :(得分:11)

Docs handling the results of the regression - 这将允许您从回归结果中提取许多值:

if (Boolean.valueOf(vars.get("DEBUG"))) {
  for (a: sampleResult.getAssertionResults()) {
    if (a.isError() || a.isFailure()) {
      log.error(sampleResult.getThreadName() + ": " 
                + sampleResult.getSampleLabel() + ": Assertion failed for response: " 
                + sampleResult.getResponseDataAsString());
    }
  }
}

如果# Given model = ols(y=rets['AAPL'], x=rets.ix[:, ['GOOG']]) 使用:

r-squared

并且在# retrieving model's r-squared value model.rsquared 使用的情况下:

p-values

有关更多参数(# return p-values and corresponding coefficients in model model.pvalues fvalues),请参阅doc

答案 1 :(得分:1)

尝试:

print model.r2

例如:

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

文档:http://statsmodels.sourceforge.net/stable/index.html