在过去的几个小时里,我有一个问题一直让我发疯。
我在 R 中使用以下包:forecast
、、、fracdiff
等。我也有以下功能。doParallel
foreach
doparPredictions <- function(train, test, cl){
training = train
pred = foreach (i = 1:length(test), .combine=c) %dopar% {
if (i > 1) {
training = c(train,test[1:i-1])
}
fit = nnetar(training, 8, P=1, 5)
forecast(fit, 1)$mean
}
}
...并且上述功能有效!
但是,如果我nnetar(training, 8, P=1, 5)
用fracdiff(training, 3, 1, h=0.00001)
函数替换开始失败并出现以下错误。
Error in { : task 1 failed - "object 'training' not found"
这就是有趣的地方。它实际上并没有在编辑的行上失败。它实际上在下一行失败:forecast(fit, 1)$mean
换句话说,以下确实有效。
fits = foreach (i = 1:length(test)) %dopar% {
if (i > 1) {
training = c(train,test[1:i-1])
}
fracdiff(training, p, q, h=0.00001)
}
但是之后...
pred = foreach (i = 1:length(test), .combine=c) %dopar% {
forecast(fits[[i]], 1)$mean
}
抛出前面提到的关于"object 'training' not found"
编辑:根据要求......一个可重现的例子......
require(quantmod)
require(forecast)
require(fracdiff)
require(doParallel)
require(foreach)
cl <- makeCluster((detectCores() - 1), type="FORK")
registerDoParallel(cl)
predictionsThatWork <- function(train, test, cl){
training = train
pred = foreach (i = 1:length(test), .combine=c) %dopar% {
if (i > 1) {
training = c(train,test[1:i-1])
}
fit = nnetar(training, 8, P=1, 5)
forecast(fit, 1)$mean
}
return(pred)
}
predictionsThatDoNotWork <- function(train, test, cl){
training = train
pred = foreach (i = 1:length(test), .combine=c) %dopar% {
if (i > 1) {
training = c(train,test[1:i-1])
}
fit = fracdiff(training, 3, 1, h=0.00001)
forecast(fit, 1)$mean
}
return(pred)
}
ticker = 'IBM' #feel free to replace with ticker of your choice
getSymbols(ticker, from='2010-01-01', to='2016-08-31')
fullts = get(ticker)
returnTS = diff(log(fullts[,4]),lag=1)[-1]
returnTS = returnTS - mean(returnTS)
numObs = length(returnTS)
cutOff = ceiling(numObs*.85)
train = returnTS[1:cutOff-1]
test = returnTS[cutOff:numObs]
predictionsThatWork(train, test, cl)
predictionsThatDoNotWork(train, test, cl)
stopCluster(cl)
编辑2:好的,这个问题与并行性无关。fracdiff
这与和之间的交互有关forecast
。以下功能也不起作用
anotherBrokenFunction <- function(train, test) {
training = train
print(exists('training'))
predictions = test
for (i in 1:length(test)){
print(exists('training'))
arf = fracdiff(x=training, nar=3, nma=1, h=0.00001)
print(exists('training'))
predictions[i] = forecast(arf, 1)$mean
print(exists('training'))
training = c(training, test[i])
}
return(predictions)
}
它评估为以下
> anotherBrokenFunction(train, test)
[1] TRUE
[1] TRUE
[1] TRUE
Error in eval(expr, envir, enclos) : object 'training' not found