滚动窗口后创建预测矩阵

时间:2015-09-08 09:27:47

标签: r matrix time-series

我有这样的数据集:

data <- as.zoo(ts.union(a=arima.sim(model=list(ar=c(.9,-.2)), n=144), 
                        b=arima.sim(model=list(ar=c(.6, -.3)), n=144), 
                        c=arima.sim(model=list(ar=c(-.2,-.6)), n=144)))

我为a制作了一个滚动窗口预测,为每一步提供了提前6步预测。

rolling.window <- rollapply(data, width = 132,
                            FUN = function(x) predict(VAR(x, type="const", ic="FPE"), 
                                                  n.ahead=6, ci=0.95)$fcst$a[,1],
                        by.column = F, align = "right")

head(rolling.window)

132  0.086474  0.031416  0.00071186 -0.016284 -0.025615 -0.030692
133  1.289223  0.762734  0.46166288  0.284157  0.180816  0.120837
134  0.307354  0.332732  0.28306490  0.223481  0.171789  0.132596
135  0.105074  0.148357  0.14704495  0.128852  0.109577  0.093722
136 -0.469992 -0.496095 -0.39268676 -0.263921 -0.155009 -0.074600
137 -1.047158 -0.720692 -0.45201041 -0.251064 -0.115632 -0.029640

现在,我想将这些预测自动存储在矩阵(或多个时间序列对象)中,如下所示:

        w132      w133    w134     w135     w136     w137
133  0.08647370   NA      NA       NA       NA       NA
134  0.03141553 1.28922   NA       NA       NA       NA
135  0.00071186 0.76273 0.30735    NA       NA       NA
136 -0.01628371 0,46166 0.33273 0.105074    NA       NA
137 -0.02561482 0.28416 0.28306 0.148357 -0.46999    NA
138 -0.03069235 0.18082 0.22348 0.147045 -0.49610 -1.04716
139     NA      0.12084 0.17179 0.128852 -0.39269 -0.72069
140     NA        NA    0.13260 0.109577 -0.26392 -0.45201
141     NA        NA      NA    0.093722 -0.15501 -0.25106
142     NA        NA      NA       NA    -0.07460 -0.11563
143     NA        NA      NA       NA       NA    -0.02964

等等。我希望每个滚动窗口在相应的时间提前6步预测。不幸的是,我完全不知道我应该从哪里开始。我尝试使用lag(),但这只适用于一个系列。我不能解决,我怎么能在rollapply()函数中做到这一点。你能给我一个暗示吗?

1 个答案:

答案 0 :(得分:2)

你可以这样做:

do.call(cbind, lapply(1:nrow(df), function(i) c(rep(NA,i-1), df[i,], rep(NA, nrow(df)-i))))

   [,1]       [,2]      [,3]      [,4]     [,5]       [,6]      
V2 0.086474   NA        NA        NA       NA         NA        
V3 0.031416   1.289223  NA        NA       NA         NA        
V4 0.00071186 0.762734  0.307354  NA       NA         NA        
V5 -0.016284  0.4616629 0.332732  0.105074 NA         NA        
V6 -0.025615  0.284157  0.2830649 0.148357 -0.469992  NA        
V7 -0.030692  0.180816  0.223481  0.147045 -0.496095  -1.047158 
   NA         0.120837  0.171789  0.128852 -0.3926868 -0.720692 
   NA         NA        0.132596  0.109577 -0.263921  -0.4520104
   NA         NA        NA        0.093722 -0.155009  -0.251064 
   NA         NA        NA        NA       -0.0746    -0.115632 
   NA         NA        NA        NA       NA         -0.02964

数据:

df = structure(list(V2 = c(0.086474, 1.289223, 0.307354, 0.105074, 
-0.469992, -1.047158), V3 = c(0.031416, 0.762734, 0.332732, 0.148357, 
-0.496095, -0.720692), V4 = c(0.00071186, 0.46166288, 0.2830649, 
0.14704495, -0.39268676, -0.45201041), V5 = c(-0.016284, 0.284157, 
0.223481, 0.128852, -0.263921, -0.251064), V6 = c(-0.025615, 
0.180816, 0.171789, 0.109577, -0.155009, -0.115632), V7 = c(-0.030692, 
0.120837, 0.132596, 0.093722, -0.0746, -0.02964)), .Names = c("V2", 
"V3", "V4", "V5", "V6", "V7"), row.names = 132:137, class = "data.frame")