Search code examples
rplotnaming

How does one add custom variable names to an IRF plot?


I have the following code:

var <- VAR(data, p=2, type="const", ic="AIC")
names <- 
 c("log(p_l)","log(val)","log(vol)","log(er)",
 "log(lr)","log(Exp)","log(Imp)","log(rgdp)","log(p_oil)","log(r)")

ir_plot <- plot(irf(var, n.ahead=20, ci=0.95, runs=100, seed=123), varnames = names)

Where the last argument varnames = names is what I am trying to accomplish. I have moved the argument around and tried variable.names to no avail. I just want to change the variable names that show up in the plots to the list in names.

edit:

One of the plots that the above code gives me is

enter image description here

I want to change all of the variable names to something more readable. How would I do this?


Solution

  • You use names= if you want to specify the variable name to be plotted:

    library(vars)
    data(Canada)
    var.2c <- VAR(Canada, p = 2, type = "const",ic="AIC")
    var_irf <- irf(var.2c, n.ahead=20, ci=0.95, runs=100, seed=123)
    plot(var_irf,names="e")
    

    enter image description here

    Let's say your would like your new labels on the y-axis to be:

    newl = paste0("log_",test$response)
    newl
    [1] "log_e"    "log_prod" "log_rw"   "log_U"
    

    Then we create a function that will change the required names but keep your original irf intact:

    plot_w_names = function(irf,newy){
    
    irf$response = newy
    for(i in 1:length(irf$irf)){colnames(irf$irf[[i]]) = newy}
    for(i in 1:length(irf$Lower)){colnames(irf$Lower[[i]]) = newy}
    for(i in 1:length(irf$Upper)){colnames(irf$Upper[[i]]) = newy}
    plot(irf)
    
    }
    

    Then run this:

    plot_w_names(var_irf,newl)
    

    enter image description here

    If you want to update everything, try this:

    plot_w_names = function(irf,newy){
    irf$response = newy
    irf$impulse = newy
    names(irf$irf) = newy
    names(irf$Lower) = newy
    names(irf$Upper) = newy
    for(i in 1:length(irf$irf)){colnames(irf$irf[[i]]) = newy}
    for(i in 1:length(irf$Lower)){colnames(irf$Lower[[i]]) = newy}
    for(i in 1:length(irf$Upper)){colnames(irf$Upper[[i]]) = newy}
    plot(irf)
    
    }