R Windows 内存错误和可能的代码改进
我尝试使用 rugarch 包在扩展的基础上拟合 eGARCH 模型。我有 6 列数据,我正在尝试为每列重新调整参数 ~6000。如果我运行以下代码,我会在第二列的 Windows 中收到错误(这意味着我成功地完成了第一个内部循环)。通过在循环中使用 gc() 并删除已安装的对象,我延长了出现内存错误所需的时间。另外,这个过程通常需要很长时间,我想知道我是否有办法改进它。该包本身似乎编写得相当高效,大部分过滤都是在低级 C 中完成的。我可能可以每 30-60 天重新安装一次模型,但我真的更喜欢这样做。我在 32 位 Windows 上运行 R 2.13.2。提前致谢。编辑:错误是:““0x6c732a07”处的指令引用了“0x00000008”处的内存。无法“读取”内存”。
library(rugarch)
library(xts)
e.spec <- ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1,1)), mean.model = list(armaOrder = c(1,0), include.mean = TRUE))
dly.xts <- xts(matrix(rnorm(8000*6), nrow = 8000, ncol = 6), as.Date(1:8000))
tst.xts <- tail(dly.xts, 6000)
names(tst.xts) <- 1:6
tst.idx <- index(tst.xts)
dly.idx <- index(dly.xts)
for(j in 1:ncol(tst.xts)){
sig.est <- rep(NA, nrow(tst.xts))
for(i in 1:nrow(tst.xts)){
print(i)
dat <- dly.xts[dly.idx <= tst.idx[i], j]
fit <- try(ugarchfit(e.spec, data = dat[-nrow(dat), ], solver = "solnp", solver.control = list(trace = FALSE)))
if(class(fit) != "try-error"){
spec.new <- ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1,1)), mean.model = list(armaOrder = c(1,0), include.mean = TRUE), fixed.pars = coef(fit))
sig.est[i] <- as.numeric(tail(sigma(ugarchfilter(spec = spec.new, data = dat)),1))
rm(spec.new)
rm(fit)
gc()
}else{
sig.est[i] <- NA
}
}
save(sig.est, file = paste("egarch", names(tst.xts)[j], ".RData", sep = ""))
}
I am try to fit an eGARCH model on an expanding basis using the rugarch package. I have 6 columns of data, and I am trying refit parameters ~6000 for each column. If I run the following code, I get an error in windows on the 2nd column (this means I am getting all the way through the first inner loop succesfully). By using gc() within the loop and removing the fitted object I have extended the length of time it takes to hit the memory error. Also, this process takes a very long time in general and I am wondering if there is anyway to improve it on my end. The package itself seems to be written pretty efficiently with most of the filtering being done in low level C. I could probably refit the model every 30-60 days, but I would really prefer to do it this way. I am running R 2.13.2 on 32-Bit windows. Thanks in advance. Edit: The error is: "The instruction at "0x6c732a07" referenced memory at "0x00000008." The memory could not be "read"".
library(rugarch)
library(xts)
e.spec <- ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1,1)), mean.model = list(armaOrder = c(1,0), include.mean = TRUE))
dly.xts <- xts(matrix(rnorm(8000*6), nrow = 8000, ncol = 6), as.Date(1:8000))
tst.xts <- tail(dly.xts, 6000)
names(tst.xts) <- 1:6
tst.idx <- index(tst.xts)
dly.idx <- index(dly.xts)
for(j in 1:ncol(tst.xts)){
sig.est <- rep(NA, nrow(tst.xts))
for(i in 1:nrow(tst.xts)){
print(i)
dat <- dly.xts[dly.idx <= tst.idx[i], j]
fit <- try(ugarchfit(e.spec, data = dat[-nrow(dat), ], solver = "solnp", solver.control = list(trace = FALSE)))
if(class(fit) != "try-error"){
spec.new <- ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1,1)), mean.model = list(armaOrder = c(1,0), include.mean = TRUE), fixed.pars = coef(fit))
sig.est[i] <- as.numeric(tail(sigma(ugarchfilter(spec = spec.new, data = dat)),1))
rm(spec.new)
rm(fit)
gc()
}else{
sig.est[i] <- NA
}
}
save(sig.est, file = paste("egarch", names(tst.xts)[j], ".RData", sep = ""))
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当将数据类型从 xts 更改为 numeric 时,问题消失了,并且处理速度显着提高。 (事后看来似乎很明显)
When changing the data types from xts to numeric, the problem went away and the speed of processing increased dramatically. (seems obvious in hindsight)