[R-sig-finance] Granger Causality

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Wed Jun 1 10:26:07 CEST 2005


On Tue, 31 May 2005 10:12:47 +0100 Uzuner, Tolga wrote:

> This was asked sometime back. Here is a rudimentary implementation,
> hope helps.

Tolga, thanks for code, but I think the question was about testing
Granger causality between two multivariate series...

However, I also had written some code for testing for Granger causality
between two univariate series as well which I have now committed to
CRAN. It is a bit more flexible in the specification of the series and
supports some more time series formats. It also leverages waldtest()
which allows for asymptotic tests etc.. It is contained in version
0.9-12 of the lmtest package which is available from the CRAN master
site and should propagate to the mirrors in the next days. See
  ?grangertest
for an example. This uses a very entertaining data set (provided to
me by Roger Koenker) which answers the question "What came first: the
chicken or the egg?". :-)

Best,
Z

 
> granger <-function(d, L, k = 1) 
> {
> #d is a bivariate time-series:  regress d[,k] on L lags of d[,1] and
> #d[,2]. This is a modified version for R, in which the command
> #ts.matrix was
> substituted by ts.intersect.
> names.d <- dimnames(d)[[2]]
> D <- d
> for(i in 1:L) 
> {
> D <-ts.intersect(D, lag(d,  - i))
> }
> dimnames(D)[[2]] <- paste(rep(names.d, L + 1), "_", rep(0:L, times =
> rep(2, L+ 1)), sep = "")
> y  <- D[, k]
> n  <- length(y)
> x1 <- D[,  - (1:2)]
> x0 <- x1[, ((1:L) * 2) - (k %% 2)]
> z1 <- lm(y ~ x1)
> z0 <- lm(y ~ x0)
> S1 <- sum(z1$resid^2)
> S0 <- sum(z0$resid^2)
> ftest <- ((S0 - S1)/L)/(S1/(n - 2 * L - 1))
> list(ftest = ftest, p.val = 1 - pf(ftest, L, n - 2 * L - 1), R2 =
> summary(z1)$r.squared)
> }
> 
> gr<-function(d,L) 
> # this loops grangers' p.val for L days, 
> {
> 	m<-matrix(nrow=L)
> 	for(i in 1:L)
> 	{
> 		m[i]<-granger(d,i)$p.val-granger(array(c(d[,2],d[,1]),dim(d)),
> 		i)$p.val
> 	}
> 	m
> }
> 
> 
> =====================================================================
> ========= This message is for the sole use of the intended recipient.
> ...{{dropped}}
> 
> _______________________________________________
> R-sig-finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>



More information about the R-sig-finance mailing list