Skip to main content
added 211 characters in body
Source Link
Rob Hyndman
  • 58.3k
  • 29
  • 148
  • 199

The fable package replaces the hts package and produces prediction intervals. It is also much easier to handle the aggregation structure. Here is some code using the same example as in your question (updated to include multiple models).

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

df <- as_tsibble(hts::htseg1$bts) %>%
  mutate(
    level1 = substr(key, 1, 1),
    level2 = substr(key, 2, 2)
  ) %>%
  as_tsibble(index=index, key=c(level1,level2)) %>%
  select(-key) %>%
  aggregate_key(level1/level2, value = sum(value))

fc <- df %>%
  model(
    ses = ETS(value ~ trend("N") + season("N")),
    holt = ETS(value ~ trend("A") + season("N")),
    arima = ARIMA(value)
  ) %>%
  reconcile(mint
    ses = min_trace(ses),
    holt = min_trace(holt),
    arima = min_trace(arima)
  ) %>%
  forecast(h=10)
fc %>%
  filter(.model=="mint", is_aggregated(level1)) %>%
  autoplot(df)

fc %>%
  mutate(PI = hilo(value, level=95))
#> # A fable: 160240 x 7 [1Y]
#> # Key:     level1, level2, .model [16][24]
#>    level1 level2 .model index         value .mean                      PI
#>    <chr*> <chr*> <chr>  <dbl>        <dist> <dbl>                  <hilo>
#>  1 A      A      arimases     2002 N(9.12, 0.048031)  9.1115 [8.682833807747,  9.545839]95497520]95
#>  2 A      A      arimases     2003 N(9.12, 0.091062)  9.0815 [8.487620664913,  9.669394]95640354]95
#>  3 A      A      arima ses  2004   2004 N(9.2, 0.13093)  9.0415 [8.343733555308,  9.746244]95749959]95
#>  4 A      A      arimases   2005  2005  N(9.2, 0.1612)  9.0115 [8.228413462905,  9.798851]95842362]95
#>  5 A      A      arimases   2006  2006  N(9.2, 0.1915)  89.9815 [8.132359381495,  9.836236]95923773]95
#>  6 A      A      arimases   2007  2007  N(9.2, 0.2119)  89.9615 [8.050547307893,  9.863163]95997374]95
#>  7 A      A      arimases     2008  N(8.9.2, 0.2422)  89.9315 [7[8.979837240208, 910.882527]95065059]95
#>  8 A      A      arimases     2009  N(8.9.2, 0.25)  89.9115 [7[8.918087177208, 910.896244]95128059]95
#>  9 A      A      arimases     2010  N(8.9.2, 0.2728)  89.8815 [7[8.863739118037, 910.905656]95187231]95
#> 10 A      A      arimases     2011  N(8.9.2, 0.2931)  89.8615 [7[8.815615062070, 910.911742]95243198]95
#> # … with 150230 more rows

Created on 2020-10-2224 by the reprex package (v0.3.0)

The fable package replaces the hts package and produces prediction intervals. It is also much easier to handle the aggregation structure. Here is some code using the same example as in your question.

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

df <- as_tsibble(hts::htseg1$bts) %>%
  mutate(
    level1 = substr(key, 1, 1),
    level2 = substr(key, 2, 2)
  ) %>%
  as_tsibble(index=index, key=c(level1,level2)) %>%
  select(-key) %>%
  aggregate_key(level1/level2, value = sum(value))

fc <- df %>%
  model(arima = ARIMA(value)) %>%
  reconcile(mint = min_trace(arima)) %>%
  forecast(h=10)
fc %>%
  filter(.model=="mint", is_aggregated(level1)) %>%
  autoplot(df)

fc %>%
  mutate(PI = hilo(value, level=95))
#> # A fable: 160 x 7 [1Y]
#> # Key:     level1, level2, .model [16]
#>    level1 level2 .model index         value .mean                     PI
#>    <chr*> <chr*> <chr>  <dbl>        <dist> <dbl>                 <hilo>
#>  1 A      A      arima   2002 N(9.1, 0.048)  9.11 [8.682833, 9.545839]95
#>  2 A      A      arima   2003 N(9.1, 0.091)  9.08 [8.487620, 9.669394]95
#>  3 A      A      arima   2004    N(9, 0.13)  9.04 [8.343733, 9.746244]95
#>  4 A      A      arima   2005    N(9, 0.16)  9.01 [8.228413, 9.798851]95
#>  5 A      A      arima   2006    N(9, 0.19)  8.98 [8.132359, 9.836236]95
#>  6 A      A      arima   2007    N(9, 0.21)  8.96 [8.050547, 9.863163]95
#>  7 A      A      arima   2008  N(8.9, 0.24)  8.93 [7.979837, 9.882527]95
#>  8 A      A      arima   2009  N(8.9, 0.25)  8.91 [7.918087, 9.896244]95
#>  9 A      A      arima   2010  N(8.9, 0.27)  8.88 [7.863739, 9.905656]95
#> 10 A      A      arima   2011  N(8.9, 0.29)  8.86 [7.815615, 9.911742]95
#> # … with 150 more rows

Created on 2020-10-22 by the reprex package (v0.3.0)

The fable package replaces the hts package and produces prediction intervals. It is also much easier to handle the aggregation structure. Here is some code using the same example as in your question (updated to include multiple models).

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

df <- as_tsibble(hts::htseg1$bts) %>%
  mutate(
    level1 = substr(key, 1, 1),
    level2 = substr(key, 2, 2)
  ) %>%
  as_tsibble(index=index, key=c(level1,level2)) %>%
  select(-key) %>%
  aggregate_key(level1/level2, value = sum(value))

fc <- df %>%
  model(
    ses = ETS(value ~ trend("N") + season("N")),
    holt = ETS(value ~ trend("A") + season("N")),
    arima = ARIMA(value)
  ) %>%
  reconcile(
    ses = min_trace(ses),
    holt = min_trace(holt),
    arima = min_trace(arima)
  ) %>%
  forecast(h=10)
fc %>%
  filter(is_aggregated(level1)) %>%
  autoplot(df)

fc %>%
  mutate(PI = hilo(value, level=95))
#> # A fable: 240 x 7 [1Y]
#> # Key:     level1, level2, .model [24]
#>    level1 level2 .model index         value .mean                      PI
#>    <chr*> <chr*> <chr>  <dbl>        <dist> <dbl>                  <hilo>
#>  1 A      A      ses     2002 N(9.2, 0.031)  9.15 [8.807747,  9.497520]95
#>  2 A      A      ses     2003 N(9.2, 0.062)  9.15 [8.664913,  9.640354]95
#>  3 A      A      ses     2004 N(9.2, 0.093)  9.15 [8.555308,  9.749959]95
#>  4 A      A      ses     2005  N(9.2, 0.12)  9.15 [8.462905,  9.842362]95
#>  5 A      A      ses     2006  N(9.2, 0.15)  9.15 [8.381495,  9.923773]95
#>  6 A      A      ses     2007  N(9.2, 0.19)  9.15 [8.307893,  9.997374]95
#>  7 A      A      ses     2008  N(9.2, 0.22)  9.15 [8.240208, 10.065059]95
#>  8 A      A      ses     2009  N(9.2, 0.25)  9.15 [8.177208, 10.128059]95
#>  9 A      A      ses     2010  N(9.2, 0.28)  9.15 [8.118037, 10.187231]95
#> 10 A      A      ses     2011  N(9.2, 0.31)  9.15 [8.062070, 10.243198]95
#> # … with 230 more rows

Created on 2020-10-24 by the reprex package (v0.3.0)

Source Link
Rob Hyndman
  • 58.3k
  • 29
  • 148
  • 199

The fable package replaces the hts package and produces prediction intervals. It is also much easier to handle the aggregation structure. Here is some code using the same example as in your question.

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

df <- as_tsibble(hts::htseg1$bts) %>%
  mutate(
    level1 = substr(key, 1, 1),
    level2 = substr(key, 2, 2)
  ) %>%
  as_tsibble(index=index, key=c(level1,level2)) %>%
  select(-key) %>%
  aggregate_key(level1/level2, value = sum(value))

fc <- df %>%
  model(arima = ARIMA(value)) %>%
  reconcile(mint = min_trace(arima)) %>%
  forecast(h=10)
fc %>%
  filter(.model=="mint", is_aggregated(level1)) %>%
  autoplot(df)

fc %>%
  mutate(PI = hilo(value, level=95))
#> # A fable: 160 x 7 [1Y]
#> # Key:     level1, level2, .model [16]
#>    level1 level2 .model index         value .mean                     PI
#>    <chr*> <chr*> <chr>  <dbl>        <dist> <dbl>                 <hilo>
#>  1 A      A      arima   2002 N(9.1, 0.048)  9.11 [8.682833, 9.545839]95
#>  2 A      A      arima   2003 N(9.1, 0.091)  9.08 [8.487620, 9.669394]95
#>  3 A      A      arima   2004    N(9, 0.13)  9.04 [8.343733, 9.746244]95
#>  4 A      A      arima   2005    N(9, 0.16)  9.01 [8.228413, 9.798851]95
#>  5 A      A      arima   2006    N(9, 0.19)  8.98 [8.132359, 9.836236]95
#>  6 A      A      arima   2007    N(9, 0.21)  8.96 [8.050547, 9.863163]95
#>  7 A      A      arima   2008  N(8.9, 0.24)  8.93 [7.979837, 9.882527]95
#>  8 A      A      arima   2009  N(8.9, 0.25)  8.91 [7.918087, 9.896244]95
#>  9 A      A      arima   2010  N(8.9, 0.27)  8.88 [7.863739, 9.905656]95
#> 10 A      A      arima   2011  N(8.9, 0.29)  8.86 [7.815615, 9.911742]95
#> # … with 150 more rows

Created on 2020-10-22 by the reprex package (v0.3.0)