Beruflich Dokumente
Kultur Dokumente
Sunil Paul
November 8, 2016
Introduction
This note explains the application of VAR using vars package in R. I try to
estimate the dynamic effects of aggregate demand and supply shocks on industrial production and the inflation rate using VAR (See Enders(4th Edition)Chapter 5- Question 11 -pp 340). This is a bi-variate VAR and uses data
on logarithmic change in the index of industrial production(IIP) as DIIPt =
ln(IIPt ) ln(IIPt1 ) and the inflation rate (as measured by the CPI) as
IN Ft = log(CP It ) log(CP It1 ).The data on these variable are given in
varQ11.csv.
It is always advisable to pretest the variables for its properties. For a stable
VAR the variables should be stationary. As a first step we plot the series.
setwd("H:/RWD/Applied/2015-16/H18")
options(digits=6, width=70,scipen=999)
#Load required packages
library(tseries)
library(vars)
library(urca)
library(xtable)
library(ggfortify)
# Load data files
mydata<-read.csv("varQ11.csv", header = TRUE, sep=",")
data1 = ts(data=mydata[,2-3], frequency =4, start=c(1960,2))
autoplot(data1)
DIIP
0.025
0.000
0.025
0.050
INF
0.01
0.00
0.01
1960
1980
2000
Pn
We use an ADF specification with drift (Yt = Yt1 + t1 Yti + t
) to test the unit roots. Number of lags in the equation were selected based
on Akaike Information Criterion(AIC). The results of this test is summarized
in the Table 1 below. According to the results we reject the null of unit root
for both DIIP and INF ( < 5% critical value for both the series) and both
the series are I(0). (I will present only the relevant results for the discussion.
You can get the detailed results by typing the name of the objects in console
directly once you run the codes. I will mention the name of the object I used
for your reference). Use summary(adfiip) and summary(adfinf) for details of
ADF test.
adfiip <- ur.df(data1[,"DIIP"], type = "drift", lags = 8,
selectlags=c("AIC"))
adfinf <- ur.df(data1[,"INF"], type = "drift", lags = 8,
selectlags=c("AIC"))
value1 <- summary(adfiip)@teststat[1,1]
c.v1 <- summary(adfiip)@cval[1,2]
DF2 <- data.frame(value1,c.v1)
DF2[2,1] <- summary(adfinf)@teststat[1,1]
DF2[2,2] <- summary(adfinf)@cval[1,2]
rownames(DF2) <- c("$DIIP_t$","$INF_t$")
2
DIIPt
IN Ft
-6.16
-2.98
c.v
-2.88
-2.88
In this step we will choose the optimum lag length of variables in the VAR.
We may choose this based on multivariate information criteria such as AIC,
Hannan Quinn (HQ) or Schwartz Bayesian Criterion(SC). Check infocrit for
the results. I have summarized AIC, SC and HQ in Table 2.
infocrit<-VARselect(data1,lag.max=8,type="const")
info1<-as.data.frame(infocrit$criteria[-4,])
library(lsr)
## Warning:
info2<-tFrame(info1)
rownames(info2)<-c("lag 1","lag 2","lag 3","lag 4","lag 5",
"lag 6","lag 7", "lag 8")
colnames(info2)<-c("AIC","HQ","SC")
info3<-round(info2,3)
The results indicate AIC is minimum when lags=5 (-21.218). But HQ (21.116) and SC (-20.980) shows that the optimum lag length is 3. We choose
lag 3 to estimate the VAR. We can re estimate the model if errors are not free
from auto correlations.
xtable(info3,caption="Lag Selection Criterion")
Estimation
(1)
lag
lag
lag
lag
lag
lag
lag
lag
1
2
3
4
5
6
7
8
AIC
-21.07
-21.11
-21.21
-21.21
-21.22
-21.20
-21.17
-21.15
HQ
-21.03
-21.04
-21.12
-21.09
-21.07
-21.02
-20.97
-20.93
SC
-20.97
-20.94
-20.98
-20.92
-20.86
-20.77
-20.68
-20.60
lag.max = 8, ic = "SC")
The coefficients of both INF and DIIP equation are given in Table 3 .The
reduced form coefficients are not that important and we may get insignificant
coefficients since the system is symmetric by construction.
Vpvalues<-lapply(coef(varest),"[",,"Pr(>|t|)")
Vcoeff<-lapply(coef(varest), "[" ,, "Estimate")
VINF<-round(cbind(Vcoeff$INF,Vpvalues$INF),2)
VDIIP<-round(cbind(Vcoeff$DIIP,Vpvalues$DIIP),2)
colnames(VDIIP)<-c("Coef.","p-values")
colnames(VINF)<-c("Coef.","p-values")
rownames(VINF)<-c("DIIP(t-1)","INF(t-1)",
"DIIP(t-2)","INF(t-2)",
"DIIP(t-3)","INF(t-3)","c")
rownames(VDIIP)<-c("DIIP(t-1)","INF(t-1)",
"DIIP(t-2)","INF(t-2)",
"DIIP(t-3)","INF(t-3)","c")
library(tables)
st <- rbind(data.frame(VDIIP, Equation = 'DIIP',
what = factor(rownames(VDIIP),
levels = rownames(VDIIP)),
4
latex(mytable)
DIIP(t-1)
INF(t-1)
DIIP(t-2)
INF(t-2)
DIIP(t-3)
INF(t-3)
c
Equation
DIIP
INF
Coef. p-values Coef. p-values
0.63
0.00
0.04
0.00
0.53
0.20
0.56
0.00
0.18
0.03
0.02
0.07
0.18
0.70
0.02
0.83
0.07
0.29
0.01
0.28
0.73
0.08
0.32
0.00
0.01
0.00
0.00
0.39
Diagnostics Tests
Before going for structural analysis we need to check the properties of VAR
system. The stability of VAR is examined using the eigen values of companion
coefficient matrix 1 (See my notes). The roots() returns moduli of eigen
values. For stability the eigen values should be less than |1|.
(Vroots<-roots(varest))
## [1] 0.883517 0.622260 0.606656 0.606656 0.398837 0.398837
# Testing serial correlation
ser11 <- serial.test(varest, lags.pt =12 , type = "PT.asymptotic")
ser11
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object varest
## Chi-squared = 46.81, df = 36, p-value = 0.107
5
Forecasting
0.06
0.00
50
100
150
200
plot(pdict, names="INF")
0.010
0.010
50
100
150
200
Innovation Accounting
7.1
(2)
, , 1
[1,]
[2,]
[,1] [,2]
1
0
0
1
, , 2
[,1]
[,2]
[1,] 0.6315657 -0.525201
[2,] 0.0422774 0.559711
, , 3
[,1]
[,2]
[1,] 0.195926 -0.444083
[2,] 0.026283 0.307271
, , 4
7
##
##
[,1]
[,2]
## [1,] 0.0762452 -0.977192
## [2,] 0.0205420 0.495736
DIIP
1
DIIPt
+
=
0
IN Ft
IN F
0 e1t
0.63 0.53 e1t1
+
+ ...
1 e2t
0.04 0.56
e2t1
Forecast error IRF traces the dynamic path of DIIPt and IN Ft due to
t
t
= 1, 12.0 = DIIP
=
one unit shock in reduced form errors (11.0 = DIIP
e1t
e2t
IN Ft
IN Ft
0, 21.0 = e1t = 0 and 22.0 = e2t = 1). Similarly, we can interpret
t
elements of 1 , 2 ..., for example, 11.1 = DIIP
e1t1 = 0.63. Also remember
DIIPt+1
t
.
11.i = DIIP
e1ti =
e1t
As you know the reduced form errors are functions of structural errors and
hence they are correlated. Remember et = B 1 t and t is the errors from
structural model (eq.4). The impulse response given by (Forecast error impulse responses) doesnt make any economic sense. To get meaningful IRF we
need to use structural errors.
As we discussed in the class the Structural VAR can not be directly estimated. We have to use estimates of reduced form VAR to recover the parameters of SVAR. Thus to calculate IRF from structural errors we need to identify
the structural errors. This can be done by Cholesky decomposition of variance
co-variance matrix of reduced form VAR (Recursive VAR).
7.1.1
= 1
2
0 2
. We have already estimated the reduced form
Yt = A0 + A1 Yt1 + A2 Yt2 + A3 Yt3 + et
where A0 = B 1 0 , A1 = B 1 1 and et = B 1 t with
2
1 12
0.00015 0.0000032
e =
=
12 22
0.0000032 0.0000040
You can get the estimates of e using the code below.
sigma_e<-summary(varest)$covres
e = B 1 B 10
(4)
as et = B 1 t .
1 b12
Given B =
we have
b21 1
2
2
1
1 12
1
b12 1
=
12 22
1
0
b21
1
0 1
2
2
b12
b21
1
(5)
i.e.
a11
a12
..
.
a12
a22
..
.
...
...
..
.
u11
a1n
u12
a2n
.. = ..
. .
0
u22
..
.
...
...
..
.
0
0
..
.
an1
an2
...
ann
u1n
u2n
...
unn
u11
0
..
.
0
u12
u22
..
.
...
...
..
.
u1n
u2n
.. (6)
.
...
unn
" a
11
a12
= a12
a22
a
11
# "
0
q
a22
a212
a11
a12
a11
a11
0
a22
#
(7)
a212
a11
1
12
1
=
22
b21
b12
1
2
1
0
0 1
1
2
2
b12
b21
1
e = B 1 2 2 B 10
(8)
12 22
b21 1
0 2
0 2 0
1
12
12
12
1
=
22
b21 1
0
2
1
0
b21 1
2
e = P P 0
(9)
(10)
1
2
Xt
1
0 0
b21 1 0 Yt
b31 b32 1
Wt
. Here Wt is contemporaneously affected Yt and Xt and corresponding coefficients in B matrix are nonzero, Yt is a function of Xt but not Wt . Similarly Xt
is not contemporaneously affected by the other two variables.) In our example,
we require only one restriction on B matrix. Since DIIP come first the DIIP is
not contemporaneously affected by INF. The equation 9 can be solved to get
the following expressions(We discussed it in the class earlier from a different
perspective):
2
1
= 12
2
2
= 22 b21 12
12
12
b21 = 2 = 2
1
1
10
library(OpenMx)
## Warning:
## Warning:
sigma_epsilon<-Diagonal(n=2, x=diag2vec(chol_sigma))
(sigma_epsilon<-as.matrix(sigma_epsilon))
##
[,1]
[,2]
## [1,] 0.0122812 0.00000000
## [2,] 0.0000000 0.00197556
1
Given P = B 1 2 we have B = 2 P 1
(Bmat<-sigma_epsilon%*%solve(chol_sigma))
##
DIIP INF
## [1,] 1.0000000
0
## [2,] -0.0210857
1
To get the structural IRF we need to express VMA representation
Yt = + 0 et + 1 et1 + 2 et2 + . . .
in terms of structural errors. Assuming cholesky decomposition we can recover
the structural errors restricting the B matrix.
Yt = + 0 B 1 t + 1 B 1 t1 + 2 B 1 t2 + . . .
(11)
(12)
(13)
11
Psi(varest, nstep=1)
##
##
##
##
##
##
##
##
##
##
##
, , 1
[,1]
[,2]
[1,] 0.012281152 0.00000000
[2,] 0.000258957 0.00197556
, , 2
[,1]
[,2]
[1,] 0.007620351 -0.00103757
[2,] 0.000664156 0.00110574
n-step head response of each variables due to one standard deviation shocks
in structural errors can be obtained using irf() function also. I have generated
plots of IRF below, you may check the impulse response stored in impulse.
The impulse response should coverage to zero for a stable VAR.You can also
generate cumulative IRF( see the documentation of irf())
impulse<-irf(varest)
irf1<-data.frame(impulse$irf$DIIP[,1],impulse$Lower$DIIP[,1],
impulse$Upper$DIIP[,1])
irf2<-data.frame(impulse$irf$DIIP[,2],impulse$Lower$DIIP[,2],
impulse$Upper$DIIP[,2])
irf3<-data.frame(impulse$irf$INF[,1],impulse$Lower$INF[,1],
impulse$Upper$INF[,1])
irf4<-data.frame(impulse$irf$INF[,2],impulse$Lower$INF[,2],
impulse$Upper$INF[,2])
par(mfrow=c(2,2), bg="azure2")
matplot(irf1, type="l", lwd=2, col="blue2",
xlab="Quarters", ylab="Response of DIIP",
main="Shocks of DIIP", lty=c(1,2,2))
matplot(irf2, type="l",lwd=2, col="red2",
ylab="Response of INF",main="Shocks of DIIP",
xlab="Quarters",lty=c(1,2,2))
matplot(irf3, type="l", lwd=2, col="blue2",
ylab="Response of DIIP",main="Shocks of INF",
xlab="Quarters",lty=c(1,2,2))
matplot(irf4, type="l", lwd=2, col="red2",
ylab="Response of INF",main="Shocks of INF",
xlab="Quarters",lty=c(1,2,2))
12
0.0008
0.0004
0.0000
Response of INF
0.010
0.005
10
Quarters
Shocks of INF
Shocks of INF
0.004
0.0000
0.0010
Response of INF
0.002
10
0.0020
Quarters
0.000
Response of DIIP
Shocks of DIIP
0.000
Response of DIIP
Shocks of DIIP
10
Quarters
10
Quarters
par(mfrow=c(1,1))
7.1.2
Variance decomposition separates the variation in an endogenous variable into
the component shocks to the VAR. Thus, the variance decomposition provides
information about the relative importance of each random innovation in affecting the variables in the VAR. See my notes for details. You can compare the
results with table given in Enders pp 341.(Multiply the results given in fevd.p1ct
with 100)
# Forecast Error Variance
fevd.p1ct<-fevd(varest,n.ahead=10)
fevd.p1ct
13
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
$DIIP
[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]
$INF
[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]
DIIP
1.000000
0.994873
0.991452
0.974689
0.950849
0.935843
0.922841
0.910446
0.901185
0.894069
DIIP
0.0168917
0.0902005
0.1087028
0.1121267
0.1293306
0.1415157
0.1472984
0.1530726
0.1579475
0.1611230
INF
0.00000000
0.00512707
0.00854766
0.02531077
0.04915107
0.06415650
0.07715917
0.08955398
0.09881469
0.10593149
INF
0.983108
0.909799
0.891297
0.887873
0.870669
0.858484
0.852702
0.846927
0.842052
0.838877
########
Questions
You may try to solve the questions 6 to 11 for practice. I will sent the solutions
after the third assessment test.
14