如何从 Pandas 的 OLS 摘要中提取特定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37508158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract a particular value from the OLS-summary in Pandas?
提问by Joe
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:
是否有可能从 Pandas 的线性回归总结中获得其他值(目前我只知道一种获得 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---------------------------------
Thanks
谢谢
采纳答案by hhbilly
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
回答by foxyblue
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']])
In the case of r-squared
use:
在r-squared
使用的情况下:
# retrieving model's r-squared value
model.rsquared
and in the case of p-values
use:
在p-values
使用的情况下:
# return p-values and corresponding coefficients in model
model.pvalues
For more parameters (fvalues
ess
) please refer to the doc
更多参数 ( fvalues
ess
) 请参考文档