如何转换百分比形式进行r中的方差分解

问题描述 投票:0回答:1

我有这样的数据,

##### 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)

我用SVAR模型来分析,

##### Step 2. SVAR Restrictions #####
# a. Diagonal Matrix
amat <- diag(3)
amat[lower.tri(amat)]<-NA
amat
# b. Lag Order Selection 
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)

当我尝试查看方差分解图时,

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

但我想要 y 轴的百分比形式, 所以我尝试下面的代码,

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

结果是“pretty.default(VD_svarmodel_1) 中出现错误: 无法强制“list”对象输入“double” 我不知道,我该如何解决它,我尝试让百分比在 y 轴上形成?非常感谢!

r plot percentage
1个回答
0
投票

图中有 3 个面板布局,因此您不能仅通过一次

axis
调用来更改所有面板布局。

我看不出有什么更好的方法来做到这一点,除了将模型的所有值乘以 100,然后扩展 y 轴限制(

vars:::plot.varfevd
函数中的默认值是 c(0, 1))。

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

您还必须更改模型对象的类,否则函数将出错。

class(VD_svarmodel_1_pct) <- "varfevd"

plot(VD_svarmodel_1_pct, ylim=c(0,100))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.