tidyverts / fabletools

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

forecasting from reconciled ETS giving problems #304

Closed robjhyndman closed 3 years ago

robjhyndman commented 3 years ago

I'm not sure what is happening here. I've narrowed it down to ATC1=="A" but not which series is causing the problem.

library(fpp3)
#> ── Attaching packages ─────────────────────────────────────── fpp3 0.3.0.9000 ──
#> ✓ tibble      3.0.5          ✓ tsibble     0.9.3     
#> ✓ dplyr       1.0.3          ✓ tsibbledata 0.2.0     
#> ✓ tidyr       1.1.2          ✓ feasts      0.1.6.9000
#> ✓ lubridate   1.7.9.2        ✓ fable       0.2.1.9000
#> ✓ ggplot2     3.3.3
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> x lubridate::date()   masks base::date()
#> x dplyr::filter()     masks stats::filter()
#> x tsibble::interval() masks lubridate::interval()
#> x dplyr::lag()        masks stats::lag()
PBSagg <- PBS %>%
  aggregate_key(ATC1/ATC2 * Concession * Type, Scripts = sum(Scripts))
PBSfit <- PBSagg %>%
  filter(Month < max(Month)-3*12) %>%
  filter(ATC1 == "A") %>%
  fill_gaps(Scripts=0) %>%
  model(etsmodel = ETS(Scripts)) %>%
  reconcile(ets2 = min_trace(etsmodel))
PBSfc <- PBSfit %>% forecast(h=1)
#> Error: Problem with `mutate()` input `ets2`.
#> x error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Join columns must be present in data.
#> x Problem with `date`.
#> ℹ Input `ets2` is `(function (object, ...) ...`.

Created on 2021-02-01 by the reprex package (v1.0.0)

robjhyndman commented 3 years ago

Here is a simpler MRE

library(fpp3)
#> ── Attaching packages ─────────────────────────────────────── fpp3 0.3.0.9000 ──
#> ✓ tibble      3.0.5          ✓ tsibble     0.9.3     
#> ✓ dplyr       1.0.3          ✓ tsibbledata 0.2.0     
#> ✓ tidyr       1.1.2          ✓ feasts      0.1.6.9000
#> ✓ lubridate   1.7.9.2        ✓ fable       0.2.1.9000
#> ✓ ggplot2     3.3.3
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> x lubridate::date()   masks base::date()
#> x dplyr::filter()     masks stats::filter()
#> x tsibble::interval() masks lubridate::interval()
#> x dplyr::lag()        masks stats::lag()
PBS %>%
  filter(ATC2 == "A15", Concession != "Concessional") %>%
  aggregate_key(Type, Scripts = sum(Scripts)) %>%
  fill_gaps(Scripts=0) %>%
  model(ets = ETS(Scripts)) %>%
  reconcile(ets2 = min_trace(etsmodel)) %>%
  forecast(h = 1)
#> Error: Problem with `mutate()` input `ets2`.
#> x object 'etsmodel' not found
#> ℹ Input `ets2` is `min_trace(etsmodel)`.

Created on 2021-02-02 by the reprex package (v1.0.0)

Session info ``` r sessionInfo() #> R version 4.0.3 (2020-10-10) #> Platform: x86_64-pc-linux-gnu (64-bit) #> Running under: KDE neon User Edition 5.20 #> #> Matrix products: default #> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 #> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0 #> #> locale: #> [1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C #> [3] LC_TIME=en_AU.UTF-8 LC_COLLATE=en_AU.UTF-8 #> [5] LC_MONETARY=en_AU.UTF-8 LC_MESSAGES=en_AU.UTF-8 #> [7] LC_PAPER=en_AU.UTF-8 LC_NAME=C #> [9] LC_ADDRESS=C LC_TELEPHONE=C #> [11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C #> #> attached base packages: #> [1] stats graphics grDevices datasets utils methods base #> #> other attached packages: #> [1] fable_0.2.1.9000 feasts_0.1.6.9000 fabletools_0.3.0.9000 #> [4] tsibbledata_0.2.0 tsibble_0.9.3 ggplot2_3.3.3 #> [7] lubridate_1.7.9.2 tidyr_1.1.2 dplyr_1.0.3 #> [10] tibble_3.0.5 fpp3_0.3.0.9000 #> #> loaded via a namespace (and not attached): #> [1] Rcpp_1.0.6 progressr_0.7.0 pillar_1.4.7 #> [4] compiler_4.0.3 highr_0.8 tools_4.0.3 #> [7] digest_0.6.27 lattice_0.20-41 anytime_0.3.9 #> [10] evaluate_0.14 lifecycle_0.2.0 gtable_0.3.0 #> [13] pkgconfig_2.0.3 rlang_0.4.10 Matrix_1.3-2 #> [16] reprex_1.0.0 cli_2.2.0 rstudioapi_0.13 #> [19] yaml_2.2.1 xfun_0.20 withr_2.4.1 #> [22] styler_1.3.2 stringr_1.4.0 knitr_1.30 #> [25] generics_0.1.0 fs_1.5.0 vctrs_0.3.6 #> [28] grid_4.0.3 tidyselect_1.1.0 glue_1.4.2 #> [31] R6_2.5.0 fansi_0.4.2 distributional_0.2.1 #> [34] rmarkdown_2.6 farver_2.0.3 purrr_0.3.4 #> [37] magrittr_2.0.1 scales_1.1.1 backports_1.2.1 #> [40] ellipsis_0.3.1 htmltools_0.5.1.1 assertthat_0.2.1 #> [43] colorspace_2.0-0 renv_0.12.5 stringi_1.5.3 #> [46] munsell_0.5.0 crayon_1.3.4.9000 ```
mitchelloharawild commented 3 years ago

Silly issue, should be fixed now. The problem arises with ATC1=="A" as some series are incomplete.

Second MRE appears to be a different issue.

mitchelloharawild commented 3 years ago

Second issue from MRE: min_trace(etsmodel) should be min_trace(ets)

library(fpp3)
#> ── Attaching packages ─────────────────────────────────────── fpp3 0.3.0.9000 ──
#> ✓ tibble      3.0.5          ✓ tsibble     0.9.4     
#> ✓ dplyr       1.0.3          ✓ tsibbledata 0.2.0     
#> ✓ tidyr       1.1.2          ✓ feasts      0.1.6.9000
#> ✓ lubridate   1.7.9.2        ✓ fable       0.2.1.9000
#> ✓ ggplot2     3.3.3
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> x lubridate::date()    masks base::date()
#> x dplyr::filter()      masks stats::filter()
#> x tsibble::intersect() masks base::intersect()
#> x tsibble::interval()  masks lubridate::interval()
#> x dplyr::lag()         masks stats::lag()
#> x tsibble::setdiff()   masks base::setdiff()
#> x tsibble::union()     masks base::union()
PBS %>%
  filter(ATC2 == "A15", Concession != "Concessional") %>%
  aggregate_key(Type, Scripts = sum(Scripts)) %>%
  fill_gaps(Scripts=0) %>%
  model(ets = ETS(Scripts)) %>%
  reconcile(ets2 = min_trace(ets)) ->fit

fit %>%
  forecast(h = 1)
#> # A fable: 6 x 5 [1M]
#> # Key:     Type, .model [6]
#>   Type         .model    Month        Scripts   .mean
#>   <chr*>       <chr>     <mth>         <dist>   <dbl>
#> 1 Co-payments  ets    2008 Jul N(0.077, 0.12)  0.0766
#> 2 Co-payments  ets2   2008 Jul N(0.079, 0.12)  0.0788
#> 3 Safety net   ets    2008 Jul   N(-0.97, 35) -0.973 
#> 4 Safety net   ets2   2008 Jul   N(-0.43, 17) -0.432 
#> 5 <aggregated> ets    2008 Jul    N(0.19, 34)  0.190 
#> 6 <aggregated> ets2   2008 Jul   N(-0.35, 17) -0.353

Created on 2021-02-02 by the reprex package (v0.3.0)