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 additive_terms_upper
#> 1 2012-05-18 40.50193 -4.547887 -4.547887 -4.547887
#> 2 2012-05-21 40.01455 -5.467254 -5.467254 -5.467254
#> 3 2012-05-22 39.85209 -5.609274 -5.609274 -5.609274
#> 4 2012-05-23 39.68963 -5.724989 -5.724989 -5.724989
#> 5 2012-05-24 39.52717 -5.806214 -5.806214 -5.806214
#> 6 2012-05-25 39.36471 -6.155667 -6.155667 -6.155667
#> weekly weekly_lower weekly_upper yearly yearly_lower yearly_upper
#> 1 0.5681288 0.5681288 0.5681288 -5.116016 -5.116016 -5.116016
#> 2 0.3000873 0.3000873 0.3000873 -5.767341 -5.767341 -5.767341
#> 3 0.3905757 0.3905757 0.3905757 -5.999850 -5.999850 -5.999850
#> 4 0.5129521 0.5129521 0.5129521 -6.237941 -6.237941 -6.237941
#> 5 0.6736987 0.6736987 0.6736987 -6.479913 -6.479913 -6.479913
#> 6 0.5681288 0.5681288 0.5681288 -6.723796 -6.723796 -6.723796
#> 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.36049 39.19215 40.50193 40.50193 35.95404
#> 2 31.12032 38.10306 40.01455 40.01455 34.54730
#> 3 30.70133 37.79524 39.85209 39.85209 34.24282
#> 4 30.54989 37.46622 39.68963 39.68963 33.96464
#> 5 30.13145 36.89207 39.52717 39.52717 33.72096
#> 6 29.46769 36.58290 39.36471 39.36471 33.20905
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)