[R] predict.Arima: warnings from xreg magic

Johann Hibschman jhibschman at gmail.com
Wed Mar 31 17:32:34 CEST 2010

When I run predict.Arima in my code, I get warnings like:

  Warning message:
  In cbind(intercept = rep(1, n), xreg) :
    number of rows of result is not a multiple of vector length (arg 1)

I think this is because I'm not running predict.Arima in the same
environment that I did the fit, so the data object used in the fit is no
longer present.  Looking at the predict.Arima source,

  predict.Arima <-
      function (object, n.ahead = 1, newxreg = NULL, se.fit = TRUE, ...)
      myNCOL <- function(x) if (is.null(x)) 0 else NCOL(x)
      rsd <- object$residuals
      xr <- object$call$xreg
      xreg <- if (!is.null(xr)) eval.parent(xr) else NULL
      ncxreg <- myNCOL(xreg)
      if (myNCOL(newxreg) != ncxreg)
          stop("'xreg' and 'newxreg' have different numbers of columns")
      class(xreg) <- NULL
      xtsp <- tsp(rsd)
      n <- length(rsd)
      arma <- object$arma
      coefs <- object$coef
      narma <- sum(arma[1L:4L])
      if (length(coefs) > narma) {
          if (names(coefs)[narma + 1L] == "intercept") {
              xreg <- cbind(intercept = rep(1, n), xreg)
              newxreg <- cbind(intercept = rep(1, n.ahead), newxreg)
              ncxreg <- ncxreg + 1L

It looks to me as if it assumes that object$call$xreg is available in
the current (well, parent) environment.

To add insult to injury, it doesn't look like xreg is used anywhere in
the code; it's calculated, then ignored.

So, here are my questions:

  1. Is this a correct diagnosis?
  2. What can I do about it?
    a. Is there a version of arima with "less magic"?
    b. Should I try to populate my environment to look like the calling
    c. Other?



More information about the R-help mailing list