【问题标题】:Using R Prophet across multiple groups跨多个组使用 R Prophet
【发布时间】:2021-08-06 14:44:52
【问题描述】:

我之前使用 Prophet 来预测单个指标,但现在我需要使用大约 3 年的历史数据并包括多个组来预测到 2022 年。我不想为每种可能性创建 1000 多个预测,所以希望我可以在 Prophet 中做到这一点。

我已经阅读了 Stackoverflow 上的 couple of solutions,它适用于单个组,但我要预测 3 个不同的组。

下面我创建了一个示例表,其中包含显示数据格式的虚拟 Y 数据: (注意:可以并且将会有零值,但如果它会对输出产生负面影响,我可以排除它们)

ds establishment category channel y
2020-01-01 High School Stationery Direct 27
2020-01-01 High School Stationery Paid Search 31
2020-01-01 High School Arts Direct 47
2020-01-01 High School Arts Paid Search 0
2020-01-01 College Stationery Direct 60
2020-01-01 College Stationery Paid Search 35
2020-01-01 College Arts Direct 54
2020-01-01 College Arts Paid Search 15
2020-01-02 High School Stationery Direct 27
2020-01-02 High School Stationery Paid Search 31
2020-01-02 High School Arts Direct 47
2020-01-02 High School Arts Paid Search 0
2020-01-02 College Stationery Direct 60
2020-01-02 College Stationery Paid Search 35
2020-01-02 College Arts Direct 54
2020-01-02 College Arts Paid Search 15
2020-01-03 High School Stationery Direct 27
2020-01-03 High School Stationery Paid Search 31
2020-01-03 High School Arts Direct 47
2020-01-03 High School Arts Paid Search 0
2020-01-03 College Stationery Direct 60
2020-01-03 College Stationery Paid Search 35
2020-01-03 College Arts Direct 54
2020-01-03 College Arts Paid Search 15
... ... ... ... ...

以下代码适用于单个组,但我想将其扩展为包括所有组。

d1 <- df %>%
  nest(-establishment) %>%
  mutate(m = map(data, prophet)) %>%
  mutate(future = map(m, make_future_dataframe, period = 730)) %>%
  mutate(forecast = map2(m, future, predict))

d <- d1 %>%
  unnest(forecast) %>%
  select(ds, establishmentShortcut, yhat)

如果有人可以推荐解决方案,我将不胜感激。

【问题讨论】:

  • 只是想我会放弃关于使用先知的警告,正如this article 中所描述的那样。如果您已经知道这一点,我深表歉意并随时忽略!

标签: r time-series facebook-prophet prophet


【解决方案1】:

这就是 fable 包的设计目的。有一个用于处理先知模型的寓言扩展名为fable.prophet。这是一个使用与您的数据相同的结构的示例。

library(fable.prophet)
library(dplyr)
library(tibble)
library(tsibble)

df <- tibble(
    ds = rep(seq(as.Date("2020-01-01"), by="1 day", length=50), rep(8,50)),
    establishment = rep(rep(c("High School","College"), c(4,4)), 50),
    category = rep(rep(c("Stationery","Arts"), c(2,2)), 100),
    channel = rep(c("Direct","Paid Search"), 200),
    y = sample(0:100, 400, replace=TRUE)
  ) %>%
  as_tsibble(index=ds, key=c("establishment","category","channel"))

fc <- df %>%
  model(
    prophet = prophet(y),
  ) %>%
  forecast(h=10)

reprex package (v2.0.1) 于 2021-08-07 创建

您可以将prophet() 换成fable 包中的任何其他型号。详情请见https://otexts.com/fpp3/

【讨论】:

  • 这看起来很有希望 Rob,但我正在为给出的示例而苦苦挣扎。我收到错误消息:“先知(y)中的错误:找不到对象'y'。”我试过直接引用该列,但得到一个无效的“pos”参数。
  • 使用最新版本的包并在新会话中运行代码。可能你有旧包,或者你可能与其他加载的包发生冲突。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多