Understanding Forecasting Model Using Prophet — A Comprehensive Guide

ryh4n
6 min readJan 3, 2024

--

In the world of data analysis, forecasting is an important part of anticipating future trends based on historical data. This article will review the use of forecasting models using Prophet, a library useful for predicting trends based on historical data. In this article, I will outline the steps for using Prophet, from data access to model performance evaluation.

Photo by Aron Visuals on Unsplash

Forecasting is an important process in the world of data analysis that allows us to predict future trends based on historical data. In an economic context, predictions on factors such as inflation, consumer prices, or stock market performance are invaluable for smart decision-making. One very useful tool in this process is a forecasting model.

In this article, I will focus on forecasting models using Prophet, a tool developed by Facebook to predict trends based on historical data very well. Prophet allows data analysts to make accurate predictions and also provides ease of implementation.

Prophet utilizes a relatively simple yet effective approach in modeling time trends. By incorporating components such as seasonality, trend, and holiday effects, Prophet can capture the complex structure in time series data better than traditional forecasting methods.

Prophet has the advantage of:

Ease of Use: With a simple syntax, Prophet is accessible to users of different levels of expertise in data analysis.

Resilience to Missing Values: Able to handle missing values in time series well.

Good Handling of Outliers: Prophet can handle data that has outliers or extreme changes.

Thus, Prophet not only predicts the future based on historical data, but also provides ease of application and handling of common problems in data analysis.

Data Collection and Model Initialization

Before we can start the forecasting process with Prophet, the first step is to access and collect the data that will be used for analysis. In this article, we will use Consumer Price Index (CPI) data from the Federal Reserve Economic Data (FRED) as an example.

First, we use the FRED API to access the CPI data. This step requires an API key obtained from FRED to authenticate the data access request.

import fredapi as fd

# Initialize the FRED API with the API key
fred = fd.Fred(api_key='Your_API_Key_Here')

# Perform a search for Consumer Price Index (CPI) data
data = fred.search('Consumer Price Index for All Urban Consumers: All Items in U.S. City Average')

# Display the first few rows of the data to check its structure
data.head()

After accessing the data, the next step is to prepare and clean the data for use in the forecasting model. This may involve steps such as:

Date format adjustment: Ensuring the date format matches Prophet’s requirements.

Checking for missing values: Handling missing values in the data.

Data filtering: Selecting relevant time ranges or adjusting unnecessary data.

Next, we will use the prepared data to initialize the Prophet model. This process involves creating an instance of the Prophet class, training the model with the prepared data, and preparing the data for future predictions.

from prophet import Prophet

# Create a Prophet model instance
model = Prophet()

# Train the model with the prepared data
model.fit(df_prepared_data)

# Create a dataframe for the future period to be predicted
future = model.make_future_dataframe(periods=10, freq='MS')

These steps lead us to the next stage in the forecasting process, where the initialized model will be used to make predictions on future data.

Data Exploration and Visualization

After collecting and cleaning the data, the next step is to explore the data to gain a better understanding of its patterns, trends and characteristics. In this context, data visualization is key to understanding and expressing the information contained in the data.

Let’s start by using Pandas to explore the data structure, check the descriptive statistics, and ensure the data is ready for further processing.

import pandas as pd

# Display info and descriptive statistics of the data
print(data.info())
print(data.describe())

Next, we will use a visual tool like Plotly Express to create an informative visualization of the CPI (Consumer Price Index) data. This visualization will help us see trends, fluctuations, and patterns in the CPI data over a specific time span.

import plotly.express as px

# Create a line plot using Plotly Express for CPI data
fig = px.line(df_cpi, x='date', y='value', title='Consumer Price Index (CPI) Trend')
fig.show()

The visual images generated from these measures will help in understanding the patterns and trends of the CPI data, allowing us to see how the CPI fluctuates over time.

Modeling and Prediction

Once the data has been prepared and explored, the next step is to model it using Prophet and perform predictions on future data.

First, we have initialized the Prophet model and trained it with the previously prepared data. Now, we will use this model to make predictions for future time periods.

from prophet import Prophet

# Create a Prophet model instance
model = Prophet()

# Train the model with the prepared data
model.fit(df_prepared_data)

# Create a dataframe for the future period to be predicted
future = model.make_future_dataframe(periods=10, freq='MS')

# Perform prediction using the model
result = model.predict(future)

The result of this prediction process will contain columns such as ‘ds’ (date), ‘yhat’ (prediction), ‘yhat_lower’ (lower limit of prediction), and ‘yhat_upper’ (upper limit of prediction). This allows us to see a forecast of future trends based on historical data.

In this context, it is important to display the prediction results with appropriate visualizations to provide a clearer picture of the trends predicted by the model.

# Visualization of prediction results using Prophet
fig = model.plot(result)

fig2=model.plot_components(result)

Model Performance Evaluation

After making predictions using Prophet models, the next important step is to evaluate how well the model predicts future data. Prophet provides tools to perform cross-validation and measure model performance using certain metrics such as Mean Absolute Error (MAE).

from prophet.diagnostics import cross_validation, performance_metrics

# Perform cross-validation to evaluate model performance
cv_results = cross_validation(model=model,
initial=pd.to_timedelta(30*20, unit='D'),
period=pd.to_timedelta(30 * 5, unit='D'),
horizon=pd.to_timedelta(30*12, unit='D')
)

# Set a 'cutoff' date for performance evaluation
cv_results['cutoff'] = pd.to_datetime('YYYY-MM-DD you want to cutoff')

# Calculate performance metrics using performance_metrics
df_performance = performance_metrics(cv_results)
df_p

The results of this performance evaluation will provide information on how well the model can predict future data. To enhance understanding, it is important to present these performance metrics visually.

from prophet.plot import plot_cross_validation_metric

# Visualize performance metrics using plots
fig = plot_cross_validation_metric(cv_results, metric = 'mae')

These visual graphs will help in interpreting performance metrics such as MAE and give an idea of how accurate the model is in making predictions.

In this article, we have explored the use of forecasting models using Prophet, a very useful tool for predicting trends based on historical data. Through the steps we covered, we have learned the process from data collection, model initialization, data exploration, prediction generation, to model performance evaluation.

Prophet excels in its ease of use as well as its ability to handle common problems in data analysis, such as missing values and outliers. However, the success of a forecasting model depends not only on the algorithm used but also on a deep understanding of the data used and a thorough evaluation process.

In an ever-changing world, the ability to make accurate predictions is crucial. With forecasting models like Prophet, data analysts can help organizations and decision makers to better anticipate future trends.

As such, an understanding of how to use Prophet to perform forecasting is invaluable for anyone involved in data analysis.

Thank you for following this guide on using forecasting models using Prophet. Hopefully, this article has provided useful insights and supported your journey in the world of data analysis.

Please don’t hesitate to share your thoughts and provide feedback.

Feel free to reach out to me via LinkedIn at: https://www.linkedin.com/in/reyhannananta

Explore my GitHub profile for Python codes. You’ll find the mentioned article’s code and corresponding graphs here: reyhannananta/ForecastingEconomicTrends (github.com)

References:

Consumer Price Index for All Urban Consumers: All Items in U.S. City Average (CPIAUCSL) | FRED | St. Louis Fed (stlouisfed.org)

--

--