I will start by reading in the data and assign it to the bike variable, so we can see what features we are going to work with.

library(readr)
library(ggplot2)
library(ggExtra)
library(RColorBrewer)
library(dplyr)

bike <- as.data.frame(read_csv("~/Desktop/bikeshare.csv"))
head(bike)
##              datetime season holiday workingday weather temp  atemp
## 1 2011-01-01 00:00:00      1       0          0       1 9.84 14.395
## 2 2011-01-01 01:00:00      1       0          0       1 9.02 13.635
## 3 2011-01-01 02:00:00      1       0          0       1 9.02 13.635
## 4 2011-01-01 03:00:00      1       0          0       1 9.84 14.395
## 5 2011-01-01 04:00:00      1       0          0       1 9.84 14.395
## 6 2011-01-01 05:00:00      1       0          0       2 9.84 12.880
##   humidity windspeed casual registered count
## 1       81    0.0000      3         13    16
## 2       80    0.0000      8         32    40
## 3       80    0.0000      5         27    32
## 4       75    0.0000      3         10    13
## 5       75    0.0000      0          1     1
## 6       75    6.0032      0          1     1

The dataset has the following features:

Before diving in, we need to have in mind the business problem - we want to predict the number of bike rentals per day. So, it is good practice to formulate a few hypothesis beforehand and think about what features could influence the demand of bikes:

Back to our dataset, it seems it is pretty cleaned already. But just to double check, are there any missing values and duplicates?

sum(is.na(bike))
## [1] 0
length(duplicated(bike)[duplicated(bike)=="TRUE"])
## [1] 0

It looks like we’re good to go! So, let’s investigate our hypothesis… is there a hourly trend? If there is, I believe it is different on weekdays vs weekends/holidays. Therefore, we should take that into consideration.

bike$hour <- sapply(bike$datetime,function(x){format(x,"%H")})
bike_by_hour <- bike %>% group_by(hour, workingday) %>% summarise(total = sum(count))
bike_by_hour$workingday <- ifelse(bike_by_hour$workingday==0,"Weekend/Holiday", "Workingday")
ggplot(data=bike_by_hour, aes(x=hour, y=total, group=1)) +
  geom_line(stat="identity", color="#37c9e1") + xlab("Hour") + facet_grid(~workingday) + theme_bw() + removeGrid()

As expected, on weekdays there are two peaks, representing commuting times. On weekends, demand is lower and smoother.

And how does it relate with temperature?

bike$day <- ifelse(bike$workingday==0,"Weekend/Holiday", "Workingday")
ggplot(filter(bike),aes(hour,count)) + geom_point(position=position_jitter(w=1, h=0),aes(color=temp),alpha=0.5) + scale_color_gradientn(colours = c('dark blue','blue','light blue','light green','yellow','orange','red')) + theme_bw() + removeGrid() + facet_grid(~day)

Typically, users ride their bikes when the temperature is mild. Especially on weekends. On working days, there are still a few users who like to commute by bike regardless of temperature.

Therefore, I it is likely that demand will also fluctuate depending on the season of the year.

ggplot(bike, aes(factor(season),count, fill=factor(season))) + geom_boxplot() + theme_bw() + removeGrid() + scale_fill_manual(breaks = c("1", "2", "3", "4"), values=c("springgreen3", "yellow2", "tan3", "dodgerblue3")) + xlab("Season") + labs(fill = "Season")

In the Summer and Fall demand is higher. I was expecting to see a stronger Spring though. It is even lower than the Winter? How come? It leads me to my hypothesis about time.

ggplot(bike, aes(datetime, count)) + geom_point(aes(color=factor(season)), alpha=0.5) + scale_color_manual(breaks = c("1", "2", "3", "4"), values=c("springgreen3", "yellow2", "tan3", "dodgerblue3")) + theme_bw() + removeGrid() + labs(fill = "Season")

The company starts its operations in the Spring 2011 and, as time passed by, it reached a more mature state by the end of the year, having collected more subscriptions in the Winter. And in the following year, the trend goes.

Therefore, we have confirmed two phenomena: * The data has seasonality, for winter and summer. * Bike rentals are increasing in general.

This may present a problem with using a linear regression model if the data is non-linear.

cor(bike[6:9])
##                  temp       atemp    humidity   windspeed
## temp       1.00000000  0.98494811 -0.06494877 -0.01785201
## atemp      0.98494811  1.00000000 -0.04353571 -0.05747300
## humidity  -0.06494877 -0.04353571  1.00000000 -0.31860699
## windspeed -0.01785201 -0.05747300 -0.31860699  1.00000000

Temp and atemp are highly correlated, so will use temp only to avoid multicollinearity.

library(caTools)
bike$hour <- as.numeric(bike$hour)
bike_vars <- select(bike, season, holiday, workingday, weather, temp, humidity, windspeed, hour, count)

sample <- sample.split(bike_vars$count, SplitRatio = 0.7)
train <- subset(bike_vars, sample == TRUE)
test <- subset(bike_vars, sample == FALSE)

lm_model <- lm(formula = count ~ . , data = bike_vars)
summary(lm_model)
## 
## Call:
## lm(formula = count ~ ., data = bike_vars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -324.61  -96.88  -31.01   55.27  688.83 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  46.91369    8.45147   5.551 2.91e-08 ***
## season       21.70333    1.35409  16.028  < 2e-16 ***
## holiday     -10.29914    8.79069  -1.172    0.241    
## workingday   -0.71781    3.14463  -0.228    0.819    
## weather      -3.20909    2.49731  -1.285    0.199    
## temp          7.01953    0.19135  36.684  < 2e-16 ***
## humidity     -2.21174    0.09083 -24.349  < 2e-16 ***
## windspeed     0.20271    0.18639   1.088    0.277    
## hour          7.61283    0.21688  35.102  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 147.8 on 10877 degrees of freedom
## Multiple R-squared:  0.3344, Adjusted R-squared:  0.3339 
## F-statistic:   683 on 8 and 10877 DF,  p-value: < 2.2e-16

We have quite a few variables which are not statistically significant. Hence, let’s see what variables our step() function suggest t improve our model.

lm_model2 <- step(lm_model)
## Start:  AIC=108785.3
## count ~ season + holiday + workingday + weather + temp + humidity + 
##     windspeed + hour
## 
##              Df Sum of Sq       RSS    AIC
## - workingday  1      1139 237748164 108783
## - windspeed   1     25854 237772879 108784
## - holiday     1     30003 237777028 108785
## - weather     1     36093 237783118 108785
## <none>                    237747025 108785
## - season      1   5615186 243362211 109037
## - humidity    1  12959175 250706200 109361
## - hour        1  26932319 264679344 109952
## - temp        1  29414497 267161522 110053
## 
## Step:  AIC=108783.4
## count ~ season + holiday + weather + temp + humidity + windspeed + 
##     hour
## 
##             Df Sum of Sq       RSS    AIC
## - windspeed  1     25767 237773931 108783
## - holiday    1     28963 237777126 108783
## - weather    1     36676 237784840 108783
## <none>                   237748164 108783
## - season     1   5615849 243364013 109036
## - humidity   1  12959835 250707998 109359
## - hour       1  26937850 264686014 109950
## - temp       1  29434096 267182260 110052
## 
## Step:  AIC=108782.5
## count ~ season + holiday + weather + temp + humidity + hour
## 
##            Df Sum of Sq       RSS    AIC
## - holiday   1     28244 237802175 108782
## - weather   1     28900 237802831 108782
## <none>                  237773931 108783
## - season    1   5590529 243364460 109034
## - humidity  1  14692463 252466394 109433
## - hour      1  27114603 264888534 109956
## - temp      1  29410448 267184379 110050
## 
## Step:  AIC=108781.8
## count ~ season + weather + temp + humidity + hour
## 
##            Df Sum of Sq       RSS    AIC
## - weather   1     28533 237830708 108781
## <none>                  237802175 108782
## - season    1   5571693 243373867 109032
## - humidity  1  14690634 252492809 109432
## - hour      1  27113838 264916013 109955
## - temp      1  29427218 267229393 110050
## 
## Step:  AIC=108781.1
## count ~ season + temp + humidity + hour
## 
##            Df Sum of Sq       RSS    AIC
## <none>                  237830708 108781
## - season    1   5659181 243489890 109035
## - humidity  1  18512650 256343359 109595
## - hour      1  27246797 265077505 109960
## - temp      1  29489309 267320018 110052
sml <- summary(lm_model2)

Based on AIC improvement, lmodel2 is the winner. We get a simpler model, which works with statistically significant features. However, it doesn’t really perform well either.

test$predictedCOUNT <- predict(lm_model2, newdata=test, type = "response")
cor(test$count,test$predictedCOUNT)
## [1] 0.5923588
mean(sml$residuals^2)
## [1] 21847.39
min_max_accuracy <- mean(apply(test[,9:10], 1, min) / apply(test[,9:10], 1, max))  

According to our R-squared, the model only explains around 33% of total variability. The correlation between predictions and actual values and the Mean Standard Error is too high. The Min/Max Accuracy is too low, suggesting our model’s prediction is off.

The above linear model is unable to take into account the seasonality and growth (towards the winter season) of our data. So, let’s try to predict bike rentals with a non-linear model, such as Decision Trees.

library(rpart)
tree <- rpart(count ~ . , data= train)
library(rpart.plot)
prp(tree)

test$treepreds <- predict(tree, test)
SSE = sum((test$treepreds - test$count)^2)
SST = sum((mean(test$count) - test$count)^2)
R2 = 1 - SSE/SST
R2
## [1] 0.6787733

Hour seems to be the most effective predictor with our new model and it looks like Decision Trees increased our R-Squared significantly. And one can beef up this model by running a Random Forest.

library(randomForest)
forest <- randomForest(count ~ .,   data=train)

test$randomF <- predict(forest,test)
SSE_RF = sum((test$randomF - test$count)^2)
SST = sum((mean(test$count) - test$count)^2)
R2_RF = 1 - SSE_RF/SST
R2_RF
## [1] 0.7886684
head(test)
##    season holiday workingday weather  temp humidity windspeed hour count
## 1       1       0          0       1  9.84       81    0.0000    0    16
## 13      1       0          0       1 17.22       77   19.0012   12    84
## 14      1       0          0       2 18.86       72   19.9995   13    94
## 15      1       0          0       2 18.86       72   19.0012   14   106
## 28      1       0          0       2 18.86       94   12.9980    3     6
## 30      1       0          0       3 17.22       77   19.9995    6     2
##    predictedCOUNT treepreds   randomF
## 1       -45.34861  32.61528  39.91053
## 13      106.78763 158.28135 181.08648
## 14      137.31535 158.28135 170.63367
## 15      144.91462 158.28135 181.94935
## 28       11.09955  32.61528  19.30356
## 30       61.19200  32.61528  32.08550

R-Squared improved even further with this Decision Tree on steroids model and seems to be the best one to predict future bike rentals.