Use a model's fitted distribution to simulate additional data with similar behaviour to the response. This is a tidy implementation of stats::simulate().

# S3 method for class 'mdl_df'
generate(x, new_data = NULL, h = NULL, times = 1, seed = NULL, ...)

# S3 method for class 'mdl_ts'
generate(
  x,
  new_data = NULL,
  h = NULL,
  times = 1,
  seed = NULL,
  bootstrap = FALSE,
  bootstrap_block_size = 1,
  ...
)

Arguments

x

A mable.

new_data

The data to be generated (time index and exogenous regressors)

h

The simulation horizon (can be used instead of new_data for regular time series with no exogenous regressors).

times

The number of replications.

seed

The seed for the random generation from distributions.

...

Additional arguments for individual simulation methods.

bootstrap

If TRUE, then forecast distributions are computed using simulation with resampled errors.

bootstrap_block_size

The bootstrap block size specifies the number of contiguous residuals to be taken in each bootstrap sample.

Details

Innovations are sampled by the model's assumed error distribution. If bootstrap is TRUE, innovations will be sampled from the model's residuals. If new_data contains the .innov column, those values will be treated as innovations for the simulated paths.

Examples

library(fable)
library(dplyr)
UKLungDeaths <- as_tsibble(cbind(mdeaths, fdeaths), pivot_longer = FALSE)
UKLungDeaths %>% 
  model(lm = TSLM(mdeaths ~ fourier("year", K = 4) + fdeaths)) %>% 
  generate(UKLungDeaths, times = 5)
#> # A tsibble: 360 x 4 [1M]
#> # Key:       .model, .rep [5]
#>    .model    index .rep   .sim
#>    <chr>     <mth> <chr> <dbl>
#>  1 lm     1974 Jan 1     2467.
#>  2 lm     1974 Feb 1     1702.
#>  3 lm     1974 Mar 1     2098.
#>  4 lm     1974 Apr 1     1909.
#>  5 lm     1974 May 1     1366.
#>  6 lm     1974 Jun 1     1011.
#>  7 lm     1974 Jul 1     1108.
#>  8 lm     1974 Aug 1     1096.
#>  9 lm     1974 Sep 1     1099.
#> 10 lm     1974 Oct 1     1457.
#> # ℹ 350 more rows