在 R 中运行 `grangertest()` 时允许混叠系数

问题描述

我目前正在尝试在 R/R Studio 中运行格兰杰因果关系分析。使用函数 grangertest() 时,我收到有关混叠系数的错误。据我了解,这是因为变量之间存在完美的多重共线性。

由于有大量的成对比较(例如 200+),我想简单地按照正常使用别名系数运行 granger,而不是返回错误。根据一个答案 here解决方案是或曾经添加singular.ok=TRUE,但要么是我做错了,答案是过时的。我试过检查文档,但结果是空的。任何帮助将不胜感激。

library(lmtest)
x <- c(0,1,2,3)
y <- c(0,3,6,9)
grangertest(x,y,1) # I want this to run successfully even if there are aliased coefficients. 
grangertest(x,singular.ok=TRUE) # this also doesn't work 

"Error in waldtest.lm(fm,...) : 
  there are aliased coefficients in the model"

另外有没有办法标记 xy 实际上是别名变量?似乎有一些类似 here 的答案,但我在让它正常工作时遇到问题。

alias((x~ y))

提前致谢。

解决方法

经过一些调查并向 grangertest 软件包的创建者发送电子邮件后,他们向我发送了此解决方案。当 granger 测试没有时,解决方案应该在别名变量上运行。当变量没有别名时,解应该给出与正常格兰杰检验相同的值。

library(lmtest)
library(dynlm)
 
# Some data that is multicolinear
x <- c(0,1,2,3,4)
y <- c(0,6,9,12)

# Some data that is not multicolinear
# x <- c(0,125,200,230,777)
# y <- c(0,200)

# Convert to time series (this is an important step)
x=ts(x)
y=ts(y)

# This will run even when the data is multicolinear (but also when it is not)
# and is functionally the same as running the granger test (which by default uses the waldtest

m1 = dynlm(x ~ L(x,1:1) + L(y,1:1))
m2 = dynlm(x ~ L(x,1:1))
result <-anova(m1,m2,test="F")
 
# This will fail if the data is multicolinear or aliased but should give the same results as the anova otherwise (F value and P value etc)
#grangertest(y,x,1)