1

I've used Prophet previously to forecast individual metrics, but I now need to forecast into 2022, using around 3 years of historic data and including multiple groups. I'd rather not create 1000+ forecasts for each possibility, so hoping I can do this within Prophet.

I've read through a couple of solutions on Stackoverflow which work for a singular group, but I have 3 different groups to forecast.

Below I've created a sample table, with dummy Y data showing how the data is formatted: (Note: There can and will be zero values, but I can exclude them if it'd negatively affect the outputs)

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
... ... ... ... ...

The following code works for a single group, but I'd like to expand this to include all groups.

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)

If anybody can recommend a solution, I'd appreciate it.

Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85
Ed Cunningham
  • 179
  • 1
  • 3
  • 17
  • Just thought I'd drop a word of caution about using prophet, as described nicely in [this article](https://medium.com/geekculture/is-facebooks-prophet-the-time-series-messiah-or-just-a-very-naughty-boy-8b71b136bc8c). If you're aware of this already, I apologize and feel free to ignore! – Daniel Molitor Aug 06 '21 at 15:16

1 Answers1

1

This is what the fable package is designed to do. There is a fable extension for handling prophet models called fable.prophet. Here is an example using the same structure as your data.

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)

Created on 2021-08-07 by the reprex package (v2.0.1)

You can swap out prophet() for any of the other models in the fable package. See https://otexts.com/fpp3/ for details.

Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85
  • This looks very promising Rob, but I'm struggling with the example given. I'm getting the error: "Error in prophet(y) : object 'y' not found." I've tried directly referencing the column, but get an invalid 'pos' argument. – Ed Cunningham Aug 09 '21 at 13:25
  • Use the most recent versions of packages and run the code in a new session. Possibly you have old packages, or perhaps you have clashes with other loaded packages. – Rob Hyndman Aug 09 '21 at 23:13