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-2.9%
,14.7%
,21.8%
,-3.2%
,27.2%
,11.1%
,-3.1%
,31.0%
,15.8%
,-37.7%
,-12.6%
,-5.8%
,18.4%
,22.4%
,-32.4%
,-15.6%
,-12.3%
,-25.4%
,-24.5%
,25.0%
,10.0%
,21.8%
,-9.0%
,14.8%
,-17.1%
,16.4%
,-5.9%
,8.6%
,2.9%
,-18.3%
,-14.6%
,-25.3%
,16.2%
,16.3%
,-2.0%
,2.0%
,-6.0%
,8.5%
,-2.0%
,52.0%
,11.8%
,16.5%
,-15.2%
,-8.3%
,3.9%
,13.8%
,7.7%
,-1.0%
,0.0%)
x <- c(2.93
,0.31
,-1.19
,0.31
,-0.57
,-8.9
,-0.62
,1.25
,2.43
,2.73
,1.65
,-0.48
,2.06
,5.13
,2
,2.93
,0.81
,-0.78
,1.62
,0.42
,3.38
,3.39
,0.74
,3.76
,0.03
,-1.48
,-0.77
,2.92
,0.15
,2.01
,0.63
,-1.32
,-2.32
,2.39
,4.59
,-0.38
,-0.25
,1.23
,1.94
,1.11
,0.3
,1.29
,-3.26
,2.07
,1.59
,1.51
,0.79
,-1.37
,1.91
,0.28
,-0.33
,-3.82
,-4.35
,0.37
,-1.49
,0.7
,2.92
,0.26
,1.31
,-0.1
,0.95
,3.15
,4.15
,2.46
,1.46
,1.95
,1.27
,2.51
,1.63
,2.12
,2.8
,1.2
,0.06
,-0.56
,-0.31
,1.76
,-0.93
,0.49
,1.37
,1.49
,4.03
,2.8
,-0.15
,1.39
,-0.2
,-1.71
,1.2
,1.33
,2.35
,0.89
,1
,-1.8
,1.29
,1.57
,3.32
,0.97
,2.48
,1.63
,-0.54
,-0.12
,-0.44
,1.06
,0.23
,2.33
,1.87
,1.65
,1.57
,1.02
,1.36
,1.99
,2.17
,0
,-0.88
,-1.7
,1.55
,2.26
,-2.52
,-0.27
,-2.53
,0.41
,-1.41
,1
,1.52
,-1.6
,-1.43
,-0.5
,-6.01
,-8.19
,-3.66
,-1.5
,0.37
,-1.41
,1.28
,3.19
,4.74
,1.53
,2.8
,2.3
,3.76
,0.54
,1.26
,2.36
,0.69
,0.81
,3.04
,1.74
,-2.66
,-1.09
,1.97
,-0.38
,2.52
,1.86
,0.26
,2.67
,1.68
,1.36
,0.36
,1.19
,-0.47
,-1.02
,-0.19
,-3.72
,-3.94
,3.03
,-1.18
,-0.21
,2.53
,1.86
,0.39
,-0.33
,-2.33
,0.18
,0.47
,1.14
,1.28
,0.43
,1.02
,1.99
,2.29
,0.36
,1
,0.99
,1.84
,-1.03
,1.39
,-0.24
,1.54
,1.51
,1.04
,1.19
,0
,1.87
,0.11
,0.06
,0.92
,1.32
,-0.64
,0.36
,-1.44
,-1.67
,0.52
,-0.27
,-1.06
,2.53
,0.38
,1.38)
par8 = '1'
par7 = '0'
par6 = '0'
par5 = '1'
par4 = '1'
par3 = '0'
par2 = '0'
par1 = '0.0'
ylab = 'y'
xlab = 'x'
par8 <- '1'
par7 <- '0'
par6 <- '0'
par5 <- '1'
par4 <- '1'
par3 <- '0'
par2 <- '0'
par1 <- '0.0'
#'GNU S' R Code compiled by R2WASP v. 1.2.327 ()
#Author: root
#To cite this work: Wessa P., (2013), Bivariate Granger Causality (v1.0.3) 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
#
library(lmtest)
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
y
(gyx <- grangertest(y ~ x, order=par8))
(gxy <- grangertest(x ~ y, order=par8))
postscript(file="/var/wessaorg/rcomp/tmp/1bgal1432227415.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)'))
(r <- ccf(x,y,main='Cross Correlation Function (transformed and differenced)',ylab='CCF',xlab='Lag (k)'))
par(op)
dev.off()
postscript(file="/var/wessaorg/rcomp/tmp/2sfnc1432227415.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()
postscript(file="/var/wessaorg/rcomp/tmp/3fvhk1432227415.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()

#Note: the /var/wessaorg/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab
load(file="/var/wessaorg/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/wessaorg/rcomp/tmp/4ypi31432227415.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/wessaorg/rcomp/tmp/5xp4x1432227415.tab") 

try(system("convert tmp/1bgal1432227415.ps tmp/1bgal1432227415.png",intern=TRUE))
try(system("convert tmp/2sfnc1432227415.ps tmp/2sfnc1432227415.png",intern=TRUE))
try(system("convert tmp/3fvhk1432227415.ps tmp/3fvhk1432227415.png",intern=TRUE))

