Hello all,

I use arima to fit the model with

fit <- arima(y, order = c(1,0,1), xreg = list.indep, include.mean = TRUE)

and would like to use predict() to forecast:

chn.forecast       <- rep(0,times=num.record)
chn.forecast[1] <- y[1]
for (j in 2:num.record){
                indep                    <- c(aa=chn.forecast[j-1], list.indep[j,2:num.indep]) # this is the newxreg in the forecast.
                chn.forecast[j]  <- predict(fit, newxreg=indep, n.ahead = 1)
}

However, I got the error message as 'xreg' and 'newxreg' have different numbers of columns".

So I debug into predict.Arima (as shown in (*)).

(*):

debugging in: predict.Arima(fit, newxreg = indep, n.ahead = 1)
debug: {
    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
        }
        xm <- if (narma == 0)
            drop(as.matrix(newxreg) %*% coefs)
        else drop(as.matrix(newxreg) %*% coefs[-(1L:narma)])
    }
    else xm <- 0
    if (arma[2L] > 0L) {
        ma <- coefs[arma[1L] + 1L:arma[2L]]
        if (any(Mod(polyroot(c(1, ma))) < 1))
            warning("MA part of model is not invertible")
    }
    if (arma[4L] > 0L) {
        ma <- coefs[sum(arma[1L:3L]) + 1L:arma[4L]]
        if (any(Mod(polyroot(c(1, ma))) < 1))
            warning("seasonal MA part of model is not invertible")
    }
    z <- KalmanForecast(n.ahead, object$model)
    pred <- ts(z[[1L]] + xm, start = xtsp[2L] + deltat(rsd),
        frequency = xtsp[3L])
    if (se.fit) {
        se <- ts(sqrt(z[[2L]] * object$sigma2), start = xtsp[2L] +
            deltat(rsd), frequency = xtsp[3L])
        return(list(pred = pred, se = se))
    }
    else return(pred)
}
Browse[3]>
debug: myNCOL <- function(x) if (is.null(x)) 0 else NCOL(x)
Browse[3]>
debug: rsd <- object$residuals
Browse[3]>
debug: xr <- object$call$xreg
Browse[3]>
debug: xreg <- if (!is.null(xr)) eval.parent(xr) else NULL
Browse[3]>
debug: eval.parent(xr)
Browse[3]>
debug: ncxreg <- myNCOL(xreg)
Browse[3]>
debug: if (myNCOL(newxreg) != ncxreg) stop("'xreg' and 'newxreg' have different numbers of columns")
Browse[3]> head(xreg)
                       aa             dummy1         dummy2 bb      cc
[1,]                  0.015538             0            0   0.941   1.241
[2,]                  0.015478             0            0   0.952   1.185
[3,]                  0.015607             0            0   0.955   1.422
[4,]                  0.015861             0            0   1.038   1.777
[5,]                  0.016005             0            0   1.286   2.118
[6,]                  0.016180             0            0   1.351   2.084
Browse[3]> newxreg
                       aa             dummy1                 dummy2                   bb
                 0.015478                  0.000000                  0.000000                  0.952000
                       cc
                 1.185000
Browse[3]> NCOL(xreg)
[1] 5
Browse[3]> NCOL(newxreg)
[1] 1
Browse[3]> NCOL(t(newxreg))
[1] 5



When comparing the column numbers of xreg and newxreg, the function NCOL is used. It seems to me that they all have the same number of columns (ncol(xreg)=ncol(newxreg)=5, yellow highlighted part), however, ncol(newxreg)=1. If I check the transpose ncol(t(newxreg)), it is 5. So I think about put t(indep) instead of indep as the newxreg, but got error message(**):
(**)

Error in as.matrix(newxreg) %*% coefs :

  requires numeric/complex matrix/vector arguments

In addition: Warning message:

In chn.forecast[j] <- predict(fit, newxreg = t(indep), n.ahead = 1) :

  number of items to replace is not a multiple of replacement length

How could I fix such a problem?

Thanks very much!

Cheers,

Rebecca

----------------------------------------------------------------------
This message, and any attachments, is for the intended r...{{dropped:5}}

