Constantine Constantine - 2 months ago 24
R Question

Object not found issue

I have an issue that has been driving me insane for the last couple hours.

I am using the following packages in R:

forecast
,
fracdiff
,
doParallel
,
foreach
and others. I also have the following function.

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
}
}


... and the above function WORKS!

However, if I replace
nnetar(training, 8, P=1, 5)
with
fracdiff(training, 3, 1, h=0.00001)
the function starts failing with the following error.

Error in { : task 1 failed - "object 'training' not found"


Here is where it gets interesting. It is not actually failing on the edited line. It is actually failing on the next line:
forecast(fit, 1)$mean


In otherwords, the following DOES in fact work.

fits = foreach (i = 1:length(test)) %dopar% {
if (i > 1) {
training = c(train,test[1:i-1])
}
fracdiff(training, p, q, h=0.00001)
}


but then...

pred = foreach (i = 1:length(test), .combine=c) %dopar% {
forecast(fits[[i]], 1)$mean
}


throws the previously mentioned error about
"object 'training' not found"


EDIT: As per request... a reproducable example...

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)


EDIT 2: Ok, this issue has nothing to do with the parallelism. This has everything to do with the interaction between
fracdiff
and
forecast
. The following function does NOT work either

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)
}


It evaluates to the following

> anotherBrokenFunction(train, test)
[1] TRUE
[1] TRUE
[1] TRUE
Error in eval(expr, envir, enclos) : object 'training' not found

Answer

This is a hacky fix, but one suggestion is to add arf$x <- training to the function:

anotherBrokenFunction <- function(train, test) {
    training = train
    predictions = test
    for (i in 1:length(test)){
        arf = fracdiff(x=training, nar=3, nma=1, h=0.00001)
        arf$x <- training # add this
        predictions[i] = forecast(arf, 1)$mean
        training = c(training, test[i])
    }
    return(predictions)
}

out <- anotherBrokenFunction(train, test)
str(out)
# An ‘xts’ object on 2015-09-02/2016-08-31 containing:
#   Data: num [1:252, 1] -0.000312 -0.000534 0.001913 0.000513 -0.000467 ...
#  - attr(*, "dimnames")=List of 2
#   ..$ : NULL
#   ..$ : chr "IBM.Close"
#   Indexed by objects of class: [Date] TZ: UTC
#   xts Attributes:  
# List of 2
#  $ src    : chr "yahoo"
#  $ updated: POSIXct[1:1], format: "2016-09-15 09:56:46"

Details: forecast.fracdiff calls getResponse, and the fracdiff method of getResponse looks like this:

getAnywhere("getResponse.fracdiff")
# A single object matching ‘getResponse.fracdiff’ was found
# It was found in the following places
#   registered S3 method for getResponse from namespace forecast
#   namespace:forecast
# with value

# function (object, ...) 
# {
#     if (is.element("x", names(object))) 
#         x <- object$x
#     else x <- eval.parent(parse(text = as.character(object$call)[2]))
#     if (is.null(tsp(x))) 
#         x <- ts(x, frequency = 1, start = 1)
#     return(x)
# }
# <bytecode: 0x7fd64bd8b698>
# <environment: namespace:forecast>

so it looks first for an element named "x" in the fracdiff object, failing which it will look for as.character(object$call)[2] (which happens to be training in this case), which generates the error. The idea is to insert training as the element x in the fracdiff object to preempt this error.

Comments