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
289 Subscriber 27 female 1 Saturday Weekend Cambridge 40 38.1 35 8 4.5 0 0.02 1063.292
382 Subscriber 29 female 1 Saturday Weekend Cambridge 40 38.1 35 8 4.5 0 0.02 3181.105
954 Subscriber 26 female 1 Saturday Weekend Cambridge 40 38.1 35 8 4.5 0 0.02 6038.003
723 Subscriber 42 female 1 Saturday Weekend Boston 40 38.1 35 8 4.5 0 0.02 1457.802
553 Subscriber 19 female 1 Saturday Weekend Brookline 40 38.1 35 8 4.5 0 0.02 4714.330
559 Customer 19 male 1 Saturday Weekend Brookline 40 38.1 35 8 4.5 0 0.02 4714.330

0. Data Preprocessing

# Sample data with 'age' variable
full_data <- full_data

# 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 age_group subscriber customer male female unspecified weekend weekday holiday Boston Brookline Cambridge Everett Quincy Somerville Monday Tuesday Wednesday Thursday Friday Saturday Sunday
289 Subscriber 27 female 1 Saturday Weekend Cambridge 40 38.1 35 8 4.5 0 0.02 1063.292 Millennials 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
382 Subscriber 29 female 1 Saturday Weekend Cambridge 40 38.1 35 8 4.5 0 0.02 3181.105 Millennials 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
954 Subscriber 26 female 1 Saturday Weekend Cambridge 40 38.1 35 8 4.5 0 0.02 6038.003 GenZ 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
723 Subscriber 42 female 1 Saturday Weekend Boston 40 38.1 35 8 4.5 0 0.02 1457.802 Millennials 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
553 Subscriber 19 female 1 Saturday Weekend Brookline 40 38.1 35 8 4.5 0 0.02 4714.330 GenZ 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0
559 Customer 19 male 1 Saturday Weekend Brookline 40 38.1 35 8 4.5 0 0.02 4714.330 GenZ 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0

1. Correlation of all the variables

  cor_mat <- full_data |>
    select(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()
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
tripduration 1.0000000 0.0197877 0.0030230 0.0039146 0.0016588 0.0009495 0.0004048 0.0013943 0.0018111 0.0089769 -0.0589334 0.0589334 -0.0042447 -0.0248469 0.0580584 0.0124568 -0.0113322 -0.0014942 0.0154679 -0.0011844 -0.0188273 0.0509451 0.0000628 -0.0022565 -0.0020107 -0.0051468 -0.0013529 -0.0019702 -0.0040945 0.0100826 0.0062958
age 0.0197877 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.0030230 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.0039146 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.0016588 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.0009495 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.0004048 -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.0013943 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.0018111 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.0089769 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.0589334 -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.0589334 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.0042447 -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.0248469 -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.0580584 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.0124568 -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.0113322 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.0014942 -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.0154679 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.0011844 -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.0188273 -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.0509451 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.0000628 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.0022565 -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.0020107 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.0051468 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.0013529 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.0019702 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.0040945 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.0100826 -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.0062958 -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.

However, the results below showed no relevance of these factors.

  model_water <- lm(tripduration ~ 
                         max_temp +
                         avg_temp +
                         min_temp +
                         max_wind +
                         avg_wind +
                         min_wind +
                         total_precip,
                       data = full_data)
  summary(model_water)
## 
## Call:
## lm(formula = tripduration ~ max_temp + avg_temp + min_temp + 
##     max_wind + avg_wind + min_wind + total_precip, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##   -1274    -740    -508    -142 2812167 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  1160.941    398.334   2.914  0.00356 **
## max_temp       41.903     23.241   1.803  0.07140 . 
## avg_temp      -55.871     49.682  -1.125  0.26077   
## min_temp       13.604     26.889   0.506  0.61291   
## max_wind       -8.589     15.428  -0.557  0.57774   
## avg_wind      -20.372     31.723  -0.642  0.52075   
## min_wind       20.777     27.890   0.745  0.45630   
## total_precip  601.303    434.687   1.383  0.16657   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16810 on 133046 degrees of freedom
## Multiple R-squared:  5.127e-05,  Adjusted R-squared:  -1.343e-06 
## F-statistic: 0.9745 on 7 and 133046 DF,  p-value: 0.4477

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(tripduration ~ 
                         start_day +
                         Monday +
                         Tuesday +
                         Wednesday +
                         Thursday +
                         Friday +
                         Saturday +
                         Sunday +
                         weekday +
                         weekend +
                         holiday,
                         data = full_data)
  summary(model_time)
## 
## Call:
## lm(formula = tripduration ~ start_day + Monday + Tuesday + Wednesday + 
##     Thursday + Friday + Saturday + Sunday + weekday + weekend + 
##     holiday, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##   -1612    -718    -475    -112 2811828 
## 
## Coefficients: (3 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1298.411    159.272   8.152 3.61e-16 ***
## start_day      8.008      5.506   1.455   0.1458    
## Monday      -473.306    298.006  -1.588   0.1122    
## Tuesday     -620.809    344.325  -1.803   0.0714 .  
## Wednesday   -477.362    341.865  -1.396   0.1626    
## Thursday    -530.556    349.978  -1.516   0.1295    
## Friday      -630.048    350.366  -1.798   0.0721 .  
## Saturday     142.633    194.300   0.734   0.4629    
## Sunday            NA         NA      NA       NA    
## weekday      109.133    291.839   0.374   0.7084    
## 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: 16810 on 133045 degrees of freedom
## Multiple R-squared:  0.0001865,  Adjusted R-squared:  0.0001263 
## F-statistic: 3.102 on 8 and 133045 DF,  p-value: 0.001673

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(tripduration ~ 
                         weekend,
                         data = full_data)
  summary(model_time)
## 
## Call:
## lm(formula = tripduration ~ weekend, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##   -1428    -709    -485    -118 2811844 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   988.66      52.38  18.874  < 2e-16 ***
## weekend       500.58     110.16   4.544 5.52e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16810 on 133052 degrees of freedom
## Multiple R-squared:  0.0001552,  Adjusted R-squared:  0.0001477 
## F-statistic: 20.65 on 1 and 133052 DF,  p-value: 5.521e-06

To be more specific, Saturday is more significant that Sunday.

  model_time <- lm(tripduration ~ 
                         Saturday +
                         Sunday,
                         data = full_data)
  summary(model_time)
## 
## Call:
## lm(formula = tripduration ~ Saturday + Sunday, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##   -1502    -709    -485    -117 2811770 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   988.66      52.38  18.873  < 2e-16 ***
## Saturday      574.82     143.60   4.003 6.26e-05 ***
## Sunday        418.42     150.10   2.788  0.00531 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16810 on 133051 degrees of freedom
## Multiple R-squared:  0.0001601,  Adjusted R-squared:  0.000145 
## F-statistic: 10.65 on 2 and 133051 DF,  p-value: 2.374e-05

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 and trip duration, age and gender did not show relevance. P.S. Although unspecified had significant relationship with trip duration, male and female did not, therefore gender is considered to be insignificant.

  model_demographic <- lm(tripduration ~ 
                         usertype +
                         age +
                         gender,
                         data = full_data)
  summary(model_demographic)
## 
## Call:
## lm(formula = tripduration ~ usertype + age + gender, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##   -5609    -443    -232     105 2807654 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         2609.430    221.690  11.771   <2e-16 ***
## usertypeSubscriber -2131.329    180.497 -11.808   <2e-16 ***
## age                    7.275      4.274   1.702   0.0887 .  
## gendermale            71.072    112.251   0.633   0.5266    
## genderunspecified   2699.006    262.825  10.269   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16770 on 133049 degrees of freedom
## Multiple R-squared:  0.004431,   Adjusted R-squared:  0.004401 
## F-statistic:   148 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 city (Cambridge and Everett)

  model_location <- lm(tripduration ~ 
                         city +
                         tract_hu_per_sq_km,
                         data = full_data)
  summary(model_location)
## 
## Call:
## lm(formula = tripduration ~ city + tract_hu_per_sq_km, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##  -20509    -745    -393     -56 2792793 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         1.216e+03  1.042e+02  11.670  < 2e-16 ***
## cityBrookline      -3.754e+02  3.329e+02  -1.128   0.2595    
## cityCambridge      -5.757e+02  1.035e+02  -5.563 2.65e-08 ***
## cityEverett         1.931e+04  1.051e+03  18.380  < 2e-16 ***
## cityQuincy          1.261e+02  1.187e+04   0.011   0.9915    
## citySomerville     -3.521e+02  1.938e+02  -1.817   0.0693 .  
## tract_hu_per_sq_km  2.404e-02  1.383e-02   1.738   0.0821 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16790 on 133047 degrees of freedom
## Multiple R-squared:  0.002948,   Adjusted R-squared:  0.002903 
## F-statistic: 65.55 on 6 and 133047 DF,  p-value: < 2.2e-16

3 Final model estimation

Aggregating the previous significant factors into a single model.

  model_location <- lm(tripduration ~ 
                         customer +
                         weekend +
                         age +
                         Cambridge +
                         Everett +
                         total_precip +
                         tract_hu_per_sq_km,
                         data = full_data)
  summary(model_location)
## 
## Call:
## lm(formula = tripduration ~ customer + weekend + age + Cambridge + 
##     Everett + total_precip + tract_hu_per_sq_km, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##  -22508    -535    -158     178 2790560 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         6.469e+01  1.779e+02   0.364 0.716172    
## customer            2.955e+03  1.539e+02  19.206  < 2e-16 ***
## weekend             2.393e+02  1.165e+02   2.055 0.039871 *  
## age                 1.752e+01  4.139e+00   4.233 2.31e-05 ***
## Cambridge          -3.518e+02  9.957e+01  -3.533 0.000411 ***
## Everett             1.842e+04  1.050e+03  17.554  < 2e-16 ***
## total_precip        7.157e+02  2.769e+02   2.585 0.009741 ** 
## tract_hu_per_sq_km  2.161e-02  1.367e-02   1.581 0.113965    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16760 on 133046 degrees of freedom
## Multiple R-squared:  0.006182,   Adjusted R-squared:  0.00613 
## F-statistic: 118.2 on 7 and 133046 DF,  p-value: < 2.2e-16
  model_finalized <- lm(tripduration ~ 
                         usertype +
                         age +
                         gender +
                         day_type +
                         Cambridge +
                         Everett +
                         avg_temp +
                         avg_wind +
                         total_precip +
                         tract_hu_per_sq_km,
                         data = full_data)
  summary(model_finalized)
## 
## Call:
## lm(formula = tripduration ~ usertype + age + gender + day_type + 
##     Cambridge + Everett + avg_temp + avg_wind + total_precip + 
##     tract_hu_per_sq_km, data = full_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##  -23903    -500    -154     178 2789272 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         2.142e+03  5.254e+02   4.078 4.55e-05 ***
## usertypeSubscriber -1.954e+03  1.827e+02 -10.697  < 2e-16 ***
## age                 4.962e+00  4.313e+00   1.150 0.249951    
## gendermale          1.094e+02  1.122e+02   0.975 0.329675    
## genderunspecified   2.712e+03  2.630e+02  10.311  < 2e-16 ***
## day_typeWeekday     2.624e+02  2.707e+02   0.969 0.332399    
## day_typeWeekend     4.422e+02  2.809e+02   1.574 0.115482    
## Cambridge          -3.750e+02  9.962e+01  -3.764 0.000167 ***
## Everett             1.836e+04  1.049e+03  17.497  < 2e-16 ***
## avg_temp            1.640e+00  8.034e+00   0.204 0.838233    
## avg_wind           -6.367e+00  1.467e+01  -0.434 0.664237    
## total_precip        7.677e+02  3.382e+02   2.270 0.023199 *  
## tract_hu_per_sq_km  2.364e-02  1.367e-02   1.729 0.083847 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16750 on 133041 degrees of freedom
## Multiple R-squared:  0.006984,   Adjusted R-squared:  0.006894 
## F-statistic: 77.97 on 12 and 133041 DF,  p-value: < 2.2e-16