You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于tidyverse的分组自上而下预测:子组预测总和需匹配总量

Hierarchical Forecasting Consistency (Top-Down Method) in R

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:34:32