Introduction to Prophet in R

Nan Hanagud

6/29/2017

Agenda

Prophet

Capabilities

The Model

y(t) = g(t) + s(t) + h(t) + ε

g(t): growth function : Logistic growth model with non-constant carrying capacity with changepoints or linear growth model with changepoints. Changepoints are used to model changing rates of growth.

s(t): seasonality (weekly or yearly)

h(t): holidays

ε: error

Data for this demo

Data Format

##        ds                   y        
##  Min.   :2015-01-01   Min.   :    0  
##  1st Qu.:2015-08-07   1st Qu.:    2  
##  Median :2016-03-13   Median : 9839  
##  Mean   :2016-03-13   Mean   : 7305  
##  3rd Qu.:2016-10-18   3rd Qu.:10925  
##  Max.   :2017-05-25   Max.   :14694

Data Graph

Training Set

Create a training set - let’s use 2 years so we can let it detect growth/seasonality.

df_train<- df[df$ds < as.Date("2017-01-01"),]
summary(df_train)
##        ds                   y        
##  Min.   :2015-01-01   Min.   :    0  
##  1st Qu.:2015-07-02   1st Qu.:    2  
##  Median :2016-01-01   Median :10015  
##  Mean   :2016-01-01   Mean   : 7384  
##  3rd Qu.:2016-07-01   3rd Qu.:11023  
##  Max.   :2016-12-31   Max.   :14694

Forecast Assumptions

Forecast - 30 Days Forward

30 Days forward

m <- prophet(df_train)
future <- make_future_dataframe(m, periods=30) 
forecast <- predict(m,future)

Forecast - 90 Days Forward

90 Days forward

m <- prophet(df_train)
future <- make_future_dataframe(m, periods=90) 
forecast <- predict(m,future)

Performance with Prophet

Performance with Prophet

Forecast - Components

prophet_plot_components(m, forecast)

Trend Changepoints

Trend Changepoints

Trend Changepoints (Prohphet Docs)

Trend Changepoints (Prohphet Docs)

Trend Changepoints - Specifying Change Points

Control fit of model via changepoints

prophet(df_train, changepoint.prior.scale = 0.1)

Forecast Uncertainty

Forecast Uncertainty Plotted

plot(m, forecast)

Seasonality

Holidays

Holidays can be incorporated into the series with a window around it to indicate change of behavior in that time frame

EoYholiday <-data.frame(
  holiday='thanksmas',
ds=as.Date(c('2015-11-26','2015-12-25','2016-11-24','2016-12-25')),
lower_window=1, upper_window=2)

SoYHoliday<-data.frame(
  holiday='memind',
  ds=as.Date(c('2015-1-1','2015-05-25','2015-07-04','2016-1-1','2016-05-30','2016-07-04', '2017-1-1')),
  lower_window=1, upper_window=0)

holidays <- bind_rows(EoYholiday,SoYHoliday)

Forecast with Holidays (90 Days)

m <- prophet(df_train, holidays=holidays)
forecast <- predict(m,future)
prophet_plot_components(m, forecast)

Forecast with Holidays (90 Days)

I may have made it worse ¯\(ツ)

Analyst in the loop

prophet(df = df, growth = “linear”, changepoints = NULL, n.changepoints = 25, yearly.seasonality = TRUE, weekly.seasonality = TRUE, holidays = NULL, seasonality.prior.scale = 10, changepoint.prior.scale = 0.05, holidays.prior.scale = 10, mcmc.samples = 0, interval.width = 0.8, uncertainty.samples = 1000, fit = TRUE)

References

[1] Taylor, S. J., & Letham, B. (2017, January 13). Forecasting at Scale. Retrieved from https://facebookincubator.github.io/prophet/static/prophet_paper_20170113.pdf

[2] Tutorial: https://facebookincubator.github.io/prophet/docs/installation.html

Questions / Comments ?

Nan (Nandini) Hanagud

slack me @ nan_hanagud on a2mads