[R] Automatically extract info from Granger causality output

ivan i.petzev at gmail.com
Thu Apr 14 19:36:58 CEST 2011


Dear Community,

this is my first programming in R and I am stuck with a problem. I
have the following code which automatically calculates Granger
causalities from a variable, say e.g. "bs" as below, to all other
variables in the data frame:

log.returns<-as.data.frame( lapply(daten, function(x) diff(log(ts(x)))))
y1<-log.returns$bs
y2<- log.returns[,!(names(log.returns) %in% "bs")]
Granger<- function(y1,y2) {models=lapply(y2, function(x)
VAR(cbind(x,y1),ic="SC") ); results=lapply(models,function(x)
causality(x,cause="y1")); print(results)}
Count<-Granger(y1,y2)

which produces the following output (I have printed only part of it
(for Granger causality of bs on ml)):

$ml
$ml$Granger

        Granger causality H0: y1 do not Granger-cause x

data:  VAR object x
F-Test = 0.2772, df1 = 1, df2 = 122, p-value = 0.5995


$ml$Instant

        H0: No instantaneous causality between: y1 and x

data:  VAR object x
Chi-squared = 19.7429, df = 1, p-value = 8.859e-06

My questions:

1)How can I edit the function above so that the output writes: Granger
causality H0: bs do not Granger-cause ml   rather than  Granger
causality H0: y1 do not Granger-cause x?

2) I want to extract the p-values of the tests into a data frame for
instance. The problem is that the output has a 3 layer structure.
Thus, for the above p-value I need to write count$ml$Granger$p.value.
I thought of a loop of something like for(i in 1:length(count))
{z=count$[[i]]$Granger$p.value} but it didn't work.

Thank you very much for your help.

Best Regards.



More information about the R-help mailing list