Crista Gregg and Halid Kopanski 7/2/2021
The following analysis breaks down bicycle sharing usage based on data gathered for every recorded Thursday 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. : 15.0 | Min. : 416 | Min. : 431 | |
1st Qu.: 246.2 | 1st Qu.:3063 | 1st Qu.:3271 | |
Median : 570.0 | Median :3950 | Median :4721 | |
Mean : 591.0 | Mean :4076 | Mean :4667 | |
3rd Qu.: 878.2 | 3rd Qu.:5524 | 3rd Qu.:6286 | |
Max. :1405.0 | Max. :6781 | Max. :7804 |
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 | 174552 | 3357 |
2012 | 310843 | 5978 |
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 | 20 | 7 | 1 |
Spring | 16 | 8 | 1 |
Summer | 18 | 7 | 1 |
Winter | 13 | 12 | 0 |
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, August has the highest number of users with a value of 6038. The month with the lowest number of users is January with an average of 2513.
The largest decrease in month to month users was October to November with an average change of -1543.
The largest increase in month to month users was April to May with an average change of 1129.
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, Thursday 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.149 | -0.119 | 0.733 | 0.553 | 0.609 |
hum | 0.149 | 1.000 | -0.310 | -0.057 | 0.007 | -0.005 |
windspeed | -0.119 | -0.310 | 1.000 | -0.172 | -0.177 | -0.183 |
casual | 0.733 | -0.057 | -0.172 | 1.000 | 0.757 | 0.833 |
registered | 0.553 | 0.007 | -0.177 | 0.757 | 1.000 | 0.992 |
cnt | 0.609 | -0.005 | -0.183 | 0.833 | 0.992 | 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=949.71
## cnt ~ season + yr + mnth + weathersit + temp + atemp + hum +
## windspeed
##
## Df Sum of Sq RSS AIC
## - temp 1 2803 20917299 947.72
## - atemp 1 52294 20966790 947.89
## <none> 20914496 949.71
## - hum 1 880367 21794863 950.68
## - mnth 11 8869335 29783831 953.16
## - season 3 3224091 24138588 954.03
## - windspeed 1 2497331 23411828 955.83
## - weathersit 2 4447899 25362395 959.59
## - yr 1 71470684 92385180 1054.67
##
## Step: AIC=947.72
## cnt ~ season + yr + mnth + weathersit + atemp + hum + windspeed
##
## Df Sum of Sq RSS AIC
## <none> 20917299 947.72
## - hum 1 891241 21808540 948.72
## - atemp 1 1384732 22302031 950.33
## - mnth 11 9203097 30120396 951.97
## - season 3 3261984 24179284 952.15
## - windspeed 1 2521646 23438945 953.91
## - weathersit 2 4709139 25626438 958.34
## - yr 1 72029922 92947222 1053.10
<- names(model$model)
variables #variables we will use for our model variables
## [1] "cnt" "season" "yr" "mnth" "weathersit" "atemp"
## [7] "hum" "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
##
## 72 samples
## 7 predictor
##
## Pre-processing: centered (20), scaled (20)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 64, 64, 65, 64, 65, 66, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 777.7939 0.8312946 630.1502
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
Our first linear model has an RMSE of 777.79.
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
##
## 72 samples
## 7 predictor
##
## Pre-processing: centered (151), scaled (151)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 64, 64, 65, 64, 65, 66, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 22551.26 0.1764429 13936.99
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
The RMSE value of the model changed to 2.255126^{4}.
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
##
## 72 samples
## 10 predictors
##
## Pre-processing: centered (23), scaled (23)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 72, 72, 72, 72, 72, 72, ...
## Resampling results across tuning parameters:
##
## mtry RMSE Rsquared MAE
## 1 1536.5087 0.6236545 1321.5472
## 2 1225.5796 0.7228303 1046.2283
## 3 1065.7195 0.7753845 898.6175
## 4 960.5088 0.8127159 799.8696
## 5 901.4282 0.8277360 744.6310
## 6 860.5639 0.8352925 707.8102
## 7 835.0243 0.8374360 687.9077
## 8 817.0103 0.8391227 676.8263
## 9 807.0021 0.8388095 670.6317
## 10 798.3287 0.8369205 664.9287
##
## 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 798.33.
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
##
## 72 samples
## 10 predictors
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 66, 64, 64, 64, 64, 64, ...
## Resampling results across tuning parameters:
##
## shrinkage interaction.depth n.trees RMSE Rsquared MAE
## 0.001 1 20 1803.7727 0.6192964 1534.6698
## 0.001 1 100 1742.8711 0.6346525 1473.6384
## 0.001 1 500 1508.6334 0.7106600 1268.8846
## 0.001 3 20 1802.9551 0.6223451 1534.1533
## 0.001 3 100 1739.4533 0.6448948 1470.6783
## 0.001 3 500 1483.5103 0.7215642 1247.9871
## 0.001 5 20 1803.0211 0.6302004 1534.0942
## 0.001 5 100 1739.3401 0.6432298 1470.6524
## 0.001 5 500 1484.1420 0.7167665 1248.0472
## 0.010 1 20 1674.8002 0.6618092 1410.3764
## 0.010 1 100 1308.5856 0.7447496 1098.2456
## 0.010 1 500 890.2355 0.8099954 748.9062
## 0.010 3 20 1664.0521 0.6612188 1403.4356
## 0.010 3 100 1253.9133 0.7654264 1051.7556
## 0.010 3 500 846.6884 0.8229541 706.1216
## 0.010 5 20 1669.1199 0.6666112 1407.5024
## 0.010 5 100 1247.1704 0.7716133 1046.4042
## 0.010 5 500 848.5163 0.8232512 709.0930
## 0.100 1 20 1079.9195 0.7586377 903.0052
## 0.100 1 100 851.1249 0.8240164 687.4616
## 0.100 1 500 825.6938 0.8306110 664.1847
## 0.100 3 20 1029.5161 0.7643701 875.5980
## 0.100 3 100 833.2091 0.8202480 676.6808
## 0.100 3 500 819.8197 0.8300415 661.8549
## 0.100 5 20 1017.7244 0.7798504 866.8972
## 0.100 5 100 806.6355 0.8363901 652.2746
## 0.100 5 500 790.5919 0.8439755 634.7244
##
## 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 | 790.59 | 0.84 |
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 | 1.161463e+06 |
Linear.Model.2 | 3.508864e+11 |
Random.Forest.Model | 9.069208e+05 |
Boosting.Model | 9.997111e+05 |
It was found that Random.Forest.Model achieves the lowest test MSE of 9.0692082^{5} for Thursday 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')