使用 R 中的 Prophet 对所有组进行月度预测

Make a monthly forecast for all groups with Prophet in R

如何使用 Prophet 对不同变量进行月度预测?假设我有一个包含多个区域的数据集(大约 70 个,请参阅下面的 table)并且我想对所有区域进行预测,是否可以?

Region Month Value
Region_1 2017-01 123123
Region_1 2017-02 223333
Region_1 2017-03 11133
Region_1 2017-04 882822
Region_2 2017-01 300000
Region_2 2017-02 22333
Region_2 2017-03 23232323
Region_2 2017-04 23232323

您可以使用 freq = "month" 选项与 dplyr 一起制作未来的数据帧。下面将预测接下来的2个月。

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

输出:

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