Wraps a fitted model (or a model column from a mable) so that forecast()
produces its distribution from bootstrapped sample paths instead of
closed-form/analytical results, and so that generate() draws its
innovations from the model's own fitted residuals instead of the model's
assumed error distribution. This allows the forecast distribution to take
a different shape to the model's theoretical one, which can better
reflect asymmetry or heavy tails in the data.
bootstrap_iid() resamples residuals independently (with replacement) for
each simulated time point. bootstrap_block() instead resamples
contiguous blocks of residuals, preserving any residual autocorrelation
the model hasn't captured.
Applied to a mdl_lst (a model column spanning multiple series/keys),
bootstrap_iid()/bootstrap_block() sample jointly across the series:
the same historical time point is drawn for every series at a given
(replicate, forecast-step) position, so the historical cross-sectional
relationship between series' residuals carries over into the simulated
innovations, rather than resampling each series as if it were independent
of the others. This matters most for hierarchies (see aggregate_key()/
reconcile_mint()): jointly bootstrapping respects the observed correlation
between a hierarchy's nodes, instead of treating every node as unrelated.
If the series don't all share the same historical time domain, sampling is
restricted to their overlapping period and a warning is raised.
Applied to a model data frame (see model_data_frame), sampling is joint across all of its models and series. The innovations of each model are then its own residuals at the same historical time points, which keeps the relationship between the models' (and series') errors.
bootstrap_iid(model, times = 5000)
bootstrap_block(model, times = 5000, block_size = NULL)A fitted model, typically a model column from a mable
modified with dplyr::mutate() (e.g. mutate(ets = bootstrap_iid(ets))).
Alternatively, an unfitted model specification can be wrapped directly
inside model() (e.g. model(ets = bootstrap_iid(ETS(Trips)))), in which
case each series is fitted and bootstrapped independently - unlike wrapping
an already-fitted mdl_lst column (which samples jointly across
series, see Description).
The number of bootstrapped sample paths to simulate.
The number of contiguous residuals resampled together in
each bootstrap draw. Defaults to the data's seasonal period (via
stats::frequency()), which preserves within-season residual
autocorrelation; use block_size = 1 for the same behaviour as
bootstrap_iid().
library(fable)
library(tsibbledata)
library(dplyr)
aus_production %>%
model(ets = ETS(Beer)) %>%
mutate(ets = bootstrap_iid(ets, times = 1000)) %>%
forecast()
#> # A fable: 8 x 4 [1Q]
#> # Key: .model [1]
#> .model Quarter Beer .mean
#> <chr> <qtr> <dist> <dbl>
#> 1 ets 2010 Q3 sample[1000] 406.
#> 2 ets 2010 Q4 sample[1000] 480.
#> 3 ets 2011 Q1 sample[1000] 417.
#> 4 ets 2011 Q2 sample[1000] 384.
#> 5 ets 2011 Q3 sample[1000] 404.
#> 6 ets 2011 Q4 sample[1000] 479.
#> 7 ets 2012 Q1 sample[1000] 416.
#> 8 ets 2012 Q2 sample[1000] 382.
# Applied to the model specification before fitting, each series is
# bootstrapped independently (no joint cross-sectional sampling):
aus_production %>%
model(ets = bootstrap_iid(ETS(Beer), times = 1000)) %>%
forecast()
#> # A fable: 8 x 4 [1Q]
#> # Key: .model [1]
#> .model Quarter Beer .mean
#> <chr> <qtr> <dist> <dbl>
#> 1 ets 2010 Q3 sample[1000] 404.
#> 2 ets 2010 Q4 sample[1000] 480.
#> 3 ets 2011 Q1 sample[1000] 417.
#> 4 ets 2011 Q2 sample[1000] 383.
#> 5 ets 2011 Q3 sample[1000] 403.
#> 6 ets 2011 Q4 sample[1000] 479.
#> 7 ets 2012 Q1 sample[1000] 415.
#> 8 ets 2012 Q2 sample[1000] 381.