Forecasting Hierarchical Time Series using R

Akshay
Brillio Data Science
4 min readJun 8, 2018

Context

This article is about a technique which can come quite handy when one wants to build multiple time series models for the time series which have inherently hierarchical structure.(Knowledge of Time series modelling is assumed but paucity of latter will not hinder you from making these models !.

Go straight to code here)

Time series can often be naturally disaggregated in a hierarchical structure using attributes such as geographical location, product type, etc. For example, the total number of Member of Parliaments(MPs) in a given election can come from different States and in turn given a particular State ,from different cities, districts and so forth.Such disaggregation imposes a hierarchical structure. We refer to these as hierarchical time series.

Another possibility is that series can be naturally grouped together based on attributes without necessarily imposing a hierarchical structure. For example the MPs in the above context can be filtered down also on the basis of sex viz.Male ,Female and Others. Grouped time series can be thought of as hierarchical time series that do not impose a unique hierarchical structure in the sense that the order by which the series can be grouped is not unique.

Though in this blog we will talk solely about hierarchical time series though grouped time series can also be handled symmetrically.

I will try and focus more on the intuition rather than the mathematical details here but if one is interested in the same please refer here

Let’s plot some hierarchical time series below.This one pertains to Total quarterly visitor nights from 1998–2011 for eight regions of Australia:

Data Description

I will show below some graphs and tables pertaining to the Data at hand so as to expose it better.

Australia Visitor Nights Hierarchy
Quarterly Visitors at State Level

Pretty self explanatory right.

Below I will try and explain the different approaches which can be used to forecast hierarchical time series in our context in layman's terms.( The optimal combination approach is skipped as its outside the scope both in terms of time and prerequisites but interested folks can refer here)

  • bottom-up approach: Here this means we will forecast lower most level of the Hierarchy i.e cities and then aggregate the results up the hierarchy.
  • top-down approach: Here this means that we will forecast at the highest level hierarchy i.e pan Australia level and then disaggregate the results down the hierarchy.This disaggregation can happen via
  1. computing relative proportions historically and using that for future periods.
  2. forecasting the proportions in isolation,normalising it and then using that to disaggregate.
  • Middle-out approach:It’s a hybrid approach that entails:
  1. A middle level is chosen let’s say the state level which has 4 series/nodes within it.
  2. 4 time series models at this level will be made.
  3. For the top mode level bottom up approach would be used to aggregate the results.
  4. For the bottom level middle level results would be disaggregated downwards.

Pretty simple right !

Now implementing this is even simpler.I will demonstrate a working example via one of the above techniques and rest one can catch up via the documentation of hts package in R language.

Application

y <- hts(vn, nodes=list(4,c(2,2,2,2)))

The above command creates a hierarchical time series with 3 levels(top most level one does not have to specify) with 4 nodes/states in the middle and 8 nodes/cities in bottom most level.(Argument ‘nodes’ does the trick for you here,also notice 2 cities are tagged to each state.)

allf <- forecast(y, h=8,method = 'tdfp',fmethod = 'ets')#here tdfp #means top-down forecast proportions and ets is roughly exponential #smoothing
plot(allf)
Forecasts across all the Hierarchies

Yes it was as easy as just those 2 small chunks of code!

The above command will give you forecasts(dotted lines in respective colours are the forecasts) across all levels in the hierarchy using top-down forecast proportions approach discussed above for 8 periods(quarters in our case) ahead.

Just to reiterate:

  • Level 0–pan Australia level
  • Level 1–State level
  • Level 2–City Level

One can go further deep in the documentation here and try out different techniques with different parameter combinations.

Below animation captures forecast by various(some) combinations of parameters/methods.

Animation capturing forecasts across hierarchy using different algorithms.

Concluding Remarks

In this post we have been able to learn from scratch(atleast at an applied and intuitive) level how open source tools like R and hts package can be leveraged to build time series models quite simply in complicated hierarchical structural data for forecasting purposes.

Also many things have been missed out courtesy paucity of space and time but in the above framework in lieu of Arima or classical statistical models one can define user defined function as well for prediction purposes which suits the context. There are many other great things one can explore in this amazing package/module.

Hope this blog was helpful.See you soon then.

Peace…

--

--