How I solved a multivariate time series traffic problem using Facebook Prophet
Tags: Data Science, Fbprophet, Machine Learning, Time Series Forecasting, Python

Whilst I am embarking upon my data science studies, I am endeavouring to learn as much as I can about time series analysis. I have previously written about univariate time series analysis using Facebook Prophet in the form of an Analytics Vidhya competition question covering the company, JetRail, with the link to it being found below:- https://medium.com/python-in-plain-english/how-i-solved-the-jetrail-time-series-problem-with-fb-prophet-441820924c29

In while the JetRail competition question covered the area of univariate time series analysis, I would like to delve a little deeper into this subject by discussing multivariate time series analysis. In order to illustrate how Facebook Prophet work with such data, I decided to use another competition question, this time from McKinsey Analytics, with the link being found below:- https://datahack.analyticsvidhya.com/contest/mckinsey-analytics-hackathon/#MySubmissions

Excerpts from the problem statement for this competition question are:-

“You are working with the government to transform your city into a smart city. The vision is to convert it into a digital and intelligent city to improve the efficiency of services for the citizens. One of the problems faced by the government is traffic.

The government wants to implement a robust traffic system for the city by being prepared for traffic peaks. They want to understand the traffic patterns of the four junctions of the city. Traffic patterns on holidays, as well as on various other occasions during the year, differ from normal working days.

To predict traffic patterns in each of these four junctions for the next 4 months.

The sensors on each of these junctions were collecting data at different times, hence you will see traffic data from different time periods. To add to the complexity, some of the junctions have provided limited or sparse data requiring thoughtfulness when creating future projections. Depending upon the historical data of 20 months, the government is looking to you to deliver accurate traffic projections for the coming four months. Your algorithm will become the foundation of a larger transformation to make your city smart and intelligent.”

In order to complete this competition question, the Python libraries need to be loaded onto the Jupyter notebook in use, mine being Google Colab.

The csv files to the problem also need to be imported into the program. I personally downloaded the files and saved them into a GitHub repository an anyone is welcome to use these files if they like. The link to this repository is found below:- https://github.com/TracyRenee61/Traffic-Forecasting

I had to prepare the data, with the first task being to create variables for the ID number on the test and train file because they will not be used during the forecast:

I then had to rename the columns because Facebook Profit is very specific in the column names to be predicted upon. “DateTime” is changed to “ds” and “Junction” is changed to “add1”. The target is generally called “y” in machine learning, so that is the name Prophet wants to see.

I then split the train dataset up for training and validation. In time series analysis, the split occurs on the time rather than an index number.

I then defined the model, in this instance being Facebook Prophet. Because it is on a multivariate dataset, add_regressor() needs to be implemented for each additional column. In this instance there was only one additional column. After defining the model, I then had to fit it and predict on the training data:-

I checked the error on this forecast, using mean_squared_error, and obtained 30.69:-

I also graphed the forecast to obtain a visual representation of the validation data as opposed to the training data:-

After predicting on the validation set, I predicted on the test set:-

Once the prediction had been carried out, I put the prediction and ID number on a dataframe. This dataframe was converted to a .csv file, which was saved and submitted to the Solution Checker in the competition page:

I achieved a 37.37 on the Solution Checker on my first attempt, which reveals I need to undertake some more work to improve the accuracy of this competition question. There are a few more things I can do with FB Prophet to try to decrease the error, so watch this space!

The program used in this blog post can be used in its entirety can be found in my GitHub account, the link provided below: https://github.com/TracyRenee61/Traffic-Forecasting/blob/main/TS_Traffic_Forecast_FB_Prophet.ipynb