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.

RInteractive R
# Load the packages and build the slice of 40 bottom-level series library(tsibble) library(fabletools) library(fable) library(dplyr) tourism <- tsibble::tourism sl <- tourism |> filter(State %in% c("Tasmania", "Western Australia")) |> filter_index(. ~ "2016 Q4") range(sl$Quarter) #> <yearquarter[2]> #> [1] "1998 Q1" "2016 Q4" #> # Year starts on: January nrow(sl) #> [1] 3040 n_keys(sl) #> [1] 40 head(sl) #> # A tsibble: 6 x 5 [1Q] #> # Key: Region, State, Purpose [1] #> Quarter Region State Purpose Trips #> <qtr> <chr> <chr> <chr> <dbl> #> 1 1998 Q1 Australia's Coral Coast Western Australia Business 26.2 #> 2 1998 Q2 Australia's Coral Coast Western Australia Business 27.2 #> 3 1998 Q3 Australia's Coral Coast Western Australia Business 33.7 #> 4 1998 Q4 Australia's Coral Coast Western Australia Business 31.0 #> 5 1999 Q1 Australia's Coral Coast Western Australia Business 28.0 #> 6 1999 Q2 Australia's Coral Coast Western Australia Business 32.8

  

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.

RInteractive R
# List each region with the state it sits in sl |> as_tibble() |> distinct(State, Region) #> # A tibble: 10 × 2 #> State Region #> <chr> <chr> #> 1 Western Australia Australia's Coral Coast #> 2 Western Australia Australia's Golden Outback #> 3 Western Australia Australia's North West #> 4 Western Australia Australia's South West #> 5 Tasmania East Coast #> 6 Western Australia Experience Perth #> 7 Tasmania Hobart and the South #> 8 Tasmania Launceston, Tamar and the North #> 9 Tasmania North West #> 10 Tasmania Wilderness West

  

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.