Facebook Prophet

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.

Load Packages

packages <- c("tidyverse", "prophet")

packages <- lapply(packages, FUN = function(x) {

if(!require(x, character.only = TRUE)) {install.packages(x)

library(x, character.only = TRUE)}})

Load Dataset

df <- data.frame(date=time(LakeHuron), level=as.matrix(LakeHuron))

Format time series data

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')

Examine structure

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 ...

Making a Forecast

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)

Make Forecast Prediction

forecast <- predict(m, future)

Visualize Forecast

Visualizing the forecast is made easy with plot();

plot(m, forecast)

Decomposing a 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)

References

Sabalico Logo
Sabalytics Logo
Senty Logo
SEO Guide Logo
World Map Logo
rStatistics Logo
Day Map Logo
Time Zone Logo
Galaxy View Logo
Periodic Table Logo
My Location Logo
Weather Track Logo
Sprite Sheet Logo
Barcode Generator Logo
Test Speed Logo
Website Tools Logo
Image Tools Logo
Color Tools Logo
Text Tools Logo
Finance Tools Logo
File Tools Logo
Data Tools Logo
History of Humanity - History Archive Logo
History of Humanity - History Mysteries Logo
History of Humanity - Ancient Mesopotamia Logo
History of Humanity - Persian Empire Logo
History of Humanity - Alexander the Great Logo
History of Humanity - Roman History Logo
History of Humanity - Punic Wars Logo
History of Humanity - Golden Age of Piracy Logo
History of Humanity - Revolutionary War Logo