我读过的关于Python中的多元回归的所有主题大多推荐Statsmodels中的OLS函数。这就是我遇到的问题,我试图解释一个基金的回报(HYFAX以绿色突出显示),通过回归14个可以解释该基金回报的自变量的回报。这应该具有显着的F检验并且在经历逐步迭代的因子之后吐出具有最高调整的R平方的最佳拟合模型。有没有办法在python中做到这一点?
答案 0 :(得分:2)
听起来你只是希望看到模型拟合的结果。下面是一个带有1个预测器的例子,但很容易扩展到14:
导入statsmodel并指定您要构建的模型(这是您包含14个预测变量的地方):
import statsmodels.api as sm
#read in your data however you want and assign your y, x1...x14 variables
model = sm.OLS(x, y)
适合模特:
results = model.fit()
现在只显示模型拟合的摘要:
print(results.summary())
这将为您提供调整后的R平方值,F测试值,β重量等。应该看起来像这样:
OLS Regression Results
==============================================================================
Dep. Variable: x R-squared: 0.601
Model: OLS Adj. R-squared: 0.594
Method: Least Squares F-statistic: 87.38
Date: Wed, 24 Aug 2016 Prob (F-statistic): 3.56e-13
Time: 19:51:25 Log-Likelihood: -301.81
No. Observations: 59 AIC: 605.6
Df Residuals: 58 BIC: 607.7
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [95.0% Conf. Int.]
------------------------------------------------------------------------------
y 0.8095 0.087 9.348 0.000 0.636 0.983
==============================================================================
Omnibus: 0.119 Durbin-Watson: 1.607
Prob(Omnibus): 0.942 Jarque-Bera (JB): 0.178
Skew: -0.099 Prob(JB): 0.915
Kurtosis: 2.818 Cond. No. 1.00
==============================================================================