Trying Facebook Prophet for Time Series Model
By: Jacques Sham
Tags: R, Time Series Forecasting, Facebook Prophet, Python, Data Science

When I was taking Time Series in graduate school, my professor told me there is no good time series package in Python that he found useful. Recently, I have learned that Facebook releases an API on Time Series Model for Python and R, it is called Facebook Prophet. Since I have also a small project on making prediction on SFO passenger traffic in R, I am glad that there is an option on building a time series in Python, so I have given it a try.

Make Prediction for SFO Passenger Traffic between 2018 and 2019

The project I did was to build a model to predict monthly passenger count between 2018 and 2019 in R with the time series model I have learned in graduate school. I obtained the passenger traffic data from Open SF and trained the models in Holt-Winters and Box-Jerkins methods (ARIMA and SARMIA, specifically). You may find my Github repository on this project in the link at the end of this article.

The below images are the plots I built for Holt-Winters, ARIMA, and SARMIA models.

Plot of Holt-Winters model

Holt-Winters Methods predicts by using exponential smoothing techniques, in other words, the model is learned by taking an exponentially weighted moving average and do not need any assumption.

Plot of ARIMA model

Plot of SARMIA model

Box-Jenkins Method is an autoregressive integrated moving average model which is learned by converting the data set into stationary. I used ARIMA and SARIMA models. The difference between the two is that ARIMA is non-seasonal while SARIMA is seasonal.

In R, you may avoid the trouble of learning the hyperparameters by using the following code from the forecast package.

library(forecast)

auto.arima()

However, the disadvantage of using auto.arima() the hyperparameters learned by R is not necessarily the most optimal. For example, the hyperparameter R learned for ARIMA model is ARIMA(0,1,0) that the prediction is purely based on last period. That’s why the plot of ARIMA model in this project looks very fun.

In order to evaluate the models among 3 models, I have used RMSE as metric. The RMSE for Holt-Winters, ARIMA, and SARIMA models are 0.1342, 0.6746, and 0.1812. Therefore, I have concluded that Holt-Winters model is the best model for prediction among the models.

Facebook Prophet

My first impression to Facebook Prophet was pretty good as this is the first time series package I see in Python and follow Sklearn model. To evaluate whether the package is useful, I use the same data set and calculate the RMSE of the model.

To use Facebook Prophet, you need to have your data set ready in two columns in pandas dataframe— date and value. Note that the column for date has to be datetime data type in ‘YYYY-MM-DD’ format (timestamp is also acceptable). However, the documentation does not recommend other datetime formats. Also, the column name for date and value has to be ‘ds’ and ‘y’.

Once the data set is ready, the next step is to set up an object for the model. If you have been using Sklearn, this is very easy for you. Follow the following code:

model = Prophet()

model.fit(dataset)

After that, you may make the prediction by passing a one-column dataframe of the dates you want to predict as input.

Unfortunately, you may only use linear model and logistic growth in Facebook Prophet. Holt-Winters and Box-Jerkins methods are not available in the package. By default, linear model is the default model in Facebook Prophet.

When making the prediction, the documentation recommends using this syntax to make a one-column dataframe of date and have the model make the prediction.

future = model.make_future_dataframe(periods=365)

forecast = model.predict(future)

This syntax is not working in my project because the “make_future_dataframe(periods=365)” return a dataframe with 365 days. However, my desired dates are last day of each month between 2018 and 2019. Alternatively, I have to make the dataframe manually.

from datetime import datetime

from dateutil.relativedelta import relativedelta future = [datetime(2016, 1, 1) + relativedelta(months=i, day=31) for i in range(24)]

future = pd.DataFrame(data=future, columns=[‘ds’])

prediction = model.predict(future)

“relativedelta” from “dateutil” is a useful date package in Python. If you have relativedelta(months=2, day=31) add to January 1, 2016, it could return to the last day of February, 2016 even you pass 31 days in the input.

Once the dataframe is pass to the model, it will return the result I want. The RMSE for this model is 1.0493. It turns out the model learned with Facebook Prophet is not as good as Holts-Winters model in R.

Another nice function of Facebook Prophet is that you may easily pass the model and prediction to Plotly for visualization. It is as easy as like this:

from fbprophet.plot import plot_plotly

import plotly.offline as py fig = plot_plotly(model, prediction)

py.plot(fig, filename=’fbprophet_plot.html’)

Based on the result I have, the plot looks like this:

Plots for linear model in Facebook Prophet

My thought

I actually like using Facebook Prophet for time series analysis, mostly because of its syntax that is very similar to SKlearn and it is available in Python. In the big data world, Python is increasingly more important so there is a need for good time series available in Python. Unfortunately, there is no Holt-Winters and Box-Jerkins models available that accuracy of the model learned is a concern to me. In the end, the RMSE of the model learned by Facebook Prophet is higher than Holt-Winters learned by R forecast package. Facebook Prophet is new, I am looking forward to the developers to add more time series methods to this package.

Also, I did have some hard time installing Facebook Prophet. If you are using iOs, be sure compilers and Python development tools are installed. The best way is to install with Anaconda. Be sure to check out the documentation when you install Facebook Prophet.

Conclusion

Facebook Prophet provides a great source to make time series model in Python but there is a lack of sophisticated statistical time series methods to build models. If you are not very picky about what statistical methods are applied, Facebook Prophet is a good package to use because the syntax is very similar to SKlearn and easy to plot in Plotly for trend analysis. In your Python code, you only need to have the code of loading data, declare model object, fit the model with the data, make prediction, and plot the model in Plotly. However, if accuracy and performance is a concern, Facebook Prophet may not be a good pick based on the result of my SFO passenger traffic prediction project. I recommend comparing the models like Holts-Winters or SARIMA learned in R with forecast package.

Summary of Facebook Prophet

Advantage

1. Follow SKlearn code

2. Easy syntax to make prediction

3. Available in both Python and R

4. Easy syntax to visualize in Plotly

Disadvantage

1. Only linear model and logistic growth model available

2. Hard to install

3. Columns names have to be ‘ds’ and ‘y’

Reference

Facebook Prophet documentation

https://facebook.github.io/prophet/docs/quick_start.html#python-api

Github link

https://github.com/jacquessham/sfotraffic