forked from PM-Cardoso/SGLT2-GLP1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.2.slade_bcf_comparison.R
308 lines (219 loc) · 10.9 KB
/
6.2.slade_bcf_comparison.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
####################
## Description:
## - In this file we make a collected of plots comparing assessment
## measurements of several models:
## - 3.2. bcf vs 3.1. grf
## - 3.2. bcf vs 5.1. Complete/Routine model/No propensity score
## - 3.2. bcf vs 5.1. Complete/Routine model/Propensity score
####################
# Used in slade to ensure the library being used is my personal library
.libPaths(.libPaths()[c(2,1,3)])
## increase memery usage to 50gb of RAM
options(java.parameters = "-Xmx50g")
library(tidyverse)
library(bcf)
library(grf)
## path to output folder
output_path <- "Samples"
## make directory for outputs
dir.create(output_path)
output_path <- "Samples/SGLT2-GLP1"
## make directory for outputs
dir.create(output_path)
## make directory for outputs
dir.create(paste0(output_path, "/Comparison"))
## make directory for outputs
dir.create("Plots")
###############################################################################
###############################################################################
######################### Read Data / Model In ################################
###############################################################################
###############################################################################
# name: final.dev
load(paste0(output_path, "/datasets/cprd_19_sglt2glp1_devcohort.Rda"))
# name: final.val
load(paste0(output_path, "/datasets/cprd_19_sglt2glp1_valcohort.Rda"))
###############################################################################
###############################################################################
################################ FUNCTIONS ####################################
###############################################################################
###############################################################################
source("0.1.slade_functions.R")
############################# BCF
### Complete model of only routine data, no propensity score (n: 9866))
#############################
data_complete_routine_dev <- final.dev %>%
select(
patid,
pateddrug,
posthba1c_final,
drugclass, ncurrtx, drugline, yrdrugstart, t2dmduration, agetx, malesex, Category, hba1cmonth, prebmi, prealt, egfr_ckdepi, prehba1cmmol
) %>%
drop_na() # removed 1302
data_complete_routine_val <- final.val %>%
select(
patid,
pateddrug,
posthba1c_final,
drugclass, ncurrtx, drugline, yrdrugstart, t2dmduration, agetx, malesex, Category, hba1cmonth, prebmi, prealt, egfr_ckdepi, prehba1cmmol
) %>%
drop_na() # removed 804
# Produce a model matrix for fitting bcf
dataset_full <- rbind(data_complete_routine_dev, data_complete_routine_val)
dataset_model.matrix <- model.matrix(~posthba1c_final + drugclass + ncurrtx + drugline + yrdrugstart + t2dmduration + agetx +
malesex + Category + hba1cmonth + prebmi + prealt + egfr_ckdepi + prehba1cmmol, dataset_full) %>%
as.data.frame() %>%
select(-`(Intercept)`) %>%
mutate(drugclass = drugclassSGLT2) %>%
select(-drugclassSGLT2)
# calculate a propensity score for the model
prop.score <- glm(drugclass ~ ncurrtx + drugline + t2dmduration + agetx +
malesex + Category + hba1cmonth + prebmi + prealt + egfr_ckdepi + prehba1cmmol, family = binomial(link = "logit"), data = dataset_full[1:nrow(data_complete_routine_dev),])
dataset_full_bcf <- dataset_model.matrix %>%
mutate_all(function(x) as.numeric(x)) %>%
as.matrix()
# fit the model
post <- bcf::bcf(y = dataset_full_bcf[1:nrow(data_complete_routine_dev),1],
z = dataset_full_bcf[1:nrow(data_complete_routine_dev),19],
x_control = dataset_full_bcf[1:nrow(data_complete_routine_dev),-c(1,19)],
pihat = prop.score$fitted.values,
nburn = 1000,
nsim = 1000)
# collect average treatment effect
bcf.effects.dev <- cbind(mean = post$tau %>% colMeans()) %>%
data.frame() %>%
set_names(c("mean"))
##########
grf_model <- grf::causal_forest(X = dataset_model.matrix %>%
slice(1:nrow(data_complete_routine_dev)) %>%
select(-posthba1c_final, -drugclass),
Y = dataset_model.matrix[1:nrow(data_complete_routine_dev), "posthba1c_final"],
W = dataset_model.matrix[1:nrow(data_complete_routine_dev), "drugclass"],
W.hat = prop.score$fitted.values)
#Dev
grf.effects.dev <- cbind(mean = grf_model$predictions) %>%
data.frame() %>%
set_names(c("mean"))
### BCF vs GRF
plot_effect_comparison_1 <- cbind(`BCF Effect` = bcf.effects.dev[,"mean"],
`GRF Effect` = grf.effects.dev[,"mean"]) %>%
as.data.frame() %>%
ggplot() +
theme_bw() +
geom_point(aes(x = `BCF Effect`, y = `GRF Effect`)) +
geom_abline(aes(intercept = 0, slope = 1), linetype ="dashed", color = viridis::viridis(1, begin = 0.6), lwd=0.75) +
geom_smooth(aes(x = `BCF Effect`, y = `GRF Effect`), colour = "red", method = "lm") +
xlab("BCF: Predicted CATE") +
ylab("GRF: Predicted CATE") +
ggtitle("BCF vs GRF")
### Complete model of only routine data, no propensity score
comp_routine_no_prop_effects_summary_dev <- readRDS(paste0(output_path, "/Final_model/Assessment/comp_routine_no_prop_effects_summary_dev.rds"))
plot_effect_comparison_2 <- as.data.frame(comp_routine_no_prop_effects_summary_dev) %>%
cbind(`BCF Effect` = bcf.effects.dev[,"mean"]) %>%
ggplot() +
theme_bw() +
geom_errorbar(aes(ymin = `5%`, ymax = `95%`, x = `BCF Effect`), colour = "grey") +
geom_point(aes(x = `BCF Effect`, y = `mean`)) +
geom_abline(aes(intercept = 0, slope = 1), linetype ="dashed", color = viridis::viridis(1, begin = 0.6), lwd=0.75) +
geom_smooth(aes(x = `BCF Effect`, y = `mean`), colour = "red", method = "lm") +
xlab("BCF: Predicted CATE") +
ylab("BART: Predicted treatment Heterogeneity") +
ggtitle("BCF vs BART (no propensity score)")
bart_comp_routine_no_prop <- readRDS(paste0(output_path, "/Model_fit/bart_comp_routine_no_prop.rds"))
# Dev
data_complete_routine_dev <- final.dev %>%
select(
patid,
pateddrug,
posthba1c_final,
colnames(bart_comp_routine_no_prop$X)
) %>%
drop_na()
# calculate effects
if (class(try(
comp_routine_no_prop_effects_dev <- readRDS(paste0(output_path, "/Comparison/comp_routine_no_prop_effects_dev.rds"))
, silent = TRUE)) == "try-error") {
comp_routine_no_prop_effects_dev <- calc_effect(bart_comp_routine_no_prop, data_complete_routine_dev)
saveRDS(comp_routine_no_prop_effects_dev, paste0(output_path, "/Comparison/comp_routine_no_prop_effects_dev.rds"))
}
# To calculate error at each iteration, we do BART effects - BCF effects.
# - A positive value: BART is over-estimating
# - A negative value: BART is under-estimating
error_comparison_2 <- comp_routine_no_prop_effects_dev - t(post$tau)
plot_error_comparison_2 <- cbind(
`5%` = apply(error_comparison_2, MARGIN = 1, function(x) quantile(c(x), probs = c(0.05))),
`50%` = apply(error_comparison_2, MARGIN = 1, function(x) quantile(c(x), probs = c(0.50))),
`95%` = apply(error_comparison_2, MARGIN = 1, function(x) quantile(c(x), probs = c(0.95))),
mean = apply(error_comparison_2, MARGIN = 1, function(x) mean(c(x))),
`BCF Effect` = bcf.effects.dev[,"mean"]
) %>%
as.data.frame() %>%
ggplot() +
theme_bw() +
geom_errorbar(aes(ymin = `5%`, ymax = `95%`, x = `BCF Effect`), colour = "grey") +
geom_point(aes(x = `BCF Effect`, y = `mean`)) +
geom_hline(aes(yintercept = 0), linetype = "dashed", color = viridis::viridis(1, begin = 0.6), lwd = 0.75) +
geom_smooth(aes(x = `BCF Effect`, y = `mean`), colour = "red", method = "lm") +
xlab("BCF: Predicted CATE") +
ylab("Treatment Effect difference") +
ggtitle("BCF vs BART (no propensity score)")
### Complete model of only routine data, propensity score
comp_routine_prop_effects_summary_dev <- readRDS(paste0(output_path, "/Final_model/Assessment/comp_routine_prop_effects_summary_dev.rds"))
plot_effect_comparison_3 <- as.data.frame(comp_routine_prop_effects_summary_dev) %>%
cbind(`BCF Effect` = bcf.effects.dev[,"mean"]) %>%
ggplot() +
theme_bw() +
geom_errorbar(aes(ymin = `5%`, ymax = `95%`, x = `BCF Effect`), colour = "grey") +
geom_point(aes(x = `BCF Effect`, y = `mean`)) +
geom_abline(aes(intercept = 0, slope = 1), linetype ="dashed", color = viridis::viridis(1, begin = 0.6), lwd=0.75) +
geom_smooth(aes(x = `BCF Effect`, y = `mean`), colour = "red", method = "lm") +
xlab("BCF: Predicted CATE") +
ylab("BART: Predicted treatment Heterogeneity") +
ggtitle("BCF vs BART (propensity score)")
bart_comp_routine_prop <- readRDS(paste0(output_path, "/Model_fit/bart_comp_routine_prop.rds"))
bart_comp_routine_prop_model <- readRDS(paste0(output_path, "/Model_fit/bart_comp_routine_prop_model.rds"))
# Dev
data_complete_routine_prop_dev <- final.dev %>%
select(
patid,
pateddrug,
posthba1c_final,
colnames(bart_comp_routine_prop_model$X)[which(colnames(bart_comp_routine_prop_model$X) != "prop_score")]
) %>%
drop_na() %>%
cbind(prop_score = bart_comp_routine_prop$p_hat_train)
# calculate effects
if (class(try(
comp_routine_prop_effects_dev <- readRDS(paste0(output_path, "/Comparison/comp_routine_prop_effects_dev.rds"))
, silent = TRUE)) == "try-error") {
comp_routine_prop_effects_dev <- calc_effect(bart_comp_routine_prop_model, data_complete_routine_prop_dev)
saveRDS(comp_routine_prop_effects_dev, paste0(output_path, "/Comparison/comp_routine_prop_effects_dev.rds"))
}
# To calculate error at each iteration, we do BART effects - BCF effects.
# - A positive value: BART is over-estimating
# - A negative value: BART is under-estimating
error_comparison_3 <- comp_routine_prop_effects_dev - t(post$tau)
plot_error_comparison_3 <- cbind(
`5%` = apply(error_comparison_3, MARGIN = 1, function(x) quantile(c(x), probs = c(0.05))),
`50%` = apply(error_comparison_3, MARGIN = 1, function(x) quantile(c(x), probs = c(0.50))),
`95%` = apply(error_comparison_3, MARGIN = 1, function(x) quantile(c(x), probs = c(0.95))),
mean = apply(error_comparison_3, MARGIN = 1, function(x) mean(c(x))),
`BCF Effect` = bcf.effects.dev[,"mean"]
) %>%
as.data.frame() %>%
ggplot() +
theme_bw() +
geom_errorbar(aes(ymin = `5%`, ymax = `95%`, x = `BCF Effect`), colour = "grey") +
geom_point(aes(x = `BCF Effect`, y = `mean`)) +
geom_hline(aes(yintercept = 0), linetype = "dashed", color = viridis::viridis(1, begin = 0.6), lwd = 0.75) +
geom_smooth(aes(x = `BCF Effect`, y = `mean`), colour = "red", method = "lm") +
xlab("BCF: Predicted CATE") +
ylab("Treatment Effect difference") +
ggtitle("BCF vs BART (propensity score)")
pdf(file = "Plots/6.2.comparison_bcf_bart.pdf")
plot_effect_comparison_1
plot_effect_comparison_2
plot_error_comparison_2
plot_effect_comparison_3
plot_error_comparison_3
dev.off()