library(knitr)
library(vars)
## Loading required package: MASS
## Loading required package: strucchange
## Loading required package: zoo
## Attaching package: 'zoo'
## The following object is masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
library(tsDyn)
## Loading required package: mgcv
## This is mgcv 1.7-24. For overview type 'help("mgcv-package")'.
## Loading required package: Matrix
## Loading required package: lattice
## Loading required package: snow
## Loading required package: mnormt
## Loading required package: foreach
## Loading required package: nlme
Step 1. Setup the Experimental Coefficient Matrices and Variance-Covariance Relations for the Bivariate VAR(1) (Work in Progress for the Specification of the Null Hypothesis for causality)
# Choose matrix of coefficients
B1 <- matrix(c(0.6, 0.3, 0.5, -0.4), 2)
# Choose variance-covariance matrix
varcov <- matrix(c(1, 0.5, 0.3, 1), 2)
# Choose sample size with the no threshold
var1 <- TVAR.sim(B = B1, nthresh = 0, n = 500, lag = 1, include = "none", varcov = varcov,
show.parMat = TRUE)
## [,1] [,2] [,3] [,4]
## [1,] 0 0 0.6 0.5
## [2,] 0 0 0.3 -0.4
Step 2. Estimate the Bivariate VAR(1)
# Estimate the Bivariate VAR lag
VARselect(var1, lag.max = 5)
## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 1 1 1 1
##
## $criteria
## 1 2 3 4 5
## AIC(n) -0.1553 -0.14396 -0.13391 -0.12357 -0.11139
## HQ(n) -0.1353 -0.11062 -0.08723 -0.06355 -0.03803
## SC(n) -0.1044 -0.05902 -0.01499 0.02932 0.07548
## FPE(n) 0.8561 0.86592 0.87467 0.88376 0.89460
# Estimate the VAR(1)
varSim <- VAR(var1, p = 1)
## Warning: No column names supplied in y, using: y1, y2 , instead.
# Estimate the causality
causality(varSim, cause = "y2")
## $Granger
##
## Granger causality H0: y2 do not Granger-cause y1
##
## data: VAR object varSim
## F-Test = 160.4, df1 = 1, df2 = 992, p-value < 2.2e-16
##
##
## $Instant
##
## H0: No instantaneous causality between: y2 and y1
##
## data: VAR object varSim
## Chi-squared = 55.72, df = 1, p-value = 8.371e-14
Figure 1. XY Plot of the Relationship between Y1 and Y2
plot(var1, type = "l", col = c(1, 2))
Step 3. Simulate with Bivariate Threshold TVAR(1) with experimental coefficient values (Work in Progress)
B2 <- rbind(c(0.4, 0.2, 0.2, 0.1), c(0.2, 0.2, 0.3, -0.2))
varThreshold <- TVAR.sim(B = B2, nthresh = 1, n = 500, lag = 1, include = "none",
varcov = varcov, show.parMat = TRUE)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 0 0 0 0 0.4 0.2 0.2 0.1
## [2,] 0 0 0 0 0.2 0.2 0.3 -0.2
Step 4. Estimate the Linear Causality with the simulated Threshold Variables
# Estimate the var
varSimThreshold <- VAR(varThreshold, p = 1)
## Warning: No column names supplied in y, using: y1, y2 , instead.
# Estimate the causality
causality(varSimThreshold, cause = "y2")
## $Granger
##
## Granger causality H0: y2 do not Granger-cause y1
##
## data: VAR object varSimThreshold
## F-Test = 76.09, df1 = 1, df2 = 992, p-value < 2.2e-16
##
##
## $Instant
##
## H0: No instantaneous causality between: y2 and y1
##
## data: VAR object varSimThreshold
## Chi-squared = 104.5, df = 1, p-value < 2.2e-16
Work in development at The Cromwell Workshop - www.cromwellworkshop.com by Jeff B. Cromwell, PhD