A question related to Plotting Multiple IRFs using ggplot and gridExtra

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

Hi, your plot is a base R plot, the one on the right a faceted ggplot.

Faceting makes it very easy to have the desired look so my suggestions would be to use ggplot2 instead of the base R ploting functionality. This way you don't even need to loop through the list. You hand the full data set to ggplot and based on the one or two variable(s) you want to use to split your plots, ggplot is going to set up the facets with subsets of the data on its own. See for example here for more: http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/

Hi,

Thank you for your suggestions.

At first, the model needs to be estimated and the stored results are plotted using a base R plot contained in BVAR package.

The problem is how to transform the estimated results into a dataframe so that I can plot them using ggplot2.

The error message says:
"Error: datamust be a data frame, or other object coercible byfortify(), not an S3 object with class mts/ts/matrix"

Thank you.

Without having your data it's difficult for us to be of any help.

What the error message states is that you cannot parse an object of class matrix (which your is) so you need to turn it into a data frame via as.data.frame() (or similarly as_tibble() from the tibble package).

Have a look here for example codes how to turn a matrix into a data frame.

Thanks for your reply.

You find the data in the second line from above and anyone can access it:

I had tried as.data.frame() and as_tibble, but not worked. I guess it's due to the use of BVAR package. The error message I get is:

Error in as.data.frame.default(value, stringsAsFactors = FALSE) :
cannot coerce class ‘"bvar"’ to a data.frame
or
Error in as.data.frame.default(bv_signs) :
cannot coerce class ‘"bvar"’ to a data.frame

Thank your for your help!

Sorry for missing that the data is included, my bad.

So what you want to plot in the end is irf(run_signs)? I have never used the package but the intended use seems to be with the included plotting functionality. I agree, a transformation into a data frame looks difficult.

A quick googling of BVAR package ggplot took me to this package: https://cran.r-project.org/web/packages/mfbvar/vignettes/mfbvar_jss.pdf The mfbvar package allows for plotting with ggplot2, might this be an option? (Not sure if this is a similar approach, didn't look into the details of both packages.)

Hope this helps!

I've just looked into the mfbvar package that indeed integrates ggplot2. But the package is not intended for an analysis with IRFs or IRFs with sign restrictions...

What a pity. Then I can simpyl hope someone else will come up with a solution. You could also contact the authors of the BVAR package and ask them. Good luck!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.