R中的列明智Granger因果检验 [英] Column wise granger's causal tests in R

查看:251
本文介绍了R中的列明智Granger因果检验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个参数不同的矩阵:M1和M3,它们的尺寸相同.我想在R中进行列明智的Grangertest.

I have 2 matrices of different parameters: M1and M3 with the same dimensions. I'll like to do a column wise grangertest in R.

M1<- matrix( c(2,3, 1, 4, 3, 3, 1,1, 5, 7), nrow=5, ncol=2) 
M3<- matrix( c(1, 3, 1,5, 7,3, 1, 3, 3, 4), nrow=5, ncol=2)

我要进行格兰杰因果关系测试,以确定M2格兰杰是否导致M1.我的实际矩阵包含更多的列和行,但这只是一个示例.两个向量之间的原始代码如下:

I'll want to do a granger's causality test to determine if M2 granger causes M1. My actual Matrices contain more columns and rows but this is just an example. The original code between two vectors is below:

library(lmtest)
data(ChickEgg)
grangertest(chicken ~ egg, order = 3, data = ChickEgg)

我该如何编写以进行列式分析,以便返回包含两行("F [2]"和"Pr(> F)[2]")和两列的矩阵作为结果?

How do I write this for a column wise analysis such that a matrix with 2 rows ( "F[2]" and "Pr(>F)[2]") and two columns is returned as results please?

推荐答案

这是否朝着正确的方向发展?

Does this go into the right direction?

library(lmtest)

M1<- matrix( c(2,3, 1, 4, 3, 3, 1,1, 5, 7), nrow=5, ncol=2) 
M3<- matrix( c(1, 3, 1,5, 7,3, 1, 3, 3, 4), nrow=5, ncol=2)

g <- list()
for (i in 1:ncol(M1)){
g[[i]]  <- grangertest(M1[ ,i] ~ M3[ ,i])
}

foo <- function(x){ 
  F <- x$F[2] 
  P <- x$`Pr(>F)`[2]
  data.frame(F = F, P = P)
  }

do.call(rbind, lapply(g, foo))

          F         P
1 0.3125000 0.6754896
2 0.1781818 0.7457180

这篇关于R中的列明智Granger因果检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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