add_constant() 在 statsmodels 中不起作用

add_constant() in statsmodels not working

我尝试将 add_constant() 函数与数据集数组一起使用。在索引 59 处它工作(列已创建)但在索引 60 处未创建。最初,testmat[59] returns 形状为 (24, 54),testmat[60] 形状为 (9, 54)。以下是我 运行 add_constant 函数时得到的结果:

In: Xnew = sm.add_constant(testmat[59])
Out:
     const  TRYSIL_PO   TRYSIL_TA
6142    1   985.7       271.65
6143    1   984.9       271.85

In: Xnew = sm.add_constant(testmat[60]):
Out:
     TRYSIL_PO  TRYSIL_TA
6166    983.6   272.75
6167    983.1   272.85

我已经检查了其他来源,但似乎没有解决这个问题。 你能解释一下吗?

如果已经有一个方差为 0 的列,即具有所有相同值的列,add_constant() 似乎不起作用。我的数据集中是这样的(上面例子中没有提到(54列))。

解决方法是在add_constant()函数中添加has_constant选项,像这样:

sm.add_constant(testmat[60], has_constant='add')

更多信息:http://www.statsmodels.org/dev/generated/statsmodels.tools.tools.add_constant.html