fable
fable copied to clipboard
[BUG] Top down `average_proportions` method fails
I'm running the following code,
library(fable)
library(tsibble)
library(lubridate)
library(dplyr)
library(readr)
library(tidyr)
# Aggregate data as required
agg_tourism <- tourism %>%
aggregate_key(State / Region, Trips = sum(Trips))
# Select training data
train_df <- agg_tourism %>%
filter(year(Quarter) <= 2014)
# Select test data
test_df <- agg_tourism %>%
filter(year(Quarter) > 2014)
naive_fit <- train_df %>%
model(naive_model = NAIVE(Trips)) %>%
reconcile(naive_agv_prop = top_down(naive_model, method='average_proportions'))
# forecasting on test data
fcasts_test <- forecast(naive_fit, test_df)
It gives me the following error,
The error arises due to this line of the fabletools package,
https://github.com/tidyverts/fabletools/blob/9794d225a37e79aaab18655692eca54bb3e1aed7/R/reconciliation.R#L248
In some cases, there are matrix S rows with only one 1 that are not part of the bottom series. In this case, since the ACT state has only one region (Canberra), its total aggregation contains only one 1.
The fix is simple: btm <- agg_data$leaf instead of btm <- which(rowSums(S) == 1L).
I'm opening a PR in the fabletools package.