-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.Rmd
530 lines (489 loc) · 17.9 KB
/
main.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
---
title: "cmp713-project"
author: Hazem Alabiad & Betül Bayrak
date: 25/01/2021
output: rmarkdown::html_vignette
---
# Introduction
Tipping someone is a common thing in our community, especially if this person is a taxi driver, waiter, or delivery officer; The tip amount varies depending on many conditions; In the case of a taxi driver, it might depend on the passenger count, trip distance, trip hour, et cetera.
In this project, we aim to study and predict the tip percentage of the total amount paid to taxi drivers. We used the data provided by NYC Taxi & Limousine Commission (TLC) from this [link](https://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page).
First, we explore our data to know what our data looks like, find potential outliers, obtain general and basic statistics, for instance, observations count, variables count, NAs' count, et cetera.
Second, we apply what is called data pre-processing and data engineering that include removing unnecessary or NAs' rows, columns, convert the data type to a more suitable one and introducing new variables to get our data ready to be fed our model that is going to predict the tip amount given other variables.
Third, we build our models that utilize the pre-processed, clean data to predict a variable "tip percentage in our case".
Lastly, we measure the performance or accuracy of our model using many approaches and mathematical formulas and plot the predictions vs. actual tip percentage to decide on which model did the best.
# Loading Libraries
```{r}
library(randomForest)
library(ggmap)
library(h2o)
library(gridExtra)
library(dplyr)
library(ggplot2)
library(nnet)
library(hrbrthemes)
library(rpart.plot)
library(mlbench)
library(e1071)
library(rpart)
library(DMwR2)
library(caret)
library(Metrics)
library(fpc)
library(Hmisc)
library(tibble)
library(knitr)
library(MLmetrics)
library(earth) # fit MARS models
```
# Custom Functions
```{r}
`%notin%` <- Negate(`%in%`)
pretty_df_print <- function (df) kable_styling(kable(df))
top_least_freq_and_neg_vals <- function (col) {
t <- table(col) %>% as.data.frame() %>% arrange(desc(Freq))
non_pos <- as.data.frame(col)
non_pos <- non_pos[non_pos <=0, ]
print("Top frequent values:")
print.data.frame(head(t))
print("Least frequent values:")
print.data.frame(tail(t))
print("Negative values:")
print.data.frame(non_pos)
print("/////////////////////////////////////////////////////")
}
na_count_df <- function (df) {
return(as.data.frame(t(data.frame(sapply(df, function(col) sum(is.na(col)))))))
}
iqr_outliers_min_max <- function(df) {
iqr_df <- as.data.frame( t(data.frame(df %>% sapply(function (x) {quantile(x, c(0.25, 0.75))}))) ) %>% cbind((data.frame(df %>% sapply(IQR))))
colnames(iqr_df) <- c("Q1", "Q3", "IQR")
iqr_df$minVal <- iqr_df$Q1 - 1.5*iqr_df$IQR
iqr_df$maxVal <- iqr_df$Q3 + 1.5*iqr_df$IQR
print.data.frame(iqr_df %>% select("minVal", "maxVal"))
}
iqr_outliers_finder <- function(x){
Q1 <- quantile(x, 0.25)
Q3 <- quantile(x, 0.75)
IQR <- Q3 - Q1
Vl <- Q1 - 1.5 * IQR
Vr <- Q3 + 1.5 * IQR
return (x[which(x < Vl | x > Vr)])
}
get_freq_df_from_vector <- function (vec, col.name) {
freq_df <- as.data.frame(table(vec))
colnames(freq_df)[1] <- col.name
freq_df <- freq_df[order(freq_df[,1], decreasing = T),]
if(nrow(freq_df) <= 10) {
print.data.frame(freq_df)}
else {
cat("Highest values :\n")
print.data.frame(head(freq_df))
cat("Lowest values :\n")
print.data.frame(tail(freq_df))
}
}
dbscan_outliers_finder <- function(data, ...) {
require(fpc, quietly=TRUE)
cl <- dbscan(data, ...)
posOuts <- which(cl$cluster == 0)
list(positions = posOuts,
outliers = data[posOuts,],
dbscanResults = cl)
}
```
# Data loading
```{r}
st <- proc.time()
green <- read.csv("data/green_tripdata_2020-06.csv")
green <- green %>% rbind(read.csv("data/green_tripdata_2020-01.csv"))
print(proc.time() - st)
cat('There are [', nrow(green), '] observations, and [',ncol(green),'] variables')
```
# Data exploratory & Preprocessing
## Dataframe head
```{r}
head(green)
```
## Dataframe tail
```{r}
tail(green)
```
## Brief info on the data
```{r}
str(green)
```
## Unnecessary Variables Deletion
```{r}
num.vars <- ncol(green)
green <- green %>% subset(select = -c( VendorID, store_and_fwd_flag))
cat("[", num.vars - ncol(green) , "] variables were delete")
```
## Summary of the data
```{r}
describe(green)
```
## Count NAs' in each column
```{r}
na_count_df(green)
```
## Remove NAs'
As we can see from the output above:
`passenger_count`, `payment_type`, `trip_type`, `congestion_surcharge` have ~ (24 K) NAs'
`fare_amount`, `extra`, `mta_tax`, `tip_amount`, `tolls_amount`, `improvement_surcharge`, `total_amount` have negative values.
Negative values: we believe have been entered mistakenly as negative values so, we need to fix this problem by obtaining the absolute values.
NAs': We need to drop rows with NAs as they critical in the classification.
As well as, we need to remove `ehail_fee` variable as it's all NAs'.
For `tip_amount` we realized that there are many `0` values, we need to get rid of them.
```{r}
green <- green %>% within(rm(ehail_fee))
num.rows <- nrow(green)
green <- green[!is.na(green$trip_type) & green$trip_distance != 0,]
cat("[", num.rows - nrow(green), "] observations were deleted out of [", num.rows, "]",
"which means ~ (", round((num.rows - nrow(green))/num.rows, digits = 2), ")")
```
```{r}
as.data.frame(t(data.frame(sapply(green, function(col) sum(is.na(col))))))
```
## Check top & least frequent and negative values
```{r}
describe(green)
```
## Handling `0` values
`passenger_count` variable has some `0` which must be an input error. We can fix this issue by replacing `0` with the rounded mean of all records.
`total_amount` variable has `0` which cannot be handled nor replaced. So, we go with deleting them.
```{r}
avg <- as.integer(round(mean(green$passenger_count), digits = 0))
green$passenger_count <- ifelse(green$passenger_count != 0, green$passenger_count, avg)
green <- green[green$total_amount > 0,]
```
```{r}
str(green)
```
## Convert the negative values to positive by obtaining `abs`
```{r}
green$fare_amount <- abs(green$fare_amount)
green$extra <- abs(green$extra)
green$mta_tax <- abs(green$mta_tax)
green$tip_amount <- abs(green$tip_amount)
green$tolls_amount <- abs(green$tolls_amount)
green$improvement_surcharge <- abs(green$improvement_surcharge)
green$total_amount <- abs(green$total_amount)
```
## Convert categorical variables to factor type
```{r}
green$RatecodeID <- factor(green$RatecodeID)
green$PULocationID <- factor(green$PULocationID)
green$DOLocationID <- factor(green$DOLocationID)
green$payment_type <- factor(green$payment_type)
green$trip_type <- factor(green$trip_type)
```
```{r}
str(green)
```
## Detecting outliers
### Using IQR
After we calculate the outliers using IQR, we inspect each variable to double check and remove manually what we, as exports, believe that it is an outlier.
```{r}
iqr_outliers_min_max(green %>% select_if(is.numeric))
```
```{r}
system.time(iqr.outliers <- green %>% select_if(is.numeric) %>% lapply(iqr_outliers_finder))
```
Now, let us inspect variable values
### passenger_count
```{r}
get_freq_df_from_vector(iqr.outliers$passenger_count, "passenger_count")
```
We can remove observations with `passenger_count > 6` as it is not logical to fit 6 or more people in a taxi.
```{r}
num.obs <- nrow(green)
green <- green[green$passenger_count <= 6, ]
cat("[", num.obs-nrow(green), "] Observation have been deleted! Which is ~ (", round((num.obs-nrow(green))/num.obs, digits = 2), ")")
```
### trip_distance
```{r}
get_freq_df_from_vector(iqr.outliers$trip_distance, "trip_distance")
```
1. Drop the row with `trip_distance == 134121.5`
2. Take the absolute value of the negative ones
```{r}
green$trip_distance <- abs(green$trip_distance)
green <- green[green$trip_distance < 150, ]
num.obs <- nrow(green)
cat("[", num.obs-nrow(green), "] Observation have been deleted! Which is ~ (", round((num.obs-nrow(green))/num.obs, digits = 3), ")")
```
### `fare_amount`
```{r}
get_freq_df_from_vector(iqr.outliers$fare_amount, "fare_amount")
```
`753` is an outlier as the trip distance is only `7.49` => drop
`335.5` is an outlier as the trip distance is only `6.97` => drop
Others seem to be accepted
```{r}
green <- green[green$fare_amount %notin% c(753, 335.5),]
num.obs <- nrow(green)
cat("[", num.obs-nrow(green), "] Observation have been deleted! Which is ~ (", round((num.obs-nrow(green))/num.obs, digits = 3), ")")
```
### `extra`
```{r}
get_freq_df_from_vector(iqr.outliers$extra, "extra")
```
### `mta_tax`
```{r}
get_freq_df_from_vector(iqr.outliers$mta_tax, "mta_tax")
```
### `tip_amount`
```{r}
get_freq_df_from_vector(iqr.outliers$tip_amount, "tip_amount")
```
We remove observations with `tip_amount > 0.7 of the `total_amount`
```{r}
green <- green[(green$tip_amount/green$total_amount) <= 0.7,]
num.obs <- nrow(green)
cat("[", num.obs-nrow(green), "] Observation have been deleted! Which is ~ (", round((num.obs-nrow(green))/num.obs, digits = 3), ")")
```
### `tolls_amount`
```{r}
get_freq_df_from_vector(iqr.outliers$tolls_amount, "tolls_amount")
```
`96.12`, `48.88`, `35.0` and all values with percentage 0.9 or higher of the `total_amount` are outliers to be removed.
```{r}
green <- green[green$tolls_amount/green$total_amount < 0.5, ]
num.obs <- nrow(green)
cat("[", num.obs-nrow(green), "] Observation have been deleted! Which is ~ (", round((num.obs-nrow(green))/num.obs, digits = 3), ")")
```
### `improvement_surcharge`
```{r}
get_freq_df_from_vector(iqr.outliers$improvement_surcharge, "improvement_surcharge")
```
### `total_amount`
```{r}
get_freq_df_from_vector(iqr.outliers$total_amount, "total_amount")
```
### `congestion_surcharge`
```{r}
get_freq_df_from_vector(iqr.outliers$congestion_surcharge, "congestion_surcharge")
```
## Save valid data "after dropping outliers"
```{r}
system.time(saveRDS(green, file = "data/valid_data.rds"))
```
## Load valid data
```{r}
system.time(green <- readRDS("data/valid_data.rds"))
```
## Density distribution of `trip_distance`
```{r}
ggplot(green, aes(x=trip_distance)) +
geom_histogram(aes(y=..density..), binwidth=.1, colour="black", fill="white") +
geom_density(alpha=.2, colour="blue", fill="#000066")+ xlim(0, 15)
```
# Classification
## Add `mean` and `median` of `trip_distance` grouped `hour` of pickup time
```{r}
st <- proc.time()
green$pickup_hour <- as.integer(format(strptime(green$lpep_pickup_datetime, "%Y-%m-%d %H:%M:%S"),"%H"))
hourly_trip_distance <- data.frame( green %>%
group_by(pickup_hour) %>%
summarise(mean_trip_dist = mean(trip_distance),
median_trip_dist = median(trip_distance)) %>% ungroup())
head(hourly_trip_distance)
print(proc.time() - st)
```
```{r}
# Mean trip distance plot
m.trip.dist.plt <- ggplot(hourly_trip_distance, aes(x=pickup_hour, y=mean_trip_dist)) + geom_bar(stat = "identity")
# Median trip distance plot
M.trip.dist.plt <- ggplot(hourly_trip_distance, aes(x=pickup_hour, y=median_trip_dist)) + geom_bar(stat = "identity")
grid.arrange(m.trip.dist.plt, M.trip.dist.plt, ncol=1, nrow =2)
```
From above, we conclude that the longest trips are at `5` & `6` in the morning. In rest of the day's hours trips are somehow close in terms of distance traveled.
//////////////////////////////////////////////////////////////////////////
## `tip_percentage` is the tip percentage based on `total_amount`
```{r}
st <- proc.time()
green$tip_percentage <- ifelse(green$tip_amount==0.0 | green$total_amount==0.0 , 0.0,
round(green$tip_amount/green$total_amount,3))
# Remove all zero percentage as they are not going to help us in classification
num.obs <- nrow(green)
green <- green[green$tip_percentage > 0,]
cat("[", num.obs-nrow(green), "] Observation have been deleted! Which is ~ (", round((num.obs-nrow(green))/num.obs, digits = 3), ")")
print(proc.time() - st)
ggplot(green, aes(x=total_amount, y=tip_amount)) +
geom_point(
color="black",
fill="#69b3a2",
shape=22,
alpha=0.3,
size=3,
stroke =2
) +
theme_ipsum()
```
`geom_point` is useful when we want to compare two continuous variables.
//////////////////////////////////////////////////////////////////////////
```{r}
cat("Average tip percentage of the total amount ~ (",
round((sum(green$tip_amount)/sum(green$total_amount)),4)*100 ," %)")
```
## Histogram of `tip_percentage`
```{r}
hist(green$tip_percentage)
```
## Classification using Decision Tree
### Split data into training & testing
```{r}
sample.size <- floor(0.75*nrow(green))
s <- sample(seq_len(nrow(green)), sample.size)
numeic.cols <- green %>% select_if(is.numeric)
train.set <- numeic.cols[s,]
test.set <- numeic.cols[-s, ]
```
```{r}
dt.model <- rpartXse(tip_percentage ~ ., train.set, se = 0.5)
dt.predicted <- round(predict(dt.model, test.set), digits = 3)
saveRDS(file = "data/dt_model.rds", object = dt.model)
saveRDS(file = "data/dt_pred.rds", object = dt.predicted)
head(dt.predicted)
print(proc.time() - st, paste("\n"))
```
## Load DT Model
```{r}
dt.model <- readRDS("data/dt_model.rds")
dt.predicted <- readRDS("data/dt_pred.rds")
```
## Predicted vs original tip percentage using `Random Forest Tree`
```{r}
dt.perf.matrix <- as.data.frame(test.set$tip_percentage) %>% cbind(dt.predicted)
colnames(dt.perf.matrix) <- c("actual", "pred")
dt.perf.matrix["error"] <- dt.perf.matrix["actual"]-dt.perf.matrix["pred"]
dt.perf.matrix["error/actual"] <- abs(dt.perf.matrix["error"]/dt.perf.matrix["actual"])
head(dt.perf.matrix)
```
## Measuring performance using Mean Absolute Percentage Error (MAPE)
```{r}
dt.mape <- mean(dt.perf.matrix$`error/actual`)
cat("Error percentage: (", round(dt.mape, 4), ") \nSuccess percentage: (", round(100-dt.mape, 4),")")
```
```{r}
dt.mse <- mse(test.set$tip_percentage, dt.predicted)
dt.mae <- mae(test.set$tip_percentage, dt.predicted)
dt.rmse <- rmse(test.set$tip_percentage, dt.predicted)
dt.r2 <- R2(test.set$tip_percentage, dt.predicted, form = "traditional")
cat(" MAE:", dt.mae, "\n", "MSE:", dt.mse, "\n",
"RMSE:", dt.rmse, "\n", "R-squared:", dt.r2)
```
As we can see from above, if we accept only 3 digits after the point, which may cause some lose in data thus error, we obtained a very good success rate using RT.
//////////////////////////////////////////////////////////////////////////
## Using Support Vector Machine (SVM)
```{r}
st <- proc.time()
svm.model <- svm(tip_percentage ~ ., train.set)
svm.predicted <- round(predict(svm.model, test.set), digits = 3)
saveRDS(object = svm.model, file = "data/svm_model.rds")
saveRDS(object = svm.predicted, file = "data/svm_pred.rds")
print(proc.time() - st)
```
## Load SVM Model
```{r}
svm.model <- readRDS("data/svm_model.rds")
svm.predicted <- readRDS("data/svm_pred.rds")
```
## Measuring performance using Mean Absolute Percentage Error (MAPE)
```{r}
svm.perf.matrix <- as.data.frame(test.set$tip_percentage) %>% cbind(svm.predicted)
colnames(svm.perf.matrix) <- c("actual", "pred")
svm.perf.matrix["error"] <- svm.perf.matrix["actual"]-svm.perf.matrix["pred"]
svm.perf.matrix["error/actual"] <- abs(svm.perf.matrix["error"]/svm.perf.matrix["actual"])
head(svm.perf.matrix)
```
```{r}
svm.mape <- mean(svm.perf.matrix$`error/actual`)
cat("Error percentage: (", round(svm.mape, 4), ") \nSuccess percentage: (", round(100-svm.mape, 4),")")
```
```{r}
svm.mse <- mse(test.set$tip_percentage, svm.predicted)
svm.mae <- mae(test.set$tip_percentage, svm.predicted)
svm.rmse <- rmse(test.set$tip_percentage, svm.predicted)
svm.r2 <- R2(test.set$tip_percentage, svm.predicted, form = "traditional")
cat(" MAE:", svm.mae, "\n", "MSE:", svm.mse, "\n",
"RMSE:", svm.rmse, "\n", "R-squared:", svm.r2)
```
## Using Neural Network
```{r}
st <- proc.time()
nn.model <- nnet(tip_percentage ~ ., train.set,
linout=TRUE,
trace=FALSE,
size=6,
decay=0.01,
maxit=2000)
nn.pred <- predict(nn.model, test.set)
saveRDS(nn.model, "data/nn_model.rds")
saveRDS(nn.pred, "data/nn_pred.rds")
print(proc.time() - st)
```
## Load NN model
```{r}
nn.model <- readRDS("data/nn_model.rds")
nn.pred <- readRDS("data/nn_pred.rds")
```
```{r}
plot(test.set$tip_percentage, nn.pred)
abline(0, 1)
```
From the plot above, we realize that when the `tip_percentage` is larger the errors, or the residuals become larger.
///////////////////////////////////////////////////////////////////////
## Starting `H2O` Scalable platform that parallelize many machine learning algorithms
```{r}
system.time(h2oInstance <- h2o.init()) # start H2O instance locally
```
## Using `H2O` build a deep neural network with the following parameters
```{r}
st <- proc.time()
trH <- as.h2o(train.set,"trH")
tsH <- as.h2o(test.set,"tsH")
mdl <- h2o.deeplearning(x=1:11, y=12, training_frame=trH,hidden = c(100, 100, 100, 100, 100, 100, 100), epochs = 1000)
preds <- as.vector(h2o.predict(mdl,tsH))
print(proc.time() - st)
```
```{r}
mean(abs(preds - as.vector(tsH$tip_percentage)))
```
```{r}
plot(as.vector(tsH$tip_percentage), preds)
abline(0, 1)
```
```{r}
plot(as.vector(tsH$tip_percentage), preds)
points(as.vector(tsH$tip_percentage), preds, col = "red")
abline(0, 1)
```
```{r}
h2o.shutdown(prompt = F);
```
```{r}
mars1 <- earth(
tip_percentage ~ .,
data = train.set)
```
```{r}
print(mars1)
```
```{r}
summary(mars1) %>% .$coefficients #%>% head(10)
```
```{r}
plot(mars1, which = 1)
```
```{r}
optimal_tree <- rpart(
formula = tip_percentage ~ .,
data = train.set,
method = "anova",
control = list(minsplit = 11, maxdepth = 8, cp = 0.01)
)
plotcp(optimal_tree)
```
# Conclusion
We conclude that data is the chief factor that levels up the model's accuracy so, we first need to be aware of the data and understand it as much as possible. After that, we build the regression model using many available ones such as SVM, Decision Tree, and DNN to predict the tip percentage of the total paid amount given other variables. All the used models gave us similar accuracies of ~ 0.99 that indicates successful training using the provided data.