-
Notifications
You must be signed in to change notification settings - Fork 2
/
04_gam.R
152 lines (138 loc) · 4.09 KB
/
04_gam.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
# ISARIC WHO CCP-UK study: 4C Mortality Score
# Generalised additive models
# 04_gam.R
# Centre for Medical Informatics, Usher Institute, University of Edinburgh 2020
# 1. GAM with complete data
# 2. GAM with mice data
# 3. purrr methods for metrics, combining by Rubin's rules.
# Packages -----------------------------------------------
library(mgcv)
library(purrr)
library(pROC)
library(yardstick)
# GAM using complete dataset-------------------------------
## Indicative regression for complete data
gam_complete = cs_train %>%
select(death, sex, no_comorbid,
age, rr_vsorres, oxy_vsorres, daily_gcs_vsorres,
daily_bun_lborres, daily_crp_lborres) %>%
drop_na() %>%
gam(death ~
sex +
ethnicity_4levels +
s(age) +
s(no_comorbid) +
s(rr_vsorres, k = 4) +
s(oxy_vsorres, k = 3) +
s(sysbp_vsorres) +
s(admission_diabp_vsorres) +
s(temp_vsorres) +
s(hr_vsorres) +
s(daily_gcs_vsorres, k = 4) +
s(daily_hb_lborres) +
s(daily_wbc_lborres) +
s(daily_neutro_lborres) +
s(daily_lymp_lborres) +
s(daily_plt_lborres) +
s(daily_sodium_lborres) +
s(daily_potassium_lborres) +
s(daily_bil_lborres) +
s(daily_creat_lborres) +
s(daily_bun_lborres) +
s(daily_crp_lborres),
data = ., family = binomial)
summary(gam_complete)
plot(gam_complete)
# AUROC
## Derivation
roc(cs_train$death, gam_complete %>% predict(cs_train)) %>%
pROC::ci(method = "bootstrap") %>%
round(3)
## Validation
roc(cs_test$death, gam_complete %>% predict(cs_test)) %>%
pROC::ci(method = "bootstrap") %>%
round(3)
# GAM with mice data ------------------------------------------------
# mids object from mice: `sets_train` and `sets_test`
## Indicative regression methods for mice
gam_mice = sets_train %>%
with(
gam(death ~
sex +
ethnicity_4levels +
s(age) +
s(no_comorbid) +
s(rr_vsorres, k = 4) +
s(oxy_vsorres, k = 3) +
s(sysbp_vsorres) +
s(admission_diabp_vsorres) +
s(temp_vsorres) +
s(hr_vsorres) +
s(daily_gcs_vsorres, k = 4) +
s(daily_hb_lborres) +
s(daily_wbc_lborres) +
s(daily_neutro_lborres) +
s(daily_lymp_lborres) +
s(daily_plt_lborres) +
s(daily_sodium_lborres) +
s(daily_potassium_lborres) +
s(daily_bil_lborres) +
s(daily_creat_lborres) +
s(daily_bun_lborres) +
s(daily_crp_lborres),
family = binomial)
)
# Extract metrics and mean as per Rubin's rules
## Note purrr methods to combine 10 models.
gam_mice %>%
magrittr::extract2(4) %>%
map_df(function(.x){
n = summary(.x) %$% n
r.sq = summary(.x) %$% r.sq
dev.expl = summary(.x) %$% dev.expl
ubre = summary(.x) %$% sp.criterion
tibble(n, r.sq, dev.expl, ubre)
}) %>%
summarise_all(mean)
# AUROC in imputed derviation data
map2(.x = gam_mice %>%
magrittr::extract2(4),
.y = sets_train %>%
mice::complete("all"),
~ roc(.y$status, .x %>% predict(.y)) %>%
pROC::ci(method = "bootstrap") # Computationally intensive, use method = "delong" for testing
) %>%
enframe() %>%
unnest_wider(value) %>%
summarise_all(mean)
# AUROC in imputed validation data
map2(.x = gam_mice %>%
magrittr::extract2(4),
.y = sets_test %>%
mice::complete("all"),
~ roc(.y$status, .x %>% predict(.y)) %>%
pROC::ci(method = "delong")
) %>%
enframe() %>%
unnest_wider(value) %>%
summarise_all(mean)
# AUROC in complete derivation data
map(gam_mice %>%
magrittr::extract2(4),
function(.x){
roc_auc_vec(
truth = cs_train$death,
estimate = .x %>% predict(cs_train),
event_level = "second")
}
)
# AUROC in complete validation data
map(gam_mice %>%
magrittr::extract2(4),
function(.x){
roc_auc_vec(
truth = cs_test$death,
estimate = .x %>% predict(cs_test),
event_level = "second")
}
)