Durbin-Watson统计一维时间序列数据

Edd*_*ett 7 python statistics numpy correlation statsmodels

我正在尝试确定一个时间序列(如一个浮点数列表)是否与自身相关.我已经acf在statsmodels中使用了这个函数(http://statsmodels.sourceforge.net/devel/generated/statsmodels.tsa.stattools.acf.html),现在我正在研究Durbin-Watson统计数据有任何价值.

看起来这种事情应该有效:

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

data = np.arange(100)  # this should be highly correlated
ols_res = OLS(data)
dw_res = np.sum(np.diff(ols_res.resid.values))
Run Code Online (Sandbox Code Playgroud)

如果你要运行它,你会得到:

Traceback (most recent call last):
...
  File "/usr/lib/pymodules/python2.7/statsmodels/regression/linear_model.py", line 165, in initialize
    self.nobs = float(self.wexog.shape[0])
AttributeError: 'NoneType' object has no attribute 'shape'
Run Code Online (Sandbox Code Playgroud)

似乎D/W通常用于比较两个时间序列(例如http://connor-johnson.com/2014/02/18/linear-regression-with-python/)的相关性,所以我认为问题是因为我没有通过另一个时间序列来比较.也许这应该在exog参数中传递给OLS

exog : array-like

A nobs x k array where nobs is the number of observations and k is
the number of regressors.
Run Code Online (Sandbox Code Playgroud)

(来自http://statsmodels.sourceforge.net/devel/generated/statsmodels.regression.linear_model.OLS.html)

旁注:我不确定"nobs x k"数组是什么意思.也许一个数组是xk

那我该怎么办呢?我预计会通过data两次,或者自己手动延迟,或者?

谢谢!

Jos*_*sef 3

OLS 是需要 y 和 x(或 endog 和 exog)的回归。在你的情况下 x 至少需要是一个常数,即。np.ones(len(endog), 1).

另外,您需要拟合模型,即ols_res = OLS(y, x).fit()

nobs x k表示二维,行中有 nobs 观察,列中有 k 个变量,即 exog.shape 是(nobs, k)

Durbin Watson 是序列相关性的检验统计量。它包含在 OLS 摘要输出中。统计模型中还包含其他不包含自相关的测试。

(我建议使用一些示例或教程笔记本。)