-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2_svariv_lpiv_chol_sn_estimation.R
319 lines (287 loc) · 14 KB
/
2_svariv_lpiv_chol_sn_estimation.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
#--------------------------------------------------------------------
#Model 1: SVAR-IV
#--------------------------------------------------------------------
#Impact of the FF shock on all variables
if (type == "baseline") {
iv_var_ff <- sovereign::VAR(data = cbind(vfci_data_mp["date"],vfci_data_mp[,c(vars_in_system_baseline)],vfci_data_mp[mp_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = mp_instrument,
instrumented = "fedfunds")
} else if (type == "stationary") {
iv_var_ff <- sovereign::VAR(data = cbind(vfci_data_mp["date"],vfci_data_mp[,c(vars_in_system_stationary)],vfci_data_mp[mp_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = mp_instrument,
instrumented = "fedfunds")
} else if (type == "vfci_lev") {
iv_var_ff <- sovereign::VAR(data = cbind(vfci_data_mp["date"],vfci_data_mp[,c(vars_in_system_vfci_lev)],vfci_data_mp[mp_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = mp_instrument,
instrumented = "fedfunds")
}
df_irf_ff <- sovereign::var_irf(iv_var_ff,
horizon = 20,
CI = c(0.05, 0.95))
df_irf_ff <- as.data.frame(df_irf_ff)
df_irf_ff_68 <- sovereign::var_irf(iv_var_ff,
horizon = 20,
CI = c(0.16, 0.84))
df_irf_ff_68 <- as.data.frame(df_irf_ff_68)
df_irf_ff$response.lower.68 <- df_irf_ff_68$response.lower
df_irf_ff$response.upper.68 <- df_irf_ff_68$response.upper
#Impact of the VFCI shock on all variables
if (type == "baseline") {
iv_var_vfci <- sovereign::VAR(data = cbind(vfci_data["date"],vfci_data[,c(vars_in_system_baseline)],vfci_data[vfci_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = vfci_instrument,
instrumented = "vfci")
} else if (type == "stationary") {
iv_var_vfci <- sovereign::VAR(data = cbind(vfci_data["date"],vfci_data[,c(vars_in_system_stationary)],vfci_data[vfci_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = vfci_instrument,
instrumented = "vfci")
} else if (type == "vfci_lev") {
iv_var_vfci <- sovereign::VAR(data = cbind(vfci_data["date"],vfci_data[,c(vars_in_system_vfci_lev)],vfci_data[vfci_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = vfci_instrument,
instrumented = "vfci_lev")
}
df_irf_vfci <- sovereign::var_irf(iv_var_vfci,
horizon = 20,
CI = c(0.05, 0.95))
df_irf_vfci <- as.data.frame(df_irf_vfci)
df_irf_vfci_68 <- sovereign::var_irf(iv_var_vfci,
horizon = 20,
CI = c(0.16, 0.84))
df_irf_vfci_68 <- as.data.frame(df_irf_vfci_68)
df_irf_vfci$response.lower.68 <- df_irf_vfci_68$response.lower
df_irf_vfci$response.upper.68 <- df_irf_vfci_68$response.upper
#Impact of the growth shock on all variables
if (type == "baseline") {
iv_var_y <- sovereign::VAR(data = cbind(vfci_data_y["date"],vfci_data_y[,c(vars_in_system_stationary)],vfci_data_y[y_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = y_instrument,
instrumented = "ygr")
} else if (type == "stationary") {
iv_var_y <- sovereign::VAR(data = cbind(vfci_data_y["date"],vfci_data_y[,c(vars_in_system_stationary)],vfci_data_y[y_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = y_instrument,
instrumented = "ygr")
} else if (type == "vfci_lev") {
iv_var_y <- sovereign::VAR(data = cbind(vfci_data_y["date"],vfci_data_y[,c(vars_in_system_vfci_lev_stationary)],vfci_data_y[y_instrument]),
p = nlags,
horizon = 20,
freq = 'quarter',
structure = 'IV',
instrument = y_instrument,
instrumented = "ygr")
}
df_irf_y <- sovereign::var_irf(iv_var_y,
horizon = 20,
CI = c(0.05, 0.95))
df_irf_y <- as.data.frame(df_irf_y)
df_irf_y_68 <- sovereign::var_irf(iv_var_y,
horizon = 20,
CI = c(0.16, 0.84))
df_irf_y_68 <- as.data.frame(df_irf_y_68)
df_irf_y$response.lower.68 <- df_irf_y_68$response.lower
df_irf_y$response.upper.68 <- df_irf_y_68$response.upper
#For a 2x2 IRF panel
if (plot_within_this_code == 1) {
df_irf_svar_iv <- df_irf_y #df_irf_ff, df_irf_vfci, df_irf_y
df_irf_svar_iv <- df_irf_svar_iv[,c(-2,-7,-8)]
df_irf_svar_iv %>% tidyr::pivot_longer(-c(target,horizon)) %>%
ggplot(aes(x=horizon, y=value, color= name,group=name))+
geom_line() + geom_hline(yintercept=0) + theme_minimal() +
scale_color_manual(values = c("blue", "gray", "gray")) +
labs(title = "SVAR-IV: Impact of Output shock",
caption = "90% Confidence Bands") +
theme(axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_text(angle = 0),
legend.title = element_blank(),
legend.position = "none",
plot.title = element_text(size=12),
plot.caption = element_text(size=9)) +
facet_wrap(~target, scales = "free")
}
#--------------------------------------------------------------------
#Model 2: LP-IV
#--------------------------------------------------------------------
#Impact of the FF shock on all variables
if (type == "baseline") {
endog_data <- vfci_data_mp[, vars_in_system_baseline]
} else if (type == "stationary") {
endog_data <- vfci_data_mp[, vars_in_system_stationary]
} else if (type == "vfci_lev") {
endog_data <- vfci_data_mp[, vars_in_system_vfci_lev]
}
shock_ff <- vfci_data_mp[, mp_instrument]
shock_ff <- as.data.frame(shock_ff)
# Estimate linear model
results_lin_iv_ff <- lpirfs::lp_lin_iv(endog_data,
lags_endog_lin = nlags,
shock = shock_ff,
trend = 2,
confint = 1.65,
adjust_se = TRUE,
use_nw = TRUE,
hor = 20)
results_lin_iv_ff.68 <- lpirfs::lp_lin_iv(endog_data,
lags_endog_lin = nlags,
shock = shock_ff,
trend = 2,
confint = 1,
adjust_se = TRUE,
use_nw = TRUE,
hor = 20)
results_lin_iv_ff$irf_lin_low.68 <- results_lin_iv_ff.68$irf_lin_low
results_lin_iv_ff$irf_lin_up.68 <- results_lin_iv_ff.68$irf_lin_up
#Impact of the VFCI shock on all variables
if (type == "baseline") {
endog_data <- vfci_data[, vars_vfci_last_baseline]
} else if (type == "stationary") {
endog_data <- vfci_data[, vars_vfci_last_stationary]
} else if (type == "vfci_lev") {
endog_data <- vfci_data[, vars_vfci_last_vfci_lev]
}
shock_vfci <- vfci_data[, vfci_instrument]
shock_vfci <- as.data.frame(shock_vfci)
# Estimate linear model
results_lin_iv_vfci <- lpirfs::lp_lin_iv(endog_data,
lags_endog_lin = nlags,
shock = shock_vfci,
trend = 2,
confint = 1.65,
adjust_se = TRUE,
use_nw = TRUE,
hor = 20)
results_lin_iv_vfci.68 <- lpirfs::lp_lin_iv(endog_data,
lags_endog_lin = nlags,
shock = shock_vfci,
trend = 2,
confint = 1,
adjust_se = TRUE,
use_nw = TRUE,
hor = 20)
results_lin_iv_vfci$irf_lin_low.68 <- results_lin_iv_vfci.68$irf_lin_low
results_lin_iv_vfci$irf_lin_up.68 <- results_lin_iv_vfci.68$irf_lin_up
#Impact of the Y shock on all variables
if (type == "baseline") {
endog_data2 <- vfci_data[, vars_in_system_stationary]
} else if (type == "stationary") {
endog_data2 <- vfci_data[, vars_in_system_stationary]
} else if (type == "vfci_lev") {
endog_data2 <- vfci_data[, vars_in_system_vfci_lev_stationary]
}
shock_y <- vfci_data[, y_instrument]
shock_y <- as.data.frame(shock_y)
# Estimate linear model
results_lin_iv_y <- lpirfs::lp_lin_iv(endog_data2,
lags_endog_lin = nlags,
shock = shock_y,
trend = 2,
confint = 1.65,
adjust_se = TRUE,
use_nw = TRUE,
hor = 20)
results_lin_iv_y.68 <- lpirfs::lp_lin_iv(endog_data2,
lags_endog_lin = nlags,
shock = shock_y,
trend = 2,
confint = 1,
adjust_se = TRUE,
use_nw = TRUE,
hor = 20)
results_lin_iv_y$irf_lin_low.68 <- results_lin_iv_y.68$irf_lin_low
results_lin_iv_y$irf_lin_up.68 <- results_lin_iv_y.68$irf_lin_up
if (plot_within_this_code == 1) {
irf_df <- as.data.frame(results_lin_iv_ff$irf_lin_mean)
plot(results_lin_iv_ff)
plot(results_lin_iv_vfci)
plot(results_lin_iv_y)
}
#--------------------------------------------------------------------
#Model 3: Cholesky
#--------------------------------------------------------------------
# VFCI ordered last
if (type == "baseline") {
chol_var_vfci <- sovereign::VAR(data = cbind(vfci_data_mp["date"],vfci_data_mp[,c(vars_vfci_last_baseline)]),
p = nlags,
horizon = 19,
freq = 'quarter',
structure = 'short')
} else if (type == "stationary") {
chol_var_vfci <- sovereign::VAR(data = cbind(vfci_data["date"],vfci_data[,c(vars_vfci_last_stationary)]),
p = nlags,
horizon = 19,
freq = 'quarter',
structure = 'short')
} else if (type == "vfci_lev") {
chol_var_vfci <- sovereign::VAR(data = cbind(vfci_data["date"],vfci_data[,c(vars_vfci_last_vfci_lev)]),
p = nlags,
horizon = 19,
freq = 'quarter',
structure = 'short')
}
chol_irf_vfci <- sovereign::var_irf(chol_var_vfci,
horizon = 19,
CI = c(0.05,0.95))
chol_irf_vfci.68 <- sovereign::var_irf(chol_var_vfci,
horizon = 19,
CI = c(0.16,0.84))
chol_irf_vfci$response.lower.68 <- chol_irf_vfci.68$response.lower
chol_irf_vfci$response.upper.68 <- chol_irf_vfci.68$response.upper
if (plot_within_this_code == 1) {
plot_irf(chol_irf_vfci)
}
#--------------------------------------------------------------------
#Model 4: Sign Restrictions
#--------------------------------------------------------------------
if (type == "baseline") {
df_sn <- cbind(vfci_data["date"],vfci_data[,c(vars_in_system_baseline)])
} else if (type == "stationary") {
df_sn <- cbind(vfci_data["date"],vfci_data[,c(vars_in_system_stationary)])
} else if (type == "vfci_lev") {
df_sn <- cbind(vfci_data["date"],vfci_data[,c(vars_in_system_vfci_lev)])
}
df_ts <- as.ts(x = df_sn[, -1], order.by = df_sn$date) #convert to ts object
lab <- c("Log GDP","Log PCE","VFCI","Fed Funds Rate")
#Impact of the FF shock on all variables
constr_mp <- c(+4,-2,-1) #first element is the shock; all other elements are optional
sn_var_mp <- VARsignR::uhlig.penalty(Y=df_ts, nlags=4, draws=10000, subdraws=200, nkeep=1000, KMIN=1,
KMAX=6, constrained=constr_mp, constant=FALSE, steps=20)
irfs_mp <- sn_var_mp$IRFS
#Impact of the VFCI shock on all variables
constr_vfci <- c(+3,-2) #-2 is restriction on negative price level
sn_var_vfci <- VARsignR::uhlig.penalty(Y=df_ts, nlags=4, draws=10000, subdraws=200, nkeep=1000, KMIN=1,
KMAX=6, constrained=constr_vfci, constant=FALSE, steps=20)
irfs_vfci <- sn_var_vfci$IRFS
if (plot_within_this_code == 1) {
irfplot(irfdraws=irfs_mp, type="median", labels=lab, save=FALSE, bands=c(0.05,0.95),grid=TRUE, bw=FALSE)
irfplot(irfdraws=irfs_vfci, type="median", labels=lab, save=FALSE, bands=c(0.16, 0.84),grid=TRUE, bw=FALSE)
}
#-------------------------------------------------------------------------------