# Why is arima in R one time step off?

I've recently noticed an odd behavior in a few timeseries methods. Let's fit an arima model (ar1) to the annual subspots data

library(forecast)
library(ggplot2)

mod_arima <- arima(sunspot.year, c(1, 0, 0), xreg = 1700:1988)


Now, if we use forecast to get the fit on the model, it's a year off. Compare these two plots:

ggplot(sun_dat,
aes(x = years, y = sunspots)) +
geom_line() +
geom_line(y = fitted(mod_arima),
alpha = 0.5, lwd = 2, color = "blue")


To one where we delete the first value and tack on an NA at the end



ggplot(sun_dat,
aes(x = years, y = sunspots)) +
geom_line() +
geom_line(y = c(fitted(mod_arima)[-1], NA),
alpha = 0.5, lwd = 2, color = "blue")


The second lines up perfectly, while the first is obviously one year off. What's going on here?

• This is completely normal if the best prediction of $y_{t+1}$ is roughly $y_{t}$, which happens when the time series is a martingale difference sequence (typical e.g. for prices of shares and other financial instruments). – Richard Hardy Apr 23 at 17:01

As Richard Hardy writes: if your prediction $$\hat{y}_{t+1}$$ of $$y_{t+1}$$ is pretty much your last observation $$y_t$$, then of course you would expect $$\hat{y}_{t+1}$$ to line up with $$y_t$$, which would show exactly as the one year lag you wonder about.

And if you specify

arima(sunspot.year, c(1, 0, 0), xreg = 1700:1988)


then you fitted exactly that: an AR(1) model. The AR(1) coefficient is estimated to be about 0.81. (With an intercept. Also, if you add the year as a regressor, you will model a trend. Did you intend to do this?)

Incidentally, if you allow auto.arima() to fit a model, it will choose an ARIMA(2,1,3) model, which will not exhibit this lag:

library(forecast)
model <- auto.arima(sunspot.year)
plot(sunspot.year)
lines(model\$fit,col="red")


You could also include the known sunspot period of length 11, though auto.arima() won't automatically fit a SARIMA.

You're fitting an $$ARIMA(1,0,0)$$ model to your data, which means that your fitted model has the form:

$$\hat{Y}_{t+1}-m = a(Y_t-m) + \epsilon$$

So it looks like it's a year off, because all the model is doing is copying the value from the current year $$Y_t$$, with an adjustment $$a$$ and a drift term, and making that the prediction for the next year $$\hat{Y}_{t+1}$$.

Your data looks highly cyclical, you might want to try fitting a seasonal ARIMA model instead of a simple AR(1) or AR(2).