Analysis and prediction of Citi Bike usage in the unpredictable 2020
By: Stanislav Rogozhin
Tags: Open Data, Data Science, Bike Sharing, Citi Bike, Cycling

Analysis and prediction of Citi Bike usage in the unpredictable 2020

Introduction

New York City’s Citi Bike is the largest bike-sharing system in the US. Since opening in 2013 with 332 stations and 6,000 bikes, the system has grown to more than 1,000 stations and 17,000 bikes by 2020. Amidst the ongoing COVID-19 pandemic Citi Bike saw a massive expansion in 2020 and set an all-time daily ridership record with 103,139 trips on September 12, 2020.

In this article I explore bike-sharing data, build a model to predict the number of daily trips and see how it performs against actual numbers. In part one I go through feature engineering and all the steps and iterations of building a predictive model. In part two I use this model to create a prediction for 2020 and compare it to actual trip numbers to see how bike-sharing has changed in the “new normal”.

Data preparation and exploratory data analysis

Citi Bike provides extensive data on trips, listing start and end times, trip duration, stations, age, gender and user type (subscriber or short-term customer) for each trip. Data is provided as lists of trips. I used this script to parse the monthly files and count the number of trips for each day. The parsed data is available here. Let’s start with some exploratory data analysis and plot the number of daily trips for all years.

Citi Bike number of daily trips 2013–2020. Image by Author.

The number of bike-sharing trips is a typical example of a time series. It has the following components:

Trend: overall year-over-year growth

Seasonality: users make more trips in the summer months and less in the winter.

Cycles: weekly cycle where more trips happen on weekdays and less on weekends

Average number of Citi Bike daily trips in 2013–2019 split by year, month and day of week. Image by Author.

This plot only shows data for 2013–2019 since the usage pattern turns out to be quite different in 2020 — more on that later. Let’s start with these time-series features to build a predictive model.

Feature engineering and model building

To build any model you need training data to train the model and test data with some metrics to evaluate the model’s performance. I’ll be using trip data from 2013 through 2018 as the training set and 2019 as the test set. The coefficient of determination (R² score) and mean adjusted percentage error (MAPE) will be the quality metrics.

Calendar features

I transform dates to numerical, cyclical and categorical features as follows:

Year: this accounts for the yearly upward trend

Day of year with cyclical encoding (sine and cosine transformation) to capture the yearly cycle.

Month as a categorical feature to identify monthly variations.

Day of week with cyclical encoding to capture the weekly cycle

Day of week as a categorical feature to account for weekend/weekday differences

Holidays: binary (0/1) feature. Data shows that fewer trips happen on and around major holidays.

I start with a simple linear regression on these features. Here’s a chart of the prediction.

Actual and predicted number of daily trips in 2019. Linear regression using calendar features. Image by Author.

A simple linear model provides only a very approximate fit with a test R² score of 0.600, so let’s try a more complex approach. Using polynomial regression with a degree of 2 provides a better result. It can be further improved through regularization (ridge regression). Regularization constrains the model’s coefficients and prevents overfitting to the training data. This leads to a lower score on the training set but a higher score on the test set. To select the best regularization parameter I’m using cross-validation with a time-series split.

Prediction scores using calendar features and different models. Image by Author.

Here is the plot of the best prediction.

Actual and predicted number of daily trips in 2019. Polynomial regression with regularization using calendar features. Image by Author.

One can see two characteristics of actual data which the model failed to predict:

Significant drops or increases in usage on some days

A surge in September 2019

This is further confirmed by the model metrics.

Polynomial regression with regularization using calendar features. Distribution of residuals and actual vs. predicted values. Image by Author.

The left plot shows the distribution of residuals (the difference between actual and predicted values) overlaid with a normal distribution curve. The residuals are not exactly normally distributed which could mean a systematic error in the model.

On the right chart predicted values are plotted against actual values. Each dot represents a single day in the dataset, where the x-axis is the actual number of trips on that day, and the y-axis is the model-predicted number of trips. This plot also looks skewed: the model overshoots on lower and undershoots on higher numbers.

Let’s try some feature engineering to fix these irregularities.

Weather

Let’s have a look at May 2019, for example. On some days, e.g. the 12th of May the predicted value overshoots the actual number of trips by almost a factor of five.

Number of trips and weather in May 2019. Image by Author.

Weather provides an easy explanation. On cold and rainy days ridership drops significantly. Let’s add weather to the parameters.

Daily weather can be obtained from the National Climatic Data Center. I’ve obtained data for 2013–2020. The following data is available:

Wind speed

AWND: average wind speed

WDF*: direction of fastest 1, 2, 5-minute wind

WSF*: fastest 1, 2, 5-minute wind

Temperature

TMAX: maximum, °F

TMIN: minimum, °F

Precipitation

PRCP: precipitation, inches

SNOW: snowfall, inches

SNWD: snow depth, inches

WT*: weather types (fog, thunder, smoke etc.)

Let’s start with some exploratory analysis. I’ve converted the number of trips to percentage of the maximum for the corresponding year and plotted it against weather factors.

Number of trips and weather factors. Image by Author.

It’s easy to see that the number of trips increases with temperature, peaking when Tₘₐₓ reaches 80 °F (27 °C) and somewhat dropping on the hottest days. The number of trips also drops sharply on days with at least some precipitation or snow.

Let’s try adding various combinations of these features to the model.

Prediction scores with different weather features. Image by Author.

As shown above the combination of these features appears to work best:

Maximum temperature

Minimum temperature

Log of precipitation, snowfall and snow depth

Log values of precipitation and snowfall yield better results. Logarithmic transformation helps capture the significant drop in trips when there is at least some precipitation or snow.

Weather types 01 (fog) and 08 (smoke/haze) do show some correlation with the number of trips but don’t improve the model’s performance. Wind speed doesn’t appear to improve the prediction either.

Here is a plot of the predictions with weather taken into account.

Actual and predicted number of daily trips in 2019. Polynomial regression with regularization using calendar and weather features. Image by Author.

Actual and predicted number of daily trips in 2019 split by month and day of week. Polynomial regression with regularization using calendar and weather features. Image by Author.

The improved model has better metrics and follows the actual data more closely. The model can predict surges and drops in ridership due to weather.

However the predicted values are now consistently higher than the actual numbers. Let’s look at other features to improve the model further.

System size

Citi Bike opened in 2013 and saw major expansions in 2015, 2016, 2017, 2019 and 2020. The most precise data on the monthly number of bikes and stations can be obtained from the Citi Bike Monthly Operating Reports. I’ve taken the number of stations, average active bikes and total fleet at the end of each month and combined them in this table.

Here’s a chart of active stations and average active bikes for each month.

Monthly number of stations (left scale) and average active bikes (right scale). Image by Author.

The number of active bikes drops in the winter months since Citi Bike is allowed to downsize their fleet to 70% due to adverse weather. Stations mostly stay open year round.

After experimenting with these features we can see that the number of stations seems to improve the model the best.

Prediction scores with different system features. Image by Author.

Here is the final prediction with calendar, weather and system features.

Actual and predicted number of daily trips in 2019. Polynomial regression with regularization using calendar, weather and system size features. Image by Author.

Actual and predicted number of daily trips in 2019 split by month and day of week. Polynomial regression with regularization using calendar, weather and system size features. Image by Author.

The predicted totals are closer to the actual numbers now.

Polynomial regression with regularization using calendar, weather and system size features. Distribution of residuals and actual vs. predicted values. Image by Author.

There is still some deviation, but the distribution of residuals is closer to normal and predicted values follow actuals more accurately. Let’s now use this model to make the best possible prediction for 2020.

Prediction 2020

Now I train the model on data for 2013–2019 and make a prediction for 2020. Here is the prediction plotted against actual ridership numbers and data from 2019 for comparison. I’ve removed the scatterplots to make the chart more readable. I’ve also highlighted the lockdown (a stay-at-home order took effect on March 22) and reopening (phase 1 of reopening began on June 8 and phase 4 on July 22) periods.

Actual and predicted number of daily trips (14-day average) in 2020 with 2019 for comparison. Image by Author.

The number of trips follows the prediction in January and February. Ridership drops in March when lockdown goes into effect but starts to recover quickly in May and catches up to the prediction in July-August after the restrictions are eased. In September, October, November and December the actual number of trips exceeds the prediction. Let’s see how the usage pattern has changed in August and onwards.

Actual and predicted number of daily trips in August-December 2020 split by month and day of week. Image by Author.

Some notable differences between actual and predicted data:

The actual ridership in September, October, November and December exceeds both the prediction and 2019 numbers

The number of trips on Saturday and Sunday is significantly higher than predicted; Saturday is the busiest day now

On weekdays, actual numbers are closer to prediction and haven’t increased that much.

There can be several reasons for these differences. First, remote work means fewer people commuting daily and thus fewer trips on weekdays. As observed in Shifting Gears: Citi Bike Recovers Faster Than Subway bike-share users make more trips on weekday evenings and weekends, while the usual weekday commuter peaks are nearly eliminated. People more often use bike-share for leisure, recreation and other activities instead of daily work trips.

At the same time people are shifting from crowded and presumably unsafe public transit to biking. As noted in the same article, subway ridership remained low in the reopening phase while bike-share use quickly returned to pre-pandemic levels. This could explain a general increase in the number of trips and better ridership numbers in September, October and November since people continue to rely more on bikes than transit.

Conclusions

This is only a simple model and an exercise in feature engineering and model building. There are specialized tools for time-series analysis like fbprophet that could provide a better fit. Nevertheless it achieves a coefficient of determination of 0.894 and provides a close prediction for 2019.

Bike-share usage has an overall upward trend, yearly seasonality and weekly cycles; weather and system size are also important factors determining the number of trips. Better data could help make a better prediction. E.g. heavy rain in peak hours affects ridership dramatically and having detailed hourly weather data would help create a more accurate model.

Walking and cycling have become more important in 2020. The pattern of bike-share use has changed: more people rely on bike-sharing for recreation, shopping and other trips. Even without the daily work commutes overall ridership has increased compared to 2019 and my prediction. It will be interesting to see whether this pattern stays the same when people begin commuting again.

One can only welcome Citi Bike’s expansion in 2020. But perhaps even more important is creating protected infrastructure like new footpaths and temporary and permanent bike lanes. New York City’s Open Streets and similar programs in other cities around the world are crucial in order to accommodate this increased demand and create a safe environment for pedestrians and cyclists.

The source code and raw data are available on Github.

Update February 2021

I’ve added data for December 2020. December 2020 was notable for heavy snow which even caused Citi Bike to completely shut down for an entire day on the 17th. Since the model was not trained on such high snow levels it started to predict negative ridership on some days. To correct this I have replaced all negative predicted values with zeroes.