R fable combination model using the median instead of mean

76 views Asked by At

I am trying to create a combination model using the R fable and/or fable.tools packages. However, instead of the mean I want to use the median.

I cannot find any examples where the cmbn_fn or cmbn_args were used where it isn't mean or weighted, but I'm thinking it must be possible.

I've tried multiple variations of something like the below without success. Hoping someone knows how to return the median of models.

comb1 = combination_model(
                TSLM(log(y) ~ trend() + season()), 
                 ARIMA(log(y), stepwise = TRUE, approximation = TRUE)),
                 THETA(log(y)),
                 ETS(log(y)),
                 cmbn_fn = combination_ensemble, 
                 cmbn_args = list(median)

Thank you, Brian

1

There are 1 answers

1
Mitchell O'Hara-Wild On

If the component models of the combination are not normally distributed, you can obtain median forecasts of combination models from bootstrapped paths. I've needed to make a slight addition to fabletools to make this possible, so you will need to install the latest development version from GitHub with remotes::install_github("tidyverts/fabletools").

Here's an example for how it works.

library(fable)
#> Loading required package: fabletools
tsibbledata::aus_production %>%
  model(
    cmbn1 = combination_model(
      SNAIVE(log(Beer)), TSLM(log(Beer) ~ trend() + season()),
      cmbn_args = list(weights = "inv_var")
    )
  ) |> 
  forecast(bootstrap = TRUE, point_forecast = list(.mean = mean, .median = median))
#> # A fable: 8 x 5 [1Q]
#> # Key:     .model [1]
#>   .model Quarter         Beer .mean .median
#>   <chr>    <qtr>       <dist> <dbl>   <dbl>
#> 1 cmbn1  2010 Q3 sample[5000]  425.    425.
#> 2 cmbn1  2010 Q4 sample[5000]  497.    497.
#> 3 cmbn1  2011 Q1 sample[5000]  423.    423.
#> 4 cmbn1  2011 Q2 sample[5000]  381.    381.
#> 5 cmbn1  2011 Q3 sample[5000]  425.    424.
#> 6 cmbn1  2011 Q4 sample[5000]  498.    498.
#> 7 cmbn1  2012 Q1 sample[5000]  423.    423.
#> 8 cmbn1  2012 Q2 sample[5000]  382.    381.

Created on 2023-10-18 with reprex v2.0.2

Note that cmbn_args are arguments passed on to the cmbn_fn, which doesn't accept the median as an input. This is for things like custom weights for the combination. The point forecasts to be produced are specified in the point_forecast argument of the forecast() function.