Skip to content

Commit f477f80

Browse files
committed
show how to forecast aggregate of several time periods in Chatper 12
1 parent 7178871 commit f477f80

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Chapter12.rmd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,44 @@ autoplot(fc_eggs_ets.aan.constrained)
183183
184184
```
185185

186+
187+
## Prediction intervals for aggregates
188+
189+
```{r echo=FALSE, message=FALSE, warning=FALSE, Forecast_aggregate_of_time_periods}
190+
191+
# For example: You may have daily data, fit a model using the data, and want to forecast the total for the next week(need to aggregate 7 days of forecasts).
192+
# If the point forecasts are means, then adding them up will give a good estimate of the total.
193+
194+
# A general solution is to use simulations.
195+
# An example using ETS models applied to Australian monthly gas production data.
196+
# First fit a model to the data
197+
gas_ets <- ets(gas/1000)
198+
199+
# Forecast six months ahead
200+
fc_gas_ets <- forecast(gas_ets, h=6)
201+
202+
# Simulate 10000 future sample paths
203+
nsim <- 10000
204+
h <- 6
205+
sim <- numeric(nsim)
206+
207+
for(i in seq_len(nsim)){
208+
# for each sample path, add 6 months' forecasts.
209+
sim[i] <- sum(simulate(gas_ets, future=TRUE, nsim=h))
210+
}
211+
212+
# get final aggregated forecast.
213+
gas_ets.sim.meanagg <- mean(sim)
214+
215+
sum(fc_gas_ets$mean[1:6])
216+
gas_ets.sim.meanagg
217+
# The results from above 2 methods are similar to each other.
218+
219+
# get prediction intervals.
220+
#80% interval:
221+
quantile(sim, prob=c(0.1, 0.9))
222+
#95% interval:
223+
quantile(sim, prob=c(0.025, 0.975))
224+
225+
```
226+

0 commit comments

Comments
 (0)