如何从R中的预测中提取预测?

deb*_*deb 4 r predict

我在具有多个位置的时间序列上运行VAR.假设loc1,loc2和loc3是时间序列数据的列名.

fitVAR = VAR(data,p=order,type = "both", ic = "AIC") 
pred = predict(fitVAR,n.ahead = L)
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过pred$fcst$loc1[,1]等等来获得预测.但是假设我想编写一个函数来执行此操作,它将位置名称作为输入变量(例如,LOC=c("loc1","loc2","loc3")).我该怎么做?

ags*_*udy 5

你可以lapply像这样使用:

 lapply(predict(pp)$fcst[LOC],'[',,1)
Run Code Online (Sandbox Code Playgroud)

例如:

data(Canada)
fit <- VAR(Canada, p = 2, type = "none")
LOC <- c('e','U')
lapply(predict(fit)$fcst[LOC],'[',,'fcst') 
lapply(predict(fit)$fcst[LOC],'[',,1)
$e
 [1] 962.3490 962.7852 963.1305 963.4016 963.6116 963.7742 
     963.9023 964.0081 964.1026 964.1954

$U
 [1] 6.764097 6.751969 6.804301 6.900299 7.030548 7.184748 
     7.353441 7.528150 7.701521 7.867432
Run Code Online (Sandbox Code Playgroud)