FB Prophet over Excel: the Fastest, Easiest Way to Generate Time Series Forecasts

Paul Yang - August 18th, 2023

FB Prophet simplifies time-series forecasting while maintaining high accuracy. This user-friendly library handles seasonality and trend changes effectively.

Introduction to Time Series Forecasting

Time Series Forecasting is a common task for business analysts and data scientists alike. We take some observations that happened over time in the past to predict that same observation in the future. It has wide applications in several fields such as finance, economics, marketing, and more.

Today, we want to show you how to use FB Prophet, a powerful, efficient, and easy-to-use Python library developed by Facebook's data science team for accurate time series forecasting. Even though it depends on Python coding, we hope by the end of this guide, you’ll think it’s actually easier than doing it in Excel or anywhere else.

Basics and A Few Common Methods You May Know About

The basic principle of time series forecasting is intuitive. If you sold 30 peaches this week, you’ll probably sell 30 next week.

  • But maybe you sold 25 two weeks ago, 28 last week… maybe you would guess 32 next week.
  • But maybe as you enter the winter, peaches go out of season.
  • Maybe there was a few weeks where they were out of stock.
  • Maybe there’s an annual town peach festival.

And so the complexities grow.

The most common / most straightforward methods to do time series forecasting include the following:

  • Linear Regression
    • Models the relationship between the target variable and one or more lagged values of itself.
    • Assumes a linear relationship between the predictors (lagged values) and the target variable.
    • May not capture complex nonlinear patterns often present in time series data.
  • ARIMA (AutoRegressive Integrated Moving Average)
    • Combines autoregressive (AR) and moving average (MA) components.
    • Models the relationship between the current value and its lagged values, as well as the relationship between the current value and past forecast errors.
    • Suitable for data with trends and seasonality.
  • SARIMA is simply ARIMA with a seasonality component
  • Exponential Smoothing
    • Models the forecast as a weighted sum of past observations.
    • Weights decrease exponentially as observations get older.

Excel has historically relied on simpler statistical methods, where the FORECAST() function employs linear regression for predicting future values. In 2016, FORECAST.ETS() was introduced, which applies exponential smoothing to identify patterns in time series data. However, Excel tends to be manual, basic, and not particularly configurable.

What is FB Prophet?

FB Prophet is a state-of-the-art forecasting library that enables both novices and seasoned data scientists to perform time series forecasting with high precision. It accomplishes this through an statistical package that blends traditional time series modeling with modern machine learning techniques, thus making it the perfect candidate for fast, easy, and accurate forecasting.

Comparatively, ARIMA and ETS models require several pre-processing steps and significant statistical expertise to establish the order of differencing (d), the order of the autoregressive term (p), and the order of the moving-average component (q). Prophet does not require the user to manipulate these components manually – and if we lost you, the key here is you don’t have to worry about anything. You just have to provide data to Prophet.

How to Use FB Prophet

Let us use the Air Passengers dataset, a commonly referenced time-series dataset from Machine Learning Mastery to demonstrate the usage of FB Prophet. This is passenger traffic by month from 1949 to 1960.

The overview is:

  • Some boilerplate code to bring in the packages we need in
  • Bring the data in, and shape the dataset
  • Hit go on the forecast
  • Visualize the results

All the steps are in this embedded canvas below. Simply open and fork it.

Install

Start by installing the necessary libraries. Make sure you have the pandas, numpy, and fbprophet libraries. You can install them using pip:

!pip install fbprophet
import pandas as pd
from fbprophet import Prophet

Load Data

We can read the CSV directly from the repository, or you can connect to your data however you like. You can also fork our Einblick canvas here to start.

df = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv')
df.rename(columns={'Month':'ds', 'Passengers':'y'}, inplace=True)
df['ds'] = pd.to_datetime(df['ds'])

In the above code, we've renamed Month to ds and Passengers to y to suit Prophet's API.

Make the Forecast

We're now ready to create a Prophet forecasting model and fit our data to it:

model = Prophet()
model.fit(df)
future = model.make_future_dataframe(periods=36, freq='M')
forecast = model.predict(future)

There, in 4 short lines of code, we have made a forecast for a specified period (36 months), and 'M' represents that these periods are days.

The predict() method generates the forecast, giving us the components like trend and seasonality, as well as the values predicted for 1961-1963 in the yhat column.

How to Visualize the Details?

Use the plot method to visualize your forecast:

model.plot(forecast);

You can also see the components with

model.plot_components(forecast);

In just a few lines of code, we get an interactive and interpretable output. We see a positive trend over time, possibly accelerating. We also see the peaks and troughs of annual travel, with the summer being the high season generally.

Conclusion

In conclusion, if time-series forecasting is part of your work or research, you should consider FB Prophet due to its simplicity, robustness and flexibility. Its easy-to-use API combined with high-performance computations makes it a versatile tool in the toolkit of any data scientist.

Need more information? Check out their documentation.

BONUS: Time Series with Generative AI

If you're interested in speeding up your workflows, check out our AI agent, Einblick Prompt, available in every Einblick data notebook--even the one you opened and forked earlier in the article. In as little as one sentence Prompt can write and fix code, create beautiful charts, build models, and so much more.

About

Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.