-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_example_ice_pdp.R
557 lines (461 loc) · 15.5 KB
/
simulation_example_ice_pdp.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
## Setup -----------------------------------------------------------------------
# packages
library(simsurv)
library(survival)
library(survex)
library(ranger)
library(randomForestSRC)
library(ggnewscale)
library(survminer)
library(pec)
source("plotting_functions.R")
# figure path
fig_path <- here::here("figures_iml")
if (!file.exists(fig_path))
dir.create(fig_path)
fig <- function(x)
here::here(fig_path, x)
#------------------------------------------------------------------------------#
#### Data Simulation ####
#------------------------------------------------------------------------------#
## Simulate survival data ------------------------------------------------------
# set seed for reproducibility
set.seed(2607)
# set number of simulated observations
n <- 3000
# simulate feature values
x <- data.frame(treatment = rbinom(n, 1, 0.5),
x1 = rnorm(n),
x2 = rnorm(n))
# simulate survival times using simsurv package
simdat <- simsurv(
dist = "weibull",
lambdas = 0.1,
gammas = 1.5,
betas = c(treatment = -2.5, x1 = 0.7),
x = x,
tde = c(treatment = 5),
tdefunction = "log",
maxt = 5
)
# remove id column from simulation dataframe
y <- simdat[, -1]
# add feature values to simulation dataframe
dat <- cbind(y, x)
# convert binary treatment variable to factor
dat$treatment <- factor(dat$treatment)
# simulate random censoring following a binomial distribution
cen <- rbinom(n, 1, 0.2)
cen_status <- ifelse((cen == 1) | (dat$status == 0), 0, 1)
dat$status <- cen_status
## Kaplan-Meier survival curves ------------------------------------------------
# create surv object
surv <- Surv(time = dat$eventtime, event = dat$status)
# summary of surv object
summary(surv)
# Kaplan Meier
km_fit <- survfit(surv ~ treatment, data = dat)
# visualize Kaplan-Meier plots with survminer
plot_km <- ggsurvplot(
km_fit,
data = dat,
risk.table = TRUE,
ggtheme = theme_bw(),
palette = c("#604E97", "#E68FAC"),
font.x = c(20),
font.y = c(20),
font.tickslab = c(18),
legend = "none"
)
plot_km # Figure 17 a)
# save Kaplan-Meier curve plot
ggsave_workaround <- function(g) {
survminer:::.build_ggsurvplot(
x = g,
surv.plot.height = NULL,
risk.table.height = NULL,
ncensor.plot.height = NULL
)
}
plot_km_save <- ggsave_workaround(plot_km)
ggsave(
fig("figure_17a.pdf"),
plot = plot_km_save,
width = 7,
height = 6,
device = "pdf"
)
#------------------------------------------------------------------------------#
#### Model Training & Set-up ####
#------------------------------------------------------------------------------#
## Model training --------------------------------------------------------------
# set seed for reproducibility
set.seed(2607)
# define the proportion of the training data
train_proportion <- 2 / 3
# row indices of the training set
train_indices <- sample(1:nrow(dat), size = floor(train_proportion * nrow(dat)))
# split the data into training and test set
train_dat <- dat[train_indices, ]
test_dat <- dat[-train_indices, ]
# define times variable
times <- seq(0, 5, by = 0.1)
# fit coxph model on training data
coxph <- coxph(
Surv(eventtime, status) ~ .,
data = train_dat,
model = TRUE,
x = TRUE
)
# fit random survival forest on training data
ranger_rsf <- rfsrc(
Surv(eventtime, status) ~ .,
data = train_dat,
mtry = 2,
ntime = times
)
## Create survex explainer objects ---------------------------------------------
# create explainer object for coxph model on test data
coxph_explainer <- explain(
coxph,
times = times,
data = test_dat[, -c(1, 2)],
y = Surv(test_dat$eventtime, test_dat$status)
)
# create explainer object for ranger model on test data
ranger_explainer <- explain(
ranger_rsf,
data = test_dat[, -c(1, 2)],
y = Surv(test_dat$eventtime, test_dat$status),
times = times
)
#------------------------------------------------------------------------------#
#### Model Performance ####
#------------------------------------------------------------------------------#
## Create Brier score plots for coxph and ranger models ------------------------
# compute Brier scores for coxph and ranger models
brier_scores <- pec(
object = list("Cox Model" = coxph, "Random Forest" = ranger_rsf),
formula = Surv(eventtime, status) ~ 1,
data = test_dat,
times = seq(0, 5, by = 20)
)
# convert the pec object into a data.frame
df_brier_data <- as.data.frame(brier_scores$AppErr)
# add the time points to the data.frame
df_brier_data$time <- brier_scores$time
# convert the data.frame into long format
df_brier_long <- reshape2::melt(
df_brier_data,
id.vars = "time",
variable.name = "models",
value.name = "brier_scores"
)
# replace model names to abbreviations used in the paper
models_vec <- ifelse(
df_brier_long$models == "Reference",
"Kaplan-Meier",
ifelse(df_brier_long$models == "Cox.Model", "coxph", "ranger")
)
df_brier_long$models <- models_vec
# create custom plot of Brier scores over time
plot_brier <-
ggplot(df_brier_long,
aes(
x = time,
y = brier_scores,
color = models,
linetype = models
)) +
geom_line(linewidth = 0.8) +
scale_color_manual(values = c("#F0E442", "#0072B2", "#D55E00")) +
scale_linetype_manual(values = c("solid", "dashed", "dotted")) +
scale_x_continuous(breaks = seq(0, 5, 0.5)) +
ylab("Brier score") +
ggtitle("") +
theme_bw() +
theme(
legend.position = "bottom",
axis.text = element_text(size = 18),
axis.title = element_text(size = 20),
legend.title = element_text(size = 20, face = "bold"),
legend.text = element_text(size = 18),
legend.background = element_rect(
colour = "grey34",
fill = "white",
linetype = "solid",
linewidth = 0.3
)
)
plot_brier # Figure 17 b)
# save custom plot
ggsave(
fig("figure_17b.pdf"),
plot = plot_brier,
width = 7,
height = 6,
device = "pdf"
)
#------------------------------------------------------------------------------#
#### Individual Conditional Expectation (ICE) & Partial Dependence (PDP) ####
#------------------------------------------------------------------------------#
## coxph -----------------------------------------------------------------------
# compute partial dependence and individual conditional expectation values
pdp_ice_coxph <- model_profile(coxph_explainer, variables = "treatment", N = NULL)
# extract relevant ice results for plotting
df_ice_coxph <-
pdp_ice_coxph$cp_profiles$result[(pdp_ice_coxph$cp_profiles$result$`_vname_` == "treatment") &
(pdp_ice_coxph$cp_profiles$result$`_times_` %in% times), ]
# rename columns
names(df_ice_coxph)[names(df_ice_coxph) == "_times_"] = "time"
names(df_ice_coxph)[names(df_ice_coxph) == "_yhat_"] = "yhat"
names(df_ice_coxph)[names(df_ice_coxph) == "_ids_"] = "ids"
# sample 200 rows
df_ice_coxph$ids <- as.numeric(df_ice_coxph$ids)
set.seed(2607)
sampled_ids <- sample(sort(unique(df_ice_coxph$ids)), 200, replace = FALSE)
df_ice_coxph <- df_ice_coxph[df_ice_coxph$ids %in% sampled_ids, ]
# drop irrelevant columns
df_ice_coxph <-
df_ice_coxph[, c("treatment", "time", "yhat", "ids")]
# aggregate ice values to obtain pdp values
df_pdp_coxph <- aggregate(yhat ~ time + treatment, data = df_ice_coxph[, c("ids", "treatment", "yhat", "time")], FUN = mean)
# select reference value for centering
df_ice_coxph_center <-
df_ice_coxph[df_ice_coxph[, "treatment"] == 0, ]
# add reference value for centering to results dataframe
df_ice_coxph_merge <-
merge(
x = df_ice_coxph,
y = df_ice_coxph_center,
by = c("ids", "time"),
all = TRUE
)
# perform centering operation
df_ice_coxph_merge[, "yhat"] <-
df_ice_coxph_merge[, "yhat.x"] - df_ice_coxph_merge[, "yhat.y"]
# rename double columns
names(df_ice_coxph_merge)[names(df_ice_coxph_merge) == "treatment.x"] = "treatment"
# extract ice values for times that should be plotted
df_ice_coxph_center <-
df_ice_coxph_merge[df_ice_coxph_merge[, "time"] %in% c(0, 1, 2, 3, 4, 5), ]
df_ice_coxph <-
df_ice_coxph[df_ice_coxph[, "time"] %in% c(0, 1, 2, 3, 4, 5), ]
# aggregate centered ice values to obtain centered pdp values
df_pdp_coxph_center <- aggregate(yhat ~ time + treatment, data = df_ice_coxph_merge[, c("ids", "treatment", "yhat", "time")], FUN = mean)
# extract pdp values for times that should be plotted
df_pdp_coxph_center <-
df_pdp_coxph_center[df_pdp_coxph_center[, "time"] %in% c(0, 1, 2, 3, 4, 5), ]
df_pdp_coxph <-
df_pdp_coxph[df_pdp_coxph[, "time"] %in% c(0, 1, 2, 3, 4, 5), ]
## Create custom plots ---------------------------------------------------------
# create custom plot of uncentered ice and pdp curves over time
plot_pdp_ice_coxph_uc <- plot_ice_pdp(
df_ice_coxph,
df_pdp_coxph,
model = "coxph",
treatment,
variable_name = "treatment",
eventtime,
status,
limits = c(0, 1),
breaks_x = seq(0, 5, by = 1),
breaks_y = seq(0, 1, by = 0.2)
)
plot_pdp_ice_coxph_uc
# create custom plot of centered ice and pdp curves over time
plot_pdp_ice_coxph_c <- plot_ice_pdp(
df_ice_coxph_center,
df_pdp_coxph_center,
model = "coxph",
treatment,
variable_name = "treatment",
eventtime,
status,
limits = c(-0.6, 0.1),
breaks_x = seq(0, 5, by = 1),
breaks_y = seq(-0.6, 0.1, by = 0.1)
)
plot_pdp_ice_coxph_c
# create custom plot of uncentered ice curves over time
plot_ice_coxph_uc <- plot_ice(df_ice_coxph, breaks_x = seq(0, 5, by = 1))
plot_ice_coxph_uc
# create custom plot of centered ice curves over time
plot_ice_coxph_c <- plot_ice(
df_ice_coxph_center,
limits_y = c(-0.6, 0.1),
breaks_y = seq(-0.6, 0.1, by = 0.1),
breaks_x = seq(0, 5, by = 1)
)
plot_ice_coxph_c
## ranger ----------------------------------------------------------------------
# compute partial dependence and individual conditional expectation values
pdp_ice_ranger <- model_profile(ranger_explainer, variables = "treatment", N = NULL)
# extract relevant ice results for plotting
df_ice_ranger <-
pdp_ice_ranger$cp_profiles$result[(pdp_ice_ranger$cp_profiles$result$`_vname_` == "treatment") &
(pdp_ice_ranger$cp_profiles$result$`_times_` %in% times), ]
# rename columns
names(df_ice_ranger)[names(df_ice_ranger) == "_times_"] = "time"
names(df_ice_ranger)[names(df_ice_ranger) == "_yhat_"] = "yhat"
names(df_ice_ranger)[names(df_ice_ranger) == "_ids_"] = "ids"
# sample 200 rows
df_ice_ranger <- df_ice_ranger[df_ice_ranger$ids %in% sampled_ids, ]
# drop irrelevant columns
df_ice_ranger <-
df_ice_ranger[, c("treatment", "time", "yhat", "ids")]
# aggregate centered ice values to obtain centered pdp values
df_pdp_ranger <- aggregate(yhat ~ time + treatment, data = df_ice_ranger[, c("ids", "treatment", "yhat", "time")], FUN = mean)
# select reference value for centering
df_ice_ranger_center <-
df_ice_ranger[df_ice_ranger[, "treatment"] == 0, ]
# add reference value for centering to results dataframe
df_ice_ranger_merge <-
merge(
x = df_ice_ranger,
y = df_ice_ranger_center,
by = c("ids", "time"),
all = TRUE
)
# perform centering operation
df_ice_ranger_merge[, "yhat"] <-
df_ice_ranger_merge[, "yhat.x"] - df_ice_ranger_merge[, "yhat.y"]
# rename double column
names(df_ice_ranger_merge)[names(df_ice_ranger_merge) == "treatment.x"] <-
"treatment"
# extract ice values for times that should be plotted
df_ice_ranger_center <-
df_ice_ranger_merge[df_ice_ranger_merge[, "time"] %in% c(0, 1, 2, 3, 4, 5), ]
df_ice_ranger <-
df_ice_ranger[df_ice_ranger[, "time"] %in% c(0, 1, 2, 3, 4, 5), ]
# aggregate centered ice values to obtain centered pdp values
df_pdp_ranger_center <- aggregate(yhat ~ time + treatment, data = df_ice_ranger_merge[, c("ids", "treatment", "yhat", "time")], FUN = mean)
# extract pdp values for times that should be plotted
df_pdp_ranger_center <-
df_pdp_ranger_center[df_pdp_ranger_center[, "time"] %in% c(0, 1, 2, 3, 4, 5), ]
df_pdp_ranger <-
df_pdp_ranger[df_pdp_ranger[, "time"] %in% c(0, 1, 2, 3, 4, 5), ]
## Create custom plots----------------------------------------------------------
# create custom plot of uncentered ice and pdp curves over time
plot_pdp_ice_ranger_uc <- plot_ice_pdp(
df_ice_ranger,
df_pdp_ranger,
model = "ranger",
treatment,
variable_name = "treatment",
eventtime,
status,
limits = c(0, 1),
breaks_x = seq(0, 5, by = 1),
breaks_y = seq(0, 1, by = 0.2)
)
plot_pdp_ice_ranger_uc
# create custom plot of centered ice and pdp curves over time
plot_pdp_ice_ranger_c <- plot_ice_pdp(
df_ice_ranger_center,
df_pdp_ranger_center,
model = "ranger",
treatment,
variable_name = "treatment",
eventtime,
status,
limits = c(-0.9, 0.4),
breaks_x = seq(0, 5, by = 1),
breaks_y = seq(-0.9, 0.4, by = 0.2)
)
plot_pdp_ice_ranger_c
# create custom plot of uncentered ice curves over time
plot_ice_ranger_uc <- plot_ice(df_ice_ranger,
model = "ranger",
breaks_x = seq(0, 5, by = 1))
plot_ice_ranger_uc
# create custom plot of centered ice curves over time
plot_ice_ranger_c <- plot_ice(
df_ice_ranger_center,
model = "ranger",
limits_y = c(-0.9, 0.4),
breaks_y = seq(-0.9, 0.4, by = 0.2),
breaks_x = seq(0, 5, by = 1)
)
plot_ice_ranger_c
## Create plot grids and save plots --------------------------------------------
# create grid of uncentered ice and pdp plots
pdp_ice_grid_uc <-
ggarrange(
plot_pdp_ice_coxph_uc,
plot_pdp_ice_ranger_uc,
ncol = 2,
nrow = 1,
common.legend = TRUE,
legend = "bottom"
) +
theme(plot.margin = margin(0.1, 0.1, 0.4, 0.1, "cm"))
pdp_ice_grid_uc # Figure 4
# save grid of uncentered ice and pdp plots
ggsave(
fig("figure_4.pdf"),
plot = pdp_ice_grid_uc,
width = 14,
height = 6,
device = "pdf"
)
# create grid of centered ice and pdp plots
pdp_ice_grid_c <-
ggarrange(
plot_pdp_ice_coxph_c,
plot_pdp_ice_ranger_c,
ncol = 2,
nrow = 1,
common.legend = TRUE,
legend = "bottom"
) +
theme(plot.margin = margin(0.1, 0.1, 0.4, 0.1, "cm"))
pdp_ice_grid_c # Figure 16
# save grid of centered ice and pdp plots
ggsave(
fig("figure_16.pdf"),
plot = pdp_ice_grid_c,
width = 14,
height = 6,
device = "pdf"
)
# create grid of uncentered ice plots
ice_grid_uc <-
ggarrange(
plot_ice_coxph_uc,
plot_ice_ranger_uc,
ncol = 2,
nrow = 1,
common.legend = TRUE,
legend = "bottom"
) +
theme(plot.margin = margin(0.1, 0.1, 0.4, 0.1, "cm"))
ice_grid_uc # Figure 2
# save grid of uncentered ice plots
ggsave(
fig("figure_2.pdf"),
plot = ice_grid_uc,
width = 14,
height = 6,
device = "pdf"
)
# create grid of centered ice plots
ice_grid_c <-
ggarrange(
plot_ice_coxph_c,
plot_ice_ranger_c,
ncol = 2,
nrow = 1,
common.legend = TRUE,
legend = "bottom"
) +
theme(plot.margin = margin(0.1, 0.1, 0.4, 0.1, "cm"))
ice_grid_c # Figure 3
# save grid of centered ice plots
ggsave(
fig("figure_3.pdf"),
plot = ice_grid_c,
width = 14,
height = 6,
device = "pdf"
)