VAR模型使用带有for循环的lapply()

时间:2018-03-08 18:07:25

标签: r time-series lapply forecasting autoregressive-models

我写了一个不能很好用的小代码。我想lapply()函数有问题。

这就是我尝试过的: (我将通过dput()

将数据集放在此问题的底部
library(vars)
library(fpp2)    
M4 = NULL
    for (i in 1:3){
      M4 = lapply(A, function(x) VAR(cbind(x, B[,i], C[,i], D[,i]), lag.max = 3, ic="AIC", type="const")) 
    }

M4x = lapply(M4, forecast, h =21)
M4x1 = data.frame(lapply(M4x, function(x) x))
M4x1 = M4x1[1:21,seq(3, ncol(M4x1),7)]
M4x1

一般来说,我想在不同的数据集上应用矢量自回归模型(VAR模型)。应使用VAR模型估算每个数据集的第一个元素。然后是第二个元素,第三个元素,依此类推......

最终结果应该是finalres(见下面的代码):

aaaaa = VAR(as.ts(cbind(A[,1], B[,1], C[,1], D[,1])), lag.max = 3, ic="AIC", type="const")
fa = as.data.frame(forecast(aaaaa, h =21))
aaaaa1 = VAR(as.ts(cbind(A[,2], B[,2], C[,2], D[,2])), lag.max = 3, ic="AIC", type="const")
fa1 = as.data.frame(forecast(aaaaa1, h =21))
aaaaa2 = VAR(as.ts(cbind(A[,3], B[,3], C[,3], D[,3])), lag.max = 3, ic="AIC", type="const")
fa2 = as.data.frame(forecast(aaaaa2, h =21))
finalres = cbind(fa[c(1:21),3], fa1[c(1:21),3], fa2[c(1:21),3])

令人惊讶的是,第三栏是正确的......

非常感谢任何帮助

数据集:

A = dput(structure(c(0.00832329992614511, 0.00835017808898186, 0.00345876664210643, 
                     -0.00702545424502254, -0.00653192186544338, 0.050352700826652, 
                     -0.00761458622624289, 0.00832329992614511, 0.00835017808898186, 
                     0.00345876664210643, -0.00702545424502254, -0.00653192186544338, 
                     0.050352700826652, -0.00761458622624289, -0.00362491226772121, 
                     -0.00789934663168967, -0.0136886268514855, -0.0172886719389682, 
                     0.025953589472115, 0.0119282246648833, 0.0101611138614111), .Dim = c(7L, 
                                                                                          3L), .Dimnames = list(NULL, c("AL1", "AAL1", "AAAL1")), .Tsp = c(1, 
                                                                                                                                                           7, 1), class = c("mts", "ts", "matrix")))
B = dput(structure(c(0.00392975349087443, 0.00590862325037733, -0.00163745686324113, 
                     0.00887094758761542, 0.024494147158741, 0.0284302480591698, 0.000629749769375465, 
                     0.00392975349087443, 0.00590862325037733, -0.00163745686324113, 
                     0.00887094758761542, 0.024494147158741, 0.0284302480591698, 0.000629749769375465, 
                     0.0103068807514664, 0.00229813178923521, -0.0086351463120895, 
                     -0.0117272319959998, 0.0149097010636208, 0.00392975349087443, 
                     0.00590862325037733), .Dim = c(7L, 3L), .Dimnames = list(NULL, 
                                                                              c("BL1", "BBL1", "BBBL1")), .Tsp = c(1, 7, 1), class = c("mts", 
                                                                                                                                       "ts", "matrix")))
C = dput(structure(c(0.000775208035641128, 0.00438569949678325, 0.0113833889456316, 
                     0.0319815685292468, 0.041566014624367, 0.0660665091926624, 0.0607876357116606, 
                     0.000775208035641128, 0.00438569949678325, 0.0113833889456316, 
                     0.0319815685292468, 0.041566014624367, 0.0660665091926624, 0.0607876357116606, 
                     0.0553361647079065, 0.0306064336224416, 0.0130411441105416, -0.00548621653886627, 
                     0.00715233529623305, 0.000775208035641128, 0.00438569949678325
), .Dim = c(7L, 3L), .Dimnames = list(NULL, c("BL5", "BBL5", 
                                              "BBBL5")), .Tsp = c(1, 7, 1), class = c("mts", "ts", "matrix"
                                              )))

D = dput(structure(c(-0.00824937560007655, -0.00616925069792629, -0.00803841546945705, 
                     0.0319503059391195, 0.0531874114658315, 0.120338282134229, 0.116593382008732, 
                     -0.00824937560007655, -0.00616925069792629, -0.00803841546945705, 
                     0.0319503059391195, 0.0531874114658315, 0.120338282134229, 0.116593382008732, 
                     -0.017160706200583, -0.0179498966889309, -0.017549007265746, 
                     -0.0207308936646786, -0.00662888248416849, -0.00824937560007655, 
                     -0.00616925069792629), .Dim = c(7L, 3L), .Dimnames = list(NULL, 
                                                                               c("BL23", "BBL23", "BBBL23")), .Tsp = c(1, 7, 1), class = c("mts", 
                                                                                                                                           "ts", "matrix")))

2 个答案:

答案 0 :(得分:1)

只考虑一次lapply调用迭代1-3:

df_list <- lapply(1:3, function(i) {
   res <- VAR(as.ts(cbind(A[,i], B[,i], C[,i], D[,i])), lag.max=3, ic="AIC", type="const")
   df <- as.data.frame(forecast(res, h =21))[c(1:21),3]

   return(df)
})

final_res <- do.call(cbind, df_list)

答案 1 :(得分:1)

这是一个非常干净的方法:

res <- sapply(1:3, function(i) {
  m <- VAR(cbind(A[, i], B[, i], C[, i], D[, i]), lag.max = 3, ic = "AIC", type = "const")
  forecast(m, h = 21)$forecast$A$mean
})
identical(res, finalres)
# [1] TRUE