在 R 中使用 Prophet 进行预测

Forecasting using Prophet in R

我有这个包含 78 个不同位置的数据集,我必须在其中预测未来 3 个月单个产品的价格。数据有大约 6 个月(4 月至 10 月)的每日条目。但是,当我 运行 模型时,它给了我一个错误。请帮我解决这个错误。

或者有没有其他方法可以预测数据? 我的代码:

Area_forecast <- data1 %>%
filter(product == "garri") %>%
select( level2code, ds, y = pred) %>%
mutate(ds = paste(ds, "01", "01", sep = "-") %>% as.Date()) %>%
group_by(level2code) %>%
do(model = prophet(.),
Predicted_values = make_future_dataframe(model,92),
results = predict(model, Predicted_values))

错误:

Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
Disabling daily seasonality. Run prophet 
with daily.seasonality=TRUE to override this.
Error: object of type 'closure' is not subsettable

Sample data set: Data is divided into 78 areas with daily values.

ds  product level2code  pred
2021-04-01  garri   NG020001    366.1540473
2021-04-01  garri   NG020002    393.3557317
2021-04-01  garri   NG020003    374.3132972
2021-04-01  garri   NG021001    353.1266783
2021-04-01  garri   NG021002    329.9673004
2021-04-01  garri   NG021003    391.1370065
2021-04-01  garri   NG021004    306.1356683
2021-04-01  garri   NG020004    372.8115048
2021-04-01  garri   NG020005    350.0100309
2021-04-01  garri   NG021005    373.0025842
2021-04-01  garri   NG020006    383.1839042
2021-04-01  garri   NG021006    383.5212653
2021-04-01  garri   NG020007    486.8226379
2021-04-01  garri   NG020008    334.8151487
2021-04-01  garri   NG021007    379.89125
2021-04-01  garri   NG021008    370.9376296
2021-04-01  garri   NG021009    389.4640031
2021-04-01  garri   NG021010    365.9266992
2021-04-01  garri   NG020009    380.4655716
2021-04-01  garri   NG020010    352.6139069
2021-04-01  garri   NG020011    243.5701952
2021-04-01  garri   NG021011    366.6883205
2021-04-01  garri   NG021012    326.9402671
2021-04-01  garri   NG020012    403.5666062
2021-04-01  garri   NG021013    405.2860516
2021-04-01  garri   NG021014    348.023662
2021-04-01  garri   NG020013    372.7586167
2021-04-01  garri   NG020014    392.5432373
2021-04-01  garri   NG020015    362.1098193
2021-04-01  garri   NG020016    343.9205353
2021-04-01  garri   NG020017    366.5290601
2021-04-01  garri   NG020018    431.0832709
2021-04-01  garri   NG020019    411.3581413
2021-04-01  garri   NG021015    348.3274897
2021-04-01  garri   NG021016    336.3743799
2021-04-01  garri   NG020020    393.8901327
2021-04-01  garri   NG021017    391.7393648
2021-04-01  garri   NG021018    346.0988762
2021-04-01  garri   NG021019    346.1310667
2021-04-01  garri   NG021020    359.2837766
2021-04-01  garri   NG020021    450.4413674
2021-04-01  garri   NG020022    373.6406838
2021-04-01  garri   NG021021    420.0838824
2021-04-01  garri   NG020023    408.6912783
2021-04-01  garri   NG020024    285.8249281
2021-04-01  garri   NG020025    388.3307999
2021-04-01  garri   NG020026    365.1686898
2021-04-01  garri   NG020027    391.6867595
2021-04-01  garri   NG021022    384.1850593
2021-04-01  garri   NG021023    397.7172044
2021-04-01  garri   NG020028    331.2182171
2021-04-01  garri   NG021024    393.323333
2021-04-01  garri   NG020029    405.5813127
2021-04-01  garri   NG021025    359.658715
2021-04-01  garri   NG021026    370.3166839
2021-04-01  garri   NG021027    391.088788
2021-04-01  garri   NG021028    364.2770249
2021-04-01  garri   NG020030    375.2600611
2021-04-01  garri   NG021029    358.9618615
2021-04-01  garri   NG020031    439.1322423
2021-04-01  garri   NG020032    402.7340519
2021-04-01  garri   NG021030    345.2253201
2021-04-01  garri   NG020033    356.2798309
2021-04-01  garri   NG020034    369.3260162
2021-04-01  garri   NG021031    368.6820545
2021-04-01  garri   NG021032    342.8985896
2021-04-01  garri   NG021033    388.3937476
2021-04-01  garri   NG020035    369.5370586
2021-04-01  garri   NG020036    374.2149029
2021-04-01  garri   NG020037    374.3105323
2021-04-01  garri   NG020038    419.9881972
2021-04-01  garri   NG020039    356.6891081
2021-04-01  garri   NG020040    385.9381759
2021-04-01  garri   NG020041    308.1801669
2021-04-01  garri   NG020042    383.1544687
2021-04-01  garri   NG020043    381.4891835
2021-04-01  garri   NG020044    362.6149694
2021-04-01  garri   NG021034    351.0315915
2021-04-02  garri   NG020001    347.5080909
2021-04-02  garri   NG020002    417.7950263
2021-04-02  garri   NG020003    357.6293544
2021-04-02  garri   NG021001    364.0809209
2021-04-02  garri   NG021002    350.5885553
2021-04-02  garri   NG021003    412.9410955
2021-04-02  garri   NG021004    326.3732426
2021-04-02  garri   NG020004    374.9451842
2021-04-02  garri   NG020005    335.0886535
2021-04-02  garri   NG021005    365.002555
2021-04-02  garri   NG020006    392.6334097
2021-04-02  garri   NG021006    379.2592638
2021-04-02  garri   NG020007    438.0161714
2021-04-02  garri   NG020008    361.915949
2021-04-02  garri   NG021007    357.060549
2021-04-02  garri   NG021008    382.9647718
2021-04-02  garri   NG021009    389.1019382
2021-04-02  garri   NG021010    333.3285699
2021-04-02  garri   NG020009    406.0860302
2021-04-02  garri   NG020010    342.7835896
2021-04-02  garri   NG020011    243.602651
2021-04-02  garri   NG021011    358.4753587
2021-04-02  garri   NG021012    333.9547987
2021-04-02  garri   NG020012    424.8915992
2021-04-02  garri   NG021013    407.2047254
2021-04-02  garri   NG021014    356.6045034
2021-04-02  garri   NG020013    358.297996
2021-04-02  garri   NG020014    432.0024885
2021-04-02  garri   NG020015    363.1590239
2021-04-02  garri   NG020016    334.1112816
2021-04-02  garri   NG020017    376.0309518
2021-04-02  garri   NG020018    426.4144669
2021-04-02  garri   NG020019    400.1660285
2021-04-02  garri   NG021015    337.3337231
2021-04-02  garri   NG021016    341.8770212
2021-04-02  garri   NG020020    391.1870559
2021-04-02  garri   NG021017    397.611482
2021-04-02  garri   NG021018    345.1756482
2021-04-02  garri   NG021019    346.5591129
2021-04-02  garri   NG021020    347.5115345
2021-04-02  garri   NG020021    435.024338
2021-04-02  garri   NG020022    382.7996258
2021-04-02  garri   NG021021    388.406993
2021-04-02  garri   NG020023    417.0865963
2021-04-02  garri   NG020024    293.7273414
2021-04-02  garri   NG020025    408.075575
2021-04-02  garri   NG020026    360.3596338
2021-04-02  garri   NG020027    398.2523865
2021-04-02  garri   NG021022    395.1357617
2021-04-02  garri   NG021023    387.7742073
2021-04-02  garri   NG020028    341.8796137
2021-04-02  garri   NG021024    383.3824892
2021-04-02  garri   NG020029    415.2975719
2021-04-02  garri   NG021025    367.5180956
2021-04-02  garri   NG021026    373.8995221
2021-04-02  garri   NG021027    390.6734957
2021-04-02  garri   NG021028    348.9460594
2021-04-02  garri   NG020030    397.2809552
2021-04-02  garri   NG021029    352.6791518
2021-04-02  garri   NG020031    431.4367737
2021-04-02  garri   NG020032    394.5424715
2021-04-02  garri   NG021030    388.4691285
2021-04-02  garri   NG020033    350.0768082
2021-04-02  garri   NG020034    370.2760778
2021-04-02  garri   NG021031    365.5463449
2021-04-02  garri   NG021032    353.6569909
2021-04-02  garri   NG021033    347.2475542
2021-04-02  garri   NG020035    371.0247191
2021-04-02  garri   NG020036    380.1494315
2021-04-02  garri   NG020037    395.302709
2021-04-02  garri   NG020038    433.3253765
2021-04-02  garri   NG020039    350.8312732
2021-04-02  garri   NG020040    382.1718306
2021-04-02  garri   NG020041    316.6798756
2021-04-02  garri   NG020042    409.7501652
2021-04-02  garri   NG020043    385.8142597
2021-04-02  garri   NG020044    379.9411943
2021-04-02  garri   NG021034    345.6267946

而不是 do,使用 nest_by 并确保突变的结果被捕获在列表中。然后你可以一次搞定所有事情。

这基于您的示例数据。如果您有大量数据和 2 级代码,请去喝杯咖啡或吃午餐,因为这需要一段时间。只是这个例子在我的机器上花了几分钟。

Area_forecast <- data1 %>%
  filter(product == "garri") %>%
  select( level2code, ds, y = pred) %>%
  mutate(ds = paste(ds, "01", "01", sep = "-") %>% as.Date()) %>%
  nest_by(level2code) %>% 
  mutate(mod = list(prophet(data)),
         Predicted_values = list(make_future_dataframe(mod,92)),
         results = list(predict(mod, Predicted_values)))

Area_forecast 

# A tibble: 78 x 5
# Rowwise:  level2code
   level2code               data mod            Predicted_values results       
   <chr>      <list<tibble[,2]>> <list>         <list>           <list>        
 1 NG020001              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
 2 NG020002              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
 3 NG020003              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
 4 NG020004              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
 5 NG020005              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
 6 NG020006              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
 7 NG020007              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
 8 NG020008              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
 9 NG020009              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
10 NG020010              [2 x 2] <prophet [31]> <df [12 x 1]>    <df [12 x 16]>
# ... with 68 more rows

如果您想查看 level2code 的个别结果,您可以使用:

Area_forecast %>% 
  select(level2code, results) %>% 
  filter(level2code == "NG020001") %>% 
  unnest(results)

# A tibble: 12 x 17
# Groups:   level2code [1]
   level2code ds                  trend zeros zeros_lower zeros_upper additive_terms additive_terms_lower
   <chr>      <dttm>              <dbl> <dbl>       <dbl>       <dbl>          <dbl>                <dbl>
 1 NG020001   2021-04-01 00:00:00  366.     0           0           0              0                    0
 2 NG020001   2021-04-02 00:00:00  348.     0           0           0              0                    0
 3 NG020001   2021-04-03 00:00:00  329.     0           0           0              0                    0
 4 NG020001   2021-04-04 00:00:00  310.     0           0           0              0                    0
 5 NG020001   2021-04-05 00:00:00  292.     0           0           0              0                    0
 6 NG020001   2021-04-06 00:00:00  273.     0           0           0              0                    0
 7 NG020001   2021-04-07 00:00:00  254.     0           0           0              0                    0
 8 NG020001   2021-04-08 00:00:00  236.     0           0           0              0                    0
 9 NG020001   2021-04-09 00:00:00  217.     0           0           0              0                    0
10 NG020001   2021-04-10 00:00:00  198.     0           0           0              0                    0
11 NG020001   2021-04-11 00:00:00  180.     0           0           0              0                    0
12 NG020001   2021-04-12 00:00:00  161.     0           0           0              0                    0
# ... with 9 more variables: additive_terms_upper <dbl>, multiplicative_terms <dbl>,
#   multiplicative_terms_lower <dbl>, multiplicative_terms_upper <dbl>, yhat_lower <dbl>, yhat_upper <dbl>,
#   trend_lower <dbl>, trend_upper <dbl>, yhat <dbl>