解决statsmodels中grangercausalitytests修改lag报错Maximum allowable lag is 1

问题描述

import pandas as pd
from statsmodels.tsa.stattools import grangercausalitytests
df = pd.read_excel(r"Granger_Causality_Test\stock.xlsx")
grangercausalitytests(df[['Open', 'Close']], maxlag=3)

在grangercausalitytests中的maxlag参数由1修改为3后报错:
Exception has occurred: ValueError
Insufficient observations. Maximum allowable lag is 1
在这里插入图片描述
数据集
在这里插入图片描述
看了源代码中参数的解释,以为是我理解有误,然后翻译了下还是一个意思:maxlag可以设置为1、2、3、4,如果设置为3,那就会从1算到3啊,那为什么我改成3就不行了呢??

Parameters
    ----------
    x : array, 2d
        data for test whether the time series in the second column Granger
        causes the time series in the first column
    maxlag : integer
        the Granger causality test results are calculated for all lags up to
        maxlag
    verbose : bool
        print results if true

解决办法

数据量太少,换一下数据。(感谢胡歌指点)

比如我把上面的数据复制了一遍就跑出结果来了。
在这里插入图片描述
在这里插入图片描述

References

猜你喜欢

转载自blog.csdn.net/weixin_43031092/article/details/109056721
今日推荐