vars
ライブラリを使用してRでベクトル自己回帰モデルを実行しています。foreach
関数を利用してモデルを並行して実行したいのですが、エラーが発生します
Error in { : task 1 failed - "object 'exogen.train' not found"
外生変数が含まれていない場合、コードは正常に実行されますが、それらをモデルに追加するとエラーが発生します。以下は、エラーの最小限の例です。
library(vars)
library(doParallel)
set.seed(123)
dat <- ts(matrix(rnorm(600), 200, 3), start = c(1961, 1), frequency = 12)
dat.train <- dat[1:100, ]
dat.test <- dat[101:200, ]
label <- sample(1:5, nrow(dat), replace = T)
exogen.train <- cbind(label = label[1:100])
exogen.test <- cbind(label = label[101:200])
ncores <- 6
cl <- makeCluster(ncores)
registerDoParallel(cl)
res <- foreach(i = 1:6, .combine = rbind, .packages = c("vars")) %dopar% {
fit.VAR <- VAR(dat.train, p = i, type = "none", exogen = exogen.train)
pred.valid <- predict(fit.VAR, dat.test, dumvar = exogen.test, n.ahead = nrow(dat.test))
res <- lapply(pred.valid$fcst, sd)
return(list(c(i, res)))
}
stopCluster(cl)
res
ループ内ですべてを移動しても、エラーは解決しません。しかし、外生変数が含まれていない場合、コードは正常に実行されます。
ncores <- 6
cl <- makeCluster(ncores)
registerDoParallel(cl)
res <- foreach(i = 1:6, .combine = rbind, .packages = c("vars")) %dopar% {
fit.VAR <- VAR(dat.train, p = i, type = "none")
pred.valid <- predict(fit.VAR, dat.test, n.ahead = nrow(dat.test))
res <- lapply(pred.valid$fcst, sd)
return(list(c(i, res)))
}
stopCluster(cl)
res
このエラーは、R 4.2 を使用する Windows および Mac、R 3.62 を使用する Linux で再現できます。