【问题标题】:Statsmodels (Python): Breusch Godfrey Lagrange Multiplier testsStatsmodels (Python):Breusch Godfrey Lagrange 乘数测试
【发布时间】:2018-02-23 12:27:28
【问题描述】:

我正在使用 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 提出了类似的问题,但没有成功。有人知道如何解决这个问题吗?非常感谢您!

【问题讨论】:

    标签: python statsmodels


    【解决方案1】:

    这些诊断测试是为单变量模型(如 OLS)设计的,其中我们有一个一维残差数组。 使用它的唯一方法很可能只使用 VAR 系统的单个方程或对每个方程或变量进行循环。

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

    【讨论】:

    • 这很棒。 test_whiteness_new 有效,尽管它并不真正适合我的观察(我相信在较大的样本中效果最好)。循环遍历每个变量还没有成功,但也许下次会成功。感谢您抽出宝贵时间回答我的问题。
    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 2022-08-10
    • 2015-07-15
    • 2012-07-01
    • 1970-01-01
    • 2016-02-13
    • 2017-07-01
    • 1970-01-01
    相关资源
    最近更新 更多