我正在使用rugarch包,我安装了一个模型。现在我想查看输出并使用绘图功能。我的问题是,第5个图包含一些子图,它们被绘制在一个设备中,但我想在单个设备中绘制每个图。我怎样才能做到这一点?作为一个例子,我给你一个代码示例,它使用包的sp500ret数据:
代码:
library(rugarch)
data(sp500ret)
somemodel<-ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(2, 2)),
mean.model = list(armaOrder = c(1, 1), include.mean = TRUE),
distribution.model = "ged")
somefit<-ugarchfit(spec=somemodel,data=sp500ret)
rollingesti = ugarchroll(somemodel, sp500ret, n.start=500,
refit.every = 100, refit.window = 'moving', window.size = 500,
calculate.VaR = FALSE, keep.coef = TRUE)
plot(rollingesti,which=5)
plot(rollingesti,which=5)
将几个图绘制成一个设备,我想隔离它们。
所以我希望将它们作为单个图形和更大,现在,它们太小,因为它们都被放入一个输出中。
答案 0 :(得分:3)
你的例子不起作用(至少对我而言),即它不会收敛。但是,这个有效:
library(rugarch)
data(sp500ret)
spec <- ugarchspec(distribution.model = "std")
mod <- ugarchroll(spec, data = sp500ret[1:2000,], n.ahead = 1,
n.start = 1000, refit.every = 100, refit.window = "moving",
solver = "hybrid", fit.control = list(),
calculate.VaR = TRUE, VaR.alpha = c(0.01, 0.025, 0.05),
keep.coef = TRUE)
首先,我们找到plot(mod, which = 5)
中使用的方法。它可以通过
getMethod("plot", c(x = "uGARCHroll", y = "missing"))
您对以下内容感兴趣
.intergarchrollPlot(x, choices = choices, plotFUN = paste(".plot.garchroll",
1:5, sep = "."), which = which, VaR.alpha = VaR.alpha,
density.support = density.support, ...)
其中choices
为"Fit Coefficients (with s.e. bands)"
。通过检查rugarch:::.intergarchrollPlot
,我们最终到达rugarch:::.plot.garchroll.5
。这些图不会以任何列表或类似方式返回,因此我提供了一些修改后的版本,以便您可以单独使用它们。在这里,我改变了前两行和最后一行:
library(xts)
x <- mod
vmodel = x@model$spec@model$modeldesc$vmodel
if (!x@model$keep.coef)
stop("\n\nplot-->error: keep.coef set to FALSE in estimation\n")
coefs = x@model$coef
m = dim(coefs[[1]]$coef)[1]
N = length(coefs)
Z = matrix(NA, ncol = m, nrow = N)
Zup = matrix(NA, ncol = m, nrow = N)
Zdn = matrix(NA, ncol = m, nrow = N)
for (i in 1:m) {
Z[, i] = sapply(coefs, FUN = function(y) y$coef[i, 1])
Zup[, i] = Z[, i] + sapply(coefs, FUN = function(y) y$coef[i,
2])
Zdn[, i] = Z[, i] - sapply(coefs, FUN = function(y) y$coef[i,
2])
}
dt = sapply(coefs, FUN = function(y) as.character(y$index))
cnames = rownames(coefs[[1]]$coef)
np = rugarch:::.divisortable(m) # added rugarch:::
这是一个单独的每个绘图的函数,i
是一个图形的数字,例如在这种情况下从1到7:
plotFun <- function(i){
plot(xts(Z[, i], as.POSIXct(dt)), type = "l",
ylim = c(min(Zdn[, i]), max(Zup[, i])), ylab = "value", xlab = "", main = "",
minor.ticks = FALSE, ann = FALSE, auto.grid = FALSE)
lines(xts(Zdn[, i], as.POSIXct(dt)), col = 2)
lines(xts(Zup[, i], as.POSIXct(dt)), col = 2)
title(cnames[i], line = 0.4, cex = 0.9)
grid()
}
例如:
plotFun(1)
plotFun(2)