tidyverts / fabletools

General fable features useful for extension packages
http://fabletools.tidyverts.org/
89 stars 31 forks source link

forecast() doesn't back transform box_cox transformations when generating coherent forecasts using reconcile() #382

Open shelby-mcneill-domo opened 1 year ago

shelby-mcneill-domo commented 1 year ago

The following code doesn't automatically back transform a box_cox transformation when generating coherent forecasts using reconcile():

library(fable)
library(dplyr)
library(feasts)
library(tsibble)

UKLungDeaths <- as_tsibble(cbind(mdeaths, fdeaths), pivot_longer = TRUE)

UKLungDeaths_gts <- UKLungDeaths |>
  aggregate_key(key, value = sum(value))

lambda <- UKLungDeaths_gts |>
  features(value, features = guerrero)

train_data <- UKLungDeaths_gts |>
  left_join(lambda, by = "key") 

test_data <- new_data(UKLungDeaths_gts, 12) %>% 
  left_join(lambda)

train_data |>
  model(base = ETS(box_cox(value, lambda_guerrero))) |>
  reconcile(
    bottom_up = bottom_up(base),
    MinT = min_trace(base, method = "mint_shrink") ) |> forecast(test_data)

Snippet of forecast output:

image

I also tried setting the transformation to use only the first value of lambda, as mentioned in #103, but get the following error:

train_data |>
  model(base = ETS(box_cox(value, first(lambda_guerrero)))) |>
  reconcile(
    bottom_up = bottom_up(base),
    MinT = min_trace(base, method = "mint_shrink") ) |> forecast(test_data)

Error in `mutate()`:
ℹ In argument: `MinT = (function (object, ...) ...`.
Caused by error in `vec_size()`:
! object 'lambda_guerrero' not found

Thank you!