Time Series Analysis using Facebook Prophet in R Programming
Last Updated :
07 Jul, 2025
Time Series Analysis using Facebook Prophet in R is a method of analyzing and forecasting data that changes over time. It allows businesses to understand patterns in time series datasets and make future predictions using trend, seasonality, and holiday effects. R provides support for Prophet via the prophet package, which makes implementation simple and interpretable.
Time Series Analysis
Time series analysis involves studying data points collected in time order to detect patterns, trends, and seasonality, helping in making informed predictions based on historical behavior. In R, this can be done using the ts() function, which transforms numeric data into a time series object.
Facebook Prophet Model
Facebook Prophet is a forecasting model developed by Facebook, designed to fit decomposable time series data using three main components: trend, seasonality, and holidays. It provides better control, automatic tuning, and interpretable output compared to traditional models like ARIMA.
Mathematical Equation of Prophet Model
The model is defined as:
y(t) = g(t) + s(t) + h(t) + e(t)
Where:
- y(t): the forecast
- g(t): the trend component
- s(t): the seasonality component
- h(t): the holidays component
- e(t): the error or residual
Important Terms Used in Facebook Prophet Model
1. Trend: A trend is a shift in development either in increment or decrement direction. Mathematically,
g(t) = \frac{C}{1 + e^{-k(t - m)}}
Where:
- C: indicates the carry capacity
- k: indicates the growth
- m: indicates the offset parameter
2. Seasonality: Seasonality is a feature of time series object that occurs at a particular time/season and changes the trend.
3. Holidays: Holidays are a time period that changes a lot to the business. It can make a profit or loss depending upon the business.
Implementation of Time Series Forecasting Using Facebook Prophet
We use the built-in AirPassengers dataset in R to forecast future airline passenger traffic using the Facebook Prophet model. This implementation includes data preparation, model fitting, and visualization of forecast results.
1. Installing the required library
We install the prophet package to use Prophet functions for time series modeling and forecasting.
- install.packages(): used to install packages from CRAN.
- prophet: provides functions to fit the Prophet model and generate forecasts.
r
install.packages("prophet")
2. Loading the required library
We load the Prophet package to access its forecasting functions.
- library(): loads an installed package into the R session.
r
3. Loading and assigning the dataset
We load the built-in AirPassengers dataset and assign it to a new variable.
- data(): loads built-in datasets into the environment.
R
data(AirPassengers)
ap <- AirPassengers
4. Viewing the dataset
We examine the built-in AirPassengers
data to understand its structure.
- AirPassengers: a ts object representing monthly airline passenger numbers.
R
Output:
Output5. Checking the length of the dataset
We check the total number of time periods (months) in the series.
- length(): returns the number of observations in a vector or time series.
r
Output:
144
We convert the time series object into a data frame with columns ds
and y
.
- seq.Date(): creates a monthly sequence of dates.
- as.numeric(): extracts numeric values from the time series.
- data.frame(): creates a data frame for Prophet input.
r
df <- data.frame(
ds = seq.Date(from = as.Date("1949-01-01"), by = "month", length.out = length(AirPassengers)),
y = as.numeric(AirPassengers)
)
7. Fitting the Prophet model
We fit the Prophet model to the prepared dataset.
- prophet(): fits a decomposable model using trend, seasonality, and holiday components.
r
8. Making future predictions
We generate future timestamps and use the model to forecast values.
- make_future_dataframe(): generates dates beyond the original time frame.
- predict(): produces the forecasted values.
R
future <- make_future_dataframe(m, periods = 12, freq = "month")
forecast <- predict(m, future)
9. Plotting the forecast
We visualize the forecasted values with the historical data.
- plot(): displays actual vs. predicted values along with uncertainty intervals.
R
Output:

The forecast plot displays the historical airline passenger data (black dots), the predicted trend (dark blue line), and the confidence intervals (shaded blue area) for the next 12 months.