rtime-seriesforecastingzoorollapply

forecasting AR models with intercept in R using rollapply


Suppose I have an AR(3) simulated data, with an intercept of 3.

set.seed(247)
library(astsa)
sim1 = 3+arima.sim(list(order=c(3,0,0), ar=c(-0.1,-0.3,-0.5)), n=60)
pacf(sim1)

I can estimate the coefficients using:

est.1 = arima(x = sim1, order = c(3, 0, 0)) 
est.1

Coefficients:
          ar1      ar2      ar3  intercept
      -0.0614  -0.5098  -0.4286     2.9811

If I try to use rollapply of the zoo library, I get that the predictions are off-setted:

library(zoo)
library(forecast)  # to compare

f1 = rollapply(zoo(sim1), 4, function(w) {sum(c(1,w[1:3])*rev(est.1$coef))}, 
               align = "right",  partial = T)
plot(sim1,type="l")
lines(f1, col = 2, lwd = 2)  ## use the rollapply
lines(fitted(est.1), col = 3, lwd = 2) ## use the forecast package
legend(0.1, 6, legend=c("rollapply", "forcast fitted"), fill = c(2,3))

enter image description here

Can't figure out why it's happening...


Solution

  • The model you are fitting is

    y_t = 3 + x_t

    where

    x_t = -0.1 x_{t-1} - 0.3 x_{t-2} - 0.5 x_{t-3} + e_t.

    This is equivalent to

    y_t = 3*(1+0.1+0.3+0.5) - 0.1 y_{t-1} - 0.3 y_{t-2} - 0.5 y_{t-3} + e_t

    so the real "intercept" is 3*(1+0.1+0.3+0.5) = 5.7.