-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbikedata_main.R
390 lines (318 loc) · 15.1 KB
/
bikedata_main.R
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
####################### Spatial prediction of rented bikes in Bosten ######################
# Author: Patrick Sogno and Ronja Lappe
# Email: [email protected]/ [email protected]
#----------------------------------------Background---------------------------------------#
# Background: This code is part of a university project on spatial modeling and prediction.
#-------------------------------------Problem formulation---------------------------------#
# => Problem formulation: Where and when do we find a high density of rented &
# returned bikes? Which land use types seem to detemermine this distribution?
# can we predict the occurance of rented and returned bikes for other times or cities?
# => Hypothesis:
# 1. Bike rentals and returns are generally highest at public transport stations (pts)
# 2. Bike rentals at touristic places are highest in the afternoons and on weekends
# 3. Bike returns in the mornings are highest in business districts
# 4. Bike rentals in the mornings are highest at pts
# 5. Bike returns in the evenings are highest at pts
# 6. Bike rentals in the evenings are highest at business districts… ?
# 7. ...?
#----------------------------------------- Data sets -------------------------------------#
# rental stations
# bike rentals & returns (station, time, trip duration, gender, age, and more)
# OSM data (public transport stations (railway, bus), tourisim, leisure, buisness?)
# evtl. additional climate data...
#------------------------------------ Content of this code -------------------------------#
# 1. data download
# 2. data preparation
# 3. species distribution modelling
# 4. visualization
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#+++++++++++#
######------------------------------- 0. Set environment ----------------------------######
## paths
dirs <- new.env()
dirs$main_dir <- "D:/Patrick/Documents/Dokumente/SoSe2019/MET/SpatialModeling/bikedata"
## packages
loadandinstall <- function(mypkg) {if (!is.element(mypkg, installed.packages()[,1])){install.packages(mypkg)}; library(mypkg, character.only=TRUE) }
packagelist <- as.list(c('bikedata', 'dplyr', 'rgdal', 'raster', 'sdm',
'data.table', 'dodgr', 'tidyverse', 'osmdata',
'osmplotr', 'OpenStreetMap', 'ellipse', 'lubridate',
'plotrix', 'scales', 'plotKML', 'colorspace', 'rasterVis'))
lapply(packagelist, function(x) loadandinstall(x))
## parameters
city <- 'bo' # Bosten(bo), Chicago(ch), Washington, D.C.(dc), Los Angeles(la), London(lo),
# Minnesota(mn), New York City(ny), Philadelphia(ph), San Fransico(sf)
city_osm <- 'boston'
dates <- 201701:201712 # only May 2017, timespan also possible, e.g. 201705:201708
mycrs <- CRS("+proj=longlat +datum=WGS84")
######------------------------- 1. Find, download & load data -----------------------######
## bike data (if more than one month... use list and write as loop)
bikedata_dl_zip <- as.list(dl_bikedata(city=city,data_dir = dirs$main_dir,
dates = dates)) # download
bikedata_dl <- lapply(bikedata_dl_zip, function(x) unzip(x,exdir = dirs$main_dir)) # unzip
bikedata <- lapply(bikedata_dl, function(x) read.csv(file.path(dirs$main_dir,basename(x)))) # read into R
### extract stations
store_bikedata(bikedb = 'bikedb', city = city, data_dir = dirs$main_dir, dates = dates) # create database of downloaded bikedata
stations <- bike_stations(bikedb = 'bikedb', city = city) # extract stations for selected city from database
stations <- as.data.frame(stations)
## OSM land use (could also be simplified I guess)
# railway stations
preds <- new.env() # create new environment for predictors
preds$c1 <- opq(bbox = city_osm) %>% # query from OSM
add_osm_feature(key = "public_transport") %>%
add_osm_feature(key = "railway") %>%
osmdata_xml(file = "OSMcommuter.osm", quiet = F) # save on harddisk
preds$c1 <- sf::st_read('OSMcommuter.osm', layer = 'points', quiet = F)# read into R
preds$c1 <- as(preds$c1, 'Spatial') # convert to spatial
plot(preds$c1,col="#009900")
points(preds$c1,col="#009900",pch=20)
# touristic places
preds$t <- opq(bbox = city_osm) %>%
add_osm_feature(key = "tourism") %>%
osmdata_xml(file = "OSMtourist.osm", quiet = F)
preds$t <- sf::st_read('OSMtourist.osm', layer = 'points', quiet = F)
preds$t <- as(preds$t, 'Spatial')
points(preds$t, col="red",pch=20)
# Offices
preds$o <- opq(bbox = city_osm) %>%
add_osm_feature(key = "office") %>%
osmdata_xml(file = "OSMoffice.osm", quiet = F)
preds$o <- sf::st_read('OSMoffice.osm', layer = 'points', quiet = F)
preds$o <- as(preds$o, 'Spatial')
plot(preds$o, col="red",add=T)
points(preds$o,col="blue",pch=20)
# university/college buildings
preds$u <- opq(bbox = city_osm) %>%
add_osm_feature(key = "amenity", value = c("university","college","research_institute")) %>%
osmdata_xml(file = "OSMuniversity.osm", quiet = F)
preds$u <- sf::st_read('OSMuniversity.osm', layer='points',quiet = F)
preds$u <- as(preds$u, 'Spatial')
points(preds$u,col="purple",pch=20)
available_features()
# possible further predictors?
######------------------------------- 2. Data preparation ---------------------------######
#--------------------------------- 2.1 Rasterize predictors ------------------------------#
# (could probably also be done more efficiently using lists and for-loops ;))
r <- new.env() # create new environment for distance rasters
stations_sp <- stations
coordinates(stations_sp) <- c("longitude","latitude")
proj4string(stations_sp) <- mycrs
e <- extent(stations_sp) # use spatial extent of projected bike stations
# railway stations
r$c1 <- raster(e, ncols = 400, nrows = 400) # For commuter land use 1
proj4string(r$c1) <- mycrs
r$c1 <- distanceFromPoints(object = r$c1, xy = preds$c1)
# touristic places
r$t <- raster(e, ncols = 400, nrows = 400) # For tourist land use
proj4string(r$t) <- mycrs
r$t <- distanceFromPoints(object = r$t, xy = preds$t)
plot(r$t)
# offices
r$o <- raster(e, ncols = 400, nrows = 400) # For leisure land use
proj4string(r$o) <- mycrs
r$o <- distanceFromPoints(object = r$o, xy = preds$o)
plot(r$o)
# universities
r$u <- raster(e, ncols = 400, nrows = 400) # For leisure land use
proj4string(r$u) <- mycrs
r$u <- distanceFromPoints(object = r$u, xy = preds$u)
plot(r$u)
# create raster stack
r_l <- as.list(r)
r_stack <- stack(r_l)
writeRaster(r_stack,file.path(dirs$main_dir,"distanceStack.tif"),overwrite=T)
#-------------------------------- 2.2 Manipulate bike dataset ----------------------------#
# create one occurence dataframe from list of bikedata frames
for (i in 1:length(bikedata)) {
if (i == 1) {
occ <- bikedata[[i]]
}else{
occ <- rbind(occ, bikedata[[i]])
}
}
# split date-time column
occ$starttime <- ymd_hms(occ$starttime)
occ$stoptime <- ymd_hms(occ$stoptime)
occ$start_year <- year(occ$starttime)
occ$start_month <- month(occ$starttime)
occ$start_day <- day(occ$starttime)
occ$start_wday <- wday(occ$starttime, # wday just started with 1, even though 1.1.17 was a Sunday
label = F, # this settings solve it and start with sunday acoordingly
week_start = getOption("lubridate.week.start",1),
locale = Sys.getlocale("LC_TIME"))
occ$start_hour <- hour(occ$starttime)
occ$stop_year <- year(occ$stoptime)
occ$stop_month <- month(occ$stoptime)
occ$stop_day <- day(occ$stoptime)
occ$stop_wday <- wday(occ$stoptime,
label = F,
week_start = getOption("lubridate.week.start",1),
locale = Sys.getlocale("LC_TIME"))
occ$stop_hour <- hour(occ$stoptime)
# create 2 spatial dataframe with trip_start and trip_end points
occ_s <- occ
coordinates(occ_s) <- c("start.station.longitude", "start.station.latitude")
proj4string(occ_s) <- mycrs
occ_e <- occ
coordinates(occ_e) <- c("end.station.longitude", "end.station.latitude")
proj4string(occ_e) <- mycrs
#------------------------------ 2.3 Subset bike dataset by time --------------------------#
# trip start at 8am, 4 pm and 8pm during the week... for trips less than 1 hour...
my_occ_s <- list()
names(occ_s)
my_occ_s[[1]] <- occ_s[which(occ_s$start_hour == 8 & occ_s$start_wday <=5 &
occ_s$tripduration < 3600 & occ_s$start_month == 5),]
my_occ_s[[2]] <- occ_s[which(occ_s$start_hour == 16 & occ_s$start_wday <=5 &
occ_s$tripduration < 3600& occ_s$start_month == 5),]
my_occ_s[[3]] <- occ_s[which(occ_s$start_hour == 20 & occ_s$start_wday <=5 &
occ_s$tripduration < 3600& occ_s$start_month == 5),]
my_occ_s[[4]] <- occ_s[which(occ_s$start_month >= 4 & occ_s$start_month <= 9 &
occ_s$start_hour >= 10 & occ_s$start_wday > 5 &
occ_s$tripduration > 3600),]
# trip ends
my_occ_e <- list()
names(occ_e)
my_occ_e[[1]] <- occ_e[which(occ_e$stop_hour == 8 & occ_e$start_wday <=5 &
occ_e$tripduration < 3600& occ_s$start_month == 5),]
my_occ_e[[2]] <- occ_e[which(occ_e$stop_hour == 16 & occ_e$start_wday <=5 &
occ_e$tripduration < 3600& occ_s$start_month == 5),]
my_occ_e[[3]] <- occ_e[which(occ_e$stop_hour == 20 & occ_e$start_wday <=5 &
occ_e$tripduration < 3600& occ_s$start_month == 5),]
my_occ_e[[4]] <- occ_s[which(occ_e$start_month >= 4 & occ_e$start_month <= 9 &
occ_e$start_hour >= 10 & occ_e$start_wday > 5 &
occ_e$tripduration > 3600),]
#----------------------- 2.4 Create presence and absence at stations ---------------------#
# trip starts
pres_s <- list()
for(i in 1:length(my_occ_s)){
frq <- table(my_occ_s[[i]]$start.station.name)
frq <- as.data.frame(frq)
names(frq) <- c("name","freq")
st_frq <- base::merge(stations,frq,all=T)
st_frq <- na.omit(st_frq)
coordinates(st_frq) <- c("longitude","latitude")
proj4string(st_frq) <- mycrs
pres_s[[i]] <- st_frq
}
# trip ends
pres_e <- list()
for(i in 1:length(my_occ_e)){
frq <- table(my_occ_e[[i]]$end.station.name)
frq <- as.data.frame(frq)
names(frq) <- c("name","freq")
st_frq <- base::merge(stations,frq,all=T)
st_frq <- na.omit(st_frq)
coordinates(st_frq) <- c("longitude","latitude")
proj4string(st_frq) <- mycrs
pres_e[[i]] <- st_frq
}
#--------------------------------- 2.4 Check for collinearity ----------------------------#
# visual inspection of collinearity
cm <- cor(getValues(r_stack), use = "complete.obs")
plotcorr(cm, col=ifelse(abs(cm) > 0.7, "red", "grey"))
######----------------------- 3. Species distribution model -------------------------######
# trip starts
p_s <- list()
for(i in 1:length(pres_s)){
d <- sdmData(formula = freq ~ c1+o+t+u, train = pres_s[[i]], predictors = r_stack)
m <- sdm(freq~., data = d, methods = c('rf','svm')) # fit species distribution model
p_s[[i]] <- predict(m, newdata = r_stack, overwrite = T) # predict
}
# trip ends
p_e <- list()
for(i in 1:length(pres_e)){
d <- sdmData(formula = freq ~ c1+o+t+u, train = pres_e[[i]], predictors = r_stack)
m <- sdm(freq~., data = d, methods = c('rf','svm')) # fit species distribution model
p_e[[i]] <- predict(m, newdata = r_stack, overwrite = T) # predict
}
######------------------------- 4. Visualization functions ---------------------------#####
#-------------------------------- 4.1 Non-spatial visualization --------------------------#
# simple line plots showing the frequency of bike usage in 2017
ts <- list()
ts[[1]] <- as.data.frame(table(occ$start_month)) # per month
ts[[2]] <- as.data.frame(table(occ$start_wday)) # per week (wday had to be changed above)
ts[[3]] <- as.data.frame(table(occ$start_hour)) # per hour
ts_pl <- list()
for (i in 1:length(ts)){
ts_pl[[i]] <- ggplot(ts[[i]],aes(x=Var1, y=Freq,group=1)) +
geom_line()+
geom_point()
}
ts_pl[[1]]
ts_pl[[2]]
ts_pl[[3]]
# I'll make them more fancy later
#--------------------------------- 4.2 Spatial visualization -----------------------------#
plot(p_e[[4]])
# base map
e_l <- as.list(e) # save station extent as list to use it as input for the openmap
map <- openmap(upperLeft = e_l[c(4,1)], lowerRight = e_l[c(3,2)], type = "stamen-toner", mergeTiles = T)
#map <- openproj(map, projection = mycrs)
plot(map)
# reproject to osm():
plotable.p_s <- lapply(p_s, function(x) reproject.RasterBrick(x, CRS = osm()))
plotable.p_e <- lapply(p_e, function(x) reproject.RasterBrick(x, CRS = osm()))
plotable.pres_s <- lapply(pres_s, function(x) spTransform(x, osm()))
plotable.pres_e <- lapply(pres_e, function(x) spTransform(x, osm()))
# function to plot frequency per station
plot.pres <- function(map, i) {
plot(map)
plot(plotable.pres_s[[i]], # start wdays 8 am
cex=pres_s[[i]]$freq/40,
col = scales::alpha("blue", 0.4),
pch = 20, add =T)
plot(plotable.pres_e[[i]], # end wdays 8 am
cex=pres_e[[i]]$freq/40,
col = scales::alpha("red", 0.4),
pch = 20, add =T)
}
#test it :D
# function to plot predicted frequency
plot.single_pred <- function(map, i, tag) {
if (tag == "s") {
plot(map)
plot(plotable.p_s[[i]]$id_2.sp_1.m_svm, alpha=0.4,add=T)
}else{
if (tag == "e") {
plot(map)
plot(plotable.p_e[[i]]$id_2.sp_1.m_svm, alpha=0.4,add=T)
}else{
plot(map)
return("Choose 's' for start or 'e' for end as 'tag'.")
}
}
}
# function to plot predicted frequency of start (blue) and end (red) as combo for a scenario i
plot.combo_pred <- function(map, i) {
plot(map)
image(plotable.p_s[[i]]$id_2.sp_1.m_svm,
col = sequential_hcl(1000, h=255, power=0.3, rev = T, alpha = 0.5),
add = T, legend = F)
image(plotable.p_e[[i]]$id_2.sp_1.m_svm,
col = sequential_hcl(1000, h=0, power=0.3, rev = T, alpha = 0.5),
add = T, legend = F)
image(plotable.p_s[[i]]$id_2.sp_1.m_svm,
col = sequential_hcl(1000, h=255, power=0.3, rev = T, alpha = 0.3),
add = T, legend = F)
}
# function to plot predicted frequency as rgb
plot.multi_pred <- function(map, first, mid, last, tag, alpha.value) {
if (tag == "s") {
plot(map)
p_s_stack <- stack(plotable.p_s[[first]],plotable.p_s[[mid]],plotable.p_s[[last]])
plotRGB(p_s_stack,1,3,5,stretch="lin",alpha=alpha.value,add=T)
}else{
if (tag == "e") {
plot(map)
p_e_stack <- stack(plotable.p_e[[first]],plotable.p_e[[mid]],plotable.p_e[[last]])
plotRGB(p_e_stack,1,3,5,stretch="lin",alpha=alpha.value,add=T)
}else{
plot(map)
return("Choose 's' for start or 'e' for end as 'tag'.")
}
}
}
#--------------------------------------- 4.2 Map making --------------------------------------#
plot.pres(map, 4)
plot.single_pred(map, 1, "s")
plot.multi_pred(map, 1,2,3, "s", 100)
plot.combo_pred(map, 2)
#--------------------------------------- 4.3 Plots --------------------------------------#