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')
#> 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.
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
#> 1 2012-05-18 40.46725 -4.539132 -4.539132
#> 2 2012-05-21 39.98010 -5.454565 -5.454565
#> 3 2012-05-22 39.81772 -5.595548 -5.595548
#> 4 2012-05-23 39.65534 -5.710402 -5.710402
#> 5 2012-05-24 39.49295 -5.790313 -5.790313
#> 6 2012-05-25 39.33057 -6.139026 -6.139026
#> additive_terms_upper weekly weekly_lower weekly_upper yearly
#> 1 -4.539132 0.5600457 0.5600457 0.5600457 -5.099177
#> 2 -5.454565 0.2927224 0.2927224 0.2927224 -5.747287
#> 3 -5.595548 0.3831188 0.3831188 0.3831188 -5.978666
#> 4 -5.710402 0.5051989 0.5051989 0.5051989 -6.215601
#> 5 -5.790313 0.6660785 0.6660785 0.6660785 -6.456392
#> 6 -6.139026 0.5600457 0.5600457 0.5600457 -6.699072
#> yearly_lower yearly_upper multiplicative_terms
#> 1 -5.099177 -5.099177 0
#> 2 -5.747287 -5.747287 0
#> 3 -5.978666 -5.978666 0
#> 4 -6.215601 -6.215601 0
#> 5 -6.456392 -6.456392 0
#> 6 -6.699072 -6.699072 0
#> multiplicative_terms_lower multiplicative_terms_upper yhat_lower
#> 1 0 0 32.53706
#> 2 0 0 30.88820
#> 3 0 0 30.94142
#> 4 0 0 30.60769
#> 5 0 0 30.53963
#> 6 0 0 29.86936
#> yhat_upper trend_lower trend_upper yhat
#> 1 39.35083 40.46725 40.46725 35.92812
#> 2 37.91681 39.98010 39.98010 34.52553
#> 3 37.62349 39.81772 39.81772 34.22217
#> 4 37.63365 39.65534 39.65534 33.94493
#> 5 37.19760 39.49295 39.49295 33.70264
#> 6 36.63099 39.33057 39.33057 33.19154
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)
You can plot the components of the forecast using the prophet_plot_components
function:
prophet_plot_components(m, forecast)