What is Facebook Prophet? Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.
While advancements in data science often increase the infamous “skills gap” surrounding the field, Prophet was intentionally designed to lower the cost of entry for analysts — who possess an “in-the-loop” understanding of the problems they are trying to solve — with automated of time series forecasting.
The procedure makes use of a decomposable time series model with three main model components: trend, seasonality, and holidays.
y(t) = g(t) + s(t) + h(t) + e(t)
g(t) = trend models non-periodic changes; linear or logistic
s(t) = seasonality represents periodic changes; i.e. weekly, monthly, yearly
h(t) = ties in effects of holidays; on potentially irregular schedules ≥ 1 day(s)
The error term e(t) represents any idiosyncratic changes which are not accommodated by the model; later we will make the parametric assumption that e(t) is normally distributed.
packages <- c("tidyverse", "prophet")
packages <- lapply(packages, FUN = function(x) {
if(!require(x, character.only = TRUE)) {install.packages(x)
library(x, character.only = TRUE)}})
df <- data.frame(date=time(LakeHuron), level=as.matrix(LakeHuron))
df$date <- as.numeric(df$date)
df$date <- as.Date(df$date, format = "%Y-%m-%d")
Prophet expects input data to have 2 columns, ds and y.
df <- df %>% dplyr::select(date, level)
names(df) <- c('ds', 'y')
str(df)
'data.frame':98 obs. of 2 variables:
$ ds: Date, format: "1875-01-01" "1876-01-01" "1877-01-01" ...
$ y : num 580 582 581 581 580 ...
Forecasts need somewhere to go, so we need to first make a Prophet model (m) based on our data (df), and have it make an empty future dataframe (future) for our desired number of periods (days in our case) that will be forecast (let’s do a year).
m <- prophet(df)
future <- make_future_dataframe(m, periods=365)
# head(future, 5)
forecast <- predict(m, future)
Visualizing the forecast is made easy with plot();
plot(m, forecast)
While there’s a lot put out by the forecast, we can focus on a few keys like;
To understand the forecast more intemently, we can plot its components with;
prophet_plot_components(m, forecast)