Crista Gregg and Halid Kopanski 7/2/2021
The following analysis breaks down bicycle sharing usage based on data gathered for every recorded Monday in the years 2011 and 2012. The data was gathered from users of Capitol Bikeshare based in Washington DC. In total, the dataset contains 731 entries. For each entry, 16 variables were recorded. The following is the list of the 16 variables and a short description of each:
Variable | Description |
---|---|
instant | record index |
dteday | date |
season | season (winter, spring, summer, fall) |
yr | year (2011, 2012) |
mnth | month of the year |
holiday | whether that day is holiday (1) or not (0) |
weekday | day of the week |
workingday | if day is neither a weekend nor a holiday value is 1, otherwise is 0. |
weathersit | Description of weather conditions (see below) |
- | 1: Clear, Few clouds, Partly cloudy, Partly cloudy |
- | 2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist |
- | 3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds |
- | 4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog |
temp | Normalized temperature in Celsius. |
atemp | Normalized perceived temperature in Celsius. |
hum | Normalized humidity. |
windspeed | Normalized wind speed. |
casual | count of casual users |
registered | count of registered users |
cnt | sum of both casual and registered users |
Sources | Raw data and more information can be found here |
In addition to summary statistics, this report will also model bicycle users by linear regression, random forests, and boosting. The model will help determine anticipated number of users based on readily available data. To achieve this, the response variables are casual, registered, and cnt. The other variables, not including the date and instant columns, will be the predictors for models developed later in this report.
Here, we set up the data for the selected day of week and convert categorical variables to factors, and then split the data into a train and test set.
set.seed(1) #get the same splits every time
<- read_csv('day.csv')
bikes
<- function(x){
day_function <- x + 1
x switch(x,"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday")
}
<- function(x){
season_function #x <- as.character(x)
switch(x, "Spring",
"Summer",
"Fall",
"Winter")
}
<- bikes %>% select(everything()) %>%
bikes mutate(weekday = sapply(weekday, day_function),
season = sapply(season, season_function))
$season <- as.factor(bikes$season)
bikes$yr <- as.factor(bikes$yr)
bikeslevels(bikes$yr) <- c('2011','2012')
$mnth <- as.factor(bikes$mnth)
bikes$holiday <- as.factor(bikes$holiday)
bikes$weekday <- as.factor(bikes$weekday)
bikes$workingday <- as.factor(bikes$workingday)
bikes$weathersit <- as.factor(bikes$weathersit)
bikeslevels(bikes$weathersit) <- c('Clear to some clouds', 'Misty', 'Light snow or rain')
<- params$day_of_week
day
#filter bikes by day of week
<- filter(bikes, weekday == day)
bikes
#split data into train and test sets
<- sample(nrow(bikes), 0.7*nrow(bikes))
train_rows <- bikes[train_rows,] %>%
train select(-instant, -weekday, -casual, -registered, -dteday)
<- bikes[-train_rows,] %>%
test select(-instant, -weekday, -casual, -registered, -dteday)
Below shows the summary statistics of bike users: casual, registered, and total.
::kable(summary(bikes[,14:16])) knitr
casual | registered | cnt | |
---|---|---|---|
Min. : 2.0 | Min. : 20 | Min. : 22 | |
1st Qu.: 253.0 | 1st Qu.:2549 | 1st Qu.:3310 | |
Median : 690.0 | Median :3603 | Median :4359 | |
Mean : 674.1 | Mean :3664 | Mean :4338 | |
3rd Qu.: 904.0 | 3rd Qu.:4841 | 3rd Qu.:5875 | |
Max. :3065.0 | Max. :6435 | Max. :7525 |
The following table tells us the total number of rentals for each of the two years of collected data, as well as the average number of rentals per day.
%>%
bikes group_by(yr) %>%
summarise(total_rentals = sum(cnt), avg_rentals = round(mean(cnt))) %>%
::kable() knitr
yr | total_rentals | avg_rentals |
---|---|---|
2011 | 180221 | 3466 |
2012 | 275282 | 5194 |
Now we will look at the number of days with each type of weather by season. 1 represents ‘Clear to some clouds’, 2 represents ‘Misty’, and 3 represents ‘Light snow or rain’.
::kable(table(bikes$season, bikes$weathersit)) knitr
Clear to some clouds | Misty | Light snow or rain | |
---|---|---|---|
Fall | 18 | 8 | 0 |
Spring | 18 | 8 | 0 |
Summer | 15 | 12 | 0 |
Winter | 15 | 9 | 2 |
The following box plot shows us how many rentals we have for days that are sunny or partly cloudy, misty, or rainy/snowy. We may expect some differences in behavior between weekend days where less people might be inclined to ride their bikes for pleasure, versus weekdays when more people might brave moderately unpleasant weather to get to work.
ggplot(bikes, aes(factor(weathersit), cnt)) +
geom_boxplot() +
labs(x = 'Type of Weather', y = 'Number of Rental Bikes', title = 'Rental Bikes by Type of Weather') +
theme_minimal()
<- bikes %>%
weather_summary group_by(weathersit) %>%
summarise(total_rentals = sum(cnt), avg_rentals = round(mean(cnt)))
<- switch(which.min(weather_summary$avg_rentals),
weather_min "clear weather",
"misty weather",
"weather with light snow or rain")
According to the above box plot, it can be seen that weather with light snow or rain brings out the least amount of total users.
Below is a chart of the relationship between casual and registered bikers. We might expect a change in the slope if we look at different days of the week. Perhaps we see more registered bikers riding on the weekday but more casual users on the weekend.
ggplot(bikes, aes(casual, registered)) +
geom_point() +
geom_smooth(formula = 'y ~ x', method = 'lm') +
theme_minimal() +
labs(title = 'Registered versus Casual Renters')
Below we see a plot of the average daily number of bikers by month. We should expect to see more bikers in the spring and summer months, and the least in the winter.
<- bikes %>%
plot_mth group_by(mnth) %>%
summarize(avg_bikers = mean(cnt))
ggplot(plot_mth, aes(mnth, avg_bikers)) +
geom_line(group = 1, color = 'darkblue', size = 1.2) +
geom_point(size = 2) +
theme_minimal() +
labs(title='Average daily number of bikers by month', y = 'Average Daily Bikers', x = 'Month') +
scale_x_discrete(labels = month.abb)
<- month.name[which.max(plot_mth$avg_bikers)]
month_max <- month.name[which.min(plot_mth$avg_bikers)]
month_min
<- max(plot_mth$avg_bikers)
user_max <- min(plot_mth$avg_bikers)
user_min
<- rep(0, 11)
changes <- rep("x", 11)
diff_mth
for (i in 2:12){
- 1] <- paste(month.name[i - 1], "to", month.name[i])
diff_mth[i - 1] <- round(plot_mth$avg_bikers[i] - plot_mth$avg_bikers[i - 1])
changes[i
}
<- as_tibble(cbind(diff_mth, changes)) diff_tab_mth
According to the graph, July has the highest number of users with a value of 5792. The month with the lowest number of users is January with an average of 1927.
The largest decrease in month to month users was November to December with an average change of -1161.
The largest increase in month to month users was March to April with an average change of 970.
We would like to see what effect public holidays have on the types of bicycle users on average for a given day. In this case, Monday data shows the following relationships:
%>% ggplot(aes(x = as.factor(workingday), y = casual)) + geom_boxplot() +
bikes labs(title = paste("Casual Users on", params$day_of_week)) +
xlab("") +
ylab("Casual Users") +
scale_x_discrete(labels = c('Public Holiday', 'Workday')) +
theme_minimal()
%>% ggplot(aes(x = as.factor(workingday), y = registered)) + geom_boxplot() +
bikes labs(title = paste("Registered Users on", params$day_of_week)) +
xlab("") +
ylab("Registered Users") +
scale_x_discrete(labels = c('Public Holiday', 'Workday')) +
theme_minimal()
Temperature and humidity have an effect on the number of users on a given day.
First, normalized temperature data (both actual temperature and perceived):
<- bikes %>% select(cnt, temp, atemp) %>%
bike_temp gather(key = type, value = temp_norm, temp, atemp, factor_key = FALSE)
ggplot(bike_temp, aes(x = temp_norm, y = cnt, col = type, shape = type)) +
geom_point() + geom_smooth(formula = 'y ~ x', method = 'loess') +
scale_color_discrete(name = "Temp Type", labels = c("Perceived", "Actual")) +
scale_shape_discrete(name = "Temp Type", labels = c("Perceived", "Actual")) +
labs(title = paste("Temperature on", params$day_of_week, "(Actual and Perceived)")) +
xlab("Normalized Temperatures") +
ylab("Total Users") +
theme_minimal()
Next the effect of humidity:
%>% ggplot(aes(x = hum, y = cnt)) + geom_point() + geom_smooth(formula = 'y ~ x', method = 'loess') +
bikeslabs(title = paste("Humidity versus Total Users on", params$day_of_week)) +
xlab("Humidity (normalized)") +
ylab("Total Number of Users") +
theme_minimal()
Here we are checking the correlation between the numeric predictors in the data.
::kable(round(cor(bikes[ , c(11:16)]), 3)) knitr
atemp | hum | windspeed | casual | registered | cnt | |
---|---|---|---|---|---|---|
atemp | 1.000 | 0.218 | -0.059 | 0.712 | 0.560 | 0.666 |
hum | 0.218 | 1.000 | -0.418 | 0.035 | -0.008 | 0.003 |
windspeed | -0.059 | -0.418 | 1.000 | -0.081 | -0.171 | -0.166 |
casual | 0.712 | 0.035 | -0.081 | 1.000 | 0.477 | 0.676 |
registered | 0.560 | -0.008 | -0.171 | 0.477 | 1.000 | 0.970 |
cnt | 0.666 | 0.003 | -0.166 | 0.676 | 0.970 | 1.000 |
corrplot(cor(bikes[ , c(11:16)]), method = "circle")
Now, we will fit two linear regression model, a random forest model, and a boosting model. We will use cross-validation to select the best tuning parameters for the ensemble based methods, and then compare all four models using the test MSE.
Linear regression is one of the most common methods for modeling. It looks at a set of predictors and estimates what will happen to the response if one of the predictors or a combination of predictors change. This model is highly interpretable, as it shows us the effect of each individual predictor as well as interactions. We can see if the change in the response goes up or down and in what quantity. The model is chosen by minimizing the squares of the distances between the estimated value and the actual value in the testing set. Below we fit two different linear regression models.
The first model will have a subset of predictors chosen by stepwise selection. Once we have chosen an interesting set of predictors, we will use cross-validation to determine the RMSE and R2.
<- lm(cnt ~ ., data = train[ , c(1:3, 6:11)])
lm_fit_select <- step(lm_fit_select) model
## Start: AIC=967.17
## cnt ~ season + yr + mnth + weathersit + temp + atemp + hum +
## windspeed
##
## Df Sum of Sq RSS AIC
## - mnth 11 5292354 27963666 960.48
## <none> 22671312 967.17
## - temp 1 858022 23529335 967.88
## - windspeed 1 1221154 23892467 969.00
## - atemp 1 1784997 24456309 970.70
## - hum 1 2576612 25247924 973.03
## - season 3 10321627 32992940 988.56
## - weathersit 2 14453386 37124699 999.17
## - yr 1 41256750 63928062 1040.85
##
## Step: AIC=960.48
## cnt ~ season + yr + weathersit + temp + atemp + hum + windspeed
##
## Df Sum of Sq RSS AIC
## - temp 1 604910 28568576 960.05
## <none> 27963666 960.48
## - windspeed 1 1677270 29640936 962.74
## - atemp 1 1751823 29715489 962.92
## - hum 1 4956187 32919853 970.40
## - weathersit 2 16072632 44036298 989.63
## - season 3 23525654 51489320 999.05
## - yr 1 47288538 75252204 1030.75
##
## Step: AIC=960.05
## cnt ~ season + yr + weathersit + atemp + hum + windspeed
##
## Df Sum of Sq RSS AIC
## <none> 28568576 960.05
## - windspeed 1 2166378 30734954 963.38
## - hum 1 4518507 33087083 968.77
## - weathersit 2 15656221 44224797 987.95
## - atemp 1 16967285 45535861 992.08
## - season 3 24905739 53474315 999.81
## - yr 1 49874459 78443035 1031.78
<- names(model$model)
variables #variables we will use for our model variables
## [1] "cnt" "season" "yr" "weathersit" "atemp" "hum"
## [7] "windspeed"
set.seed(10)
<- train(cnt ~ ., data = train[variables], method = 'lm',
lm.fit preProcess = c('center', 'scale'),
trControl = trainControl(method = 'cv', number = 10))
lm.fit
## Linear Regression
##
## 73 samples
## 6 predictor
##
## Pre-processing: centered (9), scaled (9)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 65, 65, 66, 65, 66, 66, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 795.377 0.8202952 595.303
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
Our first linear model has an RMSE of 795.38.
Adding interactions to the terms included in the first model.
set.seed(10)
<- train(cnt ~ . + .*., data = train[variables], method = 'lm',
lm.fit1 preProcess = c('center', 'scale'),
trControl = trainControl(method = 'cv', number = 10))
lm.fit1
## Linear Regression
##
## 73 samples
## 6 predictor
##
## Pre-processing: centered (41), scaled (41)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 65, 65, 66, 65, 66, 66, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 1054.246 0.7548595 725.5586
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
The RMSE value of the model changed to 1054.25.
Ensemble trees methods come in many types and are very versatile when it comes to regression or classification. For the following, we will be using the two most common and well known methods: Random Forests (a form of bagging) and Boosting. Both these tree based methods involve optimization during the development process. In the case of random forests, the optimization involves varying the number of predictors used. This is done to mitigate the effects of one or more predictors from overshadowing other predictors. Boosting is a method where the final model is developed through an iterative combination of weaker models where each iteration builds upon the last. While both methods are very flexible and tend to process good results, the models themselves are not as interpretable as linear regression. We normally just analyze the output of the models.
Below is the result of training with the random forest method. This method uses a different subset of predictors for each tree and averages the results across many trees, selected by bootstrapping. By reducing the number of predictors considered in each tree, we may be able to reduce the correlation between trees to improve our results. In the training model below, we vary the number of predictors used in each tree.
<- train(cnt ~ ., data = train, method = 'rf',
rf_fit preProcess = c('center', 'scale'),
tuneGrid = data.frame(mtry = 1:10))
rf_fit
## Random Forest
##
## 73 samples
## 10 predictors
##
## Pre-processing: centered (23), scaled (23)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 73, 73, 73, 73, 73, 73, ...
## Resampling results across tuning parameters:
##
## mtry RMSE Rsquared MAE
## 1 1411.1480 0.5677018 1174.7260
## 2 1186.9712 0.6377193 986.3843
## 3 1094.4024 0.6753772 879.8229
## 4 1046.2410 0.6982560 821.1445
## 5 1011.3763 0.7123635 777.2291
## 6 994.8440 0.7167495 748.5663
## 7 980.1268 0.7242386 728.5467
## 8 969.6457 0.7266090 710.0647
## 9 967.1118 0.7252636 698.3667
## 10 959.1956 0.7273668 687.2306
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was mtry = 10.
The best model uses 10 predictors. This gives an RMSE of 959.2.
The following are the results of Boosting model development using the provided bike data.
<- trainControl(method = "repeatedcv",
trctrl number = 10,
repeats = 3)
set.seed(2020)
<- expand.grid(n.trees = c(20, 100, 500),
boost_grid interaction.depth = c(1, 3, 5),
shrinkage = c(0.1, 0.01, 0.001),
n.minobsinnode = 10)
<- train(cnt ~ .,
boost_fit data = train,
method = "gbm",
verbose = F, #suppresses excessive printing while model is training
trControl = trctrl,
tuneGrid = data.frame(boost_grid))
A total of 27 models were evaluated. Each differing by the combination of boosting parameters. The results are show below:
print(boost_fit)
## Stochastic Gradient Boosting
##
## 73 samples
## 10 predictors
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 66, 65, 65, 65, 65, 65, ...
## Resampling results across tuning parameters:
##
## shrinkage interaction.depth n.trees RMSE Rsquared MAE
## 0.001 1 20 1738.0909 0.5099345 1393.9075
## 0.001 1 100 1697.2178 0.5213435 1365.3508
## 0.001 1 500 1538.2159 0.5789252 1245.2809
## 0.001 3 20 1735.8144 0.6076083 1391.0560
## 0.001 3 100 1684.6202 0.6182622 1351.6036
## 0.001 3 500 1486.8582 0.6470900 1187.1183
## 0.001 5 20 1735.9764 0.6187760 1391.1311
## 0.001 5 100 1684.7865 0.6233656 1351.4338
## 0.001 5 500 1487.9485 0.6494165 1187.7423
## 0.010 1 20 1649.1287 0.5327076 1331.3932
## 0.010 1 100 1398.2924 0.6163837 1128.9400
## 0.010 1 500 1028.4594 0.7129727 807.9488
## 0.010 3 20 1628.2342 0.6385100 1307.2839
## 0.010 3 100 1320.7608 0.6691349 1039.9535
## 0.010 3 500 962.2232 0.7357640 735.5945
## 0.010 5 20 1630.8654 0.6228646 1309.6462
## 0.010 5 100 1332.5015 0.6626448 1050.1001
## 0.010 5 500 971.9830 0.7323832 744.7821
## 0.100 1 20 1240.5887 0.6355194 1007.1107
## 0.100 1 100 983.5621 0.7345245 755.5325
## 0.100 1 500 952.6332 0.7578123 731.4043
## 0.100 3 20 1139.2996 0.6719673 890.3783
## 0.100 3 100 932.5326 0.7459887 698.0666
## 0.100 3 500 896.4063 0.7807940 659.3739
## 0.100 5 20 1145.8177 0.6871101 901.0546
## 0.100 5 100 922.2325 0.7542120 691.7070
## 0.100 5 500 884.9143 0.7918749 644.3137
##
## Tuning parameter 'n.minobsinnode' was held constant at a value of 10
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were n.trees = 500, interaction.depth = 5, shrinkage
## = 0.1 and n.minobsinnode = 10.
<- as_tibble(boost_fit$results[,c(1,2,4:6)]) results_tab
The attributes of the best model is shown here.
<- which.min(results_tab$RMSE)
boost_min
::kable(results_tab[boost_min,], digits = 2) knitr
shrinkage | interaction.depth | n.trees | RMSE | Rsquared |
---|---|---|---|---|
0.1 | 5 | 500 | 884.91 | 0.79 |
Here we compare the 4 models developed earlier. Each model was applied to a test set and the results were then used to calculate MSE. Below are the results.
<- predict(lm.fit, newdata = test)
lm_pred <- predict(lm.fit1, newdata = test)
lm_pred1 <- predict(rf_fit, newdata = test)
rf_pred <- predict(boost_fit, newdata = test)
boost_pred
<- as_tibble(cbind(lm_pred, lm_pred1, rf_pred, boost_pred))
prediction_values
<- mean((lm_pred - test$cnt)^2)
lm_MSE <- mean((lm_pred1 - test$cnt)^2)
lm_MSE1 <- mean((rf_pred - test$cnt)^2)
rf_MSE <- mean((boost_pred - test$cnt)^2)
boost_MSE
<- data.frame('Linear Model 1' = lm_MSE,
comp 'Linear Model 2' = lm_MSE1,
'Random Forest Model' = rf_MSE,
'Boosting Model' = boost_MSE)
::kable(t(comp), col.names = "MSE") knitr
MSE | |
---|---|
Linear.Model.1 | 991231.5 |
Linear.Model.2 | 2038346.2 |
Random.Forest.Model | 828946.9 |
Boosting.Model | 509294.0 |
It was found that Boosting.Model achieves the lowest test MSE of 5.0929405^{5} for Monday data.
Below is a graph of the Actual vs Predicted results:
<- (which.min(t(comp)))
index_val
<- as_tibble(cbind("preds" = prediction_values[[index_val]], "actual" = test$cnt))
results_plot
ggplot(data = results_plot, aes(preds, actual)) + geom_point() +
labs(x = paste(names(which.min(comp)), "Predictions"), y = "Actual Values",
title = paste(names(which.min(comp)), "Actual vs Predicted Values")) +
geom_abline(slope = 1, intercept = 0, col = 'red')