1

I'm working with the forecast package (version 8.5) in R (version 3.5.3), attempting to do some ARIMA forecasting using the magnificent auto.arima() function.

When running this function, I always receive an error code which says, "Error in eval(expr, p) : object 'fitxreg' not found". I have already tried debugging, and I wasn't able to figure out exactly what the problem was, but when I revert back to forecast 8.4, this block of code works without an issue.

arimaIssue <- function(fitxreg = NULL, forxreg = NULL){
  library(forecast)

  fit <- auto.arima(AirPassengers[1:87], 
                    seasonal = FALSE, 
                    xreg = fitxreg, lambda = 'auto', allowmean = TRUE)

  fcast <- forecast(fit, xreg = forxreg, h = 3)

  return(fcast)
}

arimaIssue()

I would expect this to return a forecast object from auto.arima() that does not use external regressors (note that both fitxreg and forxreg are NULL). However, I simply get the error described above.

Any help is greatly appreciated!

2 Answers2

1

Solution

We can add a check to see if fitxreg is NULL or not

arimaIssue <- function(fitxreg = NULL, forxreg = NULL){
  library(forecast)

  if(missing(fitxreg)){
    fit <- auto.arima(AirPassengers[1:87], 
                    seasonal = FALSE, 
                    xreg = NULL, lambda = 'auto', allowmean = TRUE)
  } else {
    fit <- auto.arima(AirPassengers[1:87], 
                            seasonal = FALSE, 
                            xreg = fitxreg, lambda = 'auto', allowmean = TRUE)
  }
  fcast <- forecast(fit, xreg = forxreg, h = 3)

  return(fcast)
}

arimaIssue()

Returns:

   Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
88       320.8124 278.8410 370.7503 259.3371 401.0221
89       310.9559 254.0070 384.2721 229.0197 431.6157
90       301.5867 239.6709 384.1640 213.1853 439.0395

Solution if you don't mind setting a variable to your global environment,

arimaIssue <- function(fitxreg = NULL, forxreg = NULL){
  library(forecast)
  fitxreg <<- fitxreg
    fit <- auto.arima(AirPassengers[1:87], 
                    seasonal = FALSE, 
                    xreg = fitxreg, lambda = 'auto', allowmean = TRUE)

  fcast <- forecast(fit, xreg = forxreg, h = 3)

  return(fcast)
}

arimaIssue()


   Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
88       320.8124 278.8410 370.7503 259.3371 401.0221
89       310.9559 254.0070 384.2721 229.0197 431.6157
90       301.5867 239.6709 384.1640 213.1853 439.0395
Hector Haffenden
  • 1,360
  • 10
  • 25
1

Got it!

The issue is that the fit object contains the name of the external regressor as 'fitxreg' and when forecast() goes to look for 'fitxreg', it finds nothing. The following update to the code now produces a forecast. Thanks to Hector for the clue as to what was going on!

arimaIssue <- function(fitxreg = NULL, forxreg = NULL){
  library(forecast)

  fit <- auto.arima(AirPassengers[1:87], seasonal = FALSE, xreg = fitxreg, lambda = 'auto',
                    allowmean = TRUE)

  if(is.null(fitxreg)){
    fit$call$xreg <- NULL
  }


  fcast <- forecast(fit, xreg = forxreg, h = 3)

  return(fcast)
}

arimaIssue()