Time Series Analysis using Facebook Prophet in R Programming
Last Updated :
22 Jul, 2020
Time Series Analysis is a way of analysing and learning the behaviour of datasets over a period. Moreover, it helps in learning the behavior of the dataset by plotting the time series object on the graph. In
R programming, it can be easily performed by using
ts()
function that takes the data vector and converts it into time series object as specified in function parameters.
Facebook Prophet is a tool developed by Facebook for forecasting time series objects or data. It helps businesses to learn the behavior of their products by forecasting prices, sales, or weather.
Facebook Prophet tool is based on decomposable model i.e., trend, seasonality and holidays that helps in making more accurate predictive models with these constraints. It is much better than the ARIMA model as it helps in tuning and adjusting the input parameters.
Mathematical Equation of Prophet Model
y(t) = g(t) + s(t) + h(t) + e(t)
where,
y(t) refers to the forecast
g(t) refers to the trend
s(t) refers to the seasonality
h(t) refers to the holidays for the forecast
e(t) refers to the error term while forecasting
Some Important Terms Used in Facebook Prophet Model
- 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
- Seasonality
Seasonality is a feature of time series object that occurs at a particular time/season and changes the trend.
- 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 in R
In the example below, let us download the dataset AirPassengers and perform the forecasting of the dataset using the Facebook Prophet model. After executing the whole code, the model will show the forecasted values with the trend and seasonality of the business.
Step 1: Installing the required library
r
# Install the library
install.packages("prophet")
Step 2: Load the required library
r
# Load the library
library(prophet)
Step 3: Download the dataset AirPassengers from
here
r
# Dataset
ap <- read.csv("example_air_passengers.csv")
Step 4: Call the prophet function to fit the model
r
Step 5: Make predictions
r
# Predictions
future <- make_future_dataframe(m,
periods = 365)
# Print predictions
cat("\nPredictions:\n")
tail(future)
Output:
Predictions:
ds
504 1961-11-26
505 1961-11-27
506 1961-11-28
507 1961-11-29
508 1961-11-30
509 1961-12-01
Step 6: Forecast data using predictions
r
# Forecast
forecast <- predict(m, future)
tail(forecast[c('ds', 'yhat',
'yhat_lower', 'yhat_upper')])
ds yhat yhat_lower yhat_upper
504 1961-11-26 497.2056 468.6301 525.7918
505 1961-11-27 496.0703 467.8579 525.6728
506 1961-11-28 493.1698 465.5788 522.9650
507 1961-11-29 496.0497 469.2889 524.6313
508 1961-11-30 492.8452 463.7279 520.4519
509 1961-12-01 493.6417 466.3496 522.9887
Step 7: Plot the forecast
r
# Output to be present
# As PNG file
png(file = "facebookprophetGFG.png")
# Plot
plot(m, forecast)
# Saving the file
dev.off()
Output:

Above graph shows the forecasted values of AirPassengers where,
Black dots refers to the original data,
Dark blue line refers to the predicted value(yhat), and
Light blue area indicates the yhat_upper and yhat_lower value.
Step 8: Plot the trend, weekly and yearly seasonality
r
# Output to be present
# As PNG file
png(file = "facebookprophettrendGFG.png")
# Plot
prophet_plot_components(m, forecast)
# Saving the file
dev.off()
Output:

The above graph shows the trend of the dataset that air passengers have been increased over a given period of time. In second graph, it shows seasonality of the dataset over a period of time i.e., yearly and signifies that air passengers were maximum between months from June to August.
Similar Reads
Time Series Analysis using Facebook Prophet
Time series analysis is one of the important methodologies that helps us to understand the hidden patterns in a dataset that is too related to the time at which it is being recorded. The article aims to explore the fundamentals of time series analysis and demonstrates the analysis using Facebook Pro
8 min read
Time Series Analysis using ARIMA model in R Programming
In R programming, data analysis and visualization is so easy to learn the behaviour of the data. Moreover, the R language is used mostly in the data science field after Python. Time series analysis is a type of analysis of data used to check the behaviour of data over a period of time. The data is c
6 min read
Social Network Analysis Using R Programming
Social Network Analysis (SNA) is the process of exploring or examining the social structure by using graph theory. It is used for measuring and analyzing the structural properties of the network. It helps to measure relationships and flows between groups, organizations, and other connected entities.
6 min read
Time Series and Forecasting Using R
Time series forecasting is the process of using historical data to make predictions about future events. It is commonly used in fields such as finance, economics, and weather forecasting. R is a powerful programming language and software environment for statistical computing and graphics that is wid
9 min read
Accessing REST API using R Programming
REST(Representational state transfer) API is an architectural style that includes specific constraints for building APIs to ensure that they are consistent, efficient, and scalable. REST API is a collection of syntax and constraints which are used in the development and operation of web services tha
3 min read
GE Stock Price Analysis Using R Language
Stock analysis is a technique used by investors and traders to make purchasing and selling choices. Investors and traders strive to obtain an advantage in the markets by making educated judgments by researching and analyzing previous and current data. In this article, we will analyze the 'GE Stock P
5 min read
Time Series Analysis in R
Time series analysis is a statistical technique used to understand how data points evolve over time. In R programming, time series analysis can be efficiently performed using the ts() function, which helps organize data with associated time stamps. This method is widely applied in business and resea
3 min read
Parsing Date and Time in R Programming - strptime() Function
strptime() function in R Language is used to parse the given representation of date and time with the given template. Syntax: strptime(x, format, tz = "")Parameters: x: given representation of date and time y: given template in which parsing is done tz: a character string specifying the time zone t
1 min read
Exploratory Data Analysis in R Programming
Exploratory Data Analysis or EDA is a statistical approach or technique for analyzing data sets to summarize their important and main characteristics generally by using some visual aids. The EDA approach can be used to gather knowledge about the following aspects of data. Main characteristics or fea
11 min read
Random Forest Approach for Regression in R Programming
Random Forest approach is a supervised learning algorithm. It builds the multiple decision trees which are known as forest and glue them together to urge a more accurate and stable prediction. The random forest approach is similar to the ensemble technique called as Bagging. In this approach, multip
3 min read