Using Prophet Package to Predict By Group in Dataframe in R

前端 未结 2 971
遇见更好的自我
遇见更好的自我 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
           <date> <fctr>    <dbl>
     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
    
    0 讨论(0)
  • 2020-12-08 06:23

    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
    
    0 讨论(0)
提交回复
热议问题