R Windows内存错误和可能的代码改进

时间:2011-10-25 17:04:54

标签: r xts

我尝试使用rugarch包在扩展的基础上使用eGARCH模型。我有6列数据,我正在尝试为每列重新调整参数~6000。如果我运行以下代码,我在第二列的窗口中出现错误(这意味着我成功地完成了第一个内部循环)。通过在循环中使用gc()并删除拟合对象,我已经延长了达到内存错误所需的时间。此外,这个过程总的来说需要很长时间,我想知道是否还有改进它。软件包本身似乎写得非常有效,大多数过滤都是在低级别C中完成的。我可能每隔30-60天重新安装一次模型,但我真的更喜欢这样做。我在32位窗口上运行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 = ""))
}

1 个答案:

答案 0 :(得分:1)

将数据类型从xts更改为数字时,问题就消失了,处理速度急剧增加。 (事后看来很明显)