Using Prophet Package to Predict by Group in Dataframe in R

Using Prophet Package to Predict By Group in Dataframe in R

Here is a solution using tidyr::nest to nest the data by group, fit the models in those groups using purrr::map and then retrieving the y-hat as requested.
I took your code, but incorporated it into mutate calls that would compute new colums using purrr::map.

library(prophet)
library(dplyr)
library(purrr)
library(tidyr)

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

Here is the output at this point:

d1
# A tibble: 2 × 5
group data m future
<fctr> <list> <list> <list>
1 A <tibble [30 × 2]> <S3: list> <data.frame [36 × 1]>
2 B <tibble [30 × 2]> <S3: list> <data.frame [36 × 1]>
# ... with 1 more variables: forecast <list>

Then I use unnest() to retrieve the data from the forecast column and select the y-hat value as requested.

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

And here is the output for the newly forecasted values:

d %>% group_by(group) %>% 
top_n(7, ds)
Source: local data frame [14 x 3]
Groups: group [2]

ds group yhat
<date> <fctr> <dbl>
1 2016-11-30 A 180.53422
2 2016-12-01 A 349.30277
3 2016-12-02 A 288.68215
4 2016-12-03 A 222.33501
5 2016-12-04 A 342.96654
6 2016-12-05 A 203.64625
7 2016-12-06 A 185.37395
8 2016-11-30 B 131.07827
9 2016-12-01 B 222.83703
10 2016-12-02 B 236.33555
11 2016-12-03 B 145.41001
12 2016-12-04 B 228.59687
13 2016-12-05 B 162.49244
14 2016-12-06 B 68.44477

Using Prophet package to forecast by groups and create plot

I couldn't find a way to pipe the call to grid.arrange, but you can use do.call to avoid manually selecting plots.

do.call(gridExtra::grid.arrange, d1$p)

Example:

library(dplyr)
library(prophet)
library(tidyr)
library(purrr)

df <- data_frame(ds = seq(as.Date("2017/01/01"), as.Date("2019/01/01"), "month"),
a = rnorm(n = 25, mean = 100000, sd = 7500),
b = rnorm(n = 25, mean = 100000, sd = 7500),
c = rnorm(n = 25, mean = 100000, sd = 7500))

d1 <- df %>%
gather(key = "prod", value = "y", a:c) %>%
nest(-prod) %>%
mutate(m = map(data, prophet)) %>%
mutate(future = map(m, make_future_dataframe, period = 12, freq = "month")) %>%
mutate(forecast = map2(m, future, predict)) %>%
mutate(p = map2(m, forecast, plot))

Make a monthly forecast for all groups with Prophet in R

You can use the freq = "month" option in making future dataframe along with dplyr. Below will predict the following 2 months.

Region <- c('Region_1','Region_1','Region_1','Region_1','Region_2','Region_2','Region_2','Region_2')
Value <- c(123123, 223333, 11133, 882822,300000,22333,23232323,23232323)
Month <- as.Date(c('2017-01-01','2017-02-01','2017-03-01','2017-04-01',
'2017-01-01','2017-02-01','2017-03-01','2017-04-01'))

df <- data.frame(Month, Region, Value)
names(df)[1] <- 'ds'
names(df)[3] <- 'y'

install.packages("prophet")
library(prophet)
library(dplyr)

new_df = df %>%
group_by(Region) %>%
do(predict(prophet(., yearly.seasonality = TRUE),
make_future_dataframe(prophet(., yearly.seasonality = TRUE), freq = "month", periods = 2))) %>%
select(ds, Region, yhat)

new_df

Output:

# A tibble: 12 x 3
# Groups: Region [2]
ds Region yhat
<dttm> <chr> <dbl>
1 2017-01-01 00:00:00 Region_1 123123.
2 2017-02-01 00:00:00 Region_1 223333
3 2017-03-01 00:00:00 Region_1 11133.
4 2017-04-01 00:00:00 Region_1 882822.
5 2017-05-01 00:00:00 Region_1 -110249.
6 2017-06-01 00:00:00 Region_1 11870506.
7 2017-01-01 00:00:00 Region_2 300000.
8 2017-02-01 00:00:00 Region_2 22333.
9 2017-03-01 00:00:00 Region_2 23232323.
10 2017-04-01 00:00:00 Region_2 23232323.
11 2017-05-01 00:00:00 Region_2 -486859040.
12 2017-06-01 00:00:00 Region_2 218123552.


Related Topics



Leave a reply



Submit