[Experimental]

Model columns of a mable can be combined with cbind() into a model data frame: a data frame of model columns (with the mdl_df class) holding several models for each series. A model data frame is created within dplyr::mutate() (e.g. mutate(fit, grp = cbind(ets, arima))), and is stored in the mable as a single model column. rbind() stacks the rows of model data frames with the same model columns, which are matched by name.

# S3 method for class 'mdl_lst'
cbind(..., deparse.level = 1)

# S3 method for class 'mdl_df'
cbind(..., deparse.level = 1)

# S3 method for class 'mdl_df'
rbind(..., deparse.level = 1)

Arguments

...

Model columns (mdl_lst) or model data frames (mdl_df) to combine.

deparse.level

For cbind(), how unnamed arguments are named. With 1 (the default) unnamed symbols (or fit$model) are used as names, and with 2 other expressions are also deparsed into names. With 0, unnamed model columns aren't allowed. Unused by rbind().

Value

A model data frame, with the mdl_df class.

Details

Unnamed model columns are named after their expression, with fit$ets named ets. Unnamed model data frames are extended with their model columns, while named model data frames are nested within the new one, so cbind(grp, arima) adds arima to the models of grp, and cbind(grp = grp, arima) gives a model data frame containing grp and arima. The model columns must all contain the same number of models (one for each series in the mable).

When printed within a mable, a model data frame is shown as a column for each of its models, under the name of its column (such as grp$ets followed by $arima).

Results computed from a mable include each model of a model data frame, labelled in the .model column by the string "<name>$<model>" (e.g. "grp$ets"). This applies to forecast(), generate(), accuracy(), fitted(), residuals(), augment(), glance(), tidy(), components() and hypothesize(). These labels must be unique, so results can't be computed from a mable that also has a model column named grp$ets. Generics that modify the models, such as refit() and stream(), return the model data frame with its models replaced. Model data frames can also be used with:

  • report() and equation(), for a mable with a single model.

  • tidyr::pivot_longer() and tidyr::gather(), which replace the model data frames being pivoted with their models (named <name>$<model>).

  • simulate_iid() and conformal_scp(), which are applied to each of its models.

  • bootstrap_iid() and bootstrap_block(), which sample jointly across all of its models: the same historical time point (or block) is drawn for each model and series at a given step of each path, so their forecasts share the same resampled residuals. This includes the models of bootstrapped or reconciled columns within the model data frame.

The reconciliation functions (such as reconcile_mint()) are applied to each of its model columns separately, as reconciliation is across the series of a model, not across models.

interpolate() requires a mable with a single model column, which can't be a model data frame.

Examples

library(fable)
library(dplyr)
fit <- as_tsibble(cbind(mdeaths, fdeaths), pivot_longer = TRUE) %>%
  model(ets = ETS(value), lm = TSLM(value ~ trend() + season()))

# Combine the models of each series
fit_grp <- fit %>%
  transmute(grp = cbind(ets, lm))
fit_grp
#> # A mable: 2 x 2
#> # Key:     key [2]
#>   key          grp$ets     $lm
#>   <chr>        <model> <model>
#> 1 fdeaths <ETS(M,N,M)>  <TSLM>
#> 2 mdeaths <ETS(M,A,A)>  <TSLM>

# The results include each model of the model data frame
fit_grp %>%
  forecast(h = "1 year")
#> # A fable: 48 x 5 [1M]
#> # Key:     key, .model [4]
#>    key     .model     index
#>    <chr>   <chr>      <mth>
#>  1 fdeaths grp$ets 1980 Jan
#>  2 fdeaths grp$ets 1980 Feb
#>  3 fdeaths grp$ets 1980 Mar
#>  4 fdeaths grp$ets 1980 Apr
#>  5 fdeaths grp$ets 1980 May
#>  6 fdeaths grp$ets 1980 Jun
#>  7 fdeaths grp$ets 1980 Jul
#>  8 fdeaths grp$ets 1980 Aug
#>  9 fdeaths grp$ets 1980 Sep
#> 10 fdeaths grp$ets 1980 Oct
#> # ℹ 38 more rows
#> # ℹ 2 more variables: value <dist>, .mean <dbl>
accuracy(fit_grp)
#> # A tibble: 4 × 11
#>   key     .model  .type           ME  RMSE   MAE    MPE  MAPE  MASE RMSSE   ACF1
#>   <chr>   <chr>   <chr>        <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl>  <dbl>
#> 1 fdeaths grp$ets Training -7.96e+ 0  65.7  45.3 -2.25   7.85 0.666 0.612 0.0463
#> 2 fdeaths grp$lm  Training  0         63.0  42.6 -0.860  7.35 0.627 0.588 0.0777
#> 3 mdeaths grp$ets Training -1.34e+ 1 155.  109.  -1.32   6.80 0.627 0.590 0.137 
#> 4 mdeaths grp$lm  Training  3.15e-15 152.  105.  -0.699  6.45 0.603 0.578 0.137