R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > y <- c(85.9,76.8,96.2,83.9,88.7,105.4,86.7,76.3,93.2,105.1,121,163,94.7,78.4,101.4,91.2,89.9,112.2,81.5,78.8,99.1,101.3,91.5,152.6,86.6,86.6,98.5,86.7,89.1,111,92.6,85.1,116.1,98.3,97.7,177.9,94.2,83.8,109.5,102.3,102.5,162.7,85.3,88.2,104.7,99.4,113.8,166.6,89.2,93.2,115,97.2,112.5,121.8,100.2,93.8,113.6,110.7,127.6,185.9) > x <- c(252.5,251.1,255.1,258.3,255.3,261.1,253.8,252.9,253.9,255.5,262,262.8,263.3,262.5,269.2,270.8,274.1,273,267.3,267.1,268.2,270.2,271.5,281,280.1,281.5,285.9,289.8,292.9,291.2,291.8,289.8,292.5,290.3,297.5,307.5,304.7,304.6,310.7,310.7,315.7,314.7,312.2,312.8,314.3,319.7,319.9,329.5,326.9,329.7,335.7,337.2,339.7,338.3,339.2,342.5,342.2,338.3,339,345.9) > par8 = '6' > par7 = '0' > par6 = '0' > par5 = '1' > par4 = '12' > par3 = '0' > par2 = '0' > par1 = '1' > #'GNU S' R Code compiled by R2WASP v. 1.0.44 () > #Author: Prof. Dr. P. Wessa > #To cite this work: Wessa P., (2008), Bivariate Granger Causality (v1.0.0) in Free Statistics Software (v$_version), Office for Research Development and Education, URL http://www.wessa.net/rwasp_grangercausality.wasp/ > #Source of accompanying publication: Office for Research, Development, and Education > #Technical description: > library(lmtest) Loading required package: zoo Attaching package: 'zoo' The following object(s) are masked from package:base : as.Date.numeric > par1 <- as.numeric(par1) > par2 <- as.numeric(par2) > par3 <- as.numeric(par3) > par4 <- as.numeric(par4) > par5 <- as.numeric(par5) > par6 <- as.numeric(par6) > par7 <- as.numeric(par7) > par8 <- as.numeric(par8) > ox <- x > oy <- y > if (par1 == 0) { + x <- log(x) + } else { + x <- (x ^ par1 - 1) / par1 + } > if (par5 == 0) { + y <- log(y) + } else { + y <- (y ^ par5 - 1) / par5 + } > if (par2 > 0) x <- diff(x,lag=1,difference=par2) > if (par6 > 0) y <- diff(y,lag=1,difference=par6) > if (par3 > 0) x <- diff(x,lag=par4,difference=par3) > if (par7 > 0) y <- diff(y,lag=par4,difference=par7) > x [1] 251.5 250.1 254.1 257.3 254.3 260.1 252.8 251.9 252.9 254.5 261.0 261.8 [13] 262.3 261.5 268.2 269.8 273.1 272.0 266.3 266.1 267.2 269.2 270.5 280.0 [25] 279.1 280.5 284.9 288.8 291.9 290.2 290.8 288.8 291.5 289.3 296.5 306.5 [37] 303.7 303.6 309.7 309.7 314.7 313.7 311.2 311.8 313.3 318.7 318.9 328.5 [49] 325.9 328.7 334.7 336.2 338.7 337.3 338.2 341.5 341.2 337.3 338.0 344.9 > y [1] 84.9 75.8 95.2 82.9 87.7 104.4 85.7 75.3 92.2 104.1 120.0 162.0 [13] 93.7 77.4 100.4 90.2 88.9 111.2 80.5 77.8 98.1 100.3 90.5 151.6 [25] 85.6 85.6 97.5 85.7 88.1 110.0 91.6 84.1 115.1 97.3 96.7 176.9 [37] 93.2 82.8 108.5 101.3 101.5 161.7 84.3 87.2 103.7 98.4 112.8 165.6 [49] 88.2 92.2 114.0 96.2 111.5 120.8 99.2 92.8 112.6 109.7 126.6 184.9 > (gyx <- grangertest(y ~ x, order=par8)) Granger causality test Model 1: ~ Lags(, 1:6) + Lags(, 1:6) Model 2: ~ Lags(, 1:6) Res.Df Df F Pr(>F) 1 41 2 47 -6 3.5407 0.00646 ** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > (gxy <- grangertest(x ~ y, order=par8)) Granger causality test Model 1: ~ Lags(, 1:6) + Lags(, 1:6) Model 2: ~ Lags(, 1:6) Res.Df Df F Pr(>F) 1 41 2 47 -6 2.5302 0.03549 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > postscript(file="/var/www/html/rcomp/tmp/1zytl1260554985.ps",horizontal=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > op <- par(mfrow=c(2,1)) > (r <- ccf(ox,oy,main='Cross Correlation Function (raw data)',ylab='CCF',xlab='Lag (k)')) Autocorrelations of series 'X', by lag -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 0.125 0.170 0.234 0.161 0.133 0.174 0.180 0.235 0.273 0.234 0.228 0.255 0.243 -1 0 1 2 3 4 5 6 7 8 9 10 11 0.292 0.367 0.238 0.201 0.218 0.197 0.225 0.241 0.175 0.144 0.154 0.116 0.134 12 13 14 0.192 0.092 0.077 > (r <- ccf(x,y,main='Cross Correlation Function (transformed and differenced)',ylab='CCF',xlab='Lag (k)')) Autocorrelations of series 'X', by lag -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 0.125 0.170 0.234 0.161 0.133 0.174 0.180 0.235 0.273 0.234 0.228 0.255 0.243 -1 0 1 2 3 4 5 6 7 8 9 10 11 0.292 0.367 0.238 0.201 0.218 0.197 0.225 0.241 0.175 0.144 0.154 0.116 0.134 12 13 14 0.192 0.092 0.077 > par(op) > dev.off() null device 1 > postscript(file="/var/www/html/rcomp/tmp/20qqt1260554985.ps",horizontal=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > op <- par(mfrow=c(2,1)) > acf(ox,lag.max=round(length(x)/2),main='ACF of x (raw)') > acf(x,lag.max=round(length(x)/2),main='ACF of x (transformed and differenced)') > par(op) > dev.off() null device 1 > postscript(file="/var/www/html/rcomp/tmp/3cds51260554985.ps",horizontal=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > op <- par(mfrow=c(2,1)) > acf(oy,lag.max=round(length(y)/2),main='ACF of y (raw)') > acf(y,lag.max=round(length(y)/2),main='ACF of y (transformed and differenced)') > par(op) > dev.off() null device 1 > > #Note: the /var/www/html/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/html/rcomp/createtable") > > a<-table.start() > a<-table.row.start(a) > a<-table.element(a,'Granger Causality Test: Y = f(X)',5,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Model',header=TRUE) > a<-table.element(a,'Res.DF',header=TRUE) > a<-table.element(a,'Diff. DF',header=TRUE) > a<-table.element(a,'F',header=TRUE) > a<-table.element(a,'p-value',header=TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Complete model',header=TRUE) > a<-table.element(a,gyx$Res.Df[1]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Reduced model',header=TRUE) > a<-table.element(a,gyx$Res.Df[2]) > a<-table.element(a,gyx$Df[2]) > a<-table.element(a,gyx$F[2]) > a<-table.element(a,gyx$Pr[2]) > a<-table.row.end(a) > a<-table.end(a) > table.save(a,file="/var/www/html/rcomp/tmp/4f3q21260554986.tab") > a<-table.start() > a<-table.row.start(a) > a<-table.element(a,'Granger Causality Test: X = f(Y)',5,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Model',header=TRUE) > a<-table.element(a,'Res.DF',header=TRUE) > a<-table.element(a,'Diff. DF',header=TRUE) > a<-table.element(a,'F',header=TRUE) > a<-table.element(a,'p-value',header=TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Complete model',header=TRUE) > a<-table.element(a,gxy$Res.Df[1]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Reduced model',header=TRUE) > a<-table.element(a,gxy$Res.Df[2]) > a<-table.element(a,gxy$Df[2]) > a<-table.element(a,gxy$F[2]) > a<-table.element(a,gxy$Pr[2]) > a<-table.row.end(a) > a<-table.end(a) > table.save(a,file="/var/www/html/rcomp/tmp/5xcqj1260554986.tab") > > system("convert tmp/1zytl1260554985.ps tmp/1zytl1260554985.png") > system("convert tmp/20qqt1260554985.ps tmp/20qqt1260554985.png") > system("convert tmp/3cds51260554985.ps tmp/3cds51260554985.png") > > > proc.time() user system elapsed 0.914 0.454 1.105