gpt4 book ai didi

r - 如何隔离这种方法的情节?

转载 作者:行者123 更新时间:2023-12-04 04:54:42 24 4
gpt4 key购买 nike

我正在使用 rugarch包,我安装了一个模型。现在我想查看输出并使用 plot 函数。我的问题是,第 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)将几个地块绘制到一个设备中,我想将它们隔离开来。

ro

所以我想把它们作为单块和更大的,现在,它们太小了,因为它们都被放在一个输出中。

最佳答案

您的示例不起作用(至少对我而言),即它不收敛。但是,这个有效:

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)

关于r - 如何隔离这种方法的情节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16923252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com