forked from PM-Cardoso/SGLT2-GLP1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.2.slade_model_fitting_bcf.R
196 lines (133 loc) · 5.17 KB
/
3.2.slade_model_fitting_bcf.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
####################
## Description:
## - In this file we use generalised random forests (grf), to model
## conditional average treatment effect in a causal model.
####################
# Used in slade to ensure the library being used is my personal library
.libPaths(.libPaths()[c(2,1,3)])
library(tidyverse)
## 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("Plots")
###############################################################################
###############################################################################
############################### Read Data In ##################################
###############################################################################
###############################################################################
# name: final.dev
load(paste0(output_path, "/datasets/cprd_19_sglt2glp1_devcohort.Rda"))
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
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)
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()
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)
effects.dev <- cbind(mean = post$tau %>% colMeans()) %>%
data.frame() %>%
set_names(c("mean"))
#########
predicted_observed_complete_routine_dev <- data_complete_routine_dev %>%
cbind(hba1c_diff = effects.dev$mean) %>%
mutate(bestdrug = ifelse(hba1c_diff < 0, "SGLT2", "GLP1"),
hba1c_diff.q = ntile(hba1c_diff, 10))
plot_effects_validation_dev <- plot_full_effects_validation(predicted_observed_complete_routine_dev, dataset = "Dev")
###
# plot residuals
resid_dev <- cbind(lower_bd = apply(post$yhat, MARGIN = 2, function(x) min(x)),
upper_bd = apply(post$yhat, MARGIN = 2, function(x) max(x)),
mean = apply(post$yhat, MARGIN = 2, function(x) mean(x)),
orig = dataset_full_bcf[1:nrow(data_complete_routine_dev),1]) %>%
as.data.frame() %>%
mutate(resid = orig - mean,
resid.low = orig - lower_bd,
resid.high = orig - upper_bd)
plot_resid_dev <- resid_dev %>%
ggplot() +
theme_bw() +
geom_errorbar(aes(ymin = lower_bd, ymax = upper_bd, x = orig), colour = "grey") +
geom_point(aes(x = orig, y = mean)) +
geom_abline(aes(intercept = 0, slope = 1), linetype ="dashed", color = viridis::viridis(1, begin = 0.6), lwd=0.75) +
xlim(min(resid_dev$orig), max(resid_dev$orig)) +
ylim(min(resid_dev$orig), max(resid_dev$orig)) +
xlab("Observed HbA1c (mmol/mol)") +
ylab("Predicted HbA1c (mmol/mol)")
#########
pdf(file = "Plots/3.2.bcf_effects.pdf")
prop.score$fitted.values %>%
as.data.frame() %>%
set_names(c("value")) %>%
ggplot() +
geom_histogram(aes(x = value)) +
ggtitle("Propensity scores")
hist_plot(effects.dev, "Dev BCF: treatment effect", -15, 20)
plot_effects_validation_dev
plot_resid_dev
dev.off()