cov_type='HAC' error statsmodels 0.7 IPython 3 notebook python 2.7 anaconda mac os x

cov_type='HAC' error statsmodels 0.7 IPython 3 notebook python 2.7 anaconda mac os x

我正在尝试拟合 OLS 模型,这在没有稳健估计的情况下也能正常工作,但我想改进我的回归,所以,如下所示,我尝试用这个问题来实现它,在评论中有其他尝试来解决它。

我不知道是否正确应用了关键字,所以我希望有任何帮助。

代码:

# Fit and summarize OLS model
sumrz = dict()

for i, ca in enumerate(ccaa):
    x = sm.add_constant(data.dy[ca])
    mod = sm.OLS(endog=data.du[ca], exog=x, hasconst=True, missing='drop')
    res = mod.fit(cov_type='HAC', cov_kwds={'maxlags':1})
#    res = res.get_robustcov_results(cov_type='HAC', maxlags=1, use_correction=True)
#    res = res.get_robustcov_results(cov_type='HC0')
    sumrz[ca] = res.summary(xname=['const','dy'], yname='du', title=ca)

错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-114-87912e59a35d> in <module>()
      9 #    res = res.get_robustcov_results(cov_type='HAC', maxlags=1, use_correction=True)
     10 #    res = res.get_robustcov_results(cov_type='HC0')
---> 11     sumrz[ca] = res.summary(xname=['const','dy'], yname='du', title=ca)

/Users/mmngreco/anaconda/lib/python2.7/site-packages/statsmodels/regression/linear_model.pyc in summary(self, yname, xname, title, alpha)
   1950         top_right = [('R-squared:', ["%#8.3f" % self.rsquared]),
   1951                      ('Adj. R-squared:', ["%#8.3f" % self.rsquared_adj]),
-> 1952                      ('F-statistic:', ["%#8.4g" % self.fvalue] ),
   1953                      ('Prob (F-statistic):', ["%#6.3g" % self.f_pvalue]),
   1954                      ('Log-Likelihood:', None), #["%#6.4g" % self.llf]),

/Users/mmngreco/anaconda/lib/python2.7/site-packages/statsmodels/tools/decorators.pyc in __get__(self, obj, type)
     92         if _cachedval is None:
     93             # Call the "fget" function
---> 94             _cachedval = self.fget(obj)
     95             # Set the attribute in obj
     96 #            print("Setting %s in cache to %s" % (name, _cachedval))

/Users/mmngreco/anaconda/lib/python2.7/site-packages/statsmodels/regression/linear_model.pyc in fvalue(self)
   1214                 # assume const_idx exists
   1215                 idx = lrange(k_params)
-> 1216                 idx.pop(const_idx)
   1217                 mat = mat[idx]  # remove constant
   1218             ft = self.f_test(mat)

TypeError: an integer is required

(很高兴在问题中看到完整的回溯。)

以下是我根据回溯的猜测

我想如果 hasconst=True 是特定的,则常量检测中存在错误。 尽量省略参数 hasconst=True.

背景

如果我们不允许错误指定的异方差性或相关性,并且我们不使用稳健的协方差矩阵,则可以从残差平方和计算 F 统计量。

如果指定稳健 cov_type,则我们对所有斜率系数均为零的零假设使用 Wald 检验。即使异方差或相关性被错误指定,这对于参数的稳健协方差也是有效的。

在这种情况下,具有常量 const_idx 的列的索引设置不正确,我们得到 TypeError。