-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_ranger_sampling_strategies-scw.R
147 lines (125 loc) · 3.99 KB
/
03_ranger_sampling_strategies-scw.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
library("sbcdata")
library("rusranger")
## don't exclude by time
ukl <- exclude_entries(
subset(sbcdata, Center == "Leipzig"), time = c(-Inf, Inf)
)
ukl <- ukl[!ukl$Excluded,]
umg <- exclude_entries(
subset(sbcdata, Center == "Greifswald"), time = c(-Inf, Inf)
)
umg <- umg[!umg$Excluded,]
ukl$Sex <- as.integer(ukl$Sex == "M")
umg$Sex <- as.integer(umg$Sex == "M")
ukl$Diagnosis <- as.integer(ukl$Diagnosis == "Sepsis")
umg$Diagnosis <- as.integer(umg$Diagnosis == "Sepsis")
set.seed(20220419)
#nsub <- 1e4
#ukl <- ukl[sample(nrow(ukl), nsub),]
#umg <- umg[sample(nrow(umg), nsub),]
train <- subset(ukl, Set == "Training")
validation <- list(
UKL = subset(ukl, Set == "Validation"), UMG = umg
)
## classic approach (6-72) h
train$Diagnosis0 <- as.integer(
train$Diagnosis & sbcdata:::.is_time_range(train, range = c(72, 6) * 3600)
)
train$Excluded0 <- !train$Diagnosis0 & train$Diagnosis
xvar <- c("Age", "Sex", "PLT", "RBC", "WBC", "HGB", "MCV")
mtry <- 3
num.trees <- 1000
replace <- TRUE # doing bootstrap, shorter runtime, b/c much lower samplesize/samplefraction
scw <- as.integer(c(1, 2, 4, 10, 20, 50, 100))
nrep <- 3
auc <- function(fit, x, y) {
p <- prediction(predict(fit, as.data.frame(x))$predictions[, 2L], y)
performance(p, "auc")@y.values[[1L]]
}
rowMedians <- function(x) {mode(x) <- "numeric"; apply(x, 1, median)}
#' Upsampling
#'
#' Upsampling former Sepsis now control cases using Daniel's approach.
#'
#' @param y current y
#' @param y0 original y (without any timerange)
#' @param scw sepsis control weighting factor
#' @return `numeric`, case weights
caseweights <- function(y, y0, scw = 1) {
isControl <- !y
isSepsis <- as.logical(y)
isSepsisControl <- as.logical(y0) & !as.logical(y)
## control = 1
w <- rep_len(1, length(y))
## sepsis cases that are now control
w[isSepsisControl] <- scw
## sepsis cases
w[isSepsis] <- (sum(isControl) + sum(isSepsisControl) * scw) / sum(isSepsis)
w
}
cwranger <- function(d, scw = 1) {
ranger(
x = as.data.frame(d[, xvar, with = FALSE]), y = d$Diagnosis,
probability = TRUE, min.node.size = 10,
mtry = mtry, num.trees = num.trees,
case.weights = caseweights(d$Diagnosis, d$Diagnosis0, scw),
sample.fraction = rusranger:::.samplefraction(d$Diagnosis),
replace = replace
)
}
# Baseline
auc.baseline <- rowMedians(do.call(cbind, lapply(seq_len(nrep), function(r) {
d <- train
d <- d[!d$Excluded0,]
rus <- rusranger(
x = as.data.frame(d[, xvar, with = FALSE]), y = d$Diagnosis0,
mtry = mtry, num.trees = num.trees, replace = replace
)
sapply(validation, function(v)
auc(rus, v[, xvar, with = FALSE], v$Diagnosis)
)
})))
# SCW
train$Diagnosis <- as.integer(
train$Diagnosis & sbcdata:::.is_time_range(train, range = c(12, 0) * 3600)
)
auc.scw <- do.call(cbind, lapply(scw, function(i) {
rowMedians(do.call(cbind, lapply(seq_len(nrep), function(r) {
rcw <- cwranger(train, scw = i)
lapply(validation, function(v)
auc(rcw, v[, xvar, with = FALSE], v$Diagnosis)
)
})))
}))
colnames(auc.scw) <- scw
plot_auc_trend <- function(baseline, scw, main) {
col <- palette.colors(2)
xscw <- as.numeric(names(scw))
plot(
NA,
xlim = c(1L, max(xscw)), ylim = c(0L, 1L),
axes = FALSE, ann = FALSE
)
title(main = main, adj = 0L)
title(ylab = "AUC", adj = 1L)
title(xlab = "SCW", adj = 1L)
axis(1, lwd.ticks = 0L, col = "#808080")
axis(2, lwd.ticks = 0L, col = "#808080")
abline(h = 0.5, lty = "dashed", col = "#808080")
abline(h = baseline, col = col[1:2])
lines(xscw, scw, col = col[2])
legend(
"bottomright",
legend = c("RUS", "SCW"),
col = col,
lwd = 1,
bty = "n"
)
}
for (nm in names(auc.baseline)) {
png(paste0("auc-scw-trend_", nm, ".png"), width = 1024, height = 1024)
plot_auc_trend(
auc.baseline[nm], auc.scw[nm,], main = paste(nm, "AUC trend")
)
dev.off()
}