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(106370,109375,116476,123297,114813,117925,126466,131235,120546,123791,129813,133463,122987,125418,130199,133016,121454,122044,128313,131556,120027,123001,130111,132524,123742,124931,133646,136557,127509,128945,137191,139716,129083,131604,139413,143125,133948,137116,144864,149277,138796,143258,150034,154708,144888,148762,156500,161088,152772,158011,163318,169969,162269,165765,170600,174681,166364,170240,176150,182056,172218,177856,182253,188090,176863,183273,187969,194650,183036,189516,193805,200499,188142,193732,197126,205140,191751,196700,199784,207360,196101,200824,205743,212489,200810,203683,207286,210910,194915,217920) > x <- c(100.3,101.9,102.1,103.2,103.7,106.2,107.7,109.9,111.7,114.9,116,118.3,120.4,126,128.1,130.1,130.8,133.6,134.2,135.5,136.2,139.1,139,139.6,138.7,140.9,141.3,141.8,142,144.5,144.6,145.5,146.8,149.5,149.9,150.1,150.9,152.8,153.1,154,154.9,156.9,158.4,159.7,160.2,163.2,163.7,164.4,163.7,165.5,165.6,166.8,167.5,170.6,170.9,172,171.8,173.9,174,173.8,173.9,176,176.6,178.2,179.2,181.3,181.8,182.9,183.8,186.3,187.4,189.2,189.7,191.9,192.6,193.7,194.2,197.6,199.3,201.4,203,206.3,207.1,209.8,211.1,215.3,217.4,215.5,210.9,212.6) > par8 = '11' > par7 = '0' > par6 = '0' > par5 = '1' > par4 = '4' > 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] 99.3 100.9 101.1 102.2 102.7 105.2 106.7 108.9 110.7 113.9 115.0 117.3 [13] 119.4 125.0 127.1 129.1 129.8 132.6 133.2 134.5 135.2 138.1 138.0 138.6 [25] 137.7 139.9 140.3 140.8 141.0 143.5 143.6 144.5 145.8 148.5 148.9 149.1 [37] 149.9 151.8 152.1 153.0 153.9 155.9 157.4 158.7 159.2 162.2 162.7 163.4 [49] 162.7 164.5 164.6 165.8 166.5 169.6 169.9 171.0 170.8 172.9 173.0 172.8 [61] 172.9 175.0 175.6 177.2 178.2 180.3 180.8 181.9 182.8 185.3 186.4 188.2 [73] 188.7 190.9 191.6 192.7 193.2 196.6 198.3 200.4 202.0 205.3 206.1 208.8 [85] 210.1 214.3 216.4 214.5 209.9 211.6 > y [1] 106369 109374 116475 123296 114812 117924 126465 131234 120545 123790 [11] 129812 133462 122986 125417 130198 133015 121453 122043 128312 131555 [21] 120026 123000 130110 132523 123741 124930 133645 136556 127508 128944 [31] 137190 139715 129082 131603 139412 143124 133947 137115 144863 149276 [41] 138795 143257 150033 154707 144887 148761 156499 161087 152771 158010 [51] 163317 169968 162268 165764 170599 174680 166363 170239 176149 182055 [61] 172217 177855 182252 188089 176862 183272 187968 194649 183035 189515 [71] 193804 200498 188141 193731 197125 205139 191750 196699 199783 207359 [81] 196100 200823 205742 212488 200809 203682 207285 210909 194914 217919 > (gyx <- grangertest(y ~ x, order=par8)) Granger causality test Model 1: ~ Lags(, 1:11) + Lags(, 1:11) Model 2: ~ Lags(, 1:11) Res.Df Df F Pr(>F) 1 56 2 67 -11 6.2124 1.454e-06 *** --- 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:11) + Lags(, 1:11) Model 2: ~ Lags(, 1:11) Res.Df Df F Pr(>F) 1 56 2 67 -11 2.7217 0.006932 ** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > postscript(file="/var/www/html/rcomp/tmp/137zi1260374888.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 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 0.530 0.548 0.572 0.596 0.622 0.639 0.663 0.690 0.718 0.737 0.765 0.795 0.829 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 0.855 0.887 0.919 0.953 0.911 0.888 0.852 0.817 0.779 0.749 0.709 0.672 0.635 10 11 12 13 14 15 16 0.607 0.569 0.535 0.499 0.474 0.439 0.407 > (r <- ccf(x,y,main='Cross Correlation Function (transformed and differenced)',ylab='CCF',xlab='Lag (k)')) Autocorrelations of series 'X', by lag -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 0.530 0.548 0.572 0.596 0.622 0.639 0.663 0.690 0.718 0.737 0.765 0.795 0.829 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 0.855 0.887 0.919 0.953 0.911 0.888 0.852 0.817 0.779 0.749 0.709 0.672 0.635 10 11 12 13 14 15 16 0.607 0.569 0.535 0.499 0.474 0.439 0.407 > par(op) > dev.off() null device 1 > postscript(file="/var/www/html/rcomp/tmp/24mxm1260374888.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/3tbhd1260374888.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/4qo7s1260374888.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/5btm61260374888.tab") > > system("convert tmp/137zi1260374888.ps tmp/137zi1260374888.png") > system("convert tmp/24mxm1260374888.ps tmp/24mxm1260374888.png") > system("convert tmp/3tbhd1260374888.ps tmp/3tbhd1260374888.png") > > > proc.time() user system elapsed 0.984 0.491 2.122