R version 2.12.0 (2010-10-15) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i486-pc-linux-gnu (32-bit) 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(16,29,22,30,20,39,18,9.6,10.2,20.2,50,120,19.8,18,3,11,15,27,28,14,5.6,6.5,8.5,87.9,5.8,25.2,7.5,13.7,34,17,9,9.2,5,24,40,86.5,0.54,14,4.8,28,16,5.8,16,9.1,6,17,26,99.6,41,72,23,42,40,18,45,18,2,10,13.6,160) > x <- c(1606,1634,2013,1654,1003,1029,1052,1653,1918,1926,1862,1816,1712,1646,1555,1402,1047,891,940,1372,2012,1879,1667,1856,1771,1721,1773,1507,1033,1011,1111,1736,1865,2078,1947,1428,1500,1950,1591,1613,1077,880,1128,1320,1692,1575,1478,1500,1368,1563,1424,1274,1047,1049,1069,981,1540,1559,1459,1559) > par8 = '5' > par7 = '1' > par6 = '0' > par5 = '0.0' > par4 = '12' > par3 = '1' > par2 = '0' > par1 = '0.0' > #'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 > 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] 0.063915662 0.007317106 -0.258150601 -0.165296808 0.042933423 [6] -0.143998308 -0.112568518 -0.186322290 0.047846276 -0.024705593 [11] -0.110625575 0.021787354 0.033882081 0.044557415 0.131197481 [16] 0.072221131 -0.013461742 0.126350792 0.167135914 0.235314087 [21] -0.075868199 0.100666172 0.155264123 -0.262148770 -0.166079251 [26] 0.124923855 -0.108310278 0.067974879 0.041712208 -0.138773312 [31] 0.015185642 -0.273951880 -0.097349792 -0.277150620 -0.275599904 [36] 0.049190244 -0.092115289 -0.221222321 -0.110892936 -0.235934242 [41] -0.028250466 0.175670701 -0.053722521 -0.296814556 -0.094128845 [46] -0.010210682 -0.012938553 0.038579482 > y [1] 0.21309322 -0.47692407 -1.99243016 -1.00330211 -0.28768207 -0.36772478 [7] 0.44183275 0.37729423 -0.59962112 -1.13388043 -1.77195684 -0.31129194 [13] -1.22782402 0.33647224 0.91629073 0.21950056 0.81831032 -0.46262352 [19] -1.13497993 -0.41985385 -0.11332869 1.30625165 1.54881329 -0.01605539 [25] -2.37404406 -0.58778666 -0.44628710 0.71480868 -0.75377180 -1.07535543 [31] 0.57536414 -0.01092907 0.18232156 -0.34484049 -0.43078292 0.14101775 [37] 4.32975821 1.63760879 1.56687830 0.40546511 0.91629073 1.13251384 [43] 1.03407377 0.68209734 -1.09861229 -0.53062825 -0.64802675 0.47401165 > (gyx <- grangertest(y ~ x, order=par8)) Granger causality test Model 1: y ~ Lags(y, 1:5) + Lags(x, 1:5) Model 2: y ~ Lags(y, 1:5) Res.Df Df F Pr(>F) 1 32 2 37 -5 0.5375 0.7463 > (gxy <- grangertest(x ~ y, order=par8)) Granger causality test Model 1: x ~ Lags(x, 1:5) + Lags(y, 1:5) Model 2: x ~ Lags(x, 1:5) Res.Df Df F Pr(>F) 1 32 2 37 -5 1.67 0.1703 > postscript(file="/var/www/rcomp/tmp/1w89h1292349401.ps",horizontal=F,onefile=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 0.233 0.075 -0.011 -0.062 0.085 0.068 -0.037 -0.415 -0.502 -0.382 -0.117 -3 -2 -1 0 1 2 3 4 5 6 7 0.301 0.312 0.136 0.042 -0.085 0.038 0.004 -0.044 -0.252 -0.368 -0.308 8 9 10 11 12 13 14 -0.075 0.219 0.249 0.124 0.041 0.002 0.143 > (r <- ccf(x,y,main='Cross Correlation Function (transformed and differenced)',ylab='CCF',xlab='Lag (k)')) Autocorrelations of series 'X', by lag -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -0.049 -0.100 0.021 -0.104 -0.077 0.035 -0.149 -0.089 -0.193 -0.250 -0.180 -2 -1 0 1 2 3 4 5 6 7 8 -0.094 0.087 0.099 -0.267 -0.212 -0.106 0.035 0.246 -0.054 -0.240 -0.008 9 10 11 12 13 -0.061 -0.088 -0.028 -0.178 -0.010 > par(op) > dev.off() null device 1 > postscript(file="/var/www/rcomp/tmp/2w89h1292349401.ps",horizontal=F,onefile=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/rcomp/tmp/3phr21292349401.ps",horizontal=F,onefile=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/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/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/rcomp/tmp/4396s1292349401.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/rcomp/tmp/5o95y1292349401.tab") > try(system("convert tmp/1w89h1292349401.ps tmp/1w89h1292349401.png",intern=TRUE)) character(0) > try(system("convert tmp/2w89h1292349401.ps tmp/2w89h1292349401.png",intern=TRUE)) character(0) > try(system("convert tmp/3phr21292349401.ps tmp/3phr21292349401.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.040 0.520 1.557