Quick Start Guide to Using Prophet

Sean J. Taylor and Ben Letham

2017-01-31

Prophet uses the normal model fitting API. We provide a prophet function that performs fitting and returns a model object. You can then call predict and plot on this model object.

First we read in the data and create the outcome variable.

library(readr)
df <- read_csv('../tests/testthat/data.csv')
#> Parsed with column specification:
#> cols(
#>   ds = col_date(format = ""),
#>   y = col_double()
#> )

We call the prophet function to fit the model. The first argument is the historical dataframe. Additional arguments control how Prophet fits the data.

m <- prophet(df)
#> STAN OPTIMIZATION COMMAND (LBFGS)
#> init = user
#> save_iterations = 1
#> init_alpha = 0.001
#> tol_obj = 1e-12
#> tol_grad = 1e-08
#> tol_param = 1e-08
#> tol_rel_obj = 10000
#> tol_rel_grad = 1e+07
#> history_size = 5
#> seed = 1217552753
#> initial log joint probability = -14.9738
#> Optimization terminated normally: 
#>   Convergence detected: relative gradient magnitude is below tolerance

We need to construct a dataframe for prediction. The make_future_dataframe function takes the model object and a number of periods to forecast:

future <- make_future_dataframe(m, periods = 365)
head(future)
#>           ds
#> 1 2012-05-18
#> 2 2012-05-21
#> 3 2012-05-22
#> 4 2012-05-23
#> 5 2012-05-24
#> 6 2012-05-25

As with most modeling procedures in R, we use the generic predict function to get our forecast:

forecast <- predict(m, future)
head(forecast)
#>           ds           t    trend yhat_lower yhat_upper trend_lower
#> 1 2012-05-18 0.000000000 39.44963   33.20437   39.48459    39.44963
#> 2 2012-05-21 0.004398827 38.86595   31.47103   37.49718    38.86595
#> 3 2012-05-22 0.005865103 38.67139   30.91618   37.17558    38.67139
#> 4 2012-05-23 0.007331378 38.47683   30.59770   36.77423    38.47683
#> 5 2012-05-24 0.008797654 38.28227   30.08714   36.48565    38.28227
#> 6 2012-05-25 0.010263930 38.08771   29.69954   35.93925    38.08771
#>   trend_upper seasonal_lower seasonal_upper    weekly weekly_lower
#> 1    39.44963      -3.140629      -3.140629 0.4077828    0.4077828
#> 2    38.86595      -4.359111      -4.359111 0.1238344    0.1238344
#> 3    38.67139      -4.613952      -4.613952 0.1865497    0.1865497
#> 4    38.47683      -4.867718      -4.867718 0.2476399    0.2476399
#> 5    38.28227      -4.932031      -4.932031 0.4918188    0.4918188
#> 6    38.08771      -5.314458      -5.314458 0.4077828    0.4077828
#>   weekly_upper    yearly yearly_lower yearly_upper  seasonal     yhat
#> 1    0.4077828 -3.548411    -3.548411    -3.548411 -3.140629 36.30900
#> 2    0.1238344 -4.482946    -4.482946    -4.482946 -4.359111 34.50684
#> 3    0.1865497 -4.800502    -4.800502    -4.800502 -4.613952 34.05744
#> 4    0.2476399 -5.115358    -5.115358    -5.115358 -4.867718 33.60911
#> 5    0.4918188 -5.423849    -5.423849    -5.423849 -4.932031 33.35024
#> 6    0.4077828 -5.722241    -5.722241    -5.722241 -5.314458 32.77326

You can use the generic plot function to plot the forecast, but you must also pass the model in to be plotted:

plot(m, forecast)

Just as in Python, you can plot the components of the forecast. In R, you use the prophet_plot_components function instead of an instance method:

prophet_plot_components(m, forecast)