从 statsmodels.api 获取 Durbin-Watson 图

Getting Durbin-Watson figure from statsmodels.api

我无法从 statsmodel.api 中提取 durbin-watson 作为它自己的值,也无法在任何地方找到任何文档来提供帮助(我在它的父库中找到了很多文档,但我不能'不要解码任何一个)。

正在计算该值,可以通过执行以下模型摘要来查看(我一直遵循此处的指导:https://www.statology.org/durbin-watson-test-python/

from statsmodels.formula.api import ols

#fit multiple linear regression model
model = ols('rating ~ points + assists + rebounds', data=df).fit()

#view model summary
print(model.summary())

但是我就是无法提取出那个数字。有什么想法吗?

此外 - 看起来 DW 在置信区间内工作。您可以 'set' 要处理的模型有 95% 的置信度吗?我基本上想多次执行测试,如果 DW 数字在 95% CI,return 是或否继续程序。

谢谢

需要直接使用durbin_watson函数

from statsmodels.formula.api import ols
from statsmodels.stats.stattools import durbin_watson
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.standard_normal((500,4)))
df.columns = ["rating", "points", "assists", "rebounds"]


#fit multiple linear regression model
model = ols('rating ~ points + assists + rebounds', data=df)
res = model.fit()

dw = durbin_watson(res.resid)
print(f"Durbin-Watson: {dw}")

产生

Durbin-Watson: 1.9818102986170278

DW 统计的临界值在 statsmodels 中不可用。 Ljung-Box 序列相关性测试是一种更通用的方法,具有可用的临界值。

from statsmodels.stats.diagnostic import acorr_ljungbox
lb = acorr_ljungbox(res.resid)
print(lb)

这给出了

     lb_stat  lb_pvalue
1   0.003400   0.953500
2   0.774305   0.678988
3   1.412020   0.702720
4   1.890551   0.755881
5   2.176684   0.824197
6   2.397583   0.879749
7   3.186928   0.867188
8   3.639602   0.888089
9   3.793818   0.924451
10  5.639786   0.844565

左列是无序列相关的检验统计量,右列是p-value.