I wrote a small code which does not work well. I guess there is something wrong in lapply()
function.
Thats what I tried:
(I will put the datasets at the bottom of this question via 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
In general I would like to apply a vector autoregressive model (VAR model) on different datasets. The first element of each dataset should be estimated by using a VAR model. Then the second element, the third element, and so on...
The final result should be like finalres
(see code below):
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])
Surprisingly the third column is correct...
Any help would be greatly appreciated
Datasets:
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")))
Here's one pretty clean approach:
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