This document provides a very brief introduction to the Prophet API. For a detailed guide on using Prophet, please visit the main site at https://facebook.github.io/prophet/.
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')
#> Rows: 510 Columns: 2
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (1): y
#> date (1): ds
#>
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
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.
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 additive_terms additive_terms_lower additive_terms_upper
#> 1 2012-05-18 42.02262 -5.622539 -5.622539 -5.622539
#> 2 2012-05-21 41.44858 -6.543832 -6.543832 -6.543832
#> 3 2012-05-22 41.25723 -6.684730 -6.684730 -6.684730
#> 4 2012-05-23 41.06588 -6.797414 -6.797414 -6.797414
#> 5 2012-05-24 40.87454 -6.870814 -6.870814 -6.870814
#> 6 2012-05-25 40.68319 -7.213554 -7.213554 -7.213554
#> weekly weekly_lower weekly_upper yearly yearly_lower yearly_upper
#> 1 0.8968967 0.8968967 0.8968967 -6.519435 -6.519435 -6.519435
#> 2 0.6338415 0.6338415 0.6338415 -7.177673 -7.177673 -7.177673
#> 3 0.7232570 0.7232570 0.7232570 -7.407987 -7.407987 -7.407987
#> 4 0.8440252 0.8440252 0.8440252 -7.641439 -7.641439 -7.641439
#> 5 1.0054458 1.0054458 1.0054458 -7.876260 -7.876260 -7.876260
#> 6 0.8968967 0.8968967 0.8968967 -8.110451 -8.110451 -8.110451
#> multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper
#> 1 0 0 0
#> 2 0 0 0
#> 3 0 0 0
#> 4 0 0 0
#> 5 0 0 0
#> 6 0 0 0
#> yhat_lower yhat_upper trend_lower trend_upper yhat
#> 1 32.99348 39.97525 42.02262 42.02262 36.40008
#> 2 31.64212 38.03261 41.44858 41.44858 34.90475
#> 3 31.41135 38.29274 41.25723 41.25723 34.57250
#> 4 30.93532 37.45746 41.06588 41.06588 34.26847
#> 5 30.57952 37.28505 40.87454 40.87454 34.00372
#> 6 29.81891 36.81012 40.68319 40.68319 33.46964
You can use the generic plot
function to plot the
forecast, but you must also pass the model in to be plotted:
You can plot the components of the forecast using the
prophet_plot_components
function: