Hierarchical and grouped time series
Today let's understand how a set of related time series is organised, and why that organisation decides how you can forecast it.
Let's say you plan tourism for two Australian states, Tasmania and Western Australia. Tourism Research Australia counts the overnight trips visitors make, every quarter, from 1998 Q1 to 2016 Q4. Tasmania has 5 tourism regions and Western Australia has 5.
The planning team wants three kinds of forecast: one for the two states together, one for each state, and one for each region.
So the team is not forecasting one series. It is forecasting a set of series where each higher one is the sum of the lower ones.
The widget shows the three levels, and together they hold 13 series: 1 + 2 + 10.
The bottom-level series in a tidy table
Let's start with the data, because everything in this lesson is built from it.
The tourism table lives in the tsibble package. A tsibble is a data frame for time series with two extra ideas. The index is the column that holds the time, here Quarter. The keys are the columns that together identify one series, here Region, State and Purpose.
We keep the two states and every quarter up to 2016 Q4. The call filter_index(. ~ "2016 Q4") does the second part: the dot stands for the start of the data, so it keeps everything up to and including 2016 Q4. Press Run.
tsibble gives the data structure, fabletools gives aggregate_key() and fable gives the forecasting models, so all three are loaded.
The range confirms the window: 1998 Q1 to 2016 Q4, which is 76 quarters. There are 3,040 rows and 40 keys. Each row is one quarter of one series, and 40 series times 76 quarters gives 3,040.
Each series is one region and one purpose of travel, which is Business, Holiday, Other or Visiting. The first one in the output is business trips in Australia's Coral Coast, and Trips is the number of overnight trips, in thousands. These 40 are the bottom-level series: the finest series the data gives us.
Now let's list each region with the state it sits in.
There are 10 regions, 5 in each state, so 10 regions times 4 purposes gives the 40 series. Every region appears under one state only. That one-state-per-region property is what a hierarchy is built on.
What is a hierarchical time series?
Add up the four purposes in a region and you get a series for the region. Add up the regions in a state and you get a series for the state. Add up the two states and you get one series for all the trips in the slice. Each of these sums is called an aggregate series.
Put the sums in order and they form a hierarchy: a structure where every series has exactly one parent. The parent of a region is its state, the parent of a state is the total, and the total has no parent. A series below another one is its child, and each layer of the structure (total, state, region) is a level.
aggregate_key() builds the structure. State / Region says that Region is nested inside State, and Trips = sum(Trips) says how to add the series up. Purpose is not named, so it is summed away. That makes the 10 region series the bottom level of this hierarchy: the finest level the structure keeps.
That is 13 series: 1 total, 2 states and 10 regions. Let's print all 13 for one quarter. yearquarter("2016 Q4") writes a quarter in a form R can compare with the Quarter column.
The key columns now show <aggregated> wherever a series covers everything under that key. The total has it in both State and Region. Tasmania has it in Region, because Tasmania adds up all of its regions. The chr* type marks the key columns that can hold <aggregated>.
Now read the Trips column. Tasmania's 833 thousand trips sit above its five regions: 106, 331, 216, 149 and 30.9. The total, 3,489, is Tasmania's 833 plus Western Australia's 2,656.
Let's check that in code. is_aggregated(Region) is TRUE on a row where Region is <aggregated>, so it picks out Tasmania's own series, and !is_aggregated(Region) picks out its five regions.
The five regions add up to 832.99, the same as the Tasmania series, so all.equal() returns TRUE. Series built with aggregate_key() add up exactly in every quarter, because each aggregate is defined as the sum of its children.
What is a grouped time series?
Now let's cross the attributes instead of nesting them. Purpose does not sit inside State. Holiday trips exist in Tasmania and in Western Australia alike, and each state has all four purposes.
When attributes cross like this, the structure is called a grouped time series. A bottom-level series then has two parents: one for its state and one for its purpose.
We cross State with Purpose using *.
That is 15 series: 1 total, 2 states, 4 purposes and 8 state and purpose series (2 states times 4 purposes). Region is summed away here, so those 8 are the bottom level. Let's count the series by which of the two keys is aggregated.
Read the rows:
- Neither key aggregated: 8 bottom-level series.
- Only
Purposeaggregated: 2 series, one per state. - Only
Stateaggregated: 4 series, one per purpose. - Both keys aggregated: 1 series, the total.
Here is the difference from a hierarchy. In a grouped structure the total can be reached two ways: by adding the 2 state series, or by adding the 4 purpose series. Let's add up the 2016 trips both ways.
Both routes reach 13,376 thousand trips for 2016. So the total series has two ways to be built.
The same is true one level down. Let's take the Tasmania holiday series and find which series it is added into.
Tasmania holiday trips, 1,752 thousand, are added into two series. One is Tasmania across all purposes, at 3,062. The other is Holiday across all states, at 5,758. Those are its two parents: the Tasmania total and the Holiday total.
Reading a state by purpose table and its totals
A two-way table lays the grouped structure out flat. The widget shows the 2016 trips, in thousands, with state down the side and purpose across the top. Press Report table to switch from the raw print to the formatted table.
The numbers come from adding up the four quarters of 2016 in the grouped structure. Each of the 8 cells inside the table is a bottom-level series. Tasmania Holiday is 1,752.
The last column adds across a row, so it holds the two state series: Tasmania at 3,062 and Western Australia at 10,314. The last row adds down a column, so it holds the four purpose series: Business at 3,274, Holiday at 5,758, Other at 634 and Visiting at 3,709. The corner, 13,376, is the total series.
So the margins of the table are series too: 8 cells, 2 row totals, 4 column totals and 1 corner make the 15 series. Add up the last column or add up the last row and you reach the same corner.
One thing to notice in the report table. The four rounded Tasmania cells add up to 3,061, but the total says 3,062. Each total is the sum of the unrounded values, so it can differ by 1 from the rounded cells.
Quick check: how many parents does a Tasmania holiday series have?
The Tasmania, Holiday cell in the table holds 1,752 thousand trips. How many series is it added into?
How to combine nesting and crossing in one structure
Real data usually has both. Region nests inside State, and Purpose crosses with all of that.
aggregate_key() uses two operators for this. The / nests, so State / Region puts each region under its state. The * crosses, so Purpose * (State / Region) crosses Purpose with the whole State and Region hierarchy. The parentheses keep the nested part together.
That is 65 series. You can predict the count before running anything. Each part of the structure has a number of nodes, where a node is one series in that part, and crossing multiplies the parts' node counts.
State / Region has 1 total, 2 states and 10 regions, so 13 nodes. Purpose has 1 total and 4 purposes, so 5 nodes. Crossing gives 13 times 5, which is 65.
The same rule gives 3 times 5, which is 15, for State * Purpose. Both match what n_keys() returned.
In the 65-series structure the bottom level is the 40 series we started with: 10 regions times 4 purposes. Everything else, 25 series, is a sum of them.
Why forecasts made series by series do not add up
The data we built adds up exactly. Forecasts made one series at a time usually do not, and we can see it in the 13-series hierarchy.
We will forecast with ETS, which is exponential smoothing. It forecasts from a smoothed level and, when the series has them, a trend and a seasonal pattern. The three letters in a name like ETS(M,N,M) stand for the error, trend and seasonal parts: A is additive, M is multiplicative and N is none. ETS() picks the form for each series automatically.
The code below fits one ETS model to each of the 13 series, and prints the ones for Tasmania.
The Tasmania series itself gets ETS(M,N,M). East Coast gets ETS(A,N,A), and Launceston, Tamar and the North gets ETS(M,N,A). Different series get different forms, each with its own smoothing parameters, and nothing in the 13 fits links one series to another.
Now let's forecast 4 quarters ahead, which is 2017 Q1 to 2017 Q4. The column .mean holds the point forecast. We add up the five regional forecasts, then round both numbers to whole thousands so each gap is the difference of the two printed numbers.
In 2017 Q1 the Tasmania forecast is 1,032 thousand trips, and its five regional forecasts add up to 999. The gap is 33 thousand trips. In 2017 Q3 the gap turns to -3, so there the regions add up to more than Tasmania.
The same thing happens one level up. Here is the slice total against the sum of the two state forecasts in 2017 Q1.
The slice total is 3,908 and the two states add up to 3,760, a gap of 148.
A set of forecasts is coherent when every aggregate forecast equals the sum of its children's forecasts. When it does not, the forecasts are incoherent, and these are. A planner who needs the state plan and the regional plans to agree cannot use them as they are.
Adjusting the forecasts so that they add up is called forecast reconciliation.
How Tasmania's five regions differ
Each of the five regions was fitted with its own model. The widget plots Tasmania's five regions for 16 quarters, 2013 Q1 to 2016 Q4, one dot per quarter. Switch to the facet_wrap view and press Run to get one panel per region.
The widget carries these 80 values, 16 quarters for each of the five Tasmania regions, taken from the same tourism data. The x axis is the year, so 2013.25 is 2013 Q2.
In the Run output all five panels share one y axis, so the levels can be compared. Hobart and the South sits high, between 179 and 446 thousand trips. East Coast runs from 42.5 to 173, Launceston, Tamar and the North from 105 to 255, and North West from 75.4 to 174. Wilderness West sits at the bottom, between 11.3 and 75.4.
The size of the seasonal swing differs too. East Coast's busiest quarter is about 4 times its quietest, 173 against 42.5. Hobart and the South's busiest is about 2.5 times its quietest, 446 against 179.
So the five regions differ in level and in swing, and each gets its own model. Every model is fitted to its own series alone, and nothing makes the five regional forecasts add up to the Tasmania forecast.
Which methods can each structure use to make forecasts add up?
Reconciliation methods differ in where they start. Four are worth knowing:
- Bottom-up forecasts the bottom-level series, then adds them up the structure. Every aggregate is a sum by construction, so the forecasts are coherent.
- Top-down forecasts the total, then splits it down the structure using shares, such as each region's share of its state.
- Middle-out starts at a middle level, such as the states. It forecasts that level, adds up for the levels above and splits down for the levels below.
- Optimal combination forecasts every series in the structure, then adjusts all the forecasts together so that they add up.
Top-down needs shares, so let's compute some. This block finds each region's share of Tasmania's 2016 trips in the hierarchy.
The five shares add up to 100%. A top-down split of a Tasmania forecast gives each region its share and nothing is left over. That works because each region has one parent, Tasmania, so its share of that parent is the only split there is.
Now the Tasmania holiday series. It has two parents, so it has two shares: one of Tasmania's trips and one of all Holiday trips. The block below uses tas_holiday, tas_all_purposes and all_states_holiday from the grouped structure.
Holiday trips are 57.2% of Tasmania and 30.4% of all Holiday trips. Both are valid shares, but they split different totals, so there is no single split. A top-down method needs a unique parent to divide.
The 65-series structure is grouped as well. A bottom series such as Tasmania East Coast holiday has two parents: the East Coast series across all purposes, and the Tasmania holiday series. So top-down does not apply there either.
Here is the answer in one table, with the structure on the left and the method across the top.
| Structure | Bottom-up | Top-down | Middle-out | Optimal combination |
|---|---|---|---|---|
Hierarchy, State / Region |
Yes | Yes | Yes | Yes |
Grouped, State * Purpose |
Yes | No | No | Yes |
Both, Purpose * (State / Region) |
Yes | No | No | Yes |
A No means the split needs a unique parent, and the structure does not supply one. Bottom-up and optimal combination need no split, so they work on either structure. Top-down and middle-out split a total by shares, so they need one parent per series.
Quick check: why do the forecasts not add up, and what can fix them?
The Tasmania forecast for 2017 Q1 is 1,032 thousand trips, and its five regional forecasts add up to 999. Which explanation and fix is right?
Your turn: declare a structure and count its series
Suppose the planners drop the State level and want Region crossed with Purpose instead. First work out how many series that structure has. Then declare it on sl and confirm the count with n_keys().
Show answer
# Cross Region with Purpose and count the series
regional <- sl |>
aggregate_key(Region * Purpose, Trips = sum(Trips))
n_keys(regional)
#> [1] 55The count comes from the node-count rule. Region has 1 + 10 = 11 nodes, Purpose has 1 + 4 = 5 nodes, and 11 times 5 is 55. There is no State level, so the 10 regions all sit directly under the total.
References
- Forecasting: Principles and Practice, 3rd edition, chapter 11 - Hyndman and Athanasopoulos (2021), OTexts. The chapter on hierarchical and grouped time series.
- Hierarchical forecasts for Australian domestic tourism - Athanasopoulos, Ahmed and Hyndman (2009), International Journal of Forecasting 25(1), 146-166. A hierarchical forecasting study of Australian domestic tourism.
- Optimal combination forecasts for hierarchical time series - Hyndman, Ahmed, Athanasopoulos and Shang (2011), Computational Statistics and Data Analysis 55(9), 2579-2589.
- Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization - Wickramasuriya, Athanasopoulos and Hyndman (2019), Journal of the American Statistical Association 114(526), 804-819.
- aggregate_key() reference page - the fabletools documentation.
Quick recap
You started from 40 bottom-level series, built a hierarchy, a grouped structure and a combination of both from them, and then saw what forecasting does to a hierarchy. To summarize:
- Count the parents of a bottom-level series. One parent means a hierarchy. Two parents means a grouped structure, where the attributes cross instead of nesting.
aggregate_key()declares the structure. The/nests and the*crosses, and the series count is the product of each part's node count: 13 forState / Region, 5 forPurpose, so 65 series together.- Series built by aggregation add up exactly: Tasmania's 833 thousand trips in 2016 Q4 equal the sum of its five regions.
- Forecasts made series by series do not. ETS fitted to each of the 13 series gave 1,032 for Tasmania in 2017 Q1 and 999 for its regions, a gap of 33, so the forecasts are incoherent. Adjusting them to add up is forecast reconciliation.
- The structure decides the method. Bottom-up and optimal combination work on a hierarchy and on a grouped structure. Top-down and middle-out split a total by shares, so they need one parent per series.
So with any set of related series, count the parents of a bottom series first: that count tells you which reconciliation methods you can use.