格兰杰因果关系测试/VAR 示例中的错误Python 3.8-

问题描述

我正在尝试/未能在 Python 中重现 VAR 模型构建的“流行”示例,但在格兰杰因果关系测试中陷入困境。我使用提供的数据集和代码,但出现以下错误。感谢您让我知道您是否认为您了解这里发生的事情!最好

文件“/Applications/anaconda3/lib/python3.8/site-packages/statsmodels/tsa/tsatools.py”,第524行,在lagmat2ds中 raise ValueError('只支持一维和二维数据。') ValueError:仅支持一维和二维数据。

完整代码如下所示 - 原始演练为 here

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# Import Statsmodels
from statsmodels.tsa.api import VAR
from statsmodels.tsa.stattools import adfuller
from statsmodels.tools.eval_measures import rmse,aicfrom 
filepath = 'https://raw.githubusercontent.com/selva86/datasets/master/Raotbl6.csv'
df = pd.read_csv(filepath,parse_dates=['date'],index_col='date')
print(df.shape)  # (123,8)
df.tail()

from statsmodels.tsa.stattools import grangercausalitytests
maxlag=12
test = 'ssr_chi2test'
def grangers_causation_matrix(data,variables,test='ssr_chi2test',verbose=False):    
"""Check Granger Causality of all possible combinations of 
the Time series.
The rows are the response variable,columns are predictors. 
The values in the table 
are the P-Values. P-Values lesser than the significance 
level (0.05),implies 
the Null Hypothesis that the coefficients of the 
corresponding past values is 
zero,that is,the X does not cause Y can be rejected.

data      : pandas dataframe containing the time series 
variables
variables : list containing names of the time series 
variables.
"""
df = pd.DataFrame(np.zeros((len(variables),len(variables))),columns=variables,index=variables)
for c in df.columns:
for r in df.index:
test_result = grangercausalitytests(data[[r,c]],maxlag=maxlag,verbose=False)
p_values = [round(test_result[i+1][0][test][1],4) for i in 
range(maxlag)]
if verbose: print(f'Y = {r},X = {c},P Values = 
{p_values}')
min_p_value = np.min(p_values)
df.loc[r,c] = min_p_value
df.columns = [var + '_x' for var in variables]
df.index = [var + '_y' for var in variables]
return df

grangers_causation_matrix(df,variables = df.columns)  

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)