使用forecast运行foreach时出错
我不知道这是否与foreach有关,但我想更多的是与预测有关 下面只是一个较大表的小例子,我想并行运行这个脚本,因为它需要一些时间。我收到错误消息: {:task 2失败中出错-“参数的长度为零” 我不知道为什么,因为预测功能在单独运行时运行良好使用forecast运行foreach时出错,r,foreach,parallel-processing,forecasting,R,Foreach,Parallel Processing,Forecasting,我不知道这是否与foreach有关,但我想更多的是与预测有关 下面只是一个较大表的小例子,我想并行运行这个脚本,因为它需要一些时间。我收到错误消息: {:task 2失败中出错-“参数的长度为零” 我不知道为什么,因为预测功能在单独运行时运行良好 # Test Data Data <- data.frame(Date = as.Date(c("2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", "2017
# Test Data
Data <- data.frame(Date = as.Date(c("2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", "2017-06-01")),
A = c(1,6,3,6,5,6),
B = c(6,5,6,3,6,1))
Data <- as.xts(Data[-1], Data[,1]) #convert to xts for the forecast package
library(foreach)
library(doSNOW)
library(forecast)
cl <- makeCluster(2, type="SOCK") # for 2 cores machine
registerDoSNOW (cl)
# forecast the values in each column and bind the result of the forecast together by column to get one data.frame
Result <- foreach(j = 1:ncol(Data), .combine = "cbind", .packages = "forecast") %dopar% {forecast(Data[,j], h = 6L)$mean}
stopCluster(cl)
# Result how it should look like
Result <- data.frame(A = c(4.7,4.7,4.7,4.7,4.7,4.7),
B = c(4.4,4.4,4.4,4.4,4.4,4.4))
测试数据
数据错误源于未导出的
xts
包。环境需要访问xts
对象数据的[/code>方法
您可以使用.packages=c(“预测”,“xts”)
(建议)或显式使用xts::`[.xts`
(不建议,但包含在其中用于证明错误发生的原因)
注意:输出与问题中发布的预期结果不匹配
library(foreach)
library(doSNOW)
library(forecast)
# Test Data
Data <- data.frame(Date = as.Date(c("2017-01-01", "2017-02-01", "2017-03-01",
"2017-04-01", "2017-05-01", "2017-06-01")),
A = c(1,6,3,6,5,6),
B = c(6,5,6,3,6,1))
Data <- xts::as.xts(Data[-1], Data[,1]) #convert to xts for the forecast package
cl <- makeCluster(2, type="SOCK") # for 2 cores machine
registerDoSNOW(cl)
# forecast the values in each column and bind the result of the forecast together by column to get one data.frame
Result_export <- foreach(j = 1:ncol(Data), .combine = "cbind",
.packages = c("forecast","xts")) %dopar% {
forecast(Data[,j], h = 6L)$mean
}
Result_colon <- foreach(j = 1:ncol(Data), .combine = "cbind",
.packages = c("forecast")) %dopar% {
forecast(xts:::`[.xts`(Data,j=j), h = 6L)$mean
}
identical(Result_export, Result_colon)
# [1] TRUE
as.data.frame(Result_export)
# result.1 result.2
# 1 4.499867 4.500107
# 2 4.499867 4.500107
# 3 4.499867 4.500107
# 4 4.499867 4.500107
# 5 4.499867 4.500107
# 6 4.499867 4.500107
stopCluster(cl)
库(foreach)
图书馆(doSNOW)
图书馆(预测)
#测试数据
数据错误源于未导出的xts
包。环境需要访问xts
对象数据的[/code>方法
您可以使用.packages=c(“预测”,“xts”)
(建议)或显式使用xts::`[.xts`
(不建议,但包含在其中用于证明错误发生的原因)
注意:输出与问题中发布的预期结果不匹配
library(foreach)
library(doSNOW)
library(forecast)
# Test Data
Data <- data.frame(Date = as.Date(c("2017-01-01", "2017-02-01", "2017-03-01",
"2017-04-01", "2017-05-01", "2017-06-01")),
A = c(1,6,3,6,5,6),
B = c(6,5,6,3,6,1))
Data <- xts::as.xts(Data[-1], Data[,1]) #convert to xts for the forecast package
cl <- makeCluster(2, type="SOCK") # for 2 cores machine
registerDoSNOW(cl)
# forecast the values in each column and bind the result of the forecast together by column to get one data.frame
Result_export <- foreach(j = 1:ncol(Data), .combine = "cbind",
.packages = c("forecast","xts")) %dopar% {
forecast(Data[,j], h = 6L)$mean
}
Result_colon <- foreach(j = 1:ncol(Data), .combine = "cbind",
.packages = c("forecast")) %dopar% {
forecast(xts:::`[.xts`(Data,j=j), h = 6L)$mean
}
identical(Result_export, Result_colon)
# [1] TRUE
as.data.frame(Result_export)
# result.1 result.2
# 1 4.499867 4.500107
# 2 4.499867 4.500107
# 3 4.499867 4.500107
# 4 4.499867 4.500107
# 5 4.499867 4.500107
# 6 4.499867 4.500107
stopCluster(cl)
库(foreach)
图书馆(doSNOW)
图书馆(预测)
#测试数据
数据谢谢!我没想过!谢谢!我没想过!