Project Workflow

Estimate a regression model using real data on all the Blue Bike trips that took place in February 2020 to explains at least ten percent of the variation in trip duration using as few explanatory variables as possible. For each variable that your model includes, explain why it might have some sort of relationship with trip duration.

Extract Data from Blue Bikes trips

library(here)
library(tidyverse)
library(knitr)
library(dplyr)

full_data <- here("bike-data.csv") |>
  read_csv()

head(full_data) |>
  kable()
tripduration usertype age gender start_day day_of_week day_type city max_temp avg_temp min_temp max_wind avg_wind min_wind total_precip tract_hu_per_sq_km
108 Subscriber 132 female 29 Saturday Weekend Cambridge 34 28.8 22 18 12.4 7 0 3463.883
1611 Subscriber 131 male 23 Sunday Weekend Somerville 57 44.8 35 15 9.0 6 0 3509.244
2091 Subscriber 131 male 23 Sunday Weekend Somerville 57 44.8 35 15 9.0 6 0 3509.244
1758 Subscriber 131 male 23 Sunday Weekend Boston 57 44.8 35 15 9.0 6 0 1457.802
876 Subscriber 131 male 24 Monday Weekday Somerville 64 49.0 36 17 9.0 0 0 3509.244
1015 Subscriber 131 male 24 Monday Weekday Cambridge 64 49.0 36 17 9.0 0 0 2479.800

0. Data Preprocessing

# Sample data
full_data <- full_data |>

# Exp the dependent variable tripduration
mutate(log_tripduration = log(tripduration))

# Function to categorize age into groups
categorize_age <- function(age) {
  ifelse(age >= 11 & age <= 26, "GenZ",
    ifelse(age >= 27 & age <= 42, "Millennials",
      ifelse(age >= 43 & age <= 58, "GenX",
        ifelse(age >= 59 & age <= 68, "Boomers II",
          ifelse(age >= 69 & age <= 132, "Others", NA)
        )
      )
    )
  )
}

# Apply the function to create a new variable for age groups
full_data$age_group <- categorize_age(full_data$age)

# Transform categorical variables (One-Hot Encoding)
full_data <- full_data |>
  mutate(subscriber = ifelse(usertype == "Subscriber", 1, 0),
         customer = ifelse(usertype == "Customer", 1, 0),
         male = ifelse(gender == "male", 1, 0),
         female = ifelse(gender == "female", 1, 0),
         unspecified = ifelse(gender == "unspecified", 1, 0),
         weekend = ifelse(day_type == "Weekend", 1, 0),
         weekday = ifelse(day_type == "Weekday", 1, 0),
         holiday = ifelse(day_type == "Holiday", 1, 0),
         Boston = ifelse(city == "Boston", 1, 0),
         Brookline = ifelse(city == "Brookline", 1, 0),
         Cambridge = ifelse(city == "Cambridge", 1, 0),
         Everett = ifelse(city == "Everett", 1, 0),
         Quincy = ifelse(city == "Quincy", 1, 0),
         Somerville = ifelse(city == "Somerville", 1, 0),
         Monday = ifelse(day_of_week == "Monday", 1, 0),
         Tuesday = ifelse(day_of_week == "Tuesday", 1, 0),
         Wednesday = ifelse(day_of_week == "Wednesday", 1, 0),
         Thursday = ifelse(day_of_week == "Thursday", 1, 0),
         Friday = ifelse(day_of_week == "Friday", 1, 0),
         Saturday = ifelse(day_of_week == "Saturday", 1, 0),
         Sunday = ifelse(day_of_week == "Sunday", 1, 0)
)


# View the modified dataset with the new 'age_group' variable
head(data)
##                                                                             
## 1 function (..., list = character(), package = NULL, lib.loc = NULL,        
## 2     verbose = getOption("verbose"), envir = .GlobalEnv, overwrite = TRUE) 
## 3 {                                                                         
## 4     fileExt <- function(x) {                                              
## 5         db <- grepl("\\\\.[^.]+\\\\.(gz|bz2|xz)$", x)                     
## 6         ans <- sub(".*\\\\.", "", x)
# Ensure the changes are made
head(full_data) |>
  kable()
tripduration usertype age gender start_day day_of_week day_type city max_temp avg_temp min_temp max_wind avg_wind min_wind total_precip tract_hu_per_sq_km log_tripduration age_group subscriber customer male female unspecified weekend weekday holiday Boston Brookline Cambridge Everett Quincy Somerville Monday Tuesday Wednesday Thursday Friday Saturday Sunday
108 Subscriber 132 female 29 Saturday Weekend Cambridge 34 28.8 22 18 12.4 7 0 3463.883 4.682131 Others 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
1611 Subscriber 131 male 23 Sunday Weekend Somerville 57 44.8 35 15 9.0 6 0 3509.244 7.384610 Others 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1
2091 Subscriber 131 male 23 Sunday Weekend Somerville 57 44.8 35 15 9.0 6 0 3509.244 7.645398 Others 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1
1758 Subscriber 131 male 23 Sunday Weekend Boston 57 44.8 35 15 9.0 6 0 1457.802 7.471932 Others 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
876 Subscriber 131 male 24 Monday Weekday Somerville 64 49.0 36 17 9.0 0 0 3509.244 6.775366 Others 1 0 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0
1015 Subscriber 131 male 24 Monday Weekday Cambridge 64 49.0 36 17 9.0 0 0 2479.800 6.922644 Others 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0

1. Correlation of all the variables

  cor_mat <- full_data |>
    select(log_tripduration,
           age,
           start_day,
           max_temp,
           avg_temp,
           max_wind,
           avg_wind,
           min_wind,
           total_precip,
           tract_hu_per_sq_km,
           subscriber,
           customer,
           male,
           female,
           unspecified,
           weekend,
           weekday,
           holiday,
           Boston,
           Brookline,
           Cambridge,
           Everett,
           Quincy,
           Somerville,
           Monday,
           Tuesday,
           Wednesday,
           Thursday,
           Friday,
           Saturday,
           Sunday) |>
    cor()
  
  cor_mat |>
    kable()
log_tripduration age start_day max_temp avg_temp max_wind avg_wind min_wind total_precip tract_hu_per_sq_km subscriber customer male female unspecified weekend weekday holiday Boston Brookline Cambridge Everett Quincy Somerville Monday Tuesday Wednesday Thursday Friday Saturday Sunday
log_tripduration 1.0000000 0.0917875 0.0232495 0.0509299 0.0388520 -0.0343468 -0.0283804 -0.0042888 -0.0327118 0.0553091 -0.2745105 0.2745105 0.0656279 -0.1499401 0.1790731 0.0684301 -0.0720619 0.0165545 0.1052628 0.0479705 -0.1297408 0.0499029 0.0044102 0.0089402 0.0151886 -0.0182087 -0.0100428 -0.0352260 -0.0345761 0.0327911 0.0582053
age 0.0917875 1.0000000 0.0193928 0.0368921 0.0400964 0.0006305 -0.0041418 0.0021547 0.0129776 0.0705172 -0.1185779 0.1185779 -0.0439118 -0.1048150 0.2937352 -0.0651839 0.0697389 -0.0185345 0.1112454 -0.0119333 -0.0957284 0.0131831 0.0058751 -0.0305442 0.0170966 0.0214422 0.0271809 0.0009035 0.0064934 -0.0447610 -0.0413061
start_day 0.0232495 0.0193928 1.0000000 0.1741430 0.0679752 0.1525912 0.3674079 0.3767859 0.2070926 0.0067041 -0.0318606 0.0318606 0.0062644 -0.0164396 0.0213818 -0.0669097 0.0518975 0.0306721 0.0080222 0.0003233 -0.0104060 0.0033711 0.0038381 0.0036246 -0.0814961 -0.0529270 -0.0009069 0.0863728 0.1428431 -0.0104260 -0.0795286
max_temp 0.0509299 0.0368921 0.1741430 1.0000000 0.8571501 0.1034673 -0.0483955 -0.1797413 0.0810345 0.0053856 -0.0409938 0.0409938 0.0070629 -0.0178760 0.0227784 -0.1819597 0.1506488 0.0593952 0.0241524 -0.0041764 -0.0153859 0.0039726 0.0094688 -0.0168203 0.4581256 0.0869069 0.0021992 -0.1121875 -0.2580398 -0.3166178 0.0850446
avg_temp 0.0388520 0.0400964 0.0679752 0.8571501 1.0000000 -0.1004241 -0.2260571 -0.3124189 0.1611642 0.0060181 -0.0089844 0.0089844 0.0047405 -0.0053882 0.0019633 -0.2788091 0.2516076 0.0385671 0.0253293 -0.0039645 -0.0149786 0.0009603 0.0059860 -0.0195483 0.3765223 0.2577681 0.2042358 -0.0902949 -0.4870188 -0.3728260 0.0129092
max_wind -0.0343468 0.0006305 0.1525912 0.1034673 -0.1004241 1.0000000 0.7785054 0.4134963 0.6422189 -0.0150749 0.0454514 -0.0454514 -0.0166760 0.0306490 -0.0304823 -0.1692617 0.1991296 -0.0936663 -0.0064279 -0.0068205 0.0081962 -0.0052781 -0.0003233 0.0015115 -0.0335037 -0.4403596 -0.0012220 0.3812736 0.3404761 -0.0859812 -0.1388774
avg_wind -0.0283804 -0.0041418 0.3674079 -0.0483955 -0.2260571 0.7785054 1.0000000 0.7774612 0.5102806 -0.0111959 0.0264896 -0.0264896 -0.0132019 0.0209078 -0.0173661 -0.0661233 0.0882935 -0.0631017 -0.0094612 -0.0040803 0.0080164 -0.0027484 -0.0013695 0.0060374 -0.0295084 -0.4763650 0.0285162 0.2648396 0.3294580 0.0594131 -0.1514683
min_wind -0.0042888 0.0021547 0.3767859 -0.1797413 -0.3124189 0.4134963 0.7774612 1.0000000 0.2818790 -0.0025898 -0.0146990 0.0146990 -0.0059127 -0.0006141 0.0123375 0.1191952 -0.0359277 -0.1973237 -0.0092780 -0.0016874 0.0038215 0.0010829 -0.0042824 0.0120358 -0.2007507 -0.3606726 0.1266308 0.0546052 0.2594098 0.2286203 -0.0778861
total_precip -0.0327118 0.0129776 0.2070926 0.0810345 0.1611642 0.6422189 0.5102806 0.2818790 1.0000000 -0.0099259 0.0674095 -0.0674095 -0.0132403 0.0362981 -0.0483201 -0.2864311 0.3225807 -0.1221714 0.0007884 -0.0064749 0.0043033 -0.0060066 -0.0026514 -0.0053981 -0.2866953 -0.2234917 0.4040302 0.4672248 -0.0229942 -0.1434795 -0.2371266
tract_hu_per_sq_km 0.0553091 0.0705172 0.0067041 0.0053856 0.0060181 -0.0150749 -0.0111959 -0.0025898 -0.0099259 1.0000000 -0.0369909 0.0369909 -0.0152082 0.0047869 0.0188993 0.0122087 -0.0137272 0.0051511 0.3485212 0.0138669 -0.3258413 -0.0368780 -0.0036766 -0.0587536 -0.0008799 0.0004229 0.0000493 -0.0073437 -0.0072746 0.0042292 0.0120790
subscriber -0.2745105 -0.1185779 -0.0318606 -0.0409938 -0.0089844 0.0454514 0.0264896 -0.0146990 0.0674095 -0.0369909 1.0000000 -1.0000000 0.0372785 0.2398293 -0.5534656 -0.1654439 0.1756995 -0.0437473 -0.0703676 -0.0049457 0.0798314 -0.0452367 -0.0113192 -0.0053399 0.0013609 0.0489956 0.0527899 0.0496135 0.0395428 -0.0870905 -0.1325583
customer 0.2745105 0.1185779 0.0318606 0.0409938 0.0089844 -0.0454514 -0.0264896 0.0146990 -0.0674095 0.0369909 -1.0000000 1.0000000 -0.0372785 -0.2398293 0.5534656 0.1654439 -0.1756995 0.0437473 0.0703676 0.0049457 -0.0798314 0.0452367 0.0113192 0.0053399 -0.0013609 -0.0489956 -0.0527899 -0.0496135 -0.0395428 0.0870905 0.1325583
male 0.0656279 -0.0439118 0.0062644 0.0070629 0.0047405 -0.0166760 -0.0132019 -0.0059127 -0.0132403 -0.0152082 0.0372785 -0.0372785 1.0000000 -0.8704219 -0.1225070 0.0142655 -0.0148602 0.0030410 -0.0491313 0.0005905 0.0409950 -0.0116401 -0.0020485 0.0197272 0.0029717 0.0041710 -0.0042039 -0.0146563 -0.0058841 0.0050875 0.0139616
female -0.1499401 -0.1048150 -0.0164396 -0.0178760 -0.0053882 0.0306490 0.0209078 -0.0006141 0.0362981 0.0047869 0.2398293 -0.2398293 -0.8704219 1.0000000 -0.3819656 -0.0676448 0.0705678 -0.0146806 0.0274842 -0.0014006 -0.0170677 -0.0053255 -0.0063871 -0.0198937 -0.0031545 0.0116154 0.0213119 0.0320770 0.0178526 -0.0351440 -0.0546846
unspecified 0.1790731 0.2937352 0.0213818 0.0227784 0.0019633 -0.0304823 -0.0173661 0.0123375 -0.0483201 0.0188993 -0.5534656 0.5534656 -0.1225070 -0.3819656 1.0000000 0.1095891 -0.1143654 0.0238868 0.0368242 0.0017149 -0.0425498 0.0325874 0.0167216 0.0030721 0.0007808 -0.0312461 -0.0350721 -0.0371523 -0.0249443 0.0612982 0.0840324
weekend 0.0684301 -0.0651839 -0.0669097 -0.1819597 -0.2788091 -0.1692617 -0.0661233 0.1191952 -0.2864311 0.0122087 -0.1654439 0.1654439 0.0142655 -0.0676448 0.1095891 1.0000000 -0.9189812 -0.0965659 -0.0300648 0.0094832 0.0155942 0.0118207 -0.0020957 0.0224638 -0.2385550 -0.2349184 -0.2592421 -0.2099973 -0.2125044 0.6792461 0.6414641
weekday -0.0720619 0.0697389 0.0518975 0.1506488 0.2516076 0.1991296 0.0882935 -0.0359277 0.3225807 -0.0137272 0.1756995 -0.1756995 -0.0148602 0.0705678 -0.1143654 -0.9189812 1.0000000 -0.3037164 0.0304156 -0.0093445 -0.0161757 -0.0125106 0.0022805 -0.0219747 0.0679926 0.2556291 0.2820973 0.2285110 0.2312391 -0.6242144 -0.5894934
holiday 0.0165545 -0.0185345 0.0306721 0.0593952 0.0385671 -0.0936663 -0.0631017 -0.1973237 -0.1221714 0.0051511 -0.0437473 0.0437473 0.0030410 -0.0146806 0.0238868 -0.0965659 -0.3037164 1.0000000 -0.0041308 0.0006737 0.0031512 0.0030174 -0.0006926 0.0011904 0.4047952 -0.0776388 -0.0856776 -0.0694025 -0.0702311 -0.0655920 -0.0619436
Boston 0.1052628 0.1112454 0.0080222 0.0241524 0.0253293 -0.0064279 -0.0094612 -0.0092780 0.0007884 0.3485212 -0.0703676 0.0703676 -0.0491313 0.0274842 0.0368242 -0.0300648 0.0304156 -0.0041308 1.0000000 -0.1442601 -0.8388263 -0.0445529 -0.0039265 -0.2669025 0.0129244 0.0069118 0.0152620 -0.0030527 0.0010443 -0.0230984 -0.0164872
Brookline 0.0479705 -0.0119333 0.0003233 -0.0041764 -0.0039645 -0.0068205 -0.0040803 -0.0016874 -0.0064749 0.0138669 -0.0049457 0.0049457 0.0005905 -0.0014006 0.0017149 0.0094832 -0.0093445 0.0006737 -0.1442601 1.0000000 -0.1179808 -0.0062664 -0.0005523 -0.0375398 -0.0047135 0.0042171 -0.0038382 -0.0036653 -0.0030357 0.0067672 0.0057427
Cambridge -0.1297408 -0.0957284 -0.0104060 -0.0153859 -0.0149786 0.0081962 0.0080164 0.0038215 0.0043033 -0.3258413 0.0798314 -0.0798314 0.0409950 -0.0170677 -0.0425498 0.0155942 -0.0161757 0.0031512 -0.8388263 -0.1179808 1.0000000 -0.0364369 -0.0032112 -0.2182819 -0.0043236 -0.0066037 -0.0103888 0.0069178 -0.0023396 0.0111470 0.0094232
Everett 0.0499029 0.0131831 0.0033711 0.0039726 0.0009603 -0.0052781 -0.0027484 0.0010829 -0.0060066 -0.0368780 -0.0452367 0.0452367 -0.0116401 -0.0053255 0.0325874 0.0118207 -0.0125106 0.0030174 -0.0445529 -0.0062664 -0.0364369 1.0000000 -0.0001706 -0.0115937 -0.0027319 -0.0022613 -0.0000258 -0.0089767 -0.0002025 0.0081845 0.0074202
Quincy 0.0044102 0.0058751 0.0038381 0.0094688 0.0059860 -0.0003233 -0.0013695 -0.0042824 -0.0026514 -0.0036766 -0.0113192 0.0113192 -0.0020485 -0.0063871 0.0167216 -0.0020957 0.0022805 -0.0006926 -0.0039265 -0.0005523 -0.0032112 -0.0001706 1.0000000 -0.0010218 0.0087851 -0.0016850 -0.0018594 -0.0015062 -0.0015242 -0.0014235 -0.0013443
Somerville 0.0089402 -0.0305442 0.0036246 -0.0168203 -0.0195483 0.0015115 0.0060374 0.0120358 -0.0053981 -0.0587536 -0.0053399 0.0053399 0.0197272 -0.0198937 0.0030721 0.0224638 -0.0219747 0.0011904 -0.2669025 -0.0375398 -0.2182819 -0.0115937 -0.0010218 1.0000000 -0.0145836 -0.0028181 -0.0080458 -0.0038975 0.0043254 0.0193716 0.0101102
Monday 0.0151886 0.0170966 -0.0814961 0.4581256 0.3765223 -0.0335037 -0.0295084 -0.2007507 -0.2866953 -0.0008799 0.0013609 -0.0013609 0.0029717 -0.0031545 0.0007808 -0.2385550 0.0679926 0.4047952 0.0129244 -0.0047135 -0.0043236 -0.0027319 0.0087851 -0.0145836 1.0000000 -0.1917976 -0.2116566 -0.1714510 -0.1734979 -0.1620375 -0.1530244
Tuesday -0.0182087 0.0214422 -0.0529270 0.0869069 0.2577681 -0.4403596 -0.4763650 -0.3606726 -0.2234917 0.0004229 0.0489956 -0.0489956 0.0041710 0.0116154 -0.0312461 -0.2349184 0.2556291 -0.0776388 0.0069118 0.0042171 -0.0066037 -0.0022613 -0.0016850 -0.0028181 -0.1917976 1.0000000 -0.2084301 -0.1688373 -0.1708530 -0.1595674 -0.1506917
Wednesday -0.0100428 0.0271809 -0.0009069 0.0021992 0.2042358 -0.0012220 0.0285162 0.1266308 0.4040302 0.0000493 0.0527899 -0.0527899 -0.0042039 0.0213119 -0.0350721 -0.2592421 0.2820973 -0.0856776 0.0152620 -0.0038382 -0.0103888 -0.0000258 -0.0018594 -0.0080458 -0.2116566 -0.2084301 1.0000000 -0.1863190 -0.1885434 -0.1760892 -0.1662945
Thursday -0.0352260 0.0009035 0.0863728 -0.1121875 -0.0902949 0.3812736 0.2648396 0.0546052 0.4672248 -0.0073437 0.0496135 -0.0496135 -0.0146563 0.0320770 -0.0371523 -0.2099973 0.2285110 -0.0694025 -0.0030527 -0.0036653 0.0069178 -0.0089767 -0.0015062 -0.0038975 -0.1714510 -0.1688373 -0.1863190 1.0000000 -0.1527283 -0.1426399 -0.1347057
Friday -0.0345761 0.0064934 0.1428431 -0.2580398 -0.4870188 0.3404761 0.3294580 0.2594098 -0.0229942 -0.0072746 0.0395428 -0.0395428 -0.0058841 0.0178526 -0.0249443 -0.2125044 0.2312391 -0.0702311 0.0010443 -0.0030357 -0.0023396 -0.0002025 -0.0015242 0.0043254 -0.1734979 -0.1708530 -0.1885434 -0.1527283 1.0000000 -0.1443428 -0.1363139
Saturday 0.0327911 -0.0447610 -0.0104260 -0.3166178 -0.3728260 -0.0859812 0.0594131 0.2286203 -0.1434795 0.0042292 -0.0870905 0.0870905 0.0050875 -0.0351440 0.0612982 0.6792461 -0.6242144 -0.0655920 -0.0230984 0.0067672 0.0111470 0.0081845 -0.0014235 0.0193716 -0.1620375 -0.1595674 -0.1760892 -0.1426399 -0.1443428 1.0000000 -0.1273098
Sunday 0.0582053 -0.0413061 -0.0795286 0.0850446 0.0129092 -0.1388774 -0.1514683 -0.0778861 -0.2371266 0.0120790 -0.1325583 0.1325583 0.0139616 -0.0546846 0.0840324 0.6414641 -0.5894934 -0.0619436 -0.0164872 0.0057427 0.0094232 0.0074202 -0.0013443 0.0101102 -0.1530244 -0.1506917 -0.1662945 -0.1347057 -0.1363139 -0.1273098 1.0000000

2. Categorizing different variables

To estimate a regression model that explains a significant portion of the variation in trip duration using as few explanatory variables as possible, I divided the variables into four groups:

2.1 Weather Variables Relationships

Variables included: max_temp, avg_temp, min_temp, max_wind, avg_wind, min_wind, total_precip Weather conditions can significantly impact biking behavior. For instance, extreme temperatures, high wind speeds, or precipitation might influence trip duration. People might take shorter or longer trips based on weather comfort.

The results below showed high relevance of max_temp, max_wind, avg_wind, min_wind, total_precip.

  model_water <- lm(log_tripduration ~ 
                         max_temp +
                         avg_temp +
                         min_temp +
                         max_wind +
                         avg_wind +
                         min_wind +
                         total_precip +
                         tract_hu_per_sq_km,
                       data = full_data)
  summary(model_water)
## 
## Call:
## lm(formula = log_tripduration ~ max_temp + avg_temp + min_temp + 
##     max_wind + avg_wind + min_wind + total_precip + tract_hu_per_sq_km, 
##     data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.4332 -0.4863 -0.0204  0.4704  8.5766 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         6.188e+00  1.771e-02 349.380  < 2e-16 ***
## max_temp            8.483e-03  1.022e-03   8.303  < 2e-16 ***
## avg_temp           -2.684e-03  2.184e-03  -1.229    0.219    
## min_temp           -1.038e-03  1.182e-03  -0.878    0.380    
## max_wind           -3.127e-03  6.782e-04  -4.611 4.01e-06 ***
## avg_wind           -8.613e-03  1.395e-03  -6.176 6.59e-10 ***
## min_wind            1.175e-02  1.226e-03   9.583  < 2e-16 ***
## total_precip       -2.502e-02  1.911e-02  -1.309    0.190    
## tract_hu_per_sq_km  1.128e-05  5.688e-07  19.837  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.739 on 133045 degrees of freedom
## Multiple R-squared:  0.008519,   Adjusted R-squared:  0.008459 
## F-statistic: 142.9 on 8 and 133045 DF,  p-value: < 2.2e-16

2.2 Time Variables Relationships

Variables included: start_day, day_of_week (Monday to Sunday), day_type (Weekend or Weekday) Behavioral patterns might differ based on the day of the week (weekday vs. weekend) or holidays. People might take longer or shorter trips depending on these factors.

  model_time <- lm(log_tripduration ~ 
                         start_day +
                         Monday +
                         Tuesday +
                         Wednesday +
                         Thursday +
                         Friday +
                         Saturday +
                         Sunday +
                         weekday +
                         weekend +
                         holiday,
                         data = full_data)
  summary(model_time)
## 
## Call:
## lm(formula = log_tripduration ~ start_day + Monday + Tuesday + 
##     Wednesday + Thursday + Friday + Saturday + Sunday + weekday + 
##     weekend + holiday, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.4240 -0.4875 -0.0187  0.4744  8.4249 
## 
## Coefficients: (3 not defined because of singularities)
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.4639399  0.0070039 922.909  < 2e-16 ***
## start_day    0.0030837  0.0002421  12.737  < 2e-16 ***
## Monday      -0.0663857  0.0131046  -5.066 4.07e-07 ***
## Tuesday     -0.1167580  0.0151415  -7.711 1.26e-14 ***
## Wednesday   -0.1043675  0.0150333  -6.942 3.87e-12 ***
## Thursday    -0.1620329  0.0153901 -10.528  < 2e-16 ***
## Friday      -0.1637322  0.0154071 -10.627  < 2e-16 ***
## Saturday    -0.0635988  0.0085442  -7.443 9.87e-14 ***
## Sunday              NA         NA      NA       NA    
## weekday     -0.0417490  0.0128334  -3.253  0.00114 ** 
## weekend             NA         NA      NA       NA    
## holiday             NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7392 on 133045 degrees of freedom
## Multiple R-squared:  0.007979,   Adjusted R-squared:  0.007919 
## F-statistic: 133.8 on 8 and 133045 DF,  p-value: < 2.2e-16

The result was insignificant when accounting all the variables into the model. Thus, after several trial and error, accompanied with guess, a strong relationship between weekend and trip duration was discovered.

  model_time <- lm(log_tripduration ~ 
                         weekend,
                         data = full_data)
  summary(model_time)
## 
## Call:
## lm(formula = log_tripduration ~ weekend, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.3643 -0.4901 -0.0205  0.4737  8.3747 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 6.353756   0.002307 2753.74   <2e-16 ***
## weekend     0.121399   0.004852   25.02   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7404 on 133052 degrees of freedom
## Multiple R-squared:  0.004683,   Adjusted R-squared:  0.004675 
## F-statistic:   626 on 1 and 133052 DF,  p-value: < 2.2e-16

2.3 Demographic Variables Relationships

Variables included: user type (Subscriber, Customer), age, gender(male, femal, unspecified) Subscriber/Customer status, age, and gender might affect trip duration. Subscribers might take more frequent but shorter trips compared to occasional customers. Age and gender might also influence trip duration due to varying biking habits.

The results of the regression model shows that there is a significant relationship between user type, age and gender.

  model_demographic <- lm(log_tripduration ~ 
                         usertype +
                         age +
                         gender,
                         data = full_data)
  summary(model_demographic)
## 
## Call:
## lm(formula = log_tripduration ~ usertype + age + gender, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8161 -0.4653 -0.0126  0.4636  7.7853 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         6.7668228  0.0093787 721.506   <2e-16 ***
## usertypeSubscriber -0.6119977  0.0076361 -80.146   <2e-16 ***
## age                 0.0036713  0.0001808  20.304   <2e-16 ***
## gendermale          0.1464544  0.0047488  30.840   <2e-16 ***
## genderunspecified   0.1104903  0.0111190   9.937   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7096 on 133049 degrees of freedom
## Multiple R-squared:  0.08573,    Adjusted R-squared:  0.0857 
## F-statistic:  3119 on 4 and 133049 DF,  p-value: < 2.2e-16

2.4 Location Variables Relationships

Variables included: city(Boston, Brookline, Cambridge, Everett, Quincy, Somerville), tract_hu_per_sq_km The city and housing density might influence trip duration. Urban areas or areas with higher housing density might have shorter trips due to proximity or more available biking infrastructure.

The results show a great significance in cites except from Quincy.

  model_location <- lm(log_tripduration ~ 
                         city +
                         tract_hu_per_sq_km,
                         data = full_data)
  summary(model_location)
## 
## Call:
## lm(formula = log_tripduration ~ city + tract_hu_per_sq_km, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5383 -0.4767 -0.0248  0.4633  8.0402 
## 
## Coefficients:
##                      Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)         6.439e+00  4.561e-03 1411.659  < 2e-16 ***
## cityBrookline       1.757e-01  1.457e-02   12.061  < 2e-16 ***
## cityCambridge      -1.847e-01  4.528e-03  -40.795  < 2e-16 ***
## cityEverett         7.786e-01  4.597e-02   16.936  < 2e-16 ***
## cityQuincy          7.822e-01  5.194e-01    1.506    0.132    
## citySomerville     -4.529e-02  8.481e-03   -5.340 9.30e-08 ***
## tract_hu_per_sq_km  3.300e-06  6.052e-07    5.452 4.98e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7345 on 133047 degrees of freedom
## Multiple R-squared:  0.02051,    Adjusted R-squared:  0.02046 
## F-statistic: 464.3 on 6 and 133047 DF,  p-value: < 2.2e-16

3 Final model estimation

Aggregating the previous significant factors into a single model.

  model_finalized <- lm(log_tripduration ~ 
                         usertype +
                         age +
                         gender +
                         weekend +
                         city +
                         max_temp +
                         max_wind +
                         avg_wind +
                         min_wind +
                         tract_hu_per_sq_km,
                         data = full_data)
  summary(model_finalized)
## 
## Call:
## lm(formula = log_tripduration ~ usertype + age + gender + weekend + 
##     city + max_temp + max_wind + avg_wind + min_wind + tract_hu_per_sq_km, 
##     data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8365 -0.4533 -0.0129  0.4582  7.4561 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         6.626e+00  1.722e-02 384.824  < 2e-16 ***
## usertypeSubscriber -5.668e-01  7.663e-03 -73.974  < 2e-16 ***
## age                 3.083e-03  1.812e-04  17.016  < 2e-16 ***
## gendermale          1.519e-01  4.712e-03  32.242  < 2e-16 ***
## genderunspecified   1.180e-01  1.104e-02  10.690  < 2e-16 ***
## weekend             5.659e-02  4.940e-03  11.454  < 2e-16 ***
## cityBrookline       1.836e-01  1.395e-02  13.162  < 2e-16 ***
## cityCambridge      -1.548e-01  4.366e-03 -35.452  < 2e-16 ***
## cityEverett         5.838e-01  4.405e-02  13.255  < 2e-16 ***
## cityQuincy          8.972e-02  4.972e-01   0.180 0.856798    
## citySomerville     -3.848e-02  8.136e-03  -4.730 2.25e-06 ***
## max_temp            4.443e-03  2.666e-04  16.662  < 2e-16 ***
## max_wind           -1.268e-03  5.219e-04  -2.430 0.015090 *  
## avg_wind           -3.461e-03  1.309e-03  -2.643 0.008209 ** 
## min_wind            3.878e-03  1.166e-03   3.324 0.000887 ***
## tract_hu_per_sq_km  2.011e-06  5.798e-07   3.469 0.000523 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.703 on 133038 degrees of freedom
## Multiple R-squared:  0.1028, Adjusted R-squared:  0.1027 
## F-statistic:  1017 on 15 and 133038 DF,  p-value: < 2.2e-16

The result indicated that the age, gender, max_temp, min_wind, tract_hu_per_sq_km has positive affect on tripduration. others listed in the model has negative effect. The model overall describes 10% of the situation where older male citizens during less windy weekends travel longer on blue bikes. The time duration varies in different cities, and Everett is relative more blue bike friendly.