Hierarchical Time Series Model Using Means: A Comprehensive Guide

hierarchical-bayesianmultilevel-analysistime series

Based on my research and (limited) understanding, I am finding that hierarchical time series modeling works by summing the nodes below to create a total value at the higher levels. I am trying to use the mean for the lowest level (mean rent prices) and want to use a hierarchical model to predict mean prices within zip codes, cities, and other higher levels. (I dont have the total number available for each zip, so I am assuming the n are equivalent in each bottom node) Is this possible?

I have been using fable in R (basing my work on Hyndman's Australian Tourism example) to build the model. But it seems to not work with using the mean.

Here is some of my code:

zri_leveled <- zri %>%
  aggregate_key((State/CountyName/City/zip), mean_rent = mean(avg_rent))

fit <- zri_VA_leveled %>%
  filter_index(~"2018-12") %>%
  model(base = ETS(mean_rent)) %>%
  reconcile(
    bu = bottom_up(base),
    ols = min_trace(base, method = "ols"),
    mint = min_trace(base, method = "mint_shrink"),
)

The data includes:

For levels, States, County, City, Zipcode.
Dates are in months.
And average rents are integers.

I hope to include other features (some will be the means and some will be indexes) later, but first I would like to get this hierarchical model working.

I appreciate any help.

Best Answer

The following works:

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

tourism_agg <- tourism %>%
  filter(Purpose == "Holiday") %>%
  aggregate_key(State / Region, Trips = mean(Trips))

fit <- tourism_agg %>%
  model(base = ETS(Trips)) %>%
  reconcile(
    bu = bottom_up(base),
    ols = min_trace(base, method = "ols"),
    mint = min_trace(base, method = "mint_shrink"),
  )

fit %>%
  forecast() 
#> # A fable: 2,720 x 6 [1Q]
#> # Key:     State, Region, .model [340]
#>    State  Region   .model Quarter        Trips .mean
#>    <chr*> <chr*>   <chr>    <qtr>       <dist> <dbl>
#>  1 ACT    Canberra base   2018 Q1 N(215, 3148)  215.
#>  2 ACT    Canberra base   2018 Q2 N(189, 2436)  189.
#>  3 ACT    Canberra base   2018 Q3 N(192, 2527)  192.
#>  4 ACT    Canberra base   2018 Q4 N(197, 3610)  197.
#>  5 ACT    Canberra base   2019 Q1 N(215, 3908)  215.
#>  6 ACT    Canberra base   2019 Q2 N(189, 3222)  189.
#>  7 ACT    Canberra base   2019 Q3 N(192, 3370)  192.
#>  8 ACT    Canberra base   2019 Q4 N(197, 4602)  197.
#>  9 ACT    Canberra bu     2018 Q1 N(215, 3148)  215.
#> 10 ACT    Canberra bu     2018 Q2 N(189, 2436)  189.
#> # … with 2,710 more rows

Created on 2021-09-19 by the reprex package (v2.0.1)

So you will need to provide more information about what the error is, and preferably provide a reproducible example of the problem.

Related Question