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)
#> Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
#> 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 = 1039955814
#> initial log joint probability = -15.5191
#> 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    trend  seasonal seasonal_lower seasonal_upper
#> 1 2012-05-18 40.50704 -4.589466      -4.589466      -4.589466
#> 2 2012-05-21 40.01718 -5.503317      -5.503317      -5.503317
#> 3 2012-05-22 39.85390 -5.642728      -5.642728      -5.642728
#> 4 2012-05-23 39.69061 -5.757367      -5.757367      -5.757367
#> 5 2012-05-24 39.52733 -5.835629      -5.835629      -5.835629
#> 6 2012-05-25 39.36404 -6.182364      -6.182364      -6.182364
#>   seasonalities seasonalities_lower seasonalities_upper    weekly
#> 1     -4.589466           -4.589466           -4.589466 0.5684724
#> 2     -5.503317           -5.503317           -5.503317 0.3006457
#> 3     -5.642728           -5.642728           -5.642728 0.3916047
#> 4     -5.757367           -5.757367           -5.757367 0.5127405
#> 5     -5.835629           -5.835629           -5.835629 0.6739651
#> 6     -6.182364           -6.182364           -6.182364 0.5684724
#>   weekly_lower weekly_upper    yearly yearly_lower yearly_upper yhat_lower
#> 1    0.5684724    0.5684724 -5.157939    -5.157939    -5.157939   32.45973
#> 2    0.3006457    0.3006457 -5.803963    -5.803963    -5.803963   31.09110
#> 3    0.3916047    0.3916047 -6.034332    -6.034332    -6.034332   30.63939
#> 4    0.5127405    0.5127405 -6.270107    -6.270107    -6.270107   30.49164
#> 5    0.6739651    0.6739651 -6.509594    -6.509594    -6.509594   30.38996
#> 6    0.5684724    0.5684724 -6.750836    -6.750836    -6.750836   29.68181
#>   yhat_upper trend_lower trend_upper     yhat
#> 1   39.57742    40.50704    40.50704 35.91757
#> 2   37.89253    40.01718    40.01718 34.51386
#> 3   37.57226    39.85390    39.85390 34.21117
#> 4   37.31981    39.69061    39.69061 33.93324
#> 5   37.14404    39.52733    39.52733 33.69170
#> 6   36.54046    39.36404    39.36404 33.18168

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)

plot of chunk unnamed-chunk-6

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)

plot of chunk unnamed-chunk-7