基于tidyverse的分组自上而下预测:子组预测总和需匹配总量
Great question! I’ve worked with Matt Dancho’s sweep vignettes and hierarchical time series forecasting workflows in R, so let me break this down clearly for you.
Core Check for Top-Down Alignment
First off: the standard workflow in Matt’s sweep vignettes doesn’t automatically enforce top-down forecast consistency out of the box. If you’re using bike_sales_monthly (total-level forecasts) and monthly_qty_by_cat2 (subgroup forecasts), you’ll need to add an explicit forecast reconciliation step to ensure the sum of subgroup total.qty values matches the total-level total.qty for each year-month combination.
In practice, that means:
- Generating your total-level and subgroup forecasts first (using sweep/forecast or another tool)
- Grouping the subgroup forecasts by year and month, summing their
total.qty - Comparing that sum to the corresponding total-level forecast value
- Adjusting the subgroup forecasts if there’s a mismatch (this is the reconciliation part)
The hts Package Limitations You Mentioned
You’re absolutely right about the tradeoffs with the hts package:
- It does natively support top-down hierarchical forecasting and reconciliation
- But its output structures are clunky and don’t play nicely with tidyverse tools like dplyr, ggplot2, etc.
- Worst of all, it doesn’t provide forecast confidence intervals— a big gap if you need to communicate uncertainty around your predictions
A Tidyverse-Friendly Alternative
If you want to keep things in the tidyverse and retain confidence intervals, the fabletools package (part of the tidyverts ecosystem) is a far better fit. It’s built for tidy hierarchical forecasting, with built-in reconciliation methods including top-down alignment.
Here’s a quick example of how you’d implement this:
library(fabletools) library(dplyr) # Assume you already have: # - total_fc: tidy forecast tibble for bike_sales_monthly (total level) # - sub_fc: tidy forecast tibble for monthly_qty_by_cat2 (subgroup level) # Define the hierarchical relationship between your subgroups and total hierarchy <- hierarchy_parse(cat2 ~ ., data = sub_fc) # Apply top-down reconciliation to align subgroup forecasts with total reconciled_sub_fc <- reconcile(sub_fc, total_fc, method = top_down(hierarchy)) # Verify the alignment works reconciled_sub_fc %>% group_by(year, month) %>% summarise(subgroup_total = sum(total.qty)) %>% left_join(total_fc %>% select(year, month, total.qty), by = c("year", "month")) %>% mutate(difference = subgroup_total - total.qty)
After running this, the difference column should be zero (or negligible due to floating point) for every year-month pair.
Final Takeaway
To circle back to your original question: Matt’s sweep vignette doesn’t handle this reconciliation automatically, but you can extend it with a step like the one above. And if you want to avoid the pain points of hts, fabletools is the way to go for tidy, confidence-interval-friendly hierarchical forecasting.
内容的提问来源于stack exchange,提问作者Kasia Kulma

