You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python statsmodels做VAR模型的多元Breusch-Godfrey LM自相关检验

解决方案

核心问题说明

statsmodels内置的acorr_breusch_godfrey接口仅支持单方程OLS模型的序列相关检验,无法适配VAR这类多方程系统模型。你之前单独提取单序列残差做检验的写法,也仅能得到单个方程的单变量检验结果,并非VAR系统层面的多元Breusch-Godfrey LM检验结果,所以无法匹配Eviews的输出。

实现方法

statsmodels的VAR拟合结果对象自带test_serially_correlated方法,指定method='lm'即可调用与Eviews逻辑完全一致的多元残差自相关LM检验,参考代码如下:

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

data = pd.read_csv('http://web.pdx.edu/~crkl/ceR/data/cjx.txt', sep='\s+', index_col='YEAR', nrows=39)

X = np.log(data.X)
L = np.log(data.L1)
K = np.log(data.K1)
df = pd.DataFrame({'X': X, 'L': L, 'K': K})

# 拟合VAR模型
model_var = VAR(endog=df[['L','K']], exog=df['X']).fit(maxlags=2)
# 执行多元BG LM检验,nlags设置为需要检验的最大滞后阶数
lm_test_res = model_var.test_serially_correlated(nlags=15, method='lm')
# 输出检验结果
print(lm_test_res.summary())

结果对齐说明

上述代码输出的结果会包含每阶滞后对应的卡方统计量、自由度、p值,和Eviews中View > Residual Tests > Autocorrelation LM Test的输出项完全对应,你可以自行提取lm_test_res.test_statistics、lm_test_res.pvalues属性整理成和Eviews格式一致的表格。

内容的提问来源于stack exchange,提问作者dingo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 12:33:03