Statsmodels (Python):Breusch Godfrey Lagrange 乘数测试

Statsmodels (Python): Breusch Godfrey Lagrange Multiplier tests

我正在使用 Statsmodels 在 Python 中使用自回归模型。包裹很棒,我得到了我需要的确切结果。但是,残差相关性测试 (Breusch-Godfrey LM-test) 似乎不起作用,因为我收到一条错误消息。

我的代码:

import pandas as pd
import datetime
import numpy as np
from statsmodels.tsa.api import VAR
import statsmodels.api as sm

df = pd.read_csv('US_data.csv')

# converting str formatted dates to datetime and setting the index
j = []
for i in df['Date']:
    j.append(datetime.datetime.strptime(i, '%Y-%m-%d').date())
df['Date'] = j
df = df.set_index('Date')

# dataframe contains three columns (GDP, INV and CONS)

# log difference
df = pd.DataFrame(np.log(df)*100)
df = df.diff()

p = 4 # order
model = VAR(df[1:])
results = model.fit(p, method='ols')
sm.stats.diagnostic.acorr_breusch_godfrey(results)

错误信息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-11abf518baae> in <module>()
----> 1 sm.stats.diagnostic.acorr_breusch_godfrey(results)

/home/****/anaconda3/lib/python3.6/site-packages/statsmodels/sandbox/stats/diagnostic.py in acorr_breusch_godfrey(results, nlags, store)
    501         nlags = int(nlags)
    502 
--> 503     x = np.concatenate((np.zeros(nlags), x))
    504 
    505     #xdiff = np.diff(x)

ValueError: all the input arrays must have same number of dimensions

五个多月前 here 提出了类似的问题,但没有成功。有人知道如何解决这个问题吗?非常感谢您!

这些诊断测试是为像 OLS 这样的单变量模型设计的,我们有一个 one-dimensional 残差数组。 使用它的唯一方法很可能是仅使用 VAR 系统的单个方程或循环遍历每个方程或变量。

statsmodels master 中的 VARResults 有一个 test_whiteness_new 方法,该方法用于检验 VAR 的多变量残差不存在自相关性。 它使用了 Portmanteau 测试,我认为它与 Ljung-Box 相同。 状态空间模型也使用 Ljung-Box 进行相关测试。