-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanagement.R
407 lines (377 loc) · 20.4 KB
/
management.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
# This script validates for predicting the management.
########################## Load data
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
load("app_data_clean.Rda")
########################## Variables to be considered in the analysis
var_names_full <- c("Age", "BMI", "Sex", "Height", "Weight",
"AS", "PAS",
"Visibility of Appendix", "Appendix Diameter", "Migration of Pain", "Tenderness in RLQ",
"Rebound Tenderness", "Cough Tenderness", "Psoas Sign",
"Nausea/Vomitting", "Anorexia", "Body Temperature", "WBC Count", "Neutrophil Percentage",
"Ketones in Urine", "Erythrocytes in Urine", "WBC in Urine", "CRP",
"Dysuria", "Stool", "Peritonitis/Abdominal Guarding", "Free Intraperitoneal Fluid",
"Appendix Layers", "Target Sign", "Appendix Perfusion",
"Surrounding Tissue Reaction", "Path. Lymph Nodes",
"Mesenteric Lymphadenitis", "Bowel Wall Thickening", "Ileus", "Coprostasis",
"Meteorism", "Enteritis")
# Uncomment an approrpiate feature set:
# Full model
vars_incl <- c("Age", "BMI", "Sex", "Height", "Weight", "TreatmentGroupBinar",
"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")
# Without US
#vars_incl <- c("Age", "BMI", "Sex", "Height", "Weight", "TreatmentGroupBinar",
# "AlvaradoScore", "PediatricAppendicitisScore", #"SecondaryCondition", (US!)
# "MigratoryPain", "LowerAbdominalPainRight", #"AppendixOnSono", "AppendixDiameter", (US!)
# "ReboundTenderness", "CoughingPain", "PsoasSign", # "AbdominalGuarding", (kicked out!)
# "Nausea", "AppetiteLoss", "BodyTemp", "WBCCount", "NeutrophilPerc",
# "KetonesInUrine", "ErythrocytesInUrine", "WBCInUrine", "CRPEntry",
# "Dysuria", "Stool", "Peritonitis") #"FreeFluids" (US!)
# Without peritonitis
#vars_incl <- c("Age", "BMI", "Sex", "Height", "Weight", "TreatmentGroupBinar",
# "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", "FreeFluids", #"Peritonitis",
# "AppendixWallLayers", "Kokarde", "TissuePerfusion", #"AppendixPerforation", (this variable is operative!)
# "SurroundingTissueReaction", "PathLymphNodes",
# "MesentricLymphadenitis", "BowelWallThick", "Ileus", "FecalImpaction",
# "Meteorism", "Enteritis")
# Without US & peritonitis
#vars_incl <- c("Age", "BMI", "Sex", "Height", "Weight", "TreatmentGroupBinar",
# "AlvaradoScore", "PediatricAppendicitisScore", #"SecondaryCondition", (US!)
# "MigratoryPain", "LowerAbdominalPainRight", #"AppendixOnSono", "AppendixDiameter", (US!)
# "ReboundTenderness", "CoughingPain", "PsoasSign", # "AbdominalGuarding", (kicked out!)
# "Nausea", "AppetiteLoss", "BodyTemp", "WBCCount", "NeutrophilPerc",
# "KetonesInUrine", "ErythrocytesInUrine", "WBCInUrine", "CRPEntry",
# "Dysuria", "Stool") #"FreeFluids" (US!), "Peritonitis"
# Without AS, PAS
#vars_incl <- c("Age", "BMI", "Sex", "Height", "Weight", "TreatmentGroupBinar",
# "AlvaradoScore", "PediatricAppendicitisScore", # "SecondaryCondition", (not pre-treatment!)
# "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.trt <- app.data[!is.na(app.data$DiagnosisByCriteria), vars_incl]
########################## 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
########################## Logistic regression
# Fit the model
logreg.trt <- glm(TreatmentGroupBinar ~ ., data = app.data.trt.imputed, family = "binomial")
summary(logreg.trt)
# Look at the training set performance
pred <- prediction(predict(logreg.trt, type = "response"), app.data.trt.imputed$TreatmentGroupBinar)
perf <- performance(pred, "tpr", "fpr")
# ROC curve
plot(perf, colorize = TRUE, asp = 1)
abline(a = 0, b = 1, col = "gray", lty = 2)
# AUROC
(auc_train <- performance(pred, measure = "auc")@y.values[[1]])
# PR curve
perf <- performance(pred, measure = "prec", x.measure = "rec")
[email protected][[1]][1] <- 1.0
plot(perf, colorize = TRUE, asp = 1)
# AUPR
(aupr_train <- computeArea([email protected][[1]], [email protected][[1]]))
# K-fold CV
set.seed(1799)
K <- 10
aucs.logreg <- numeric(K)
auprs.logreg <- numeric(K)
specs.logreg <- numeric(K)
sens.logreg <- numeric(K)
ppv.logreg <- numeric(K)
npv.logreg <- numeric(K)
accs.logreg <- numeric(K)
balaccs.logreg <- 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
logreg.k <- glm(TreatmentGroupBinar ~ ., data = d.k.imputed, family = "binomial")
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(logreg.k, d.test.imputed, type = "response"), 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")
[email protected][[1]][1] <- [email protected][[1]][2]
aupr_test <- computeArea([email protected][[1]], [email protected][[1]])
aucs.logreg[k] <- auc_test
auprs.logreg[k] <- aupr_test
cm_test <- confusionMatrix(factor(as.numeric(predict(logreg.k, d.test.imputed, type = "response") >= 0.5) + 1, levels = c(1, 2), labels = c("conservative", "surgical")),
factor(as.numeric(d.test.imputed$TreatmentGroupBinar), levels = c(1, 2), labels = c("conservative", "surgical")))
specs.logreg[k] <- unname(cm_test$byClass["Specificity"])
sens.logreg[k] <- unname(cm_test$byClass["Sensitivity"])
ppv.logreg[k] <- unname(cm_test$byClass["Pos Pred Value"])
npv.logreg[k] <- unname(cm_test$byClass["Neg Pred Value"])
accs.logreg[k] <- unname(cm_test$overall["Accuracy"])
balaccs.logreg[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
}
}
c(mean(aucs.logreg), sd(aucs.logreg), mean(aucs.logreg) - 2 * sd(aucs.logreg), mean(aucs.logreg) + 2 * sd(aucs.logreg))
c(mean(auprs.logreg), sd(auprs.logreg), mean(auprs.logreg) - 2 * sd(auprs.logreg), mean(auprs.logreg) + 2 * sd(auprs.logreg))
c(mean(sens.logreg), sd(sens.logreg), mean(sens.logreg) - 2 * sd(sens.logreg), mean(sens.logreg) + 2 * sd(sens.logreg))
c(mean(specs.logreg), sd(specs.logreg), mean(specs.logreg) - 2 * sd(specs.logreg), mean(specs.logreg) + 2 * sd(specs.logreg))
c(mean(ppv.logreg), sd(ppv.logreg), mean(ppv.logreg) - 2 * sd(ppv.logreg), mean(ppv.logreg) + 2 * sd(ppv.logreg))
c(mean(npv.logreg), sd(npv.logreg), mean(npv.logreg) - 2 * sd(npv.logreg), mean(npv.logreg) + 2 * sd(npv.logreg))
c(mean(accs.logreg), sd(accs.logreg), mean(accs.logreg) - 2 * sd(accs.logreg), mean(accs.logreg) + 2 * sd(accs.logreg))
c(mean(balaccs.logreg), sd(balaccs.logreg), mean(balaccs.logreg) - 2 * sd(balaccs.logreg), mean(balaccs.logreg) + 2 * sd(balaccs.logreg))
cm_overall
########################## Random forest
# Fit the model
rf.trt <- randomForest(TreatmentGroupBinar ~ ., data = app.data.trt.imputed, importance = TRUE)
# Examine variable importance
oldpars <- par()
par(mar = c(15, 5, 5, 5))
barplot(rf.trt$importance[, 3], las = 2)
par(mar = oldpars$mar)
# Variability of variable importance values
set.seed(1799)
B <- 300
imps_b <- matrix(nrow = B, ncol = ncol(app.data.trt.imputed) - 1)
colnames(imps_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[b, ] <- (rf_b$importance[, 1] + rf_b$importance[, 2]) / 2
}
oldpars <- par()
jpeg("varimp_trt.jpg", width = 2200, height = 1300,
pointsize = 32)
par(mar = c(15, 5, 2, 2))
boxplot(as.data.frame(imps_b), cex=0.1, las=2, ylab = "Bootstrapped RF Variable Importance",
names = rep("", length(var_names_full)), notch = TRUE, cex.axis = 1.2, cex.lab = 1.2,
lwd = 2)
mtext("b", side = 3, adj = 0, line = 1.2, cex = 1.2, font = 2)
dev.off()
par(mar = oldpars$mar)
# Look at the training set performance
pred <- prediction(predict(rf.trt, type = "prob")[, 2], app.data.trt.imputed$TreatmentGroupBinar)
perf <- performance(pred, "tpr", "fpr")
# ROC curve
plot(perf, colorize = TRUE, asp = 1)
abline(a = 0, b = 1, col = "gray", lty = 2)
# AUROC
(auc_train <- performance(pred, measure = "auc")@y.values[[1]])
# PR curve
perf <- performance(pred, measure = "prec", x.measure = "rec")
[email protected][[1]][1] <- 1.0
plot(perf, colorize = TRUE, asp = 1)
# AUPR
(aupr_train <- computeArea([email protected][[1]], [email protected][[1]]))
# K-fold CV
set.seed(1799)
K <- 10
aucs.rf <- numeric(K)
auprs.rf <- numeric(K)
specs.rf <- numeric(K)
sens.rf <- numeric(K)
ppv.rf <- numeric(K)
npv.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
rocs <- list()
prcs <- list()
prcs_rand <- list()
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)
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")
rocs[[k]] <- perf
auc_test <- performance(pred, measure = "auc")@y.values[[1]]
perf <- performance(pred, measure = "prec", x.measure = "rec")
[email protected][[1]][1] <- [email protected][[1]][2]
prcs[[k]] <- perf
aupr_test <- computeArea([email protected][[1]], [email protected][[1]])
aucs.rf[k] <- auc_test
auprs.rf[k] <- aupr_test
# Random classifier
pred_rand <- prediction(sample(predict(rf.k, d.test.imputed, type = "prob")[, 2]), d.test.imputed$TreatmentGroupBinar)
perf <- performance(pred_rand, measure = "prec", x.measure = "rec")
prcs_rand[[k]] <- perf
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"])
ppv.rf[k] <- unname(cm_test$byClass["Pos Pred Value"])
npv.rf[k] <- unname(cm_test$byClass["Neg Pred Value"])
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
}
}
c(mean(aucs.rf), sd(aucs.rf), mean(aucs.rf) - 2 * sd(aucs.rf), mean(aucs.rf) + 2 * sd(aucs.rf))
c(mean(auprs.rf), sd(auprs.rf), mean(auprs.rf) - 2 * sd(auprs.rf), mean(auprs.rf) + 2 * sd(auprs.rf))
c(mean(sens.rf), sd(sens.rf), mean(sens.rf) - 2 * sd(sens.rf), mean(sens.rf) + 2 * sd(sens.rf))
c(mean(specs.rf), sd(specs.rf), mean(specs.rf) - 2 * sd(specs.rf), mean(specs.rf) + 2 * sd(specs.rf))
c(mean(ppv.rf), sd(ppv.rf), mean(ppv.rf) - 2 * sd(ppv.rf), mean(ppv.rf) + 2 * sd(ppv.rf))
c(mean(npv.rf), sd(npv.rf), mean(npv.rf) - 2 * sd(npv.rf), mean(npv.rf) + 2 * sd(npv.rf))
c(mean(accs.rf), sd(accs.rf), mean(accs.rf) - 2 * sd(accs.rf), mean(accs.rf) + 2 * sd(accs.rf))
c(mean(balaccs.rf), sd(balaccs.rf), mean(balaccs.rf) - 2 * sd(balaccs.rf), mean(balaccs.rf) + 2 * sd(balaccs.rf))
cm_overall
# Plot ROC curves
plot(0.5, 0.5, pch = "", xlim = c(0, 1), ylim = c(0, 1), asp = 1.0,
xlab = "False Positive Rate", ylab = "True Positive Rate")
for (k in 1 : K) {
lines(rocs[[k]]@x.values[[1]], rocs[[k]]@y.values[[1]], col = makeTransparent("red"),
lwd = 2)
}
abline(a = 0, b = 1, col = "gray", lty = 2, lwd = 2)
# Plot PR curves
plot(0.5, 0.5, pch = "", xlim = c(0, 1), ylim = c(0, 1), asp = 1.0,
xlab = "Recall", ylab = "Precision")
for (k in 1 : K) {
lines(prcs[[k]]@x.values[[1]], prcs[[k]]@y.values[[1]], col = makeTransparent("red"),
lwd = 2)
}
for (k in 1 : K) {
lines(prcs_rand[[k]]@x.values[[1]], prcs_rand[[k]]@y.values[[1]], col = makeTransparent("gray"),
lwd = 2, lty = 2)
}
partialPlot(rf.trt, x.var = "Peritonitis", pred.data = app.data.trt.imputed)
plot(app.data.trt.imputed$TreatmentGroupBinar ~ app.data.trt.imputed$Peritonitis, xlab = "Peritonitis", ylab = "Treatment")
########################## Gradient boosting
# Fit the model
ntr <- 100
gb.trt <- gbm(as.numeric(TreatmentGroupBinar)-1 ~ ., data = app.data.trt.imputed, n.trees = ntr)
# Examine variable importance
s <- summary.gbm(gb.trt, plotit = FALSE)
oldpars <- par()
par(mar = c(15, 5, 5, 5))
barplot(height = s[, 2], names.arg = as.character(s[, 1]), las = 2)
par(mar = oldpars$mar)
# Look at the training set performance
pred <- prediction(predict(gb.trt, type = "response", n.trees = ntr), app.data.trt.imputed$TreatmentGroupBinar)
perf <- performance(pred, "tpr", "fpr")
# ROC curve
plot(perf, colorize = TRUE, asp = 1)
abline(a = 0, b = 1, col = "gray", lty = 2)
# AUROC
(auc_train <- performance(pred, measure = "auc")@y.values[[1]])
# PR curve
perf <- performance(pred, measure = "prec", x.measure = "rec")
[email protected][[1]][1] <- 1.0
plot(perf, colorize = TRUE, asp = 1)
# AUPR
(aupr_train <- computeArea([email protected][[1]], [email protected][[1]]))
# K-fold CV
set.seed(1799)
ntr <- 100
K <- 10
aucs.gb <- numeric(K)
auprs.gb <- numeric(K)
specs.gb <- numeric(K)
sens.gb <- numeric(K)
ppv.gb <- numeric(K)
npv.gb <- numeric(K)
accs.gb <- numeric(K)
balaccs.gb <- 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
gb.k <- gbm(as.numeric(TreatmentGroupBinar)-1 ~ ., data = d.k.imputed, n.trees = ntr)
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(gb.k, d.test.imputed, type = "response", n.trees = ntr), 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")
[email protected][[1]][1] <- [email protected][[1]][2]
aupr_test <- computeArea([email protected][[1]], [email protected][[1]])
aucs.gb[k] <- auc_test
auprs.gb[k] <- aupr_test
cm_test <- confusionMatrix(factor(as.numeric(predict(gb.k, d.test.imputed, type = "response", n.trees = ntr) >= 0.5) + 1, levels = c(1, 2), labels = c("conservative", "surgical")), d.test.imputed$TreatmentGroupBinar)
specs.gb[k] <- unname(cm_test$byClass["Specificity"])
sens.gb[k] <- unname(cm_test$byClass["Sensitivity"])
ppv.gb[k] <- unname(cm_test$byClass["Pos Pred Value"])
npv.gb[k] <- unname(cm_test$byClass["Neg Pred Value"])
accs.gb[k] <- unname(cm_test$overall["Accuracy"])
balaccs.gb[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
}
}
c(mean(aucs.gb), sd(aucs.gb), mean(aucs.gb) - 2 * sd(aucs.gb), mean(aucs.gb) + 2 * sd(aucs.gb))
c(mean(auprs.gb), sd(auprs.gb), mean(auprs.gb) - 2 * sd(auprs.gb), mean(auprs.gb) + 2 * sd(auprs.gb))
c(mean(sens.gb), sd(sens.gb), mean(sens.gb) - 2 * sd(sens.gb), mean(sens.gb) + 2 * sd(sens.gb))
c(mean(specs.gb), sd(specs.gb), mean(specs.gb) - 2 * sd(specs.gb), mean(specs.gb) + 2 * sd(specs.gb))
c(mean(ppv.gb), sd(ppv.gb), mean(ppv.gb) - 2 * sd(ppv.gb), mean(ppv.gb) + 2 * sd(ppv.gb))
c(mean(npv.gb), sd(npv.gb), mean(npv.gb) - 2 * sd(npv.gb), mean(npv.gb) + 2 * sd(npv.gb))
c(mean(accs.gb), sd(accs.gb), mean(accs.gb) - 2 * sd(accs.gb), mean(accs.gb) + 2 * sd(accs.gb))
c(mean(balaccs.gb), sd(balaccs.gb), mean(balaccs.gb) - 2 * sd(balaccs.gb), mean(balaccs.gb) + 2 * sd(balaccs.gb))
cm_overall
########################## Statistical tests for model comparison
t.test(x = aucs.rf, y = aucs.logreg, paired = TRUE)
t.test(x = auprs.rf, y = auprs.logreg, paired = TRUE)
t.test(x = aucs.gb, y = aucs.logreg, paired = TRUE)
t.test(x = auprs.gb, y = auprs.logreg, paired = TRUE)
########################## Baseline: Random classifier
n_perms <- 10000
sens.rand <- 0
spec.rand <- 0
ppv.rand <- 0
npv.rand <- 0
for (i in 1:n_perms) {
rand_perm <- sample(app.data.trt.imputed$TreatmentGroupBinar,
size = length(app.data.trt.imputed$TreatmentGroupBinar), replace = FALSE)
cm <- confusionMatrix(rand_perm, app.data.trt.imputed$TreatmentGroupBinar)
sens.rand <- sens.rand + unname(cm$byClass["Sensitivity"])
spec.rand <- spec.rand + unname(cm$byClass["Specificity"])
ppv.rand <- ppv.rand + unname(cm$byClass["Pos Pred Value"])
npv.rand <- npv.rand + unname(cm$byClass["Neg Pred Value"])
}
(sens.rand / n_perms)
(spec.rand / n_perms)
(ppv.rand / n_perms)
(npv.rand / n_perms)