Exclude Weekends in Dot in Dplyr
Asked Answered
O

1

2

This is a continuation question from this answer: https://mcmap.net/q/899232/-using-prophet-package-to-predict-by-group-in-dataframe-in-r

I am using the do function in dplyr within the prophet package. When attempting this I want to make a future dataframe with weekends excluded. Below is my current code:

Current dataframe:

dataset
          ds     group     y
  2021-12-15         A     5
  2021-12-16         A     6
  2021-12-15         B    10
  2021-12-16         B     7
         etc       etc   etc

Prediction

library(dplyr)
library(prophet)

data = dataset %>%  
group_by(group) %>%
do(predict(prophet(., daily.seasonality = TRUE, yearly.seasonality = TRUE), 
make_future_dataframe(prophet(.,daily.seasonality = TRUE, yearly.seasonality = TRUE), periods = 14))) %>%
select(ds, group, yhat)

How do I rewrite the above code to filter for the make_future_dataframe dataset not have weekends?

I want it to looks something like this, however this is not working:

data = dataset %>%  
  group_by(group) %>%
  do(predict(prophet(., daily.seasonality = TRUE, yearly.seasonality = TRUE), 
  make_future_dataframe(prophet(.[which(weekdays(.$ds) != 'Saturday' | weekdays(.$ds) != 'Sunday'),],daily.seasonality = TRUE, yearly.seasonality = TRUE), periods = 14))) %>%
  select(ds, group, yhat)
Osmose answered 20/12, 2021 at 5:0 Comment(0)
B
2

We could remove weekend days prior to predict:

df %>% 
  group_by(group) %>% 
  mutate(weekdays = weekdays(ds)) %>% 
  filter(weekdays != "Saturday" & weekdays != "Sunday") %>% 
  do(predict(prophet(., daily.seasonality = TRUE, yearly.seasonality = TRUE), 
  filter(make_future_dataframe(prophet(., daily.seasonality = TRUE, yearly.seasonality = TRUE), periods = 14), weekdays(ds) != "Saturday" & weekdays(ds) != "Sunday"))) %>%
  select(ds, group, yhat)
Beecher answered 20/12, 2021 at 5:24 Comment(4)
Thanks, for some reason the output still includes weekend data. Any idea why this would still be the case?Osmose
Is this an accurate way to use the dot in the make_future_dataframe section? make_future_dataframe(prophet(.[which(weekdays(.$ds) != 'Saturday' | weekdays(.$ds) != 'Sunday')?Osmose
following up on this as weekends are still appearing in outputOsmose
made edit to make it correctOsmose

© 2022 - 2025 — McMap. All rights reserved.