我读过的关于Python中多元回归的所有线程都推荐在statmodels中使用OLS函数。这是我遇到的问题,我试图通过将其回报与14个可以解释该基金回报的独立变量进行回归来解释基金的回报(HYFAX以绿色突出显示)。这应该有一个显著的F检验,并通过逐步迭代的因素,吐出最高调整R平方的最佳拟合模型。在python中有办法做到这一点吗?
基金回报与因素对比
听起来你只是想看看你的模型适合的结果。下面是一个只有一个预测器的例子,但是很容易扩展到14个:
导入statmodels并指定您想要构建的模型(这是您将包含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测试值,beta权重等。应该看起来像这样:
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
==============================================================================