-
Notifications
You must be signed in to change notification settings - Fork 1
/
variable_selection.R
479 lines (441 loc) · 23.2 KB
/
variable_selection.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
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
# Script for variable selection using random forests.
# NB: this script may take a while to run!
########################## Load data
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
load("app_data_clean.Rda")
##############################################################################
########################## Disease Status ####################################
##############################################################################
########################## Variables to be considered in the analysis
vars_incl <- c("Age", "BMI", "Sex", "Height", "Weight", "DiagnosisByCriteria",
"AlvaradoScore", "PediatricAppendicitisScore", #"SecondaryCondition", (not pre-treatment!)
"AppendixOnSono", "AppendixDiameter", "MigratoryPain", "LowerAbdominalPainRight",
"ReboundTenderness", "CoughingPain", "PsoasSign", #"AbdominalGuarding", (kicked out!)
"Nausea", "AppetiteLoss", "BodyTemp", "WBCCount", "NeutrophilPerc",
"KetonesInUrine", "ErythrocytesInUrine", "WBCInUrine", "CRPEntry",
"Dysuria", "Stool", "Peritonitis", "FreeFluids",
"AppendixWallLayers", "Kokarde", "TissuePerfusion", #"AppendixPerforation", (this variable is operative!)
"SurroundingTissueReaction", "PathLymphNodes",
"MesentricLymphadenitis", "BowelWallThick", "Ileus", "FecalImpaction",
"Meteorism", "Enteritis")
app.data.dis <- app.data[!is.na(app.data$DiagnosisByCriteria), vars_incl]
########################## Variable selection based on RF importance
ps <- 1 : (length(vars_incl) - 1)
aucs.p <- numeric(length(ps))
aucs.sd.p <- numeric(length(ps))
auprs.p <- numeric(length(ps))
auprs.sd.p <- numeric(length(ps))
bestvars <- list()
for (p in ps) {
print(paste0("Fitting RF based on ", p, " best predictor(s)"))
# K-fold CV
set.seed(1799)
K <- 10
aucs.rf <- numeric(K)
auprs.rf <- numeric(K)
specs.rf <- numeric(K)
sens.rf <- numeric(K)
accs.rf <- numeric(K)
balaccs.rf <- numeric(K)
fold_membership <- sample(c(rep(1 : K, floor(nrow(app.data.dis) / K)),
1 : (nrow(app.data.dis) %% K)), size = nrow(app.data.dis),
replace = FALSE)
cm_overall <- NA
for (k in 1 : K) {
# We perform separate imputation for train and test sets!
train_flags <- fold_membership != k
d.k <- app.data.dis[train_flags, ]
d.k.imputed <- kNN(d.k[, -which(colnames(d.k) == "DiagnosisByCriteria")])[, 1 : (ncol(d.k) - 1)]
d.k.imputed$DiagnosisByCriteria <- d.k$DiagnosisByCriteria
rf.k <- randomForest(DiagnosisByCriteria ~ ., data = d.k.imputed, importance = TRUE)
rf.vimp <- (importance(rf.k)[, 1] + importance(rf.k)[, 2]) / 2
imp.vars <- names(tail(sort(rf.vimp), p))
bestvars[[p]] <- imp.vars
rf.k <- randomForest(DiagnosisByCriteria ~ ., data = d.k.imputed[, c(imp.vars,
"DiagnosisByCriteria")])
d.test <- app.data.dis[!train_flags, ]
d.test.imputed <- kNN(d.test[, -which(colnames(d.test) == "DiagnosisByCriteria")])[, 1 : (ncol(d.test) - 1)]
d.test.imputed$DiagnosisByCriteria <- d.test$DiagnosisByCriteria
pred <- prediction(predict(rf.k, d.test.imputed, type = "prob")[, 2],
d.test.imputed$DiagnosisByCriteria)
perf <- performance(pred, "tpr", "fpr")
auc_test <- performance(pred, measure = "auc")@y.values[[1]]
perf <- performance(pred, measure = "prec", x.measure = "rec")
perf@y.values[[1]][1] <- 1.0
aupr_test <- computeArea(perf@x.values[[1]], perf@y.values[[1]])
aucs.rf[k] <- auc_test
auprs.rf[k] <- aupr_test
cm_test <- confusionMatrix(predict(rf.k, d.test.imputed, type = "response"),
d.test.imputed$DiagnosisByCriteria)
specs.rf[k] <- unname(cm_test$byClass["Specificity"])
sens.rf[k] <- unname(cm_test$byClass["Sensitivity"])
accs.rf[k] <- unname(cm_test$overall["Accuracy"])
balaccs.rf[k] <- unname(cm_test$byClass["Balanced Accuracy"])
if (is.na(cm_overall)) {
cm_overall <- cm_test$table
} else {
cm_overall <- cm_overall + cm_test$table
}
}
aucs.p[p] <- mean(aucs.rf)
auprs.p[p] <- mean(auprs.rf)
aucs.sd.p[p] <- sd(aucs.rf)
auprs.sd.p[p] <- sd(auprs.rf)
}
# Plot performance for different numbers of predictors
jpeg("varselec_dis_auroc.jpg", width = 1800, height = 1300, pointsize = 40)
plot(ps, aucs.p, xlab = "Number of Predictors", ylab = "AUROC in 10-fold CV", pch = "",
ylim = c(0.65, 1), xaxt = "n", main = "Diagnosis: AUROC")
# Error bars are 95% confidence t-intervals
segments(ps, aucs.p - qt(p = 0.975, df = 9) * aucs.sd.p / sqrt(10), ps,
aucs.p + qt(p = 0.975, df = 9) * aucs.sd.p / sqrt(10), lwd = 3)
lines(ps, aucs.p, col = "red", lwd = 3)
points(ps, aucs.p, col = "red", cex = 1.5, lwd = 3)
axis(side = 1, at = ps[seq(1, length(vars_incl), 3)])
legend("bottomright", legend = c("Average AUROC", "95% confidence interval"),
col = c("red", "black"), pch = c(1, NA), lwd = c(3, 3), lty = c(NA, 1))
mtext("a", side = 3, adj = 0, line = 1.2, cex = 1.2, font = 2)
dev.off()
jpeg("varselec_dis_aupr.jpg", width = 1800, height = 1300, pointsize = 40)
plot(ps, auprs.p, xlab = "Number of Predictors", ylab = "AUPR in 10-fold CV", pch = "",
ylim = c(0.7, 1), xaxt = "n", main = "Diagnosis: AUPR")
# Error bars are 95% confidence t-intervals
segments(ps, auprs.p - qt(p = 0.975, df = 9) * auprs.sd.p / sqrt(10), ps,
auprs.p + qt(p = 0.975, df = 9) * auprs.sd.p / sqrt(10), lwd = 3)
lines(ps, auprs.p, col = "red", lwd = 3)
points(ps, auprs.p, col = "red", cex = 1.5, lwd = 3)
axis(side = 1, at = ps[seq(1, length(vars_incl), 3)])
legend("bottomright", legend = c("Average AUPR", "95% confidence interval"),
col = c("red", "black"), pch = c(1, NA), lwd = c(3, 3), lty = c(NA, 1))
mtext("b", side = 3, adj = 0, line = 1.2, cex = 1.2, font = 2)
dev.off()
##############################################################################
########################## Treatment Assignment ##############################
##############################################################################
########################## Variables to be considered in the analysis
vars_incl <- c("Age", "BMI", "Sex", "Height", "Weight", "TreatmentGroupBinar",
"AlvaradoScore", "PediatricAppendicitisScore",
"AppendixOnSono", "AppendixDiameter", "MigratoryPain", "LowerAbdominalPainRight", #"SecondaryCondition", (not pre-treatment!)
"ReboundTenderness", "CoughingPain", "PsoasSign", #"AbdominalGuarding", (kicked out!)
"Nausea", "AppetiteLoss", "BodyTemp", "WBCCount", "NeutrophilPerc",
"KetonesInUrine", "ErythrocytesInUrine", "WBCInUrine", "CRPEntry",
"Dysuria", "Stool", "Peritonitis", "FreeFluids",
"AppendixWallLayers", "Kokarde", "TissuePerfusion", #"AppendixPerforation", (this variable is operative!)
"SurroundingTissueReaction", "PathLymphNodes",
"MesentricLymphadenitis", "BowelWallThick", "Ileus", "FecalImpaction",
"Meteorism", "Enteritis")
app.data.trt <- app.data[!is.na(app.data$DiagnosisByCriteria), vars_incl]
########################## Variable selection based on RF importance
ps <- 1 : (length(vars_incl) - 1)
aucs.p <- numeric(length(ps))
aucs.sd.p <- numeric(length(ps))
auprs.p <- numeric(length(ps))
auprs.sd.p <- numeric(length(ps))
bestvars <- list()
for (p in ps) {
print(paste0("Fitting RF based on ", p, " best predictor(s)"))
# K-fold CV
set.seed(1799)
K <- 10
aucs.rf <- numeric(K)
auprs.rf <- numeric(K)
specs.rf <- numeric(K)
sens.rf <- numeric(K)
accs.rf <- numeric(K)
balaccs.rf <- numeric(K)
fold_membership <- sample(c(rep(1 : K, floor(nrow(app.data.trt) / K)),
1 : (nrow(app.data.trt) %% K)), size = nrow(app.data.trt),
replace = FALSE)
cm_overall <- NA
for (k in 1 : K) {
# We perform separate imputation for train and test sets!
train_flags <- fold_membership != k
d.k <- app.data.trt[train_flags, ]
d.k.imputed <- kNN(d.k[, -which(colnames(d.k) == "TreatmentGroupBinar")])[, 1 : (ncol(d.k) - 1)]
d.k.imputed$TreatmentGroupBinar <- d.k$TreatmentGroupBinar
rf.k <- randomForest(TreatmentGroupBinar ~ ., data = d.k.imputed, importance = TRUE)
rf.vimp <- (importance(rf.k)[, 1] + importance(rf.k)[, 2]) / 2
imp.vars <- names(tail(sort(rf.vimp), p))
bestvars[[p]] <- imp.vars
rf.k <- randomForest(TreatmentGroupBinar ~ ., data = d.k.imputed[, c(imp.vars,
"TreatmentGroupBinar")])
d.test <- app.data.trt[!train_flags, ]
d.test.imputed <- kNN(d.test[, -which(colnames(d.test) == "TreatmentGroupBinar")])[, 1 : (ncol(d.test) - 1)]
d.test.imputed$TreatmentGroupBinar <- d.test$TreatmentGroupBinar
pred <- prediction(predict(rf.k, d.test.imputed, type = "prob")[, 2],
d.test.imputed$TreatmentGroupBinar)
perf <- performance(pred, "tpr", "fpr")
auc_test <- performance(pred, measure = "auc")@y.values[[1]]
perf <- performance(pred, measure = "prec", x.measure = "rec")
perf@y.values[[1]][1] <- 1.0
aupr_test <- computeArea(perf@x.values[[1]], perf@y.values[[1]])
aucs.rf[k] <- auc_test
auprs.rf[k] <- aupr_test
cm_test <- confusionMatrix(predict(rf.k, d.test.imputed, type = "response"),
d.test.imputed$TreatmentGroupBinar)
specs.rf[k] <- unname(cm_test$byClass["Specificity"])
sens.rf[k] <- unname(cm_test$byClass["Sensitivity"])
accs.rf[k] <- unname(cm_test$overall["Accuracy"])
balaccs.rf[k] <- unname(cm_test$byClass["Balanced Accuracy"])
if (is.na(cm_overall)) {
cm_overall <- cm_test$table
} else {
cm_overall <- cm_overall + cm_test$table
}
}
aucs.p[p] <- mean(aucs.rf)
auprs.p[p] <- mean(auprs.rf)
aucs.sd.p[p] <- sd(aucs.rf)
auprs.sd.p[p] <- sd(auprs.rf)
}
# Plot performance for different numbers of predictors
jpeg("varselec_trt_auroc.jpg", width = 1800, height = 1300, pointsize = 40)
plot(ps, aucs.p, xlab = "Number of Predictors", ylab = "AUROC in 10-fold CV", pch = "",
ylim = c(0.850, 1), xaxt = "n", main = "Management: AUROC")
# Error bars are 95% confidence t-intervals
segments(ps, aucs.p - qt(p = 0.975, df = 9) * aucs.sd.p / sqrt(10), ps,
aucs.p + qt(p = 0.975, df = 9) * aucs.sd.p / sqrt(10), lwd = 3)
lines(ps, aucs.p, col = "red", lwd = 3)
points(ps, aucs.p, col = "red", cex = 1.5, lwd = 3)
axis(side = 1, at = ps[seq(1, length(vars_incl), 3)])
legend("bottomright", legend = c("Average AUROC", "95% confidence interval"),
col = c("red", "black"), pch = c(1, NA), lwd = c(3, 3), lty = c(NA, 1))
mtext("c", side = 3, adj = 0, line = 1.2, cex = 1.2, font = 2)
dev.off()
jpeg("varselec_trt_aupr.jpg", width = 1800, height = 1300, pointsize = 40)
plot(ps, auprs.p, xlab = "Number of Predictors", ylab = "AUPR in 10-fold CV", pch = "",
ylim = c(0.8, 1), xaxt = "n", main = "Management: AUPR")
# Error bars are 95% confidence t-intervals
segments(ps, auprs.p - qt(p = 0.975, df = 9) * auprs.sd.p / sqrt(10), ps,
auprs.p + qt(p = 0.975, df = 9) * auprs.sd.p / sqrt(10), lwd = 3)
lines(ps, auprs.p, col = "red", lwd = 3)
points(ps, auprs.p, col = "red", cex = 1.5, lwd = 3)
axis(side = 1, at = ps[seq(1, length(vars_incl), 3)])
legend("bottomright", legend = c("Average AUPR", "95% confidence interval"),
col = c("red", "black"), pch = c(1, NA), lwd = c(3, 3), lty = c(NA, 1))
mtext("d", side = 3, adj = 0, line = 1.2, cex = 1.2, font = 2)
dev.off()
##############################################################################
########################## Complicated Appendicitis ##########################
##############################################################################
########################## Variables to be considered in the analysis
vars_incl <- c("Age", "BMI", "Sex", "Height", "Weight", "AppendicitisComplications",
"AlvaradoScore", "PediatricAppendicitisScore", #"SecondaryCondition", (not pre-treatment!)
"AppendixOnSono", "AppendixDiameter", "MigratoryPain", "LowerAbdominalPainRight",
"ReboundTenderness", "CoughingPain", "PsoasSign", #"AbdominalGuarding", (kicked out!)
"Nausea", "AppetiteLoss", "BodyTemp", "WBCCount", "NeutrophilPerc",
"KetonesInUrine", "ErythrocytesInUrine", "WBCInUrine", "CRPEntry",
"Dysuria", "Stool", "Peritonitis", "FreeFluids",
"AppendixWallLayers", "Kokarde", "TissuePerfusion", #"AppendixPerforation", (this variable is operative!)
"SurroundingTissueReaction", "PathLymphNodes",
"MesentricLymphadenitis", "BowelWallThick", "Ileus", "FecalImpaction",
"Meteorism", "Enteritis")
app.data.comp <- app.data[!is.na(app.data$DiagnosisByCriteria), vars_incl]
########################## Variable selection based on RF importance
ps <- 1 : (length(vars_incl) - 1)
aucs.p <- numeric(length(ps))
aucs.sd.p <- numeric(length(ps))
auprs.p <- numeric(length(ps))
auprs.sd.p <- numeric(length(ps))
bestvars <- list()
for (p in ps) {
print(paste0("Fitting RF based on ", p, " best predictor(s)"))
# K-fold CV
set.seed(1799)
K <- 10
aucs.rf <- numeric(K)
auprs.rf <- numeric(K)
specs.rf <- numeric(K)
sens.rf <- numeric(K)
accs.rf <- numeric(K)
balaccs.rf <- numeric(K)
fold_membership <- sample(c(rep(1 : K, floor(nrow(app.data.comp) / K)),
1 : (nrow(app.data.comp) %% K)), size = nrow(app.data.comp),
replace = FALSE)
cm_overall <- NA
for (k in 1 : K) {
# We perform separate imputation for train and test sets!
train_flags <- fold_membership != k
d.k <- app.data.comp[train_flags, ]
d.k.imputed <- kNN(d.k[, -which(colnames(d.k) == "AppendicitisComplications")])[, 1 : (ncol(d.k) - 1)]
d.k.imputed$AppendicitisComplications <- d.k$AppendicitisComplications
rf.k <- randomForest(AppendicitisComplications ~ ., data = d.k.imputed, importance = TRUE)
rf.vimp <- (importance(rf.k)[, 1] + importance(rf.k)[, 2]) / 2
imp.vars <- names(tail(sort(rf.vimp), p))
bestvars[[p]] <- imp.vars
rf.k <- randomForest(AppendicitisComplications ~ .,
data = d.k.imputed[, c(imp.vars, "AppendicitisComplications")])
d.test <- app.data.comp[!train_flags, ]
d.test.imputed <- kNN(d.test[, -which(colnames(d.test) == "AppendicitisComplications")])[, 1 : (ncol(d.test) - 1)]
d.test.imputed$AppendicitisComplications <- d.test$AppendicitisComplications
pred <- prediction(predict(rf.k, d.test.imputed, type = "prob")[, 2],
d.test.imputed$AppendicitisComplications)
perf <- performance(pred, "tpr", "fpr")
auc_test <- performance(pred, measure = "auc")@y.values[[1]]
perf <- performance(pred, measure = "prec", x.measure = "rec")
perf@y.values[[1]][1] <- 1.0
aupr_test <- computeArea(perf@x.values[[1]], perf@y.values[[1]])
aucs.rf[k] <- auc_test
auprs.rf[k] <- aupr_test
cm_test <- confusionMatrix(predict(rf.k, d.test.imputed, type = "response"),
d.test.imputed$AppendicitisComplications)
specs.rf[k] <- unname(cm_test$byClass["Specificity"])
sens.rf[k] <- unname(cm_test$byClass["Sensitivity"])
accs.rf[k] <- unname(cm_test$overall["Accuracy"])
balaccs.rf[k] <- unname(cm_test$byClass["Balanced Accuracy"])
if (is.na(cm_overall)) {
cm_overall <- cm_test$table
} else {
cm_overall <- cm_overall + cm_test$table
}
}
aucs.p[p] <- mean(aucs.rf)
auprs.p[p] <- mean(auprs.rf)
aucs.sd.p[p] <- sd(aucs.rf)
auprs.sd.p[p] <- sd(auprs.rf)
}
# Plot performance for different numbers of predictors
jpeg("varselec_comp_auroc.jpg", width = 1800, height = 1300, pointsize = 40)
plot(ps, aucs.p, xlab = "Number of Predictors", ylab = "AUROC in 10-fold CV", pch = "",
ylim = c(0.75, 1), xaxt = "n", main = "Severity: AUROC")
# Error bars are 95% confidence t-intervals
segments(ps, aucs.p - qt(p = 0.975, df = 9) * aucs.sd.p / sqrt(10), ps,
aucs.p + qt(p = 0.975, df = 9) * aucs.sd.p / sqrt(10), lwd = 3)
lines(ps, aucs.p, col = "red", lwd = 3)
points(ps, aucs.p, col = "red", cex = 1.5, lwd = 3)
axis(side = 1, at = ps[seq(1, length(vars_incl), 3)])
legend("bottomright", legend = c("Average AUROC", "95% confidence interval"),
col = c("red", "black"), pch = c(1, NA), lwd = c(3, 3), lty = c(NA, 1))
mtext("e", side = 3, adj = 0, line = 1.2, cex = 1.2, font = 2)
dev.off()
jpeg("varselec_comp_aupr.jpg", width = 1800, height = 1300, pointsize = 40)
plot(ps, auprs.p, xlab = "Number of Predictors", ylab = "AUPR in 10-fold CV", pch = "",
ylim = c(0.4, 1), xaxt = "n", main = "Severity: AUPR")
# Error bars are 95% confidence t-intervals
segments(ps, auprs.p - qt(p = 0.975, df = 9) * auprs.sd.p / sqrt(10), ps,
auprs.p + qt(p = 0.975, df = 9) * auprs.sd.p / sqrt(10), lwd = 3)
lines(ps, auprs.p, col = "red", lwd = 3)
points(ps, auprs.p, col = "red", cex = 1.5, lwd = 3)
axis(side = 1, at = ps[seq(1, length(vars_incl), 3)])
legend("bottomright", legend = c("Average AUPR", "95% confidence interval"),
col = c("red", "black"), pch = c(1, NA), lwd = c(3, 3), lty = c(NA, 1))
mtext("f", side = 3, adj = 0, line = 1.2, cex = 1.2, font = 2)
dev.off()
########################## Inspect RF feature importance for the models trained on the full dataset
########################## Imputation
# Perform imputation using the kNN method
# NB: make sure to exclude the response variable from imputation!
set.seed(1799)
app.data.trt.imputed <- kNN(app.data.trt[, -which(colnames(app.data.trt) == "TreatmentGroupBinar")])[, 1 : (ncol(app.data.trt) - 1)]
app.data.trt.imputed$TreatmentGroupBinar <- app.data.trt$TreatmentGroupBinar
set.seed(1799)
app.data.dis.imputed <- kNN(app.data.dis[, -which(colnames(app.data.dis) == "DiagnosisByCriteria")])[, 1 : (ncol(app.data.dis) - 1)]
app.data.dis.imputed$DiagnosisByCriteria <- app.data.dis$DiagnosisByCriteria
set.seed(1799)
app.data.comp.imputed <- kNN(app.data.comp[, -which(colnames(app.data.comp) == "AppendicitisComplications")])[, 1 : (ncol(app.data.comp) - 1)]
app.data.comp.imputed$AppendicitisComplications <- app.data.comp$AppendicitisComplications
########################## Diagnosis
set.seed(1799)
# Fit the RF
rf.dis <- randomForest(DiagnosisByCriteria ~ ., data = app.data.dis.imputed, importance = TRUE)
# Importances
rf.vimp <- (importance(rf.dis)[, 1] + importance(rf.dis)[, 2]) / 2
(imp.vars <- names(tail(sort(rf.vimp), 3)))
# Perform bootstrapping, to see which variables have beeen selected most frequently
set.seed(1799)
B <- 300
freqs_b <- numeric(ncol(app.data.dis.imputed) - 1)
names(freqs_b) <- row.names(rf.dis$importance)
for (b in 1 : B) {
idx <- 1 : nrow(app.data.dis.imputed)
idx_b <- sample(idx, size=length(idx), replace = TRUE)
app.data.dis.imputed_b <- app.data.dis.imputed[idx_b, ]
rf_b <- randomForest(DiagnosisByCriteria ~ ., data = app.data.dis.imputed_b, importance = TRUE)
imps_b <- (rf_b$importance[, 1] + rf_b$importance[, 2]) / 2
sel_b <- which(names(freqs_b) %in% names(tail(sort(imps_b), 3)))
freqs_b[sel_b] <- freqs_b[sel_b] + 1
}
(freqs_b <- freqs_b / B)
sort(freqs_b[freqs_b > 0])
# Look at subsets of different sizes
set.seed(1799)
q_max <- ncol(app.data.dis.imputed) - 1
freqs <- matrix(nrow = q_max, ncol = ncol(app.data.dis.imputed))
colnames(freqs) <- colnames(app.data.dis.imputed)
for (q in 1 : q_max) {
print(q)
freqs[q, ] <- rfVarSelection(DiagnosisByCriteria ~ ., data = app.data.dis.imputed, q = q, B = 100)
}
heatmap.2(freqs, Rowv = FALSE, Colv = FALSE, dendrogram = "none", trace = "none",
sepcolor = "black", colsep = 1 : ncol(freqs), rowsep = 1 : nrow(freqs),
sepwidth = c(0.01, 0.01), margins = c(12, 8))
########################## Treatment
set.seed(1799)
# Fit the RF
rf.trt <- randomForest(TreatmentGroupBinar ~ ., data = app.data.trt.imputed, importance = TRUE)
# Importances
rf.vimp <- (importance(rf.trt)[, 1] + importance(rf.trt)[, 2]) / 2
(imp.vars <- names(tail(sort(rf.vimp), 14)))
# Perform bootstrapping, to see which variables have beeen selected most frequently
set.seed(1799)
B <- 300
freqs_b <- numeric(ncol(app.data.trt.imputed) - 1)
names(freqs_b) <- row.names(rf.trt$importance)
for (b in 1 : B) {
idx <- 1 : nrow(app.data.trt.imputed)
idx_b <- sample(idx, size=length(idx), replace = TRUE)
app.data.trt.imputed_b <- app.data.trt.imputed[idx_b, ]
rf_b <- randomForest(TreatmentGroupBinar ~ ., data = app.data.trt.imputed_b, importance = TRUE)
imps_b <- (rf_b$importance[, 1] + rf_b$importance[, 2]) / 2
sel_b <- which(names(freqs_b) %in% names(tail(sort(imps_b), 14)))
freqs_b[sel_b] <- freqs_b[sel_b] + 1
}
(freqs_b <- freqs_b / B)
sort(freqs_b[freqs_b > 0])
# Look at subsets of different sizes
set.seed(1799)
q_max <- ncol(app.data.trt.imputed) - 1
freqs <- matrix(nrow = q_max, ncol = ncol(app.data.trt.imputed))
colnames(freqs) <- colnames(app.data.trt.imputed)
for (q in 1 : q_max) {
print(q)
freqs[q, ] <- rfVarSelection(TreatmentGroupBinar ~ ., data = app.data.trt.imputed, q = q, B = 100)
}
heatmap.2(freqs, Rowv = FALSE, Colv = FALSE, dendrogram = "none", trace = "none",
sepcolor = "black", colsep = 1 : ncol(freqs), rowsep = 1 : nrow(freqs),
sepwidth = c(0.01, 0.01), margins = c(12, 8))
########################## Complicated appendicitis
set.seed(1799)
# Fit the RF
rf.comp <- randomForest(AppendicitisComplications ~ ., data = app.data.comp.imputed, importance = TRUE)
# Importances
rf.vimp <- (importance(rf.comp)[, 1] + importance(rf.comp)[, 2]) / 2
(imp.vars <- names(tail(sort(rf.vimp), 11)))
# Perform bootstrapping, to see which variables have beeen selected most frequently
set.seed(1799)
B <- 300
freqs_b <- numeric(ncol(app.data.comp.imputed) - 1)
names(freqs_b) <- row.names(rf.comp$importance)
for (b in 1 : B) {
idx <- 1 : nrow(app.data.comp.imputed)
idx_b <- sample(idx, size=length(idx), replace = TRUE)
app.data.comp.imputed_b <- app.data.comp.imputed[idx_b, ]
rf_b <- randomForest(AppendicitisComplications ~ ., data = app.data.comp.imputed_b, importance = TRUE)
imps_b <- (rf_b$importance[, 1] + rf_b$importance[, 2]) / 2
sel_b <- which(names(freqs_b) %in% names(tail(sort(imps_b), 11)))
freqs_b[sel_b] <- freqs_b[sel_b] + 1
}
(freqs_b <- freqs_b / B)
sort(freqs_b[freqs_b > 0])
# Look at subsets of different sizes
set.seed(1799)
q_max <- ncol(app.data.comp.imputed) - 1
freqs <- matrix(nrow = q_max, ncol = ncol(app.data.comp.imputed))
colnames(freqs) <- colnames(app.data.comp.imputed)
for (q in 1 : q_max) {
print(q)
freqs[q, ] <- rfVarSelection(AppendicitisComplications ~ ., data = app.data.comp.imputed, q = q, B = 100)
}
heatmap.2(freqs, Rowv = FALSE, Colv = FALSE, dendrogram = "none", trace = "none",
sepcolor = "black", colsep = 1 : ncol(freqs), rowsep = 1 : nrow(freqs),
sepwidth = c(0.01, 0.01), margins = c(12, 8))