# Residuals in R using auto.arima and forecast package

I am fitting a model using the auto.arima function in package forecast. I get a model that is AR(1), for example. I then extract residuals from this model. How does this generate the same number of residuals as the original vector? If this is an AR(1) model then the number of residuals should be 1 less than the dimensionality of the original time series. What am I missing?

Example:

require(forecast)
arprocess = as.numeric(arima.sim(model = list(ar=.5), n=100))
#auto.arima(arprocess, d=0, D=0, ic="bic", stationary=T)
#  Series: arprocess
#  ARIMA(1,0,0) with zero mean

#  Coefficients:
#          ar1
#       0.5198
# s.e.  0.0867

# sigma^2 estimated as 1.403:  log likelihood=-158.99
# AIC=321.97   AICc=322.1   BIC=327.18
r = resid(auto.arima(arprocess, d=0, D=0, ic="bic", stationary=T))
> length(r)
 100


Update: Digging into the code of auto.arima, I see that it uses Arima which in turn uses stats:::arima. Therefore the question is really how does stats:::arima compute residuals for the very first observation?

• You need to find out how they find the fitted value for the first observation. One way is to remove the first observation. Then reverse the whole time series data that you have and then fit your AR(1) to this new data. Finally, forecast for just one step a head (i.e. h=1) to find the fitted value for the first observation. I am not sure if they implement this, but I have already read this method somewhere! I cannot remember the reference though! – Stat Sep 7 '13 at 3:11

The residuals are the actual values minus the fitted values. For the first observation, the fitted value is the estimated mean of the process. For subsequent observations, the fitted value is $\phi$ times the previous observation, assuming an AR(1) process had been estimated.