Search code examples
rhierarchytime-seriesforecasting

How to use ‘hts’ with multi-level hierarchies?


I am forecasting on a large set of time series (5,000+). I would like to do this with a hierarchical approach were I do the forecasting at a higher level and then allocate the forecast down to each SKU. I see a need for doing this in order to zoom into a lower geographic level of detail, while doing the forecasting on a higher level (top-down).

For example, below you see a sample of the structure that I am thinking about.

Total
  => Europe
     => Netherlands
        => RegionA
           => Client_A_in_Netherlands
              => SKU1
              => SKU2
              => SKU3
           => Client_Q_in_Netherlands
              => SKU15
     => Germany1
        => (...)
           => ClientY_in_Germany
              => SKU89
  => Africa
     => South Africa
        => (...)
           => Client_Z_in_SouthAfrica
              => SKU792

I would like to do the top-down forecast at continent level (i.e. Europe or Africa) level. Then allocate the appropriate share to the countries, then to the clients within that country and then to the SKU.

In the documentation of the ‘hts’ package there is an example on how to do this with a two-level hierarchy. I was wondering whether somebody could advise on how to do this with a multi-level hierarchy?


Solution

  • We introduced a new concept nodes into the hts package (v4+) to replace the old gmatrix. To illustrate the usage of nodes, here's an example of a hierarchy with 4 levels (excluding total) and 24 bottom time series.

    bts <- ts(matrix(rnorm(240), nrow = 10, ncol = 24)) 
    nodes <- list(2, rep(2, 2), rep(2, 4), rep(3, 8))
    hts(bts, nodes = nodes)
    

    Each element of nodes specifies the number of children each node has at that level.

    The tree plot is shown below:

    => A
      => AA
        => AAA
          => 3 bottom time series
        => AAB
          => 3 bottom time series
      => AB
        => ABA
          => 3 bottom time series
        => ABB
          => 3 bottom time series
    => B
      => BA
        => BAA
          => 3 bottom time series
        => BAB
          => 3 bottom time series
      => BB
        => BBA
          => 3 bottom time series
        => BBB
          => 3 bottom time series