r - 使用 lapply() 和 for 循环的 VAR 模型

标签 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")))

最佳答案

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

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

关于r - 使用 lapply() 和 for 循环的 VAR 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49179719/

相关文章:

r - 计算出现在非缺失值之间的 NA

R - 设置使用 by() 创建的对象的类

r - 使用 sjplot 的 plot_model 函数时调整刻面顺序和图例标签

r - 从特定文件夹加载所有 R 数据文件

r - data.table 有效替代分组分配为 DT[,x :=f(y), by=z]?

python - 复杂的行和列操作 Pandas

python - 如何使用时间列创建包含当天部分时间的新列 ['morning' 、 'afternoon' 、 'evening' 、 'night' ]?

r - R中的快速傅立叶变换

r - 如何在R中的全局环境中编写函数的结果

r - 将 row_number() 分配为 R 中数据帧列表上的列值