Sie sind auf Seite 1von 14

Tutorial: Vector Auto Regressions

Applied Financial Econometrics

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.

Pre-testing the variables

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

colnames(DF2) <- c("$\\tau_{\\mu}$","c.v")


print(xtable(round(DF2,3),caption="Summary of ADF test statistics"),
sanitize.colnames.function = identity,
sanitize.rownames.function = identity)

DIIPt
IN Ft

-6.16
-2.98

c.v
-2.88
-2.88

Table 1: Summary of ADF test statistics

Selection of Lag length

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:

package lsr was built under R version 3.3.2

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

In this step we estimate the reduced form VAR


Yt = A0 + A1 Yt1 + A2 Yt2 + A3 Yt3 + et
3

(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

Table 2: Lag Selection Criterion


where Yt is a vector with DIIP and INF as elements. Ordering of these variables
are important for recovering the structural parameters( we will discuss it later).
We can directly specify the lag length as an argument in VAR() while estimating. There is also an option to specify the information criteria to choose the
lag length (see the documentation of VAR() for details of these arguments). The
following code include an argument to choose the optimum lag length based on
SC. summary(varest) will give all required details of estimated VAR such as
estimated coefficients, R2 , variance co-variance matrix of residuals etc.
varest <-VAR(data1,type = "const",

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

row.names= NULL, check.names = FALSE),


data.frame(VINF, Equation = 'INF',
what = factor(rownames(VINF),
levels = rownames(VINF)),
row.names = NULL,check.names = FALSE))
mytable <- tabular(Heading()*what ~ Equation*
(`Coef.` +`p-values`)*
Heading()*(identity),data=st)

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

# fail to reject the null of no serial correlation


The VAR is stable, since all the roots of companion coefficient matrix is
less than |1|. Multivariate Portmanteau- and Breusch-Godfrey test for serially
correlated errors also indicates there is no auto correlation left in the errors (We
fail to reject the null of no serial correlation as p- values are greater than 0.05).

Forecasting

One of the important objective of VAR is to get forecasts of variables under


consideration. We can make conditional forecasts based on the estimates of
reduced form VAR. (See pdict for the forecast values of each variable).
#Forecasting
pdict<-predict(varest, n.ahead=10,ci=.95)
plot(pdict, names="DIIP")

0.06

0.00

Forecast of series DIIP

50

100

150

200

plot(pdict, names="INF")

0.010

0.010

Forecast of series INF

50

100

150

200

Innovation Accounting

7.1

Impulse response functions

In a VAR, we are often interested in obtaining the impulse response functions.


Impulse responses trace out the response of current and future values of each
of the variables to a one-unit increase (or to a one- standard deviation increase,
when the scale matters) in the current value of one of the VAR errors, assuming
that this error returns to zero in subsequent periods and that all other errors
are equal to zero.
We have already estimated the reduced form VAR(eq 1)
Yt = A0 + A1 Yt1 + A2 Yt2 + A3 Yt3 + et
In lag notation we have
A(L)Yt = A0 + et
2

(2)

where A(L) = I (A1 L + A2 L + A3 L ).


This can be represented as Vector Moving average(VMA) form
Yt = + 0 et + 1 et1 + 2 et2 + . . .
(3)
P
where = A0 A(L)1 and A(L)1 =
i=0 i . Thus the coefficients in i
captures the effect of changes in et on Yt .(I used for these IRF in my notes,
but vars package use to refer forecast error IRF).
You can get the estimated i using the function Phi()
Phi(varest,nstep=3)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

, , 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

Identification using Cholesky Decomposition

We have already discussed the recursive system of identification in the class.


Here I am going to discuss how it is actually implemented. To begin with
consider
BYt = 0 + 1 Yt1 + 2 Yt2 + 3 Yt3 + t The variance co-variance matrix of
structural errors is

 2
0

= 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

where = 1 b12 b21 is the determinant of B.


Careful examination of above expression shows that we have to recover 4
unknown values in and B using the 3 known estimated values of e . In
order to recover the unknowns we have restrict some of the elements is B matrix.
Recursive VAR uses cholesky decomposition to recover the elements in . (If
we use restrictions based on economic theory it becomes structural VAR)
In general for each symmetric, positive definite matrix X where aij = aji , i 6=
j; the Choleski decomposition is an upper triangular matrix U such that
X = U 0U

(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

If we have X we can easily find the elements in U and vice versa.


In a 2 2 matrix the Cholesky factors can be expressed in terms of aij as
follows (note that a12 = a21 )

a11
a21

 " a
11
a12
= a12
a22

a
11

# "

0
q

a22

a212
a11

a12
a11

a11
0

a22

#
(7)

a212
a11

Coming back to our example we have


e = B 1 B 10
12
12



1
12
1
=
22
b21

b12
1

 2
1
0

 
0 1
1
2
2
b12

b21
1

this can be written as


1

e = B 1 2 2 B 10

(8)

In order to get Cholesky factor matrix we need to make B 1 a lower triangle


matrix. It can be done by restricting b12 = 0 ( = 1 0 b21 = 1)
 2
 




1 12
1
0 1 0
1 0
1 b21
=

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

where P = B 1 . The eq 9 is similar to X = U 0 U .


Thus in recursive VAR the Cholesky factors are used as estimates of unknowns and it is done by restricting the upper triangle of B matrix. In this
scheme, ordering of variables while estimating VAR matters.(For example consider a 3 3 B matrix.

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

You can get the cholesky factor of e (P ) using chol() function.


(chol_sigma<-t(chol(sigma_e)))
##
DIIP
INF
## DIIP 0.012281152 0.00000000
## INF 0.000258957 0.00197556
The diagonal of cholesky factor matrix gives the standard deviation of struc1
tural error i.e 2

10

library(OpenMx)
## Warning:
## Warning:

package OpenMx was built under R version 3.3.2


package Matrix was built under R version 3.3.2

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)

. The elements in i B 1 captures the effect of one unit shock in structural


errors in the endogenous variables. Usually the structural IRF is obtained for
one standard deviation shock in respective errors. This can be obtained as
follows: This can be represented as Vector Moving average(VMA) form
Yt = + 0 P t + 1 P t1 + 2 P t2 + . . .

(12)

where P = B 1 2 . Substituting i = i P we get


Yt = + 0 t + 1 t1 + 2 t2 + . . .

(13)

. The diagonal elements of 0 is nothing but standard deviations of t s.Remember


that 0 was an identity matrix.
You can use Psi() to obtain the elements of . You may cross check your
ans by checking 0 P = P .

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

Das könnte Ihnen auch gefallen