-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_rf_model.R
274 lines (224 loc) · 8.29 KB
/
1_rf_model.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
# Change this to your working directory containing the competition data
setwd("C:/Users/jeff.keller/Desktop/RFDilemmaZone")
# packages needed (run the line below to install)
# install.packages(c("randomForest", "ggplot2", "reshape2", "scales", "data.table))
library(randomForest)
library(ggplot2)
library(reshape2)
library(scales)
library(data.table)
# Load the data
load(file = "driving_data.rdata")
head(data)
# Summarize the data
summary(data)
### Calculate a response variable
data$outcome <- 0
# Stopped in time or went through in time
e <- data[, "Stop.Dist"] >= 0
e[is.na(e)] <- FALSE
data[, "outcome"][e | (is.na(data[, "Stop.Time"]) & data[, "Stopline.Time"] < data[, "Event2.Time"])] <- "safe"
# Ran the light
e <- data[, "Stop.Dist"] < 0
e[is.na(e)] <- FALSE
data[, "outcome"][e | (is.na(data[, "Stop.Time"]) & data[, "Stopline.Time"] >= data[, "Event2.Time"])] <- "unsafe"
data[, "outcome"] <- as.factor(data[, "outcome"])
table(data[, "outcome"])
prop.table(table(data[, "outcome"]))
### Calculate some additional variables
# If accel.dir is na, it means they didn't move their foot
data[, "Accel.Dir"] <- as.character(data[, "Accel.Dir"])
data[, "Accel.Dir"][is.na(data[, "Accel.Dir"])] <- "None"
data[, "Accel.Dir"] <- factor(data[, "Accel.Dir"])
data[, "Accel.Min"][is.na(data[, "Accel.Min"])] <- 0
data[, "Accel.Max"][is.na(data[, "Accel.Max"])] <- 0
# Structure of the data
str(data)
### Estimate a random forest model
# Setting a random seed ensures a reproducable model
set.seed(1987)
# Run the model with default settings
rf.all <- randomForest(formula = outcome ~ Event1.Vel + Event1.Dist +
Event2.Vel + Age + Device + Treatment + Gender + Accel.Min + Accel.Max,
data = data,
na.action = na.fail,
importance = TRUE,
do.trace = 50)
# Training Data Miss-classification Rate
y <- data[, "outcome"]
y_hat <- predict(object = rf.all, newdata = data)
table(y, y_hat) # 0%! (Over-fitting)
# Model summary statistics and plots
err.rate <- melt(rf.all$err.rate) # convert to long format
names(err.rate) <- c("ntree", "Measure", "rate")
levels(err.rate[, "Measure"]) <- c("Overall", "Safe", "Unsafe")
# OOB Error Rates (Figure 1)
ggplot(err.rate, aes(x = ntree, y = rate)) + theme_bw() +
geom_line(aes(group = Measure, color = Measure)) +
xlab("Number of Trees") + ylab("OOB Error Rate") + ggtitle("Out of Bag Error Rates") +
scale_y_continuous(labels = percent, lim = c(0, 1))
### K-fold cross validation to tune the mtry parameter
k <- 10 # 10 folds
p <- 1:9 # there are 9 indicators, so we could try up to 9 at each node split
folds <- sample(1:k, nrow(data), replace = TRUE)
results <- matrix(0, nrow = k * length(p), ncol = 6)
colnames(results) <- c("m", "fold", "CV False Positive", "CV False Negative", "CV Total Error Rate", "Total Error Rate")
results[,1] <- rep(1:9, each = k)
results[,2] <- 1:k
for (m in p) {
for (i in c(1:k)) {
include <- !folds == i
rf <- randomForest(formula = outcome ~ Event1.Vel + Event1.Dist +
Event2.Vel + Age + Device + Treatment + Gender + Accel.Min + Accel.Max,
data = data,
na.action = na.fail,
subset = include,
mtry = m)
pred <- predict(object = rf, newdata = data[!include,])
confusion <- table(actual = data[!include, "outcome"], predicted = pred)
results[(m-1)*k+i, 3] <- confusion[1,2] / sum(confusion[1,])
results[(m-1)*k+i, 4] <- confusion[2,1] / sum(confusion[2,])
results[(m-1)*k+i, 5] <- sum(confusion[c(2,3)]) / sum(confusion)
results[(m-1)*k+i, 6] <- 1 - sum(diag(rf[["confusion"]][, -3]))/sum(rf[["confusion"]][, -3])
}
}
results <- data.table(results)
plot(results[, mean(`Total Error Rate`), by = m], type = "l", ylim = c(0.15, 0.19),
ylab = "Classification Error Rate")
lines(results[, mean(`CV Total Error Rate`), by = m], col = "blue")
legend(x = 6, y = 0.19, legend = c("OOB", "Cross-Validation"), col = c("black", "blue"), lty = c(1, 1), box.col = "white")
### Rerun the main model with the tuned mtry parameter
rf.all <- randomForest(formula = outcome ~ Event1.Vel + Event1.Dist +
Event2.Vel + Age + Device + Treatment + Gender + Accel.Min + Accel.Max,
data = data,
na.action = na.fail,
importance = TRUE,
do.trace = 50,
mtry = 5)
# Confusion matrix (Table 2)
rf.all[["confusion"]]
# Variable Importance (Figure 2)
varImpPlot(rf.all)
### Sensitivity analysis (Table 3)
# A single baseline record (a "median" record)
newentry <- list()
newentry$Event1.Vel <- 40
newentry$Event1.Dist <- 200
newentry$Event2.Vel <- 10
newentry$Accel.Min <- -18
newentry$Accel.Max <- 14
newentry$Age <- factor("M", levels = c("Y", "M", "O"))
newentry$Gender <- factor("M", levels = c("M", "F"))
newentry$Treatment <- factor("B", levels = c("B", "O", "I"))
newentry$Device <- factor("HH", levels = c("HF", "HH", "HS"))
newentry <- data.frame(newentry)
newentry
alpha <- 0.05
# baseline
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# -5 mph Event 1
newentry$Event1.Vel <- 35
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# +5 mph Event 1
newentry$Event1.Vel <- 45
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# -8 mph Event 2
newentry$Event1.Vel <- 40
newentry$Event2.Vel <- 2
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# -4 mph Event 2
newentry$Event2.Vel <- 6
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# Young
newentry$Event2.Vel <- 10
newentry$Age <- factor("Y", levels = c("Y", "M", "O"))
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# Old
newentry$Age <- factor("O", levels = c("Y", "M", "O"))
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# Female
newentry$Age <- factor("M", levels = c("Y", "M", "O"))
newentry$Gender <- factor("F", levels = c("M", "F"))
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# Outgoing Call
newentry$Gender <- factor("M", levels = c("M", "F"))
newentry$Treatment <- factor("O", levels = c("B", "O", "I"))
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# Incoming Call
newentry$Treatment <- factor("I", levels = c("B", "O", "I"))
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# Headset Device + Outgoing Call
newentry$Treatment <- factor("O", levels = c("B", "O", "I"))
newentry$Device <- factor("HS", levels = c("HF", "HH", "HS"))
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# Handheld Device
newentry$Device <- factor("HH", levels = c("HF", "HH", "HS"))
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe
# Hands-Free Device
newentry$Device <- factor("HF", levels = c("HF", "HH", "HS"))
y_hat <- predict(object = rf.all, newdata = newentry, type = "prob")
p_hat <- y_hat[,2]
p_hat
n <- rf.all$ntree
moe <- sqrt(p_hat * (1 - p_hat) / n) * qnorm(1 - alpha / 2)
moe