如何使用 auto.arima 预测样本内?

how to predict in-sample using auto.arima?

考虑这个简单的例子

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

我知道我可以使用 forecast 包安装 ARIMA 模型。这很简单。

> 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

问题是我想在我的训练样本中预测提前一步预测。也就是说,我怎样才能在包含 fit?

的预测的原始数据中包含一列 prediction

使用 forecast 只会 return obs 的预测。 101110(样本外)。

> 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

我也想要所有以前的(样本内)预测(使用相同的参数)。就像用 lmbroom:augment() 一样。

我怎么能在这里做到这一点?

谢谢!

请参阅下面的解决方案。

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

由于您使用的是 dplyr,对于最后一步,您还可以执行以下操作:

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

如果您想知道为什么拟合值与原始观察结果完全一致,您可以阅读 forecast 包文档。 Rob Hyndman 开发了这个包,它非常复杂。他使用一系列移动平均线的回溯和预测来填补缺失的信息。

有关更多解释,请参阅他在 https://otexts.com/fpp2/ and the documentation for the forecast at https://cran.r-project.org/web/packages/forecast/forecast.pdf 上关于预测的在线书籍。