-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvarex.Rmd
executable file
·289 lines (181 loc) · 5.98 KB
/
svarex.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
---
title: "SVAR Example Using R"
author: "Emmanuel Davila"
date: "7/4/2022"
header-includes:
- \usepackage{setspace}\doublespacing
output:
html_document: default
pdf_document: default
always_allow_html: true
---
This is a simple example of how to make a Structural Vector Autoregression in R.
I used this [video](https://www.youtube.com/watch?v=hwB2TbhqgH0&t=410s) by
Justin Eloriagaas as a guide to make this.
## Loading Packages and the Data Set
<br>
First we will load the relevant packages to perform our analysis.
<br>
```{r, results='hide', warning=FALSE, message=FALSE}
library(urca)
library(vars)
library(mFilter)
library(tseries)
library(TSstudio)
library(forecast)
library(tidyverse)
```
<br>
Next we will load our data set. All data was obtained from the **Federal Reserve
Economic Database (FRED)** website. All data is quarterly from Q1 1970 to Q1 2022.
I will provide the .csv file. The data includes the US output gap, US CPI, and
the Federal Funds Rate. You will need to replace *~/Documents/Rprojects/svardataex.csv* with
your own file path.
<br>
```{r}
macro <- read.csv("~/Documents/Rprojects/svardataex.csv")
head(macro)
```
<br>
## Creating Time Series Objects and Plotting Them
<br>
Now that the data is loaded onto R, we will transform the data into Time Series
Objects using ts(). We use *start = c(1970,1,1), frequency = 4* to signify that
our data starts on 1970 January 1, and the frequency is quarterly.
<br>
```{r}
y <- ts(macro$output_gap, start = c(1970,1,1), frequency = 4)
pi <- ts(macro$core_cpi, start = c(1970,1,1), frequency = 4)
r <- ts(macro$fed_funds_rate, start = c(1970,1,1), frequency = 4)
```
<br>
Lets plot the three series' to make sure they look like they're supposed to.
<br>
```{r, fig.align='center'}
plot(y, main = "Output Gap", xlab = "Time", ylab = "Output Gap")
plot(pi, main = "Inflation Rate", xlab = "Time", ylab = "CPI")
plot(r, main = "Federal Funds Rate", xlab = "Time", ylab = "FFR")
```
<br>
## Applying Restrictions to the SVAR
<br>
One of the most important parts in creating our SVAR is imposing the restrictions.
This is done by creating a matrix and ordering it according to economic
principles and intuition. The matrix represents contemporaneous shocks affecting
the variables in the systems.
<br>
<center>$$\begin{bmatrix}
y & 0 & 0 \\
a_{21} & pi & 0 \\
a_{31} & a_{32} & ffr
\end{bmatrix}$$</center>
<br>
The figure above is what our matrix will look like. The output gap (first column) can
influence both inflation (second column) and the ffr (third column) in the same period.
Inflation influences the ffr but not the output gap in the same period (hence the free
variable to the left of pi). The ffr does not influence either in the same period
(hence the two free variables to the left of ffr). Therefore, the zeros in the upper
triangular part of the matrix are our restrictions.
<br>
Now we can start creating our matrix. First, we create an **Identity** matrix.
<br>
```{r, fig.align='center'}
amat <- diag(3)
amat
```
<br>
Then, we assign the free variables to the lower triangular part.
<br>
```{r, fig.align='center'}
amat[2,1] <- NA
amat[3,1] <- NA
amat[3,2] <- NA
amat
```
<br>
Once we estimate our SVAR, the free variables will fill up and replace the *NA*s.
<br>
## Building the Model
<br>
It's finally time to build our model. We will follow the economic intuition we
discussed previously to bind our variables.
<br>
```{r}
svar1 <- cbind(y, pi, r)
colnames(svar1) <- c('OutputGap', 'CPI', 'FFR')
head(svar1)
```
<br>
We also have to select the number of lags that we will use in the model.
For this, we will use the *VARselect()* function. Since this is quarterly data,
we expect a lag order of around four to six. So we will specify a max of
eight lags, just to be safe.
<br>
```{r}
lagselect <- VARselect(svar1, lag.max = 8, type = "both")
lagselect$selection
```
<br>
So according to AIC, SBIC, HQIC, and FPE selection criteria, the optimal number of
lags for the model is 5.
<br>
## Estimating the Model
<br>
Now that the framework for our model is built, we can estimate it. First, we'll need
to estimate a regular VAR so we can later impose our restrictions.
<br>
```{r}
varmodel1 <- VAR(svar1, p = 5, season = NULL, exog = NULL, type = "const")
varmodel1
```
<br>
Next, we will estimate the actual SVAR model. In the *Amat=* option, we will
input the matrix we created earlier for our restrictions (amat).
<br>
```{r}
svarmodel1 <- SVAR(varmodel1, Amat = amat, Bmat = NULL, hessian = TRUE,
estmethod = c("scoring", "direct"))
svarmodel1
```
<br>
## Impulse Response Functions
### Telling the Story
<br>
The final step is to plot our impulse response functions (irf). First, we will see
what a positive exogenous shock does to the output gap.
<br>
```{r, warning=FALSE, message=FALSE}
svarog <- irf(svarmodel1, impulse = "OutputGap", response = "OutputGap")
svarog
plot(svarog)
```
<br>
As expected, a positive shock to the output gap causes the gap to increase initially.
But almost immediately it starts to slowly decrease.
<br>
Now let's see how inflation (CPI) responds to a positive shock caused by the output gap.
<br>
```{r, warning=FALSE, message=FALSE}
svarinf <- irf(svarmodel1, impulse = "OutputGap", response = "CPI")
svarinf
plot(svarinf)
```
<br>
Inflation increases due to the output gap increase. The increase in the output gap
overheats the economy, causing inflationary pressures.
<br>
Now we'll see what the Federal Reserve will do to limit inflation.
<br>
```{r, warning=FALSE, message=FALSE}
svarffr <- irf(svarmodel1, impulse = "CPI", response = "FFR")
svarffr
plot(svarffr)
```
<br>
This response is unexpected. Usually, central banks will fight inflation by increasing
their policy rate. Here we see that the ffr is decreasing. This might be an error, or possibly
there is a story to tell. This could be the result of the Volcker Disinflation period.
<br>
Hopefully this helps anyone wanting to see an example of how to use SVARs in R.
<br>
<br>