-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredictions.R
361 lines (271 loc) · 13.1 KB
/
predictions.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
load("../Rdata/shifts_design_matrix.Rdata")
library(lubridate)
library(dplyr)
library(ggplot2)
library(glmnet)
library(lubridate)
library(broom)
library(tidyr)
library(ROCR)
library(stargazer)
library(caret)
source("model_prediction_functions.R")
# PRE-DEFINING OUR FORMULAS FOR CLASSIFICATION AND REGRESSION #
formula1_class = as.formula(efficiency_category ~
hack_license)
formula2_class = as.formula(efficiency_category ~
as.factor(start_hour)*as.factor(is_week_end))
formula3_class = as.formula(efficiency_category ~
hack_license +
as.factor(start_hour)*as.factor(is_week_end))
formula4_class = as.formula(efficiency_category ~
hack_license +
as.factor(start_hour)*as.factor(is_week_end) +
avg_trip_time +
avg_trip_distance)
formula5_class = as.formula(efficiency_category ~
hack_license +
as.factor(start_hour)*as.factor(is_week_end) +
prcp)
formula6_class = as.formula(efficiency_category ~
hack_license +
as.factor(start_hour)*as.factor(is_week_end) +
avg_trip_time +
avg_trip_distance +
prcp)
formula1_reg = as.formula(efficiency ~
hack_license)
formula2_reg = as.formula(efficiency ~
as.factor(start_hour)*as.factor(is_week_end))
formula3_reg = as.formula(efficiency ~
hack_license +
as.factor(start_hour)*as.factor(is_week_end))
formula4_reg = as.formula(efficiency ~
hack_license +
as.factor(start_hour)*as.factor(is_week_end) +
avg_trip_time +
avg_trip_distance)
formula5_reg = as.formula(efficiency ~
hack_license +
as.factor(start_hour)*as.factor(is_week_end) +
prcp)
formula6_reg = as.formula(efficiency ~
hack_license +
as.factor(start_hour)*as.factor(is_week_end) +
avg_trip_time +
avg_trip_distance +
prcp)
############################################
#Added new columns for efficiency_category#
############################################
med = median(shifts_design_matrix$efficiency)
shifts_design_matrix <- shifts_design_matrix %>% mutate(t_ave = ((tmin+tmax)/2))
# 0 - LOW EFFICIENCY | 1 - HIGH EFFICIENCY
shifts_design_matrix = shifts_design_matrix %>%
mutate(efficiency_category = ifelse(efficiency < med, 0, 1))
###############################################
#REMOVE DRIVERS WHO ARE OUT OF SHIFT THRESHOLD#
###############################################
min_num_shifts = 15
valid_drivers =
shifts_design_matrix %>%
group_by(hack_license) %>%
summarize(num_shifts = n()) %>%
filter(num_shifts >= min_num_shifts) %>%
select(hack_license)
# join data frames to only select shifts with drivers who meet threshold
valid_shifts =
left_join(valid_drivers, shifts_design_matrix)
####################################
######### CLASSIFICATION ###########
####################################
formula_class = formula2_class
train = get_training_data(valid_shifts)
test = get_test_data(valid_shifts, train)
classification_model = train_model_classification(train, formula_class)
test = test_model_classification(formula_class, test, classification_model)
# Classifying predictions with threshold of 0.5
test <- test %>%
mutate(efficiency_category_predicted = ifelse(predicted > 0.5, 1, 0))
#ASSESSING PERFORMANCE WITH ACCURACY, ROC CURVE, AUC, CALIBRATION
test_confusion_matrix =
confusionMatrix(test$efficiency_category_predicted, test$efficiency_category)
test_confusion_matrix$overall["Accuracy"]
plot_roc_auc(test)
calibration_plot <- plot_calibration(test)
calibration_plot
#savePlot(filename = paste("calibration_for_hl_wkend_str_hr", type = "png"))
# AUC = 0.616 for features:as.factor(is_week_end)*as.factor(start_hour) , accuracy = 56.74%, sd = NA
# AUC = 0.778 for features:hack_license + as.factor(is_week_end)*as.factor(start_hour), accuracy = 70.67%, sd = 2.302
# AUC = 0.784 for features:hack_license + as.factor(is_week_end)*as.factor(start_hour) + avg_trip_time + avg_trip_distance
# AUC = 0.7849 for features:hack_license + as.factor(is_week_end)*as.factor(start_hour) + avg_trip_time
# AUC = 0.783 for features: hack_license + as.factor(is_week_end)*as.factor(start_hour) + avg_trip_distance
# separating `hack_license` label from its value to plot distribution of coef
hack_licenses_coef <- extract_hack_licenses_coef(classification_model)
ggplot(hack_licenses_coef, aes(x = estimate)) + geom_histogram(binwidth = 0.1)
ggsave("../figures/distribution_of_hl_wkend_str_hr.png")
sd(hack_licenses_coef$estimate)
#####################################################
### REPEAT CLASSIFICATION FOR SHUFFLED DATA FRAME ###
#####################################################
# Shuffle hack licenses
hack_licenses_shuffled <-sample(valid_shifts$hack_license, nrow(valid_shifts))
random_shifts = valid_shifts
random_shifts$hack_license = hack_licenses_shuffled
# train/test and model
train = get_training_data(random_shifts)
test = get_test_data(random_shifts, train)
classification_model = train_model_classification(train, formula_class)
test = test_model_classification(formula_class, test, classification_model)
# Classifying predictions with threshold of 0.5
test <- test %>%
mutate(efficiency_category_predicted = ifelse(predicted > 0.5, 1, 0))
#ASSESSING PERFORMANCE WITH ACCURACY, ROC CURVE, AUC, CALIBRATION
test_confusion_matrix =
confusionMatrix(test$efficiency_category_predicted, test$efficiency_category)
test_confusion_matrix$overall["Accuracy"]
plot_roc_auc(test)
calibration_plot <- plot_calibration(test)
calibration_plot
# AUC = 0.566, for features hack_license + as.(is_week_end)*as.factor(start_hour) , accurarcy = 54.55%, 0.523
# AUC = 0.609 for features as.(is_week_end)*as.factor(start_hour), accuracy = 54.55%
# separating `hack_license` label from its value to plot distribution of coef
hack_licenses_coef <- extract_hack_licenses_coef(classification_model)
ggplot(hack_licenses_coef, aes(x = estimate)) + geom_histogram(binwidth = 0.1)
sd(hack_licenses_coef$estimate)
ggsave("../figures/coef_distribution_of_hack_licenses_shuffled.png")
###############################
######### REGRESSION ##########
###############################
formula_reg = formula5_reg
train = get_training_data(valid_shifts)
test = get_test_data(valid_shifts, train)
X = sparse.model.matrix(formula_reg, train)
Y = train$efficiency
regression_model <- glmnet(X, Y, lambda = 0)
#save(regression_model, file = "../Rdata/regression_model.Rdata")
# PREDICTION
xtest = sparse.model.matrix(formula_reg, test)
test$predicted <- predict(regression_model, newx = xtest, type = "response")
# Assessing prediction - RMSE
RMSE <- sqrt(mean((test$efficiency-test$predicted)^2))
# separating `hack_license` label from its value and plotting distribution of coef
hack_licenses_coef <- extract_hack_licenses_coef(regression_model)
ggplot(hack_licenses_coef, aes(x = estimate)) + geom_histogram(binwidth = 0.1)
#ggsave("../figures/coef_distribution_of_model_without_hack_license.png")
sd(hack_licenses_coef$estimate)
# RMSE = 5.60 for features as.factor(is_week_end)*as.factor(start_hour)
# RMSE = 4.76 for features hack_license + as.factor(is_week_end)*as.factor(start_hour)
#################################################
### REPEAT REGRESSION FOR SHUFFLED DATA FRAME ###
#################################################
#Shuffling the hack_licenses
hack_licenses_shuffled <-sample(valid_shifts$hack_license, nrow(valid_shifts))
random_shifts = valid_shifts
random_shifts$hack_license = hack_licenses_shuffled
train = get_training_data(random_shifts)
test = get_test_data(random_shifts, train)
X = sparse.model.matrix(formula_reg, train)
Y = train$efficiency
regression_model <- glmnet(X, Y, lambda = 0)
# PREDICTION
xtest = sparse.model.matrix(formula_reg, test)
test$predicted <- predict(regression_model, newx = xtest, type = "response")
# Assessing prediction - RMSE
RMSE <- sqrt(mean((test$efficiency-test$predicted)^2))
# separating `hack_license` label from its value and plotting distribution of coef
hack_licenses_coef <- extract_hack_licenses_coef(regression_model)
ggplot(hack_licenses_coef, aes(x = estimate)) + geom_histogram(binwidth = 0.1)
#ggsave("../figures/coef_distribution_of_model_without_hack_license.png")
sd(hack_licenses_coef$estimate)
################################################################
# REGRESSION FOR FORMULA WITH ALL PICKUP/DROPOFF NEIGHBORHOODS #
################################################################
rm(list = setdiff(ls(), lsf.str())) # removes all objects (not functions!)
load("../Rdata/shifts_design_matrix_nbhd.Rdata")
theme_set(theme_bw())
min_num_shifts = 15
valid_drivers =
shifts_design_matrix_nbhd %>%
group_by(hack_license) %>%
summarize(num_shifts = n()) %>%
filter(num_shifts >= min_num_shifts) %>%
select(hack_license)
# join data frames to only select shifts with drivers who meet threshold
valid_shifts =
left_join(valid_drivers, shifts_design_matrix_nbhd)
# colnames(shifts_design_matrix_nbhd)
# we want: 1, 9, 12, 38, 42, 47, 48, 49:559
# we also include 3 (start) so we can arrange by start time and then randomize
nhbd_features_df = valid_shifts[, c(1, 3, 9, 12, 38, 42, 47, 48, 49:559)]
rm(shifts_design_matrix_nbhd, valid_shifts)
backup = nhbd_features_df
names(nhbd_features_df) = gsub(" ", "_", names(nhbd_features_df))
names(nhbd_features_df) = gsub("-", "_", names(nhbd_features_df))
names(nhbd_features_df) = gsub(",", "_", names(nhbd_features_df))
names(nhbd_features_df) = gsub("'", "", names(nhbd_features_df))
names(nhbd_features_df) = gsub("\\.", "_", names(nhbd_features_df))
train = get_training_data(nhbd_features_df)
test = get_test_data(nhbd_features_df, train)
formula7_reg = as.formula(efficiency ~ as.factor(is_week_end)*as.factor(start_hour) + .)
formula_reg = formula7_reg
#remove start time b/c we dont need it in our model and row_num to keep the features the same
train = train[, -2]
train = train[, -519] #index subtracts by 1 since we remove a column above
test = test[, -2]
X = sparse.model.matrix(formula_reg, train)
Y = train$efficiency
regression_model = glmnet(X, Y, lambda = 0)
#save(regression_model, file = "../Rdata/regression_model.Rdata")
# PREDICTION
xtest = sparse.model.matrix(formula_reg, test)
test$predicted <- predict(regression_model, newx = xtest, type = "response")
# Assessing prediction - RMSE
RMSE <- sqrt(mean((test$efficiency-test$predicted)^2))
# separating `hack_license` label from its value and plotting distribution of coef
hack_licenses_coef <- extract_hack_licenses_coef(regression_model)
ggplot(hack_licenses_coef, aes(x = estimate)) + geom_histogram(binwidth = 0.1)
ggplot(hack_licenses_coef, aes(x = estimate)) + geom_density()
#ggsave("../figures/coef_distribution_of_model_without_hack_license.png")
sd(hack_licenses_coef$estimate)
##################################
# REPEAT FOR SHUFFLED DATA FRAME #
##################################
# Shuffle hack licenses
hack_licenses_shuffled <-sample(nhbd_features_df$hack_license,
nrow(nhbd_features_df))
random_shifts = nhbd_features_df
random_shifts$hack_license = hack_licenses_shuffled
train = get_training_data(random_shifts)
test = get_test_data(random_shifts, train)
formula7_reg = as.formula(efficiency ~ as.factor(is_week_end)*as.factor(start_hour) + .)
formula_reg = formula7_reg
#remove start time b/c we dont need it in our model and row_num to keep the features the same
train = train[, -2]
train = train[, -519] #index subtracts by 1 since we remove a column above
test = test[, -2]
X = sparse.model.matrix(formula_reg, train)
Y = train$efficiency
regression_model = glmnet(X, Y, lambda = 0)
#save(regression_model, file = "../Rdata/regression_model.Rdata")
# PREDICTION
xtest = sparse.model.matrix(formula_reg, test)
test$predicted <- predict(regression_model, newx = xtest, type = "response")
# Assessing prediction - RMSE
RMSE <- sqrt(mean((test$efficiency-test$predicted)^2))
# separating `hack_license` label from its value and plotting distribution of coef
hack_licenses_coef_shuffled <- extract_hack_licenses_coef(regression_model)
ggplot(hack_licenses_coef_shuffled, aes(x = estimate)) + geom_density()
#ggsave("../figures/coef_distribution_of_model_without_hack_license.png")
sd(hack_licenses_coef$estimate)
df1<- hack_licenses_coef_shuffled %>% mutate(shuffled = T)
df2 <- hack_licenses_coef %>% mutate(shuffled = F)
plot_df <- rbind(df1, df2)
ggplot(plot_df) +
geom_density(aes(x=estimate, color=shuffled)) +
xlim(-15,15) +
scale_color_discrete(labels = c("Actual", "Shuffled")) +
theme(legend.title = element_blank(),
legend.position = c(0.9, 0.5))+
xlab("driver coefficient ($/hour)")
ggsave("../figures/driver_coef_distribution.png")