-1

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!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • 1
    Are you asking for the ARIMA-predicted values for each observation in your data set? – hmhensen May 01 '19 at 17:10
  • yes correct, this is what I want to do. – ℕʘʘḆḽḘ May 01 '19 at 17:13
  • Possible duplicate of [display predicted values for initial data using auto.arima in R](https://stackoverflow.com/questions/53924851/display-predicted-values-for-initial-data-using-auto-arima-in-r) – hmhensen May 01 '19 at 17:16
  • @hmhensen thanks. but do you know how to properly align the fitted values with the data then? I would like the predictions to be in my original tibble – ℕʘʘḆḽḘ May 01 '19 at 17:21
  • here we obviously lose one obs when we fit the arma. how can I get back the prediction to the tibble `WWWusage %>% as_tibble()` – ℕʘʘḆḽḘ May 01 '19 at 17:23
  • thanks. do you mind posting this as a solution? also, doing `df$fitted` as you suggest will generate a crazy column name. how can I just have `fitted` in the tibble instead? – ℕʘʘḆḽḘ May 01 '19 at 17:42

1 Answers1

1

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.

hmhensen
  • 2,974
  • 3
  • 22
  • 43