0

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

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

完整的代码如下所示 - 原始演练在这里

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)  
4

0 回答 0