非线性测试 NaN 错误

Non-Linearity Test NaN error

我想用一个简单的例子来尝试 statsmodel 的 linear_harvey_collier 测试。但是,结果我得到了 nan 。你能看出我的错误在哪里吗?

import numpy as np
from statsmodels.regression.linear_model import OLS

np.random.seed(44)
n_samples, n_features = 50, 4
X = np.random.randn(n_samples, n_features)
coef=np.random.uniform(-12,12,4)
y = np.dot(X, coef)
var = 400
y += var**(1/2) * np.random.normal(size=n_samples)
regr=OLS(y, X).fit()
print(regr.params)
print(regr.summary())

sms.linear_harvey_collier(regr)

我得到了结果 Ttest_1sampResult(statistic=nan, pvalue=nan).

如果我在排除一个变量的情况下执行测试,我会得到一个结果:

X3=X[:,:3]
regr3=OLS(y, X3).fit()
In [1]: sms.linear_harvey_collier(regr3)
Out[2]: Ttest_1sampResult(statistic=0.2447803429683807, pvalue=0.806727747845282)

不加常量和截距有问题吗?这只是一种感觉,如果确实有问题,我不明白为什么。

linear_harvey_collier 中存在一个错误,它将初始观察的数量硬编码为 3。 https://github.com/statsmodels/statsmodels/pull/6727

linear_harvey_collier只有两行代码
解决方法是直接计算测试

res = regr
from scipy import stats
skip = len(res.params)  # bug in linear_harvey_collier
rr = sms.recursive_olsresiduals(res, skip=skip, alpha=0.95, order_by=None)
stats.ttest_1samp(rr[3][skip:], 0)

Ttest_1sampResult(statistic=0.03092937323130299, pvalue=0.9754626388210277)