rplotpercentage

How to transform percentage form for variance decomposition


I have a data like this,

library(vars)
##### Step 1. dataframe #####
var_1 <- ts(c(0.0242964582, 0.0294573276, -0.0406138524, 0.0734349713, 0.2361871259, 0.2029464090, 
           -0.0674527667, -0.0865866204, -0.0677017382, 0.0064997957, 0.0306759752, -0.0348514188, 
           0.3077929490, 0.6501245473, 0.4040370401, 0.0367393504, -0.0939273959, -0.0059671701, 
           0.0004939013, -0.0368954264, 0.0189354253, -0.0295075205, -0.0144733327, 0.0594286409,
          -0.0067517162, -0.0211709703, 0.0296407577, 0.0020384478, -0.0783685670, 0.0467989571, 
           -0.0272364972, 0.1245476170, 0.2074924219, 0.0765883084, 0.0297613902, 0.1066229399, 
          0.1144391761, 0.0691379798, 0.0613940636, 0.0562433653, 0.0026634629, -0.0492358856,
          -0.0564408102, -0.0009823786, -0.0152348481))
var_2 <- ts(c(0.07180005, 0.18148396, 0.15866440, 0.17907557, 0.18708040, 0.22191278, 0.16989487, 
           0.07142307, 0.11280345, 0.10856817, 0.04717572, 0.15681239, 0.12041914, 0.07782770, 
           0.10910264, 0.10396963, 0.11468800, 0.11140086, 0.10013803, 0.08931388, 0.08638342, 
           0.08307560, 0.08056766, 0.07320740, 0.04571968, 0.05207004, -0.02045457, 0.04930865, 
           0.02719897, 0.05971614, 0.03727725, 0.04356057, 0.06103950, -0.01879438, -0.01503039, 
           0.08462488, 0.01425433, 0.02872101, 0.03960403, 0.06265019, 0.04786012, 0.02890597, 
           0.02409209, 0.02154608, 0.02988910))
var_3 <- ts(c(1.62, 0.81, 2.35, 2.06, 3.68, 7.89, 8.05, 1.70, 0.80, -0.01, -0.10, 0.42, 0.31, 0.78, 
           2.70, 2.64, 2.83, 2.98, 2.15, 2.84, 3.04, 2.32, 0.75, 1.23, 0.32, 1.01, -0.18, -0.17, 
           -0.16, 1.41, 1.62, 0.83, 1.63, 2.69, -0.39, 1.05, 1.29, 1.55, 0.99, 1.32, -0.59, 1.01, 
           1.09, 1.52, 0.56))
mydata <- cbind(var_1, var_2, var_3)
mydata <- as.data.frame(mydata)

I use SVAR model to analysis,

##### Step 2. SVAR Restrictions #####
# a. Diagonal Matrix
amat <- diag(3)
amat[lower.tri(amat)]<-NA
amat
# b. Lag Order Selection 
library(vars)
lagselect <- VARselect(mydata, lag.max = 3, type = "both")
lagselect$selection
# c. Estimating the Model # 
model_1 <- VAR(mydata, p=1, type="both") 
summary(model_1)
# Choleski decomposition
bmat <- diag(diag(chol(summary(model_1)$covres)), nrow=3, ncol=3, names=T) ###B
diag(bmat) <- NA
# svar, AB model #
svarmodel_1 <- SVAR(model_1, Amat=amat, Bmat=bmat, hessian=T, 
                    estmethod=c("scoring", "direct"), lrtest = F)
summary(svarmodel_1)

And when I try to see the plot of variance decomposition,

##### Step 3. variance decomposition #####
VD_svarmodel_1 <- fevd(svarmodel_1, n.head=10, rel = F)
plot(VD_svarmodel_1)

but I want percentage form in y axis, so I try the code down below,

plot(VD_svarmodel_1, yaxt='n')
axis(2, at=pretty(VD_svarmodel_1), lab=pretty(VD_svarmodel_1) * 100, "%", las=TRUE)

it turns out "error in pretty.default(VD_svarmodel_1): 'list' object cannot be coerced to type 'double" I have no idea, how can I fix it, I try to let percentage form in y axis? Thanks a lot!


Solution

  • You've got 3 panel layouts in the plot, so you can't just change all of them with one axis call.

    I can't see any better way to do this, except to multiply all values of the model by 100 and then extend the y-axis limit (the default is c(0, 1) in the vars:::plot.varfevd function).

    VD_svarmodel_1_pct <- lapply(VD_svarmodel_1, \(x) x*100)
    

    You also have to change the class of the model object or the function will error.

    class(VD_svarmodel_1_pct) <- "varfevd"
    
    plot(VD_svarmodel_1_pct, ylim=c(0,100))
    

    enter image description here