tidyverts / fabletools

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

accuracy by horizon #341

Closed robjhyndman closed 2 years ago

robjhyndman commented 2 years ago

MRE:

library(fpp3)
#> ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
#> ✓ tibble      3.1.6          ✓ tsibble     1.1.1     
#> ✓ dplyr       1.0.7          ✓ tsibbledata 0.4.0.9000
#> ✓ tidyr       1.1.4          ✓ feasts      0.2.2     
#> ✓ lubridate   1.8.0          ✓ fable       0.3.1     
#> ✓ ggplot2     3.3.5
#> ── 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()

fit <- aus_arrivals %>%
  filter(year(Quarter) <= 2009) %>% 
  model(ets = ETS(Arrivals))
fc <- fit %>% 
  forecast(h = 11) %>%
  group_by(Origin, .model) %>%
  mutate(h = row_number()) %>%
  ungroup()
fc %>% 
  accuracy(aus_arrivals, by=c(".model","h"))
#> Error: The result is not a valid tsibble.
#> ℹ Do you need `as_tibble()` to work with data frame?

Created on 2022-01-12 by the reprex package (v2.0.1)

Not a problem if there is no key in the fable other than .model.

mitchelloharawild commented 2 years ago

Added. Previously by was assumed to only be for adding new grouping structures, like h, and required all existing keys to be used. This is no longer required.

library(fpp3)
#> ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
#> ✓ tibble      3.1.6          ✓ tsibble     1.0.1     
#> ✓ dplyr       1.0.7          ✓ tsibbledata 0.4.0.9000
#> ✓ tidyr       1.1.4          ✓ feasts      0.2.2.9000
#> ✓ lubridate   1.8.0          ✓ fable       0.3.1     
#> ✓ ggplot2     3.3.5.9000
#> ── 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()
fit <- aus_arrivals %>%
  filter(year(Quarter) <= 2009) %>% 
  model(ets = ETS(Arrivals))
fc <- fit %>% 
  forecast(h = 11) %>%
  group_by(Origin, .model) %>%
  mutate(h = row_number()) %>%
  ungroup()
fc %>% 
  accuracy(aus_arrivals, by=c(".model","h"))
#> # A tibble: 11 × 11
#>    .model     h .type     ME    RMSE     MAE    MPE  MAPE  MASE RMSSE  ACF1
#>    <chr>  <int> <chr>  <dbl>   <dbl>   <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 ets        1 Test   6570.  77733.  61636.  -6.26  40.6  4.17  3.89 0.393
#>  2 ets        2 Test  -3355. 120400.  88372. -32.5   73.0  6.02  6.05 0.129
#>  3 ets        3 Test   8025. 136534.  94424. -18.2   58.8  6.47  6.88 0.177
#>  4 ets        4 Test   9444. 127585. 102493. -16.9   62.5  6.96  6.40 0.320
#>  5 ets        5 Test   1983.  88967.  73629. -12.6   52.9  5.03  4.48 0.379
#>  6 ets        6 Test  -1959. 131268.  97567. -45.0   91.8  6.66  6.62 0.138
#>  7 ets        7 Test  -1575. 141055. 103902. -27.6   74.6  7.08  7.12 0.235
#>  8 ets        8 Test   6508. 139005. 113831. -21.8   74.1  7.72  7.01 0.312
#>  9 ets        9 Test  11618.  99632.  81964.  -5.57  55.8  5.58  5.04 0.429
#> 10 ets       10 Test    387. 139894. 106160. -40.6   96.4  7.26  7.11 0.135
#> 11 ets       11 Test   4697. 150610. 112844. -22.8   79.9  7.75  7.68 0.255

Created on 2022-01-12 by the reprex package (v2.0.0)