-
Notifications
You must be signed in to change notification settings - Fork 3
/
Standard_SVAR.R
434 lines (379 loc) · 15.5 KB
/
Standard_SVAR.R
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
library(tseries)
library(vars)
library(moments)
library(tidyverse)
library(readxl)
library(AER)
library(svars)
library(backtest)
library(stargazer)
library(xtable)
setwd("C:/Users/Tom/OneDrive/Documents/Thesis Project/Thesis Data")
data = read_excel("Prelim_Dataset.xlsx", sheet = 1)
COD = ts(data[,2], start = 1987, frequency = 12) #Consumer Discretionary
ENE = ts(data[,3], start = 1987, frequency = 12) #Energy
FIN = ts(data[,4], start = 1987, frequency = 12) #Financials
UTL = ts(data[,5], start = 1987, frequency = 12) #Utilities
HCR = ts(data[,6], start = 1987, frequency = 12) #Healthcare
INT = ts(data[,7], start = 1987, frequency = 12) #Information Technology
RES = ts(data[,8], start = 1987, frequency = 12) #Real Estate
IND = ts(data[,9], start = 1987, frequency = 12) #Industrials
MAT = ts(data[,10], start = 1987, frequency = 12) #Materials
CST = ts(data[,11], start = 1987, frequency = 12) #Consumer Stationary
TEL = ts(data[,12], start = 1987, frequency = 12) #Telecommunications
COMP = ts(data[,13], start = 1987, frequency = 12) #US Composite
WTI = ts(data[,14], start = 1987, frequency = 12) #WTI Oil
HTO = ts(data[,15], start = 1987, frequency = 12) #No.2 Heating Oil
IP = ts(data[,16], start = 1987, frequency = 12) #Industrial Production
IR = ts(data[,17], start = 1987, frequency = 12) #Fed Funds Effective Interest Rate
CPI = ts(data[,18], start = 1987, frequency = 12) #CPI
WMKT = ts(data[,19], start = 1987, frequency = 12) #World Market Composite
TEN = ts(data[,20], start = 1987, frequency = 12) #10-Year Treasury Rate
y1 = ts(c(NA, diff(log(COMP))), start=1987, frequency=12) #y1 = COMP return
y2 = ts(c(NA, diff(log(COD))), start=1987, frequency=12) #y2 = COD return
y3 = ts(c(NA, diff(log(CST))), start=1987, frequency=12) #y3 = CST return
y4 = ts(c(NA, diff(log(ENE))), start=1987, frequency=12) #y4 = ENE return
y5 = ts(c(NA, diff(log(FIN))), start=1987, frequency=12) #y5 = FIN return
y6 = ts(c(NA, diff(log(HCR))), start=1987, frequency=12) #y6 = HCR return
y7 = ts(c(NA, diff(log(IND))), start=1987, frequency=12) #y7 = IND return
y8 = ts(c(NA, diff(log(INT))), start=1987, frequency=12) #y8 = INT return
y9 = ts(c(NA, diff(log(MAT))), start=1987, frequency=12) #y9 = MAT return
y10 = ts(c(NA, diff(log(RES))), start=1987, frequency=12) #y10 = RES return
y11 = ts(c(NA, diff(log(TEL))), start=1987, frequency=12) #y11 = TEL return
y12 = ts(c(NA, diff(log(UTL))), start=1987, frequency=12) #y12 = UTL return
x1 = ts(c(NA, diff(log(WTI))), start=1987, frequency=12) #x1 = WTI return
iv1 = ts(c(NA, diff(log(HTO))), start=1987, frequency=12) #iv1 = HTO return
c1 = ts(c(NA, diff(log(IP))), start=1987, frequency=12) #c1 = IP growth rate
c2 = ts(c(NA, diff(IR)), start=1987, frequency=12) #c2 = IR monthly change
c3 = ts(c(NA, diff(log(CPI))), start=1987, frequency=12) #c3 = Inflation Rate
c4 = ts(c(NA, diff(log(WMKT))), start=1987, frequency=12) #c4 = WMKT return
c5 = ts(c(NA, diff(log(TEN))), start=1987, frequency=12) #c5 = TEN monthly change
variables.df = data.frame("y1", 'y2', 'y3', 'y4', 'y5', 'y6', 'y7', 'y8', 'y9', 'y10', 'y11', 'y12', 'x1', 'iv1', 'c1', 'c3', 'c4', 'c5')
# y1
tsmat = window(ts.union(y1, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
VARselect(tsmat, lag.max = 12, type="const")
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs1 = irfy.df[2]
irfs1 = irfs1[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs1, xlim=c(1.4, 11.6), ylim=c(-1.95, 1.48), xlab = "", ylab = "Market Return", main = "US Total Market", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs1, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y2
tsmat = window(ts.union(y2, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs2 = irfy.df[2]
irfs2 = irfs2[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs2, xlim=c(1.4, 11.6), ylim=c(-1.3, 0.5), xlab = "", ylab = "Market Return", main = "Consumer Discretionary", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs2, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y3
tsmat = window(ts.union(y3, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs3 = irfy.df[2]
irfs3 = irfs3[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs3, xlim=c(1.4, 11.6), ylim=c(-1.7, 0.85), xlab = "", ylab = "Market Return", main = "Consumer Stationary", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs3, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y4
tsmat = window(ts.union(y4, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs4 = irfy.df[2]
irfs4 = irfs4[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs4, xlim=c(1.4, 11.6), ylim=c(-0.25, 3.6), xlab = "", ylab = "Market Return", main = "Energy", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs4, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y5
tsmat = window(ts.union(y5, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs5 = irfy.df[2]
irfs5 = irfs5[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs5, xlim=c(1.4, 11.6), ylim=c(-0.95, 1.05), xlab = "", ylab = "Market Return", main = "Financials", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs5, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y6
tsmat = window(ts.union(y6, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs6 = irfy.df[2]
irfs6 = irfs6[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs6, xlim=c(1.4, 11.6), ylim=c(-0.75, 1.38), xlab = "", ylab = "Market Return", main = "Healthcare", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs6, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y7
tsmat = window(ts.union(y7, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs7 = irfy.df[2]
irfs7 = irfs7[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs7, xlim=c(1.4, 11.6), ylim=c(-0.85, 1.153), xlab = "", ylab = "Market Return", main = "Industrials", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs7, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y8
tsmat = window(ts.union(y8, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs8 = irfy.df[2]
irfs8 = irfs8[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs8, xlim=c(1.4, 11.6), ylim=c(-3.9, 0.25), xlab = "", ylab = "Market Return", main = "Information Technology", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs8, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y9
tsmat = window(ts.union(y9, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs9 = irfy.df[2]
irfs9 = irfs9[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs9, xlim=c(1.4, 11.6), ylim=c(-0.25, 3.0), xlab = "", ylab = "Market Return", main = "Materials", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs9, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y10
tsmat = window(ts.union(y10, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs10 = irfy.df[2]
irfs10 = irfs10[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs10, xlim=c(1.4, 11.6), ylim=c(-0.55, 1.25), xlab = "", ylab = "Market Return", main = "Real Estate", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs10, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y11
tsmat = window(ts.union(y11, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs11 = irfy.df[2]
irfs11 = irfs11[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs11, xlim=c(1.4, 11.6), ylim=c(-1.3, 0.65), xlab = "", ylab = "Market Return", main = "Telecommunications", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs11, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
# y12
tsmat = window(ts.union(y12, x1, c1, c3, c4, c5), start = c(1987, 2), frequency = 12)
var1 = VAR(y = tsmat, p=1)
amat = diag(6)
diag(amat) = NA
svar1 = SVAR(var1, estmethod="direct", Amat = amat)
irfs = irf(svar1, n.ahead = 12, cumulative = FALSE, ci = 0.95, boot=TRUE, runs=100)
irfy = irfs$irf[1]
irfy.df = as.data.frame(irfy)
sel = irfs$Lower[1]
sel.df = as.data.frame(sel)
seu = irfs$Upper[1]
seu.df = as.data.frame(seu)
irfs12 = irfy.df[2]
irfs12 = irfs12[2:13,]*100
lb = sel.df[2]
lb = lb[2:13,]*100
ub = seu.df[2]
ub = ub[2:13,]*100
response.time = c(1:12)
plot(response.time, irfs12, xlim=c(1.4, 11.6), ylim=c(-0.7, 1.45), xlab = "", ylab = "Market Return", main = "Utilities", type = "l", cex.lab=1.2, cex.axis=1.2)
polygon(c(response.time, rev(response.time)), c(lb, rev(ub)), col='grey90', border=FALSE)
lines(response.time, irfs12, lwd=2)
lines(response.time, lb, col='red', lwd=2, lty=2)
lines(response.time, ub, col='red', lwd=2, lty=2)
abline(h=0, col="blue", lwd=2, lty=2)
grid()
irfs_all = cbind(irfs1, irfs2, irfs3, irfs4, irfs5, irfs6, irfs7, irfs8, irfs9, irfs10, irfs11, irfs12)
irf.df = as.data.frame(irfs_all)
stargazer(irf.df, summary=FALSE)