Hello,
I try to figure out how to edit a default graph for impulse response functions (R-package "BVAR") using ggplot2 and gridExtra. So far, I could not make it to transform the estimated impulse response functions into a dataframe, which is needed for ggplot.
Last year, tanga94 asked a similar question (https://forum.posit.co/t/plotting-multiple-irfs-using-ggplot-and-gridextra/47344). But, unfortunately, the very nice answer offered there as well as other tips on stackexchange.com have not worked in my case. I would very much appreciate it if somebody could help me out.
What I want to do is basically just like tanga94 did: The use of ggplot and gridExtra, so that the default graph for impulse response functions generated by BVAR package looks like the one created by Mr @Hlynur (left: default plot, right: by Hlynur):
Here is the code to estimate a Bayesian VAR model with sign restrictions using "BVAR" package. The data used here is public:
**# package and data (necessary to get a graph, but not important to my question)**
library("BVAR")
data("fred_qd")
df <- fred_qd[, c("GDPC1", "CPIAUCSL", "FEDFUNDS")]
for (i in c("GDPC1", "CPIAUCSL"))
df[5:nrow(df), i] <- diff(log(df[, i]), lag = 4) *100
df <- df[5:nrow(df),]
plot.ts(df)
**# prior configuration for Bayesian estimation (necessary to get a graph, but not important to my question)**
mn <- bv_minnesota(
lambda = bv_lambda(mode = 0.2, sd = 0.4, min = 0.0001, max = 5),
alpha = bv_alpha(mode = 2, sd = 0.25, min = 1, max = 3),
var = 1e07
)
priors <- bv_priors(hyper = "auto", mn = mn)
mh <- bv_metropolis(scale_hess = 0.005, adjust_acc = TRUE,
acc_lower = 0.25, acc_upper = 0.35, acc_change = 0.02)
**# sign restrictions are provided (necessary to get a graph, but not important to my question)**
signs <- matrix(c(1, NA, NA, NA, 1, NA, -1, -1, 1), ncol = 3)
irf_signs <- bv_irf(horizon = 12, fevd = TRUE, identification = TRUE, sign_restr = signs)
fcasts <- NULL
**# model is estimated (necessary to get a graph, but not important to my question)**
run_signs <- bvar(df, lags = 5, n_draw = 25000, n_burn = 10000, priors = priors, mh = mh,
fcast = fcasts, irf = irf_signs, verbose = TRUE)
*# Here is the code for the default graph that I want to edit using ggplot and gridExtra:*
plot(irf(run_signs), vars_impulse = c("FEDFUNDS"), vars_response = c(1:3))
Thank you so much in advance for your help!
Best regards from Japan