2

I would like to use plotmo instruction from Plotmo package to plot an arima object I estimate arima model with a matrix of explanatory variables X ( transfer function)

arima.model<-arima(y,c(3,1,3),xreg=X)

When plotting this object I have the next error:

plotmo(arima.model) stats::predict(Arima.object, data.frame[3,1], type="response")

Error in predict.Arima(list(coef = c(0, 0, 0.426819838403672, -0.23337107002535, : 'xreg' and 'newxreg' have different numbers of columns

How could I fix this problem? Thanks C

Community
  • 1
  • 1

1 Answers1

0

Plotmo isn't really meant for time-series models like arima models and doesn't support them.

However, if you just want to plot the fitted model and some future values, the following function will do it (there may be a simpler way using the ts.plot function):

plarima <- function(ts, ..., n.ahead=1, main=deparse(substitute(ts)))
{
    model <- arima(ts, ...)
    if(!inherits(model, "Arima"))
        stop("this function requires 'arima' from the standard stats package")

    # calculations so we can extend the x axis
    n <- length(ts)
    x <- xy.coords(ts)$x
    if(any(is.na(x)))
        stop("NA in time")
    xdelta <- (x[n] - x[1]) / n

    plot(ts + model$residuals, # plot the fit in gray
         xlim=c(x[1], x[n] + xdelta * n.ahead),
         main=main, col="gray", lwd=3)
    lines(ts)                  # plot the data

    # predict n.ahead values and plot them in red
    forecast <- predict(model, n.ahead=n.ahead)
    lines(x=x[n] + xdelta * (0:n.ahead), y=c(ts[n], forecast$pred), col=2)
    legend("topleft", legend=c("data", "fitted", "forecast"),
           col=c(1, "gray", 2), lwd=c(1,3,1), lty=1, bg="white")

    model                      # return the arima model
}

For example

plarima(lh, order=c(3,0,0), n.ahead=10)
plarima(USAccDeaths, order=c(0,1,1), seas=list(order=c(0,1,1)), n.ahead=10)

gives the following plots

plot

(I'm assuming you are using the arima function from the standard stats package. I think the forecast package also has an arima function.)

Stephen Milborrow
  • 976
  • 10
  • 14