You need to enable JavaScript to run this app. R语言Prophet实现多维度分组批量时间序列预测方案咨询
优惠活动
大模型
产品
解决方案
定价
更多

R语言Prophet实现多维度分组批量时间序列预测方案咨询

多维度分组Prophet批量预测修改方案

前置处理(可选)

你可以先过滤y为0的记录,避免零值对模型效果产生负面影响:

df_clean <- df %>% 
  filter(y != 0)

核心代码修改

仅需要调整nest的分组参数,将三个业务维度都设为分组键,每个唯一的(establishment, category, channel)组合会自动生成独立数据子集、对应训练一个Prophet模型:

兼容新旧版tidyr写法

d1 <- df_clean %>%
  nest(-establishment, -category, -channel) %>%
  mutate(m = map(data, prophet)) %>%
  # 按需调整period参数,预测2022年全年设为365即可
  mutate(future = map(m, make_future_dataframe, period = 365)) %>%
  mutate(forecast = map2(m, future, predict))

d <- d1 %>%
  unnest(forecast) %>%
  # 按需保留预测字段,可追加yhat_lower、yhat_upper等置信区间字段
  select(ds, establishment, category, channel, yhat)

新版tidyr(>=1.0.0)推荐标准写法

d1 <- df_clean %>%
  nest(data = -c(establishment, category, channel)) %>%
  mutate(m = map(data, prophet)) %>%
  mutate(future = map(m, make_future_dataframe, period = 365)) %>%
  mutate(forecast = map2(m, future, predict))

d <- d1 %>%
  unnest(forecast) %>%
  select(ds, establishment, category, channel, yhat)

效率优化建议

针对1000+模型的训练场景,可使用furrr包替换purrr的map函数实现并行训练,大幅降低运行耗时:

# 加载并行包
library(furrr)
# 开启多线程,预留1核给系统
plan(multisession, workers = availableCores() - 1)

d1 <- df_clean %>%
  nest(data = -c(establishment, category, channel)) %>%
  mutate(m = future_map(data, prophet)) %>%
  mutate(future = future_map(m, make_future_dataframe, period = 365)) %>%
  mutate(forecast = future_map2(m, future, predict))

内容的提问来源于stack exchange,提问作者Ed Cunningham

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 00:57:02