-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap_validation_analysis.R
338 lines (255 loc) · 9.53 KB
/
bootstrap_validation_analysis.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
#:--------------------------------------------------------
#
# In this file we check Rhat and calibration plots for
# the riley simulation
#
#:--------------------------------------------------------
# load libraries
library(patchwork)
library(nimble)
library(tidyverse)
# load functions needed
source("data/create_data.R")
source("new_data_predictions/prediction_functions.R")
# load rds objects
bootstrap_t1d <- readRDS("bootstrap_riley_simulation/output/simulation_t1d.rds")
bootstrap_t2d <- readRDS("bootstrap_riley_simulation/output/simulation_t2d.rds")
predictions_dataset.UNITED_type1_all_genes_with_T <- readRDS("model_predictions/predictions_dataset.UNITED_type1_all_genes_with_T.rds")
predictions_dataset.UNITED_type2_all_genes_new <- readRDS("model_predictions/predictions_dataset.UNITED_type2_all_genes_new.rds")
# load datasets
## Load population representative dataset
dataset.UNITED_type1_all_genes <- create_data(dataset = "united t1d", commonmody = FALSE) %>%
## if MODY testing missing, change to 0
mutate(M = ifelse(is.na(M), 0, M))
dataset.UNITED_type2_all_genes <- create_data(dataset = "united t2d", commonmody = FALSE)
############################################################
# Type 1 model
# Calculate Rhat values for all parameters of the model in all simulations
rhat_summary_t1d <- NULL
## append below the rhat values for each simulation
for (i in 1:bootstrap_t1d$iterations) {
rhat_summary_t1d <- rbind(
rhat_summary_t1d,
bootstrap_t1d$simulations[[i]]$rhat_recalibration
)
}
## create summary statistics
rhat_summary_t1d <- rhat_summary_t1d %>%
# group by each parameters of the model
group_by(key) %>%
# create variables
mutate(
low_ci = quantile(rhat, probs = 0.025),
median = quantile(rhat, probs = 0.5),
high_ci = quantile(rhat, probs = 0.975)
) %>%
# remove grouping
ungroup() %>%
# remove rhat variable with values
select(-rhat) %>%
# keep only unique entries
unique()
############################
# Type 2 model
# Calculate Rhat values for all parameters of the model in all simulations
rhat_summary_t2d <- NULL
## append below the rhat values for each simulation
for (i in 1:bootstrap_t2d$iterations) {
rhat_summary_t2d <- rbind(
rhat_summary_t2d,
bootstrap_t2d$simulations[[i]]$rhat_recalibration
)
}
## create summary statistics
rhat_summary_t2d <- rhat_summary_t2d %>%
# group by each parameters of the model
group_by(key) %>%
# create variables
mutate(
low_ci = quantile(rhat, probs = 0.025),
median = quantile(rhat, probs = 0.5),
high_ci = quantile(rhat, probs = 0.975)
) %>%
# remove grouping
ungroup() %>%
# remove rhat variable with values
select(-rhat) %>%
# keep only unique entries
unique()
############################################################
# Calibration plots for the predictions
# Type 1 model
dataset_t1d_real <- data.frame(
id = 1:nrow(dataset.UNITED_type1_all_genes),
recalibration_real = predictions_dataset.UNITED_type1_all_genes_with_T$prob
)
## appending the probabilities together
comparing_t1d_simulation <- NULL
for (i in 1:bootstrap_t1d$iterations) {
comparing_t1d_simulation <- rbind(
comparing_t1d_simulation,
bootstrap_t1d$simulations[[i]]$bootstrap_dataset
)
}
combined_probabilities_t1d <- comparing_t1d_simulation %>%
# join real probability from original model
left_join(dataset_t1d_real, by = c("id")) %>%
# remove id variable
select(-id) %>%
# rename variables
rename("x" = "recalibration_real", "y" = "recalibration") %>%
# group by real value
group_by(x) %>%
# create some summary variables
mutate(
y_max = quantile(y, probs = c(0.975)),
y_min = quantile(y, probs = c(0.025))
) %>%
# remove grouping
ungroup() %>%
# create grouping variable
mutate(group = cut(x, breaks = unique(c(seq(0, 0.1, 0.01), seq(0.1, 0.9, 0.1), seq(0.9, 1, 0.01))))) %>%
# group by groung variable
group_by(group) %>%
# create summary variables
mutate(
x_mean = mean(x),
ymin = quantile(y, probs = c(0.025)),
ymin_75 = quantile(y, probs = c(0.125)),
ymin_50 = quantile(y, probs = c(0.25)),
ymin_25 = quantile(y, probs = c(0.375)),
ymax_25 = quantile(y, probs = c(0.625)),
ymax_50 = quantile(y, probs = c(0.75)),
ymax_75 = quantile(y, probs = c(0.875)),
ymax = quantile(y, probs = c(0.975))
) %>%
# remove grouping
ungroup() %>%
# remove variables not needed
select(-y, -x, -y_max, -y_min, - group) %>%
# keep unique
distinct()
## custom points for legend
legend_dataset <- cbind(
x = c(-1, -1, -1, -1),
y = c(-1, -1, -1, -1),
alpha = c(0.1, 0.2, 0.3, 0.4)
) %>%
as.data.frame()
plot_simulation_riley_t1d <- combined_probabilities_t1d %>%
ggplot() +
geom_ribbon(aes(x = x_mean, ymin = ymin, ymax = ymax), alpha = 0.1) +
geom_ribbon(aes(x = x_mean, ymin = ymin_75, ymax = ymax_75), alpha = 0.2) +
geom_ribbon(aes(x = x_mean, ymin = ymin_50, ymax = ymax_50), alpha = 0.3) +
geom_ribbon(aes(x = x_mean, ymin = ymin_25, ymax = ymax_25), alpha = 0.4) +
geom_point(data = legend_dataset, aes(x = x, y = y, alpha = alpha), shape = "square", size = 4) +
geom_segment(x = 0, xend = 1, y = 0, yend = 1) +
xlab("Estimated risk from developed model") +
ylab("Estimated risk from bootstrap models") +
guides(alpha = guide_legend(title = "Credible interval")) +
scale_alpha_continuous(labels = c("95%", "75%", "50%", "25%"), range = c(0.1, 0.4)) +
theme_bw() +
scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
scale_x_continuous(limits = c(0, 1), labels = scales::percent) +
theme(
axis.text = element_text(size = 12),
strip.text = element_text(size = 16),
title = element_text(size = 17),
legend.position = "bottom",
legend.text = element_text(size = 12)) +
coord_cartesian(ylim = c(0, 1),
xlim = c(0, 1))
# Type 2 model
dataset_t2d_real <- data.frame(
id = 1:nrow(dataset.UNITED_type2_all_genes),
recalibration_real = predictions_dataset.UNITED_type2_all_genes_new$prob
)
## appending the probabilities together
comparing_t2d_simulation <- NULL
for (i in 1:bootstrap_t2d$iterations) {
comparing_t2d_simulation <- rbind(
comparing_t2d_simulation,
bootstrap_t2d$simulations[[i]]$bootstrap_dataset
)
}
combined_probabilities_t2d <- comparing_t2d_simulation %>%
# join real probability from original model
left_join(dataset_t2d_real, by = c("id")) %>%
# remove id variable
select(-id) %>%
# rename variables
rename("x" = "recalibration_real", "y" = "recalibration") %>%
# group by real value
group_by(x) %>%
# create some summary variables
mutate(
y_max = quantile(y, probs = c(0.975)),
y_min = quantile(y, probs = c(0.025))
) %>%
# remove grouping
ungroup() %>%
# create grouping variable
mutate(group = cut(x, breaks = unique(c(seq(0, 0.1, 0.01), seq(0.1, 0.9, 0.1), seq(0.9, 1, 0.01))))) %>%
# group by groung variable
group_by(group) %>%
# create summary variables
mutate(
x_mean = mean(x),
ymin = quantile(y, probs = c(0.025)),
ymin_75 = quantile(y, probs = c(0.125)),
ymin_50 = quantile(y, probs = c(0.25)),
ymin_25 = quantile(y, probs = c(0.375)),
ymax_25 = quantile(y, probs = c(0.625)),
ymax_50 = quantile(y, probs = c(0.75)),
ymax_75 = quantile(y, probs = c(0.875)),
ymax = quantile(y, probs = c(0.975))
) %>%
# remove grouping
ungroup() %>%
# remove variables not needed
select(-y, -x, -y_max, -y_min, - group) %>%
# keep unique
distinct()
## custom points for legend
legend_dataset <- cbind(
x = c(-1, -1, -1, -1),
y = c(-1, -1, -1, -1),
alpha = c(0.1, 0.2, 0.3, 0.4)
) %>%
as.data.frame()
plot_simulation_riley_t2d <- combined_probabilities_t2d %>%
ggplot() +
geom_ribbon(aes(x = x_mean, ymin = ymin, ymax = ymax), alpha = 0.1) +
geom_ribbon(aes(x = x_mean, ymin = ymin_75, ymax = ymax_75), alpha = 0.2) +
geom_ribbon(aes(x = x_mean, ymin = ymin_50, ymax = ymax_50), alpha = 0.3) +
geom_ribbon(aes(x = x_mean, ymin = ymin_25, ymax = ymax_25), alpha = 0.4) +
geom_point(data = legend_dataset, aes(x = x, y = y, alpha = alpha), shape = "square", size = 4) +
geom_segment(x = 0, xend = 1, y = 0, yend = 1) +
xlab("Estimated risk from developed model") +
ylab("Estimated risk from bootstrap models") +
guides(alpha = guide_legend(title = "Credible interval")) +
scale_alpha_continuous(labels = c("95%", "75%", "50%", "25%"), range = c(0.1, 0.4)) +
theme_bw() +
scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
scale_x_continuous(limits = c(0, 1), labels = scales::percent) +
theme(
axis.text = element_text(size = 12),
strip.text = element_text(size = 16),
title = element_text(size = 17),
legend.position = "bottom",
legend.text = element_text(size = 12)) +
coord_cartesian(ylim = c(0, 1),
xlim = c(0, 1))
plot_simulation_riley_combined <- wrap_plots(
plot_simulation_riley_t1d,
plot_simulation_riley_t2d,
nrow = 1
) +
patchwork::plot_annotation(tag_levels = list(c("A", "B"))) +
plot_layout(guides = 'collect') &
theme(
legend.position = "bottom"
)
pdf("figures/bootstrap_riley_simulation.pdf", width = 10, height = 5)
plot_simulation_riley_combined
dev.off()