先知套餐-在R中按组将假日添加到预测中

时间:2018-09-15 12:40:29

标签: r time-series forecasting facebook-prophet

我正在阅读有关R中各小组先知奔跑的问答。 Using Prophet Package to Predict By Group in Dataframe in R

提供的答案是有用的,但不包括为先知函数添加的holidays参数。

按组运行该功能时,如何将假期data.frame传递给先知功能?

这是我的示例数据:

# time series df
ds <- as.Date(c('2016-11-01','2016-11-02','2016-11-03','2016-11-04',
        '2016-11-05','2016-11-06','2016-11-07','2016-11-08',
        '2016-11-09','2016-11-10','2016-11-11','2016-11-12',
        '2016-11-13','2016-11-14','2016-11-15','2016-11-16',
        '2016-11-17','2016-11-18','2016-11-19','2016-11-20',
        '2016-11-21','2016-11-22','2016-11-23','2016-11-24',
        '2016-11-25','2016-11-26','2016-11-27','2016-11-28',
        '2016-11-29','2016-11-30',


        '2016-11-01','2016-11-02','2016-11-03','2016-11-04',
        '2016-11-05','2016-11-06','2016-11-07','2016-11-08',
        '2016-11-09','2016-11-10','2016-11-11','2016-11-12',
        '2016-11-13','2016-11-14','2016-11-15','2016-11-16',
        '2016-11-17','2016-11-18','2016-11-19','2016-11-20',
        '2016-11-21','2016-11-22','2016-11-23','2016-11-24',
        '2016-11-25','2016-11-26','2016-11-27','2016-11-28'))


y <-c(15,17,18,19,20,54,67,23,12,34,12,78,34,12,3,45,67,89,12,111,123,112,14,566,345,123,567,56,87,90, 45,23,12,10,21,34,12,45,12,44,87,45,32,67,1,57,87,99,33,234,456,123,89,333,411,232,455,55)

y <- as.numeric(y)

group <- c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
 "A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
 "B","B","B","B","B","B","B","B","B","B","B","B","B","B","B",
 "B","B","B","B","B","B","B","B","B","B","B","B","B")

df <- data.frame(ds,group, y)
df

        ds      group y
1  2016-11-01     A  15
2  2016-11-02     A  17
3  2016-11-03     A  18
4  2016-11-04     A  19
5  2016-11-05     A  20
6  2016-11-06     A  54
7  2016-11-07     A  67
8  2016-11-08     A  23
9  2016-11-09     A  12
10 2016-11-10     A  34
11 2016-11-11     A  12
12 2016-11-12     A  78
13 2016-11-13     A  34
14 2016-11-14     A  12
15 2016-11-15     A   3
16 2016-11-16     A  45
17 2016-11-17     A  67
18 2016-11-18     A  89
19 2016-11-19     A  12
20 2016-11-20     A 111
21 2016-11-21     A 123
22 2016-11-22     A 112
23 2016-11-23     A  14
24 2016-11-24     A 566
25 2016-11-25     A 345
26 2016-11-26     A 123
27 2016-11-27     A 567
28 2016-11-28     A  56
29 2016-11-29     A  87
30 2016-11-30     A  90
31 2016-11-01     B  45
32 2016-11-02     B  23
33 2016-11-03     B  12
34 2016-11-04     B  10
35 2016-11-05     B  21
36 2016-11-06     B  34
37 2016-11-07     B  12
38 2016-11-08     B  45
39 2016-11-09     B  12
40 2016-11-10     B  44
41 2016-11-11     B  87
42 2016-11-12     B  45
43 2016-11-13     B  32
44 2016-11-14     B  67
45 2016-11-15     B   1
46 2016-11-16     B  57
47 2016-11-17     B  87
48 2016-11-18     B  99
49 2016-11-19     B  33
50 2016-11-20     B 234
51 2016-11-21     B 456
52 2016-11-22     B 123
53 2016-11-23     B  89
54 2016-11-24     B 333
55 2016-11-25     B 411
56 2016-11-26     B 232
57 2016-11-27     B 455
58 2016-11-28     B  55

# holidays df
ds <- as.Date(c('2016-11-10','2016-11-23','2016-11-19','2016-11-28'))

group <- c("A","A","B","B")

holiday <- c('holiday_a', 'holiday_b','holiday_c', 'holiday_d')

holidays <- data.frame(ds,group, holiday)

holidays 

      ds      group  holiday
1 2016-11-10     A holiday_a
2 2016-11-23     A holiday_b
3 2016-11-19     B holiday_c
4 2016-11-28     B holiday_d

在将其传递给先知函数时,我尝试按假日data.frame参数进行分组,但输出不正确。所有假期a,b,b,d均由假期数据组中的组独立传递给每个组;这是不正确的。

df %>%  
   group_by(group) %>%
   do(predict(prophet(., holidays = group_by(holidays, group)), 
   make_future_dataframe(prophet(.), periods = 7))) %>%
   select(ds, group, yhat,holiday_a,holiday_b,holiday_c,holiday_d)  %>%
   as.data.frame()  

        ds       group  yhat     holiday_a holiday_b holiday_c  holiday_d
1  2016-11-01     A -94.6419164   0.00000   0.00000   0.00000    0.00000
2  2016-11-02     A -96.5462126   0.00000   0.00000   0.00000    0.00000
3  2016-11-03     A  90.9201486   0.00000   0.00000   0.00000    0.00000
4  2016-11-04     A  11.6291775   0.00000   0.00000   0.00000    0.00000
5  2016-11-05     A -39.6195794   0.00000   0.00000   0.00000    0.00000
6  2016-11-06     A 101.3672497   0.00000   0.00000   0.00000    0.00000
7  2016-11-07     A -27.1164855   0.00000   0.00000   0.00000    0.00000
8  2016-11-08     A -26.1950639   0.00000   0.00000   0.00000    0.00000
9  2016-11-09     A -28.0996817   0.00000   0.00000   0.00000    0.00000
10 2016-11-10     A  72.4524666 -86.91389   0.00000   0.00000    0.00000
11 2016-11-11     A  80.0750655   0.00000   0.00000   0.00000    0.00000
12 2016-11-12     A  28.8259872   0.00000   0.00000   0.00000    0.00000
13 2016-11-13     A 169.8124950   0.00000   0.00000   0.00000    0.00000
14 2016-11-14     A  41.3284385   0.00000   0.00000   0.00000    0.00000
15 2016-11-15     A  42.2498601   0.00000   0.00000   0.00000    0.00000
16 2016-11-16     A  40.3452425   0.00000   0.00000   0.00000    0.00000
17 2016-11-17     A 227.8112824   0.00000   0.00000   0.00000    0.00000
18 2016-11-18     A 148.5199899   0.00000   0.00000   0.00000    0.00000
19 2016-11-19     A  45.7585824   0.00000   0.00000 -51.51233    0.00000
20 2016-11-20     A 238.2574195   0.00000   0.00000   0.00000    0.00000
21 2016-11-21     A 109.7733629   0.00000   0.00000   0.00000    0.00000
22 2016-11-22     A 110.6947844   0.00000   0.00000   0.00000    0.00000
23 2016-11-23     A  62.5667545   0.00000 -46.22341   0.00000    0.00000
24 2016-11-24     A 296.2562062   0.00000   0.00000   0.00000    0.00000
25 2016-11-25     A 216.9649135   0.00000   0.00000   0.00000    0.00000
26 2016-11-26     A 165.7158351   0.00000   0.00000   0.00000    0.00000
27 2016-11-27     A 306.7023427   0.00000   0.00000   0.00000    0.00000
28 2016-11-28     A 126.2788594   0.00000   0.00000   0.00000  -51.93943
29 2016-11-29     A 179.1397075   0.00000   0.00000   0.00000    0.00000
30 2016-11-30     A 177.2350897   0.00000   0.00000   0.00000    0.00000
31 2016-12-01     A 364.7011292   0.00000   0.00000   0.00000    0.00000
32 2016-12-02     A 285.4098366   0.00000   0.00000   0.00000    0.00000
33 2016-12-03     A 234.1607582   0.00000   0.00000   0.00000    0.00000
34 2016-12-04     A 375.1472658   0.00000   0.00000   0.00000    0.00000
35 2016-12-05     A 246.6632091   0.00000   0.00000   0.00000    0.00000
36 2016-12-06     A 247.5846306   0.00000   0.00000   0.00000    0.00000
37 2016-12-07     A 245.6800127   0.00000   0.00000   0.00000    0.00000
38 2016-11-01     B -71.4343718   0.00000   0.00000   0.00000    0.00000
39 2016-11-02     B -77.8417828   0.00000   0.00000   0.00000    0.00000
40 2016-11-03     B   8.7292616   0.00000   0.00000   0.00000    0.00000
41 2016-11-04     B  33.1001674   0.00000   0.00000   0.00000    0.00000
42 2016-11-05     B -27.3674191   0.00000   0.00000   0.00000    0.00000
43 2016-11-06     B  72.5228028   0.00000   0.00000   0.00000    0.00000
44 2016-11-07     B  53.5127980   0.00000   0.00000   0.00000    0.00000
45 2016-11-08     B   6.8424459   0.00000   0.00000   0.00000    0.00000
46 2016-11-09     B   0.4350352   0.00000   0.00000   0.00000    0.00000
47 2016-11-10     B  43.3701494 -43.63593   0.00000   0.00000    0.00000
48 2016-11-11     B 111.3769861   0.00000   0.00000   0.00000    0.00000
49 2016-11-12     B  50.9093997   0.00000   0.00000   0.00000    0.00000
50 2016-11-13     B 150.7996217   0.00000   0.00000   0.00000    0.00000
51 2016-11-14     B 131.7896172   0.00000   0.00000   0.00000    0.00000
52 2016-11-15     B  85.1848200   0.00000   0.00000   0.00000    0.00000
53 2016-11-16     B  78.8429644   0.00000   0.00000   0.00000    0.00000
54 2016-11-17     B 165.4795640   0.00000   0.00000   0.00000    0.00000
55 2016-11-18     B 189.9160251   0.00000   0.00000   0.00000    0.00000
56 2016-11-19     B  75.9048249   0.00000   0.00000 -53.60917    0.00000
57 2016-11-20     B 229.4697708   0.00000   0.00000   0.00000    0.00000
58 2016-11-21     B 210.5253213   0.00000   0.00000   0.00000    0.00000
59 2016-11-22     B 163.9205243   0.00000   0.00000   0.00000    0.00000
60 2016-11-23     B 146.5432931   0.00000 -11.03538   0.00000    0.00000
61 2016-11-24     B 244.2152686   0.00000   0.00000   0.00000    0.00000
62 2016-11-25     B 268.6517298   0.00000   0.00000   0.00000    0.00000
63 2016-11-26     B 208.2496985   0.00000   0.00000   0.00000    0.00000
64 2016-11-27     B 308.2054755   0.00000   0.00000   0.00000    0.00000
65 2016-11-28     B 178.5735017   0.00000   0.00000   0.00000 -110.68752
66 2016-11-29     B 242.6562289   0.00000   0.00000   0.00000    0.00000
67 2016-11-30     B 236.3143734   0.00000   0.00000   0.00000    0.00000
68 2016-12-01     B 322.9509732   0.00000   0.00000   0.00000    0.00000
69 2016-12-02     B 347.3874344   0.00000   0.00000   0.00000    0.00000
70 2016-12-03     B 286.9854031   0.00000   0.00000   0.00000    0.00000
71 2016-12-04     B 386.9411801   0.00000   0.00000   0.00000    0.00000
72 2016-12-05     B 367.9967306   0.00000   0.00000   0.00000    0.00000

如何将假期data.frame传递给按组运行的先知函数,以预测每个带有相应假期的组?

1 个答案:

答案 0 :(得分:1)

我找到了答案,使用map2 purrr函数预测了每个带有相应假期的人群

df_nested <- df %>%  
group_by(group) %>%
nest()    

holidays_nested <- holidays %>%  
group_by(group) %>%
nest() %>%
rename(holidays = data)    

df_input <- df_nested %>% left_join(holidays_nested)     

df_input %>%
   mutate(forecast = map2(data, holidays, ~predict(prophet(df = .x, holidays = .y), make_future_dataframe(prophet(df = .x, holidays = .y), periods = 7)))) %>%
   unnest(forecast) %>%
   select(ds, group, yhat,holiday_a,holiday_b,holiday_c,holiday_d)  %>%
   as.data.frame()

感谢那些调查问题的人。