-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBike_Maintenance.Rmd
796 lines (621 loc) · 32.2 KB
/
Bike_Maintenance.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
---
title: "Better Management of Bicycle Fleet - BikeShare"
output:
powerpoint_presentation:
reference_doc: Bikes_Template.pptx
df_print: paged
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
rm(list=ls(all=TRUE))
if(!require(tidyverse)) install.packages("tidyverse", repos = "http://cran.us.r-project.org")
if(!require(tidymodels)) install.packages("tidymodels", repos = "http://cran.us.r-project.org")
if(!require(lubridate)) install.packages("lubridate", repos = "http://cran.us.r-project.org")
if(!require(ggthemes)) install.packages("ggthemes", repunios = "http://cran.us.r-project.org")
if(!require(patchwork)) install.packages("patchwork", repos = "http://cran.us.r-project.org")
if(!require(gridtext)) install.packages("gridtext", repos = "http://cran.us.r-project.org")
if(!require(grid)) install.packages("grid", repos = "http://cran.us.r-project.org")
if(!require(magick)) install.packages("magick", repos = "http://cran.us.r-project.org")
if(!require(gtools)) install.packages("gtools", repos = "http://cran.us.r-project.org")
if(!require(ggmap)) install.packages("gtools", repos = "http://cran.us.r-project.org")
if(!require(mltools)) install.packages("mltools", repos = "http://cran.us.r-project.org")
if(!require(flextable)) install.packages("knitr", repos = "http://cran.us.r-project.org")
if(!require(officer)) install.packages("knitr", repos = "http://cran.us.r-project.org")
library(magick)
library(tidyverse)
library(tidymodels)
library(ggthemes)
library(patchwork)
library(grid)
library(gridtext)
library(rvest)
library(RColorBrewer)
library(lubridate)
library(flextable)
library(officer)
seasons <- tibble(season=c("DJF","MAM","JJA","SON"),
season_name=c("winter","spring","summer","autumn"))
```
```{r load_data, echo=FALSE, include=FALSE}
if(file.exists("data.RData")){
load("data.RData")
}else{
if(file.exists("trip_data.RDS")){
trip_data <- readRDS("trip_data.RDS")
}else{
library(aws.s3)
#filesat https://s3.amazonaws.com/capitalbikeshare-data/index.html"
bucket_id <- 'capitalbikeshare-data'
bucket <- get_bucket(bucket_id,max = Inf)
keys <- c()
for(i in seq(1:length(bucket))){
keys[i] <- bucket[[i]]$Key
}
#ignore 2020
keys <- c(keys[grepl('2018',keys)],keys[grepl('2019',keys)],keys[grepl('2017',keys)])
save_object(keys[1], file = keys[1], bucket = bucket_id)
unzip(keys[1],exdir = "./zip")
file.remove(keys[1])
files <- list.files("./zip")
files <- files[!grepl('MACOSX',files)]
#first file
trip_data <- read_csv(paste("./zip/",files[1],sep=""))
if(length(files)>1){
for(j in 2:length(files)){
trip_data_j <- read_csv(paste("./zip/",files[j],sep=""))
trip_data <- rbind(trip_data,trip_data_j)
rm(trip_data_j)
}
file.remove(paste("./zip/",files,sep=""))
}
###other files
if(length(keys)>1){
for(i in 1:length(keys)){
save_object(keys[i], file = keys[i], bucket = bucket_id)
unzip(keys[i],exdir = "./zip")
file.remove(keys[i])
files <- list.files("./zip")
files <- files[!grepl('MACOSX',files)]
for(j in 1:length(files)){
trip_data_j <- read_csv(paste("./zip/",files[j],sep=""))
trip_data <- rbind(trip_data,trip_data_j)
rm(trip_data_j)
}
file.remove(paste("./zip/",files,sep=""))
}
}
saveRDS(trip_data,"trip_data.RDS")
if(file.exists("weather_data.RDS")){
weather_data <- readRDS("weather_data.RDS")
}else{
files <- list.files("./weather/")
weather_data <- read.csv(paste("./weather/",files[1],sep=""))
if(length(files)>1){
for(j in 2:length(files)){
weather_data_j <- read.csv(paste("./weather/",files[j],sep=""))
message("read")
weather_data <- gtools::smartbind(weather_data,weather_data_j)
rm(weather_data_j)
}
}
weather_data_station <- weather_data %>% group_by(STATION,DATE) %>%
summarise(TAVG=median(TAVG,na.rm = TRUE), #avg temp
TMIN=median(TMIN,na.rm = TRUE), #min temp
TMAX=median(TMAX,na.rm = TRUE), #mav temp
PRCP=median(PRCP,na.rm = TRUE), #precipitation
SNOW=median(SNOW,na.rm = TRUE), #snowfall
AWND=median(AWND,na.rm = TRUE), #avg wind speed
WSF2=median(WSF2,na.rm = TRUE),#fastest 2 min gust
WDMV=mean(WDMV,na.rm = TRUE), #total wind movement
WT01=sum(WT01,na.rm = TRUE), #WT01 - Fog, ice fog, or freezing fog (may include heavy fog)
WT02=sum(WT02,na.rm = TRUE), #WT02 - Heavy fog or heaving freezing fog
WT03=sum(WT03,na.rm = TRUE), #WT03 - Thunder
WT04=sum(WT04,na.rm = TRUE), #WT04 - Ice pellets, sleet, snow pellets, or small hail
WT05=sum(WT05,na.rm = TRUE), #WT05 - Hail (may include small hail)
WT06=sum(WT06,na.rm = TRUE), #WT06 - Glaze or rime
WT08=sum(WT08,na.rm = TRUE), #WT08 - Smoke or haze
WT09=sum(as.numeric(WT09),na.rm = TRUE), #WT09 - Blowing or drifting snow
WT11=sum(WT11,na.rm = TRUE), #WT11 - High or damaging winds
.groups = 'drop') %>%
mutate(WT01=if_else(is.na(WT01),0,WT01),
WT02=if_else(is.na(WT02),0,WT01),
WT03=if_else(is.na(WT03),0,WT01),
WT04=if_else(is.na(WT04),0,WT01),
WT05=if_else(is.na(WT05),0,WT01),
WT06=if_else(is.na(WT06),0,WT01),
WT08=if_else(is.na(WT08),0,WT01),
WT09=if_else(is.na(WT09),0,WT01),
WT11=if_else(is.na(WT11),0,WT01),
) %>%
mutate(Date=as_date(parse_date_time(DATE, orders = "Y-m-d"))) %>% select(-DATE)
weather_data <- weather_data %>% group_by(DATE) %>%
summarise(TAVG=median(TAVG,na.rm = TRUE), #avg temp
TMIN=median(TMIN,na.rm = TRUE), #min temp
TMAX=median(TMAX,na.rm = TRUE), #mav temp
PRCP=median(PRCP,na.rm = TRUE), #precipitation
SNOW=median(SNOW,na.rm = TRUE), #snowfall
AWND=median(AWND,na.rm = TRUE), #avg wind speed
WSF2=median(WSF2,na.rm = TRUE),#fastest 2 min gust
WDMV=mean(WDMV,na.rm = TRUE), #total wind movement
WT01=sum(WT01,na.rm = TRUE), #WT01 - Fog, ice fog, or freezing fog (may include heavy fog)
WT02=sum(WT02,na.rm = TRUE), #WT02 - Heavy fog or heaving freezing fog
WT03=sum(WT03,na.rm = TRUE), #WT03 - Thunder
WT04=sum(WT04,na.rm = TRUE), #WT04 - Ice pellets, sleet, snow pellets, or small hail
WT05=sum(WT05,na.rm = TRUE), #WT05 - Hail (may include small hail)
WT06=sum(WT06,na.rm = TRUE), #WT06 - Glaze or rime
WT08=sum(WT08,na.rm = TRUE), #WT08 - Smoke or haze
WT09=sum(as.numeric(WT09),na.rm = TRUE), #WT09 - Blowing or drifting snow
WT11=sum(WT11,na.rm = TRUE), #WT11 - High or damaging winds
.groups = 'drop') %>%
mutate(WT01=if_else(is.na(WT01),0,WT01),
WT02=if_else(is.na(WT02),0,WT01),
WT03=if_else(is.na(WT03),0,WT01),
WT04=if_else(is.na(WT04),0,WT01),
WT05=if_else(is.na(WT05),0,WT01),
WT06=if_else(is.na(WT06),0,WT01),
WT08=if_else(is.na(WT08),0,WT01),
WT09=if_else(is.na(WT09),0,WT01),
WT11=if_else(is.na(WT11),0,WT01),
) %>%
mutate(Date=as_date(parse_date_time(DATE, orders = "Y-m-d"))) %>% select(-DATE)
saveRDS(weather_data_station,"weather_data_station.RDS")
saveRDS(weather_data,"weather_data.RDS")
}
#No need to rds ..local small file, manually created from google restuls
public_holidays <- read_csv("public_holidays.csv")
public_holidays <- public_holidays %>% mutate(Date=as_date(parse_date_time(Date, orders = "d b Y")))
if(file.exists("bike_stations.RDS")){
bike_stations <- readRDS("bike_stations.RDS")
}else{
bike_stations <- rgdal::readOGR("https://opendata.arcgis.com/datasets/a1f7acf65795451d89f0a38565a975b3_5.geojson")
saveRDS(bike_stations,"bike_stations.RDS")
}
station_daily <- trip_data %>%
mutate(Date=as_date(`Start date`),
`Start station number`=as.numeric(`Start station number`)) %>%
group_by(Date,`Start station number`,`End station number`) %>%
summarise(n=n(),.groups = 'drop') %>% ungroup()
station_demand <- station_daily %>% mutate(Station=`Start station number`) %>%
group_by(Date,Station) %>%
summarise(n=sum(n),.groups = 'drop') %>% ungroup() %>%
mutate(season = metR::season(lubridate::month(Date))) %>%
left_join(seasons, by="season") %>% select(-season) %>%
left_join(public_holidays,by="Date") %>%
mutate(weekend=if_else((lubridate::wday(Date,label=TRUE) %in% c("Sat","Sun")),TRUE,FALSE),
season=season_name,
public_holiday=if_else(is.na(Event),FALSE,TRUE,missing=FALSE),
free_day = ifelse(weekend |public_holiday,"Non-working day","Working day")
) %>%
group_by(free_day,season,Station) %>%
summarise(daily_avg=mean(n),.groups = 'drop') %>%
mutate(Type="Start") %>%
arrange(-daily_avg) %>% ungroup() %>%
left_join((as.data.frame(bike_stations) %>%
mutate(TERMINAL_NUMBER=as.numeric(TERMINAL_NUMBER)) %>%
select(Station=TERMINAL_NUMBER,LATITUDE,LONGITUDE)),
by="Station")
workshop<- station_daily %>% filter(`Start station number`==0) %>%
group_by(Date) %>%
summarise(In=sum(n),.groups = 'drop') %>%
left_join( station_daily %>% filter(`End station number`==0) %>%
group_by(Date) %>%
summarise(Out=sum(n),.groups = 'drop'), by="Date")
stat <- trip_data %>% mutate(year=lubridate::year(`Start date`)) %>% filter(year==2017)
daily_demand <- trip_data %>% mutate(Date =as_date(`Start date`)) %>%
group_by(Date,`Member type`) %>% summarise(n=n(),.groups = 'drop') %>%
pivot_wider(id_cols = Date,names_from = `Member type`,values_from = n) %>%
left_join(public_holidays,by="Date") %>%
mutate(Total=Casual+Member,
day=lubridate::wday(Date,label=TRUE),
weekend=if_else((day %in% c("Sat","Sun")),TRUE,FALSE),
season=metR::season(month(Date)),
public_holiday=if_else(is.na(Event),FALSE,TRUE,missing=FALSE)
) %>%
left_join(seasons,by="season") %>%
mutate(season=season_name) %>% select(-season_name) %>%
left_join(weather_data,by="Date") %>%
mutate(too_hot=(TMAX>=35 | TAVG>=30),
too_cold=(TMIN<=-5 | TAVG<=4),
too_windy=(AWND>30 |WSF2 >=50),
too_rainy=(PRCP>15 |SNOW >10),
too_bad = if_else( WT01>4 |
WT02>0 |
WT03>2 |
WT04>4 |
WT05>0 |
WT06>0 |
WT08>5 |
WT09>3 |
WT11>0, TRUE,FALSE),
Good_Weather=ifelse(!too_hot & !too_cold & !too_windy & !too_rainy & !too_bad,'Good','Bad')
) %>%
pivot_longer(cols=c("Casual","Member"),names_to = "type", values_to = "trips") %>%
select(-Total)
daily_demand$Good_Weather <- factor(daily_demand$Good_Weather, levels=c('Good','Bad'))
daily_demand$type <- factor(daily_demand$type, levels=c('Casual','Member'))
daily_demand$season <- factor(daily_demand$season, levels=c('winter','spring','summer','autumn'))
bikes_per_day<-trip_data %>%
mutate(Date=as_date(`Start date`)) %>%
group_by(Date,`Bike number`) %>%
summarise(n=n(),.groups = 'drop') %>% ungroup() %>%
select(-n) %>% group_by(Date) %>%
summarise(bikes=n(),.groups = 'drop') %>% ungroup()
station_capacity <- station_data$data$stations %>% select(Station=short_name,capacity,lon,lat)
rm(station_data)
bikes_per_station_start <- trip_data %>%
mutate(Date =as_date(`Start date`)) %>%
group_by(`Start station number`,Date,`Bike number`) %>%
summarise(n=n(),duration=mean(Duration),.groups = 'drop') %>% ungroup() %>% select(-n) %>%
group_by(`Start station number`,Date) %>%
summarise(bikes=n(),duration=mean(duration),.groups = 'drop') %>% ungroup() %>%
select(Date,Station=`Start station number`,start_bikes=bikes,duration)
bikes_per_station_start <- bikes_per_station_start %>%
left_join(station_capacity,by="Station") %>%
filter(!is.na(capacity)) %>%
mutate(start_cap=round(start_bikes/capacity,2))
stations <- bikes_per_station_start %>% pull(Station) %>% unique(.)
if(exists("station_demand_mod")){
rm(station_demand_mod)
}
for(i in 1:length(stations)){
station_demand_mod_i <- weather_data %>%
left_join((bikes_per_station_start %>%
filter(Station %in% stations[i])),
by="Date") %>%
mutate(start_cap=ifelse(is.na(start_cap),0,start_cap),
Station=ifelse(is.na(Station),stations[i],Station)
)
if(exists("station_demand_mod")){
station_demand_mod <- rbind(station_demand_mod,station_demand_mod_i)
}else{
station_demand_mod <- station_demand_mod_i
}
}
rm(i,stations,station_demand_mod_i)
station_demand_mod_backup <- station_demand_mod
station_filter <- station_demand_mod_backup %>%
filter(!(start_cap==0)) %>%
group_by(Station) %>%
summarise(n=n(),.groups = 'drop') %>%
arrange(n)
station_demand_mod_remnant <- station_demand_mod_backup %>%
left_join(station_filter,by="Station") %>%
mutate(n=ifelse(is.na(n),0,n))%>%
filter(n<0)
station_demand_mod <- station_demand_mod_backup %>%
filter(!(Station %in% (station_demand_mod_remnant %>%
pull(Station) %>% unique(.))))
resolution <- 0.3
station_demand_mod <- station_demand_mod %>%
left_join(station_demand_mod %>%
group_by(Station) %>%
summarise(avg_cap=round(mean(start_cap),2), .groups = 'drop'),
by="Station") %>%
mutate(start_cap_m=start_cap - avg_cap) %>%
mutate(start_cap_m=round(start_cap_m/resolution)*resolution)
station_demand_mod <- station_demand_mod %>%
left_join(public_holidays,by="Date") %>%
mutate(condition1=WT04+WT05+WT06+WT08+WT09+WT11) %>%
mutate(day=lubridate::wday(Date,label=TRUE),
weekend=if_else((day %in% c("Sat","Sun")),TRUE,FALSE),
month=lubridate::month(Date,label=TRUE),
season=metR::season(month(Date)),
public_holiday=if_else(is.na(Event),FALSE,TRUE,missing=FALSE)
) %>%
left_join(seasons,by="season") %>%
mutate(season=season_name) %>% select(-season_name) %>%
mutate(workday=as.numeric(!public_holiday & !weekend))
station_demand_mod1 <- station_demand_mod
station_profile <- station_demand_mod1 %>% group_by(Station) %>% summarise(min=min(start_cap_m),
max=max(start_cap_m),
median=median(start_cap_m),
zeros=sum(start_cap==0),
sd=sd(start_cap_m),
.groups = 'drop') %>% ungroup()
station_profile1 <- station_profile
cluster_n <-8
sp <- station_profile1 %>% select(-Station)
###The, we run GMM as per the manual
fit <- kmeans(sp, cluster_n, iter.max = 50, nstart = 1)
station_profile1$group <- fit$cluster
station_profile <- station_profile %>%
left_join(station_profile1 %>% select(Station,group),by="Station") %>%
mutate(group=ifelse(is.na(group),cluster_n+1,group)) %>%
select(Station,group)
}
trips_stat <- round(nrow(stat)/10^6,2)
bike_fleet <- length(stat %>% pull(`Bike number`) %>% unique(.))
}
```
## Introduction and Purpose
:::::: {.columns}
::: {.column width="60%"}
As the Council-owned bike sharing scheme for the City, *BikeShare* has the mission to provide a reliable, cost-effective bicycle sharing service across the Metropolitan area. In 2017 alone, *BikeShare* served over
`r sprintf("%.1f %%", trips_stat)` million trips with a fleet of `r formatC(bike_fleet, format="f", big.mark=",", digits=0)` bicycles.
As of today, bike repairs only occur when a bike is detected broken. Apart from the obvious impact on customer satisfaction when a bike breaks, this model is also inefficient from a workshop’s workload perspective, affecting workload and time to repair.
In order to address, this BikeShare can tap into the existing operational data and use analytics to optimise this problem.
:::
::: {.column width="40%"}
```{r traffic, echo=FALSE,include=FALSE}
fill.breaks <- quantile(station_demand$daily_avg,seq(0,1,0.2))
fill.colors <- RColorBrewer::brewer.pal(length(fill.breaks),"YlOrRd")
station_demand$`Daily Avg Traffic` <- cut(station_demand$daily_avg, fill.breaks)
station_demand <- station_demand %>% filter(!is.na(`Daily Avg Traffic`) &
!is.na(LONGITUDE) &
!is.na(LATITUDE))
bbox <- make_bbox(LONGITUDE, LATITUDE, station_demand, f = 0.005)
m <- get_map(bbox, source = "stamen", zoom = 13)
map <- ggmap(m) +
geom_point(data=station_demand,aes(x = LONGITUDE, y = LATITUDE,colour=`Daily Avg Traffic`)) +
labs(title="Daily trips per Station") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) +
scale_color_manual(values = fill.colors,
breaks = waiver()) +
facet_wrap(~season)
rm(m,bbox,fill.colors,fill.breaks)
```
```{r map, dpi = 800, dev.args = list(bg = 'transparent')}
map
```
:::
::::::
## Data analysis of fleet and station utilisation
:::::: {.columns}
::: {.column width="40%"}
```{r}
fleet_size <- nrow(stat %>% select(`Bike number`) %>% unique(.) )
zeros_stat <- round(nrow(station_profile1 %>% filter(zeros<=20))*100/nrow(station_profile1),0)
zeros_stat2 <- round(nrow(station_profile1 %>%
filter(zeros>=nrow(weather_data)*1/2))*100/nrow(station_profile1),0)
quarter_stat <- round(nrow(station_demand_mod_backup %>% group_by(Station) %>% summarise(avg_cap=mean(start_cap),.groups="drop") %>%
filter(avg_cap<0.25))/nrow(station_demand_mod_backup %>% select(Station)%>% unique(.))*100,0)
```
However, when further analysis has been conducted, it has become apparent that the previous problem statement is **not** relevant:
* The fleet (`r sprintf("%.0f %%", fleet_size) ` bicycles) exceeds by far its daily demand.
* Only a fraction (`r zeros_stat` %) of all stations are used daily (or nearly daily). Over a quarter (`r zeros_stat2 ` %) of all stations are half of the time or less.
* Nearly `r quarter_stat` percent of the stations operate at an average utilisation of less than 25%, while a similar amount operates with an average utilisation of over 75%.
:::
::: {.column width="60%"}
```{r}
dl <- daily_demand %>% group_by(Date) %>%
summarise(trips=sum(trips),.groups = 'drop') %>%
ungroup() %>%
left_join(bikes_per_day,by="Date")
graph1 <- dl %>% ggplot(aes(trips,bikes)) + geom_point(color="lightblue") +
geom_hline(yintercept=fleet_size, linetype="dashed", color = "red") +
theme_fivethirtyeight() +
labs(title="Daily trips and unique bicycle use - 2017 and 2018",
x="Daily Trips",
y="Unique number of bikes used each day") +
theme(legend.position = "bottom",
plot.title = element_text(size=12),
axis.title.x = element_text(size = 10),
axis.text.x = element_text(angle = 0, hjust = 1,size = 8),
axis.title.y = element_text(size = 10),
axis.text.y = element_text(size = 8),
strip.text.x = element_text(size = 8),
strip.text.y = element_text(size = 8, angle = 90),
legend.title=element_text(size=8),
legend.text=element_text(size=8)) +
scale_y_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))+
scale_x_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))
graph2 <- station_profile1 %>% ggplot(aes(zeros))+
geom_histogram( colour="navy", fill="lightblue",binwidth = 200) +
theme_fivethirtyeight() +
labs(title="Distribution of Stations by number of days with zero used",
x="Number of day with zero bikes used",
y="Unique bike stations") +
theme(legend.position = "bottom",
plot.title = element_text(size=12),
axis.title.x = element_text(size = 10),
axis.text.x = element_text(angle = 0, hjust = 1,size = 8),
axis.title.y = element_text(size = 10),
axis.text.y = element_text(size = 8),
strip.text.x = element_text(size = 8),
strip.text.y = element_text(size = 8, angle = 90),
legend.title=element_text(size=8),
legend.text=element_text(size=8)) +
scale_y_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))+
scale_x_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))
graph1 / graph2
```
:::
::::::
## Further Analysis : There is a geographic station gap.
:::::: {.columns}
::: {.column width="60%"}
In order to perform further analysis, bike stations were clustered using their key statistical measures. After reviewing the results, they are were aggregated into three distinct usage groups:
* **High use**. These stations are frequently use and during summer they may carry several times their capacity. They exhibit the highest seasonality and variation. They are mostly located in the inner city and dense suburban pockets, and they are close to each other.
* **Medium use**. Stations with lower average usage compared with the high use group. They have a slightly lower seasonality, They are located in inner city districts (sometime opposite the street from high use locations) or along peri-urban commuting corridors and dense near-city suburban centres.
* **Low use**. These stations have very low usages, and many of them go unused for days. They are located in the city but also peripheral suburbs. The low volume of data makes it difficult to forecast demand.
It is interesting to note that stations of the three categories share the inner city. This makes any demand prediction challenging, due to utilisation influence by other variables difficult to quantify, such as proximity to key landmark of public transport exit.
:::
::: {.column width="40%"}
```{r echo=FALSE, warning=FALSE,message=FALSE}
map_data <- station_capacity %>%
left_join(station_profile1, by="Station") %>%
mutate(business_group=if_else(group==7,"high use",
ifelse(group==4,"medium use","low use"),
missing="low use"))
bbox <- make_bbox(LONGITUDE, LATITUDE, station_demand, f = 0.005)
m <- get_map(bbox, source = "stamen", zoom = 13)
map <- ggmap(m) +
geom_point(data=map_data,aes(x = lon, y = lat,colour=business_group)) +
labs(title="Station Groups") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) +
scale_color_discrete() + facet_wrap(.~ business_group)
map
```
:::
::::::
## Results and recommendations
Based on the previous analysis, we can confirm that maintenance planning and demand utilisation are not priority items to address, given the existing fleet size and location in each station.
Nevertheless, it has become clear that the points to tackle are the under-utilisation of the fleet and disparate station usage. Therefore, we present the below recommendations:
* Address the large number of spares by reducing the fleet or leveraging this asset in the below recommendations.
* Conduct experiments to redirect demand in inner city to medium and low use stations.
* After redirection attempts, optimise location and size or inner city stations. Optimise the peripheral network.
* Use excess capacity to deploy "pop up" stations on high usage events and locations (e.g. offer them as a public transport alternative during massive events, bring bikes to parks in spring).
* Explore innovate bike use models/service offerings, such as:
** Multi-day (weekend) trips for registered users.
** Allow suburban commuters to take bike home overnight - taking advantage of excess bikes.
** Allow bikes to be parked on regular bike parking spots - to reduce walking distances.
** Explore park&ride scheme with car parks in the inner city fringe.
** Explore ride & train scheme for suburbs.
## Appendix
The below slides were used in previous stages and are presented here for reference purposes
## Fleet maintenance volumes and windows need to be carefully chosen
:::::: {.columns}
::: {.column width="40%"}
In order to **<span style='color: brown;'>provide a good service</span>** for the users of the bike sharing scheme, **<span style='color: brown;'>unavailability needs to be avoided</span>**. Unavailability is caused by lack of maintenance or lack of supply. Thus, maintenance windows need to be selected **<span style='color: brown;'>smartly</span>**.
- Shared bikes are **<span style='color: brown;'>used every day of the week</span>**. Registered users provide a constant demand baseline.
- There is **<span style='color: brown;'>some seasonality</span>** in the demand: registered users brave through the winter keeping the numbers high.
- At first look, it seems that **<span style='color: brown;'>weather conditions are the biggest deterrent</span>** for bike usage. If it is too cold, or too hot or too windy casual users won't ride; registered users' numbers will also drop.
- Bikes will **<span style='color: brown;'>keep breaking</span>** - even if they are well built! Thus, **<span style='color: brown;'>every opportunity</span>** to undertake maintenance **<span style='color: brown;'>needs to be taken</span>**.
:::
::: {.column width="60%"}
```{r problem_statement_chart,dpi = 800, dev.args = list(bg = 'transparent'), echo = FALSE}
text_value <- "**<span style='color: brown;'>Challenge</span>**: Using data analytics, we can leverage the **<span style='color: brown;'>weather forecast </span>** to optimise the fleet **<span style='color: brown;'>maintenace without compromising availibilty SLAs</span>**."
g <-daily_demand %>%
ggplot(aes(x=day,y=trips,color=Good_Weather, group=1)) +
geom_jitter(shape=16, position=position_jitter(0.2)) +
facet_grid(type ~ season, space='free_y') +
theme_fivethirtyeight() +
scale_shape_tableau() +
scale_colour_tableau('Classic Color Blind') +
scale_y_continuous(labels = comma) +
theme(legend.position = "bottom",
plot.title = element_text(size=14),
axis.title.x = element_text(size = 10),
axis.text.x = element_text(angle = 90, hjust = 1,size = 8),
axis.title.y = element_text(size = 10),
axis.text.y = element_text(size = 8),
strip.text.x = element_text(size = 8),
strip.text.y = element_text(size = 8, angle = 90),
legend.title=element_text(size=8),
legend.text=element_text(size=8)) +
labs(title="Bike Demand per Season and User Type",
x="Day of the Week",
y="Bikes Rented") +
guides(col = guide_legend(ncol = 2),fill = guide_legend(title = "Weather", title.position = "left"))
t<- textbox_grob(
text_value,
gp = gpar(fontsize = 12),
box_gp = gpar(col = "grey80", fill = NA,lineend="round",lwd=3),
r = unit(10, "pt"),
padding = unit(c(10, 10, 10, 10), "pt"),
width = unit(1, "npc"),
# height = unit(1, "npc"),
margin = unit(c(10, 10,10, 10), "pt")
)
layout <- "
AAAA
AAAA
AAAA
AAAA
AAAA
####
CCCC
CCCC
"
g + t + plot_layout(design = layout)
rm(g,t,layout,text_value)
```
:::
::::::
## By addressing uneven bike utilisation, it is possible to extend the life of the fleet
:::::: {.columns}
::: {.column width="40%"}
Some bicycles in the fleet are heavily used almost every single days, while others are seldomly ridden. As a result:
- **<span style='color: brown;'>Heavily used bikes will be at higher risk of failure</span>** and accelerated asset ageing.
- **<span style='color: brown;'>Unused bikes will be at risk of neglect</span>** - e.g. left with flat tyres in an low traffic station.
By obtaining a detailed log of all bike travel, it will be possible to identify which bikes are being used the most and calculate their risk of failing. This will allow to proactively book them for service.
When those bike are taken out for service, they can be replaced with lower used bikes , evening asset utilisation.
:::
::: {.column width="60%"}
```{r bike usage, dpi = 800, dev.args = list(bg = 'transparent')}
t<- trip_data %>% mutate(Date=as_date(`Start date`)) %>%
group_by(Date,`Bike number`) %>%
summarise(trips=n(),.groups = 'drop') %>% ungroup()
t<- t %>% mutate(Month=format(as.Date(Date), "%Y-%m")) %>%
filter(Month %in% c("2017-05","2017-06","2017-07","2017-08"))
selection <- t %>% group_by(`Bike number`) %>% summarise(n=n(),.groups = 'drop') %>% ungroup() %>% arrange(-n)
selection2 <- rbind(rbind(selection[1:7,],
selection[(nrow(selection)-6):nrow(selection),]),
selection[(round(nrow(selection)/2,0)-3):(round(nrow(selection)/2,0)+3),])
bike_use <- t %>% filter(`Bike number` %in% selection2$`Bike number`) %>%
ggplot(aes(Date,`Bike number` , fill= trips)) +
geom_tile()+
scale_fill_gradient(low="lightblue", high="navy") +
theme_fivethirtyeight() +
labs(title="Daily trips - selected bikes from May to Aug 2017",
x="Date",
y="Bike Number") +
theme(legend.position = "bottom",
plot.title = element_text(size=12),
axis.title.x = element_text(size = 10),
axis.text.x = element_text(angle = 90, hjust = 1,size = 8),
axis.title.y = element_text(size = 10),
axis.text.y = element_text(size = 8),
strip.text.x = element_text(size = 8),
strip.text.y = element_text(size = 8, angle = 90),
legend.title=element_text(size=8),
legend.text=element_text(size=8))
rm(t,selection,selection2)
bike_use
```
:::
::::::
## Data Collection to Optimise Maintenance workload and bike availability
:::::: {.columns}
::: {.column width="40%"}
To achieve the objectives, the below data needs to be collected:
- [Detailed trip data, itemised per each trip taken by each bicycle](https://www.capitalbikeshare.com/system-data)
- [Weather Observations/Forecast for each day of the year](https://www.ncdc.noaa.gov/cdo-web/datasets)
- Bike failure rates.
- Workshop's repair turnaround stats.
:::
::: {.column width="40%"}
Based on the data, the below metrics will be generated:
```{r table}
table_data <- data.frame(Metric=c("Daily Service Capacity",
"Need to Service Factor"),
Description=c("Number of bikes that can be maintain any day, based on workshop capacity and predicted demand",
"Indicator whether a bike needs service ahead of probable failure"),
Source=c("Workshop Turnaround stats, Summary of daily trip data, weather",
"Detailed trip data")
)
table <- table_data %>% flextable() %>%
width(j=1,width=1.5) %>%
width(j=2:3,width=3) %>%
bold(i = NULL, j = 1, bold = TRUE, part = "body") %>%
bold(i = NULL, j = NULL, bold = TRUE, part = "header") %>%
align(i = NULL, j = NULL, align = "left", part = "all") %>%
border(
i = NULL,
j = NULL,
border = NULL,
border.top = NULL,
border.bottom = fp_border(color = "grey"),
border.left = NULL,
border.right = NULL,
part = "body"
)
knitr::knit_print(table)
```
These metrics (combined with spontaneous failure rates) should serve as input for a proactive service plan.
:::
::::::
## Data Sources
- Bike scheme data: https://www.capitalbikeshare.com/system-data
- Weather data: https://www.ncdc.noaa.gov/cdo-web/search;jsessionid=FC6D9E3A1F49486E29E7130142A60038