在 R 中运行 `grangertest()` 时允许混叠系数 [英] Allowing for aliased coefficients when running `grangertest()` in R

查看:108
本文介绍了在 R 中运行 `grangertest()` 时允许混叠系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently trying to run a granger causality analysis in R/R Studio. I am receiving errors about aliased coefficients when using the function grangertest(). From my understanding, this occurs because there is perfect multicolinearity between the variables.

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

Due to having a very large number of pairwise comparisons (e.g. 200+), I would like to simply run the granger with the aliased coefficients as per normal rather than returning an error. According to one answer here, the solution is or was to add set singular.ok=TRUE, but either I am doing it incorrectly the answer s out of date. I've tried checking the documentation, but have come up empty. Any help would be appreciated.

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,y,1, singular.ok=TRUE) # this also doesn't work 

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

另外有没有办法标记 xy 实际上是别名变量?似乎有一些答案,例如此处,但我在使其正常工作时遇到问题.

Additionally is there a way to flag x and y are actually aliased variables? There seem to be some answers like here but I'm having issues getting it working properly.

alias((x~ y))

提前致谢.

推荐答案

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

After some investigation and emailing the creator of the grangertest package, they sent me this solution. The solution should run on aliased variables when granger test does not. When the variables are not aliased, the solution should give the same values as the normal granger test.

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

# Some data that is not multicolinear
# x <- c(0,125,200,230,777)
# y <- c(0,3,6,9,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)

这篇关于在 R 中运行 `grangertest()` 时允许混叠系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆