I am using fable package to forecast for hierarchical time series and depth of all nodes is not equal.
Use case is, forecasting contacts at country -> state -> district level. Forecast values have to add up to country level when aggregated (lower-level forecasts equate to the upperlevel forecasts.)
https://robjhyndman.com/papers/Foresight-hts-final.pdf
Given below is the code i tried and while forecasting on test data.
library(fable)
library(tsibble)
library(tsibbledata)
library(lubridate)
library(dplyr)
# selecting train data
train_df <- tourism %>%
filter(year(Quarter) <= 2014 & Region %in% c("MacDonnell", "Melbourne"))
# selecting test data
test_df <- tourism %>%
filter(year(Quarter) > 2014 & Region %in% c("MacDonnell", "Melbourne"))
# fitting ets model with reconcilliation
ets_fit <- train_df %>%
aggregate_key(Purpose * (State / Region), Trips = sum(Trips)) %>%
model(ets=ETS(Trips)) %>%
reconcile(ets_adjusted = min_trace(ets))
# forecasting on test data
fcasts_test <- forecast(ets_fit, test_df)
Getting error as
Error: Provided data contains a different key structure to the models.
Run `rlang::last_error()` to see where the error occurred.
How can I solve this?