Search code examples
rtime-seriesforecasting

how to predict in-sample using auto.arima?


Consider this simple example

> WWWusage %>% as_tibble() %>% head()
# A tibble: 6 x 1
      x
  <dbl>
1    88
2    84
3    85
4    85
5    84
6    85

I know I can fit a ARIMA model using the forecast package. This is easy.

> fit <- auto.arima(WWWusage)
> fit
Series: WWWusage 
ARIMA(1,1,1) 

Coefficients:
         ar1     ma1
      0.6504  0.5256
s.e.  0.0842  0.0896

sigma^2 estimated as 9.995:  log likelihood=-254.15
AIC=514.3   AICc=514.55   BIC=522.08

The problem is that I would like to predict the one-step ahead forecast in my training sample. That is, how can I have a column prediction in my original data which contains the predictions from fit?

Using forecast will only return the forecasts for obs. 101 to 110 (out-of-sample).

> forecast(fit)
    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
101       218.8805 214.8288 222.9322 212.6840 225.0770
102       218.1524 208.4496 227.8552 203.3133 232.9915

I want all the previous (in-sample) forecasts as well (using the same parameters). Like doing broom:augment() with lm.

How can I do that here?

Thanks!


Solution

  • See the solution below.

    df <- WWWusage %>% as_tibble()
    fit <- auto.arima(df)
    df$fitted <- fitted(fit)
    

    Since you're using dplyr, for the last step, you can also do:

    df <- df %>% 
      mutate(fitted = fitted(fit))
    

    If you're wondering why the fitted values line up exactly with the original observations, you can read the forecast package documentation. Rob Hyndman developed the package and it's quite sophisticated. He uses backcasts and forecasts of a series' moving averages to fill in the missing information.

    For more explanation, see his online book on forecasting at https://otexts.com/fpp2/ and the documentation for the forecast at https://cran.r-project.org/web/packages/forecast/forecast.pdf.