Using Prophet Package to Predict By Group in Dataframe in R

前端 未结 2 983
遇见更好的自我
遇见更好的自我 2020-12-08 05:35

I am using the new package released from Facebook called Prophet. It does time series predictions and I want to apply this function By Group.

Scroll down to R Secti

2条回答
  •  情书的邮戳
    2020-12-08 06:09

    I was looking for a solution for the same problem. I came up with the following code, which is a bit simpler than the accepted answer.

    library(tidyr)
    library(dplyr)
    library(prophet)
    
    data = df %>%  
           group_by(group) %>%
           do(predict(prophet(.), make_future_dataframe(prophet(.), periods = 7))) %>%
           select(ds, group, yhat)
    

    And here are the predicted values

    data %>% group_by(group) %>% 
             top_n(7, ds)
    
    # A tibble: 14 x 3
    # Groups:   group [2]
               ds  group     yhat
                
     1 2016-12-01      A 316.9709
     2 2016-12-02      A 258.2153
     3 2016-12-03      A 196.6835
     4 2016-12-04      A 346.2338
     5 2016-12-05      A 208.9083
     6 2016-12-06      A 216.5847
     7 2016-12-07      A 206.3642
     8 2016-12-01      B 230.0424
     9 2016-12-02      B 268.5359
    10 2016-12-03      B 190.2903
    11 2016-12-04      B 312.9019
    12 2016-12-05      B 266.5584
    13 2016-12-06      B 189.3556
    14 2016-12-07      B 168.9791
    

提交回复
热议问题