This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.slade_aurum_insulin_treated_validation.R
266 lines (189 loc) · 9.84 KB
/
12.slade_aurum_insulin_treated_validation.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
####################
## Description:
## - In this file we validation the semaglutide population
####################
## Load libraries
library(tidyverse)
## Set up directory path to save files (stagered to ensure folders are created)
dir.create("Samples")
dir.create("Samples/SGLT2-GLP1")
output_path <- "Samples/SGLT2-GLP1/Aurum"
dir.create(output_path)
## make directory for outputs
dir.create("Plots")
## male directory for outputs
dir.create(paste0(output_path, "/insulin_treated"))
###############################################################################
###############################################################################
############################### Read Data In ##################################
###############################################################################
###############################################################################
## Load functions required
source("01.slade_aurum_functions.R")
source("02.slade_aurum_set_data.R")
###############################################################################
###############################################################################
########################## General variables ##################################
###############################################################################
###############################################################################
## Load dataset
insulin.dataset <- set_up_data_sglt2_glp1(dataset.type="insulin.dataset")
# load in variables used in the model
variables_mu <- readRDS(paste0(output_path, "/response_model_bcf/variables_mu.rds"))
variables_tau <- readRDS(paste0(output_path, "/response_model_bcf/variables_tau.rds"))
bcf_model <- readRDS(paste0(output_path, "/response_model_bcf/bcf_model.rds"))
# Create interim dataset with the variables needed
interim.dataset <- insulin.dataset %>%
# select variables to make prediction
select(all_of(c("patid", "pated", "ethnicity", "drugclass", "posthba1cfinal", unique(c(variables_mu, variables_tau))))) %>%
drop_na()
# Predict treatment effect for these patients from our model
if (class(try(
patient_effects <- readRDS(paste0(output_path, "/insulin_treated/patient_effects.rds"))
# predictions.interim <- readRDS(paste0(output_path, "/insulin_treated/predictions.interim.rds"))
, silent = TRUE)) == "try-error") {
require(bcf)
predictions.interim <- predict(object = bcf_model,
x_predict_control = interim.dataset %>%
select(
all_of(variables_mu)
) %>%
mutate_all(funs(as.numeric(.))) %>%
as.matrix(),
x_predict_moderate = interim.dataset %>%
select(
all_of(variables_tau)
) %>%
mutate_all(funs(as.numeric(.))) %>%
as.matrix(),
pi_pred = rep(0.5, interim.dataset %>% nrow()),
z_pred = interim.dataset %>%
select(drugclass) %>%
mutate(drugclass = ifelse(drugclass == "GLP1", 0, 1)) %>%
unlist(),
save_tree_directory = paste0(output_path, "/response_model_bcf/trees_no_prop"))
saveRDS(predictions.interim, paste0(output_path, "/insulin_treated/predictions.interim.rds"))
# Combine and remove the extra iteration added
patient_effects <- interim.dataset %>%
select(patid, pated) %>%
cbind(effects = colMeans(predictions.interim$tau))
# slice(-nrow(interim.dataset))
saveRDS(patient_effects, paste0(output_path, "/insulin_treated/patient_effects.rds"))
}
#:-------------------------------------------------------------------------------
## Validation of individualised treatment effects
### create dataset with treatment effect
predicted_observed <- interim.dataset %>%
left_join(patient_effects, by = c("patid", "pated")) %>%
rename("hba1c_diff" = "effects") %>%
mutate(hba1c_diff.q = ntile(hba1c_diff, 10))
# posthba1c ~ drugclass + adjust (all variables used in bcf)
if (class(try(
ATE_adjust_validation <- readRDS(paste0(output_path, "/response_model_bcf/assessment/ATE_adjust_validation.rds"))
, silent = TRUE)) == "try-error") {
ATE_adjust_validation <- calc_ATE(data = predicted_observed,
validation_type = "Adjust", variable = "posthba1cfinal",
quantile_var = "hba1c_diff.q",
order = "largest", breakdown = unique(c(variables_tau, variables_mu)))
saveRDS(ATE_adjust_validation, paste0(output_path, "/response_model_bcf/assessment/ATE_adjust_validation.rds"))
}
# Plot calibration of treatment effects
plot_ATE_adjust_validation <- ATE_plot(ATE_adjust_validation[["effects"]], "hba1c_diff.pred", "obs", "lci", "uci", ymin = -12, ymax = 12) +
ggtitle(paste0("Adjusted model (n=", format(nrow(predicted_observed),big.mark=",",scientific=FALSE), ")"))
pdf("Plots/11.12.insulin_treated.pdf")
plot_ATE_adjust_validation
dev.off()
#:-------------------------------------------------------------------------------
#:-------------------------------------------------------------------------------
#:-------------------------------------------------------------------------------
### Validation of treatment effects - test to check if the model needs any adjustment (intercept or intercept + slope)
#closed testing function
closedtest <- function(cohort,dataset,observed,predicted,p.value){
#Original model
#Residuals
resid <- observed-predicted
#variance of residuals
sigma2 <- var(resid)
#original log-likelihood
n <- length(resid)
logLik.original <- -n/2 * log(2*pi*sigma2) - 1/(2*sigma2) * sum(resid^2)
#Update intercept
#Model with updated intercept
m <- lm(observed-predicted~1,data=dataset)
#Extract coefficient
m1.intercept <- cbind(m$coefficients[1],confint(m)[1],confint(m)[2])
#Residuals (actual - predicted)
resid <- residuals(m)
#variance of residuals
sigma2 <- var(resid)
#log-likelihood
n <- length(resid)
logLik.intercept <- -n/2 * log(2*pi*sigma2) - 1/(2*sigma2) * sum(resid^2)
#Update slope & intercept
#Model with updated slope & intercept
m <- lm(observed~predicted,data=dataset)
#Extract coefficient
m2.intercept <- cbind(m$coefficients[1],confint(m)[1,1],confint(m)[1,2])
m2.slope <- cbind(m$coefficients[2],confint(m)[2,1],confint(m)[2,2])
#Residuals (actual - predicted)
resid <- residuals(m)
#variance of residuals
sigma2 <- var(resid)
#log-likelihood
n <- length(resid)
logLik.recal <- -n/2 * log(2*pi*sigma2) - 1/(2*sigma2) * sum(resid^2)
#test significance
#1. Test recal in the large against the original model (no extra coeffs estimated)
#2. If 2. is significant, test full recal against the recal in the large model using p df + 1 (1 extra coef estimated)
#3. If 3. is significant, select 3. as final model, if not select 2. If neither, select 1
#ll diff
dev_intercept <- -2*logLik.original + 2*logLik.intercept
dev_recal <- -2*logLik.intercept + 2*logLik.recal
#Diff in ll
ncolx <- ncolx
test1 <- (1-pchisq(dev_intercept, ncolx)) < p.value
test2 <- (1-pchisq(dev_recal, ncolx+1)) < p.value
#p.value
p1 <- (1-pchisq(dev_intercept, ncolx))
p2 <- (1-pchisq(dev_recal, ncolx+1))
#Which model is chosen
test_intercept <- 1 * (!test1)
test_recal <- 2 * ((!test1)&(!test2))
index_test <- (test_intercept + test_recal)
res <- data.frame(cohort=c(cohort,cohort,cohort),
n=c(nrow(dataset),nrow(dataset),nrow(dataset)),
model=c("Original","Updated intercept","Recalibrated"),
loglikelihood=c(logLik.original,logLik.intercept,logLik.recal),
intercept=c(NA,
m1.intercept[1],
m1.intercept[2]),
intercept.w.ci=c(NA,
paste0(round(m1.intercept[1],2), " (",paste0(round(m1.intercept[2],2),", ",paste0(round(m1.intercept[3],2)),")")),
paste0(round(m2.intercept[1],2), " (",paste0(round(m2.intercept[2],2),", ",paste0(round(m2.intercept[3],2)),")"))),
slope=c(NA,NA,m2.slope[1]),
slope.w.ci=c(NA,
NA,
paste0(round(m2.slope[1],2), " (",paste0(round(m2.slope[2],2),", ",paste0(round(m2.slope[3],2)),")"))),
p.value=c(NA,
round(p1,5),
round(p2,5)),
model.selected=c(ifelse(test1==FALSE & test2==FALSE,"Yes","No"),
ifelse(test1==TRUE & test2==FALSE,"Yes","No"),
ifelse(test2==TRUE,"Yes","No"))
)
return(res)
}
# global settings
p.value <- 0.05
ncolx <- 10
sample_frac <- 1
cohort <- "insulin"
observed <- interim.dataset$posthba1cfinal
predicted <- colMeans(predictions.interim$mu)
# dataset required for the calibration
dataset <- insulin.dataset %>%
# select variables to make prediction
select(all_of(c("patid", "pated", "ethnicity", "drugclass", "posthba1cfinal", unique(c(variables_mu, variables_tau))))) %>%
drop_na()
# Run test
insulin.test <- closedtest(cohort,dataset,observed,predicted,p.value)