-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05 Predicted benefits (calibration).R
311 lines (233 loc) · 11.7 KB
/
05 Predicted benefits (calibration).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
# Predict benefits
# Plot calibration plot
############################################################################################
# Setup
library(tidyverse)
library(survival)
library(survminer)
library(broom)
library(rms)
library(ggthemes)
library(PSweight)
library(grid)
options(dplyr.summarise.inform = FALSE)
rm(list=ls())
############################################################################################
# 1 Cohort selection (see script 00)
setwd("/slade/CPRD_data/Katie SGLT2/Processed data")
load("treatment_outcome_cohort_jun24.rda")
# Add survival variables
setwd("/slade/CPRD_data/Katie SGLT2/Scripts/Functions")
source("survival_variables.R")
cohort <- add_surv_vars(cohort, main_only=T)
# now using functions to define covariates adjusted and weighted for
setwd("/slade/CPRD_data/Katie SGLT2/Scripts/Functions")
source("full_covariate_set.R")
############################################################################################
# 2 Calculate SGLT2 benefits
cohort <- cohort %>%
mutate(qdhf_survival=(100-qdiabeteshf_5yr_score)/100,
qdhf_survival_sglt2=qdhf_survival^0.63,
qdiabeteshf_5yr_score_sglt2=100-(qdhf_survival_sglt2*100),
qdhf_sglt2_benefit=qdhf_survival_sglt2-qdhf_survival)
############################################################################################
# 3 Histogram of predicted benefits (not truncated)
summary(cohort$qdhf_sglt2_benefit*100)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.02814 0.55854 1.00996 1.36487 1.79742 14.09242
# setwd("/slade/CPRD_data/Katie SGLT2/Plots/")
# tiff("histogram_benefits.tiff", width=9, height=5, units = "in", res=800)
#
# ggplot(cohort, aes(x=qdhf_sglt2_benefit*100)) +
# geom_histogram(aes(y = after_stat(count / sum(count))*100), binwidth=0.1, alpha=0.5, position="identity") +
# ylab("Proportion of study population (%)") + xlab("Predicted 5-year SGLT2i benefit (%)") +
# scale_x_continuous(limits=c(0,15),breaks=c(seq(0,15,by=1))) +
# scale_y_continuous(limits=c(0,7),breaks=c(seq(0, 7, by=1))) +
# theme_base() +
# theme(plot.background = element_blank(),
# panel.border=element_blank())
#
# dev.off()
############################################################################################
# 4 Compare to adjusted observed by decile
# Cut into decile of predicted benefit
cohort$predicted_benefit_decile <- as.factor(ntile(cohort$qdhf_sglt2_benefit,10))
## Average benefit per decile
predicted <- cohort %>%
group_by(predicted_benefit_decile) %>%
summarise(pred.mean=mean(qdhf_sglt2_benefit,na.rm=T),
pred.median=median(qdhf_sglt2_benefit,na.rm=T),
sd=sd(qdhf_sglt2_benefit,na.rm=T),
l_iqr=quantile(qdhf_sglt2_benefit,na.rm=T,probs=0.25),
u_iqr=quantile(qdhf_sglt2_benefit,na.rm=T,probs=0.75),
min=min(qdhf_sglt2_benefit,na.rm=T),
max=max(qdhf_sglt2_benefit,na.rm=T))
## Observed - adjusted and weighted with overlap weights - using this now
ps.formula <- formula(paste0("studydrug ~ ", return_cov_set("weight")))
overlap <- SumStat(ps.formula=ps.formula, data=as.data.frame(cohort), weight="overlap")
cohort$overlap_weights <- overlap$ps.weights$overlap
# Remove constant variables otherwise datadist has issues
cohort <- cohort %>% select(patid, studydrug, qdiabeteshf_5yr_score, dstartdate_age, ethnicity_qrisk2_decoded, malesex, dstartdate_dm_dur_all, imd2015_10, drugline_all, ncurrtx_cat, INS, initiation_year, prebmi, prehba1c2yrs, presbp, qrisk2_smoking_cat, hypertension, hosp_admission_prev_year_count, hf_censtime_yrs, hf_censvar, overlap_weights, qdhf_sglt2_benefit, predicted_benefit_decile, predrug_af)
ddist <- datadist(cohort)
options(datadist='ddist')
f_adjusted <- as.formula(paste0("Surv(hf_censtime_yrs, hf_censvar) ~ studydrug + ", return_cov_set("adjust")))
# This is without studydrug * risk interaction term - which would allow HR to vary with risk
model <- cph(f_adjusted, data=cohort, weights=overlap_weights, x=T, y=T, surv=TRUE)
cohort.t <- data.frame(cohort) %>% mutate(studydrug="DPP4SU")
DPP4SU.pred = survest(model,newdata=cohort.t,times=5,se.fit=F)
cohort.t <- data.frame(cohort) %>% mutate(studydrug="SGLT2")
SGLT2.pred = survest(model,newdata=cohort.t,times=5,se.fit=F)
cohort$DPP4SU.pred <- DPP4SU.pred$surv
cohort$SGLT2.pred <- SGLT2.pred$surv
obs <- cohort %>%
mutate(obs=(SGLT2.pred-DPP4SU.pred)*100) %>%
group_by(predicted_benefit_decile) %>%
dplyr::summarise(
obs.gp = median(obs,na.rm=T),
l_iqr=quantile(obs,0.25,na.rm=T),
u_iqr=quantile(obs,0.75,na.rm=T)
)
predicted.p <- predicted %>%
mutate(pred=pred.median*100) %>%
select(predicted_benefit_decile,pred)
plot.data <- merge(obs,predicted.p,by="predicted_benefit_decile")
# ## Observed - adjusted and weighted with overlap weights, with studydrug*decile interaction - not using
#
# ps.formula <- formula(paste0("studydrug ~ ", return_cov_set("weight")))
#
# overlap <- SumStat(ps.formula=ps.formula, data=as.data.frame(cohort), weight="overlap")
# cohort$overlap_weights <- overlap$ps.weights$overlap
#
# # Remove constant variables otherwise datadist has issues
# cohort <- cohort %>% select(patid, studydrug, qdiabeteshf_5yr_score, dstartdate_age, ethnicity_qrisk2_decoded, malesex, dstartdate_dm_dur_all, imd2015_10, drugline_all, ncurrtx_cat, INS, initiation_year, prebmi, prehba1c2yrs, presbp, qrisk2_smoking_cat, hypertension, hosp_admission_prev_year_count, hf_censtime_yrs, hf_censvar, overlap_weights, predicted_benefit_decile, predrug_af)
#
# ddist <- datadist(cohort)
# options(datadist='ddist')
#
# f_adjusted <- as.formula(paste0("Surv(hf_censtime_yrs, hf_censvar) ~ studydrug*predicted_benefit_decile + ", return_cov_set("adjust")))
#
# model <- cph(f_adjusted, data=cohort, weights=overlap_weights, x=T, y=T, surv=TRUE)
#
# cohort.t <- data.frame(cohort) %>% mutate(studydrug="DPP4SU")
# DPP4SU.pred = survest(model,newdata=cohort.t,times=5,se.fit=F)
# cohort.t <- data.frame(cohort) %>% mutate(studydrug="SGLT2")
# SGLT2.pred = survest(model,newdata=cohort.t,times=5,se.fit=F)
# # from Thijs code
# SGLT2.pred2 = survfit(model,newdata=cohort.t) %>%
# tidy() %>%
# filter(time==5) %>%
# pivot_longer(cols=-c(time, n.risk, n.event, n.censor), names_to = c(".value", "group"), names_pattern = "(.*)\\.(.*)") %>%
# select(group, estimate_sglt2=estimate)
#
#
# test <- cbind(SGLT2.pred2, SGLT2.pred$surv)
# #gives exactly the same answer!!
# Overall estimates
summary((cohort %>% mutate(obs=(SGLT2.pred-DPP4SU.pred)*100))$obs)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.02795 0.50804 0.89648 1.30174 1.61432 13.13283
summary(cohort$qdhf_sglt2_benefit*100)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.02814 0.55854 1.00996 1.36487 1.79742 14.09242
cal_plot <- ggplot(data=plot.data, aes(x=pred,y=obs.gp)) +
geom_point(alpha=1) + theme_bw() +
geom_errorbar(aes(ymin=l_iqr, ymax=u_iqr), colour="black", width=.1) +
ylab("Observed 5-year SGLT2i benefit (%)") + xlab("Predicted 5-year SGLT2i benefit (%)") +
scale_x_continuous(limits=c(0,4.5),breaks=c(seq(0,4.5,by=1))) +
scale_y_continuous(limits=c(0,5),breaks=c(seq(0,5,by=1))) +
theme_base() +
theme_bw() +
theme(text = element_text(size = 18),
axis.line = element_line(colour = "grey50" ),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
#plot.margin = unit(c(0.5, 0, 0, 0), "cm"),
legend.position = "none") +
geom_abline(intercept=0,slope=1, color="red", lwd=0.75) + ggtitle("") +
geom_vline(xintercept=0, linetype="dashed", color = "grey60") + geom_hline(yintercept=0, linetype="dashed", color = "grey60")
cal_plot <- cal_plot +
annotate(geom="text", x=0.1, y=4.8, label="Overall median observed benefit (IQR): 0.90% (0.51-1.6%)\nOverall median predicted benefit: 1.0%", color="black", hjust=0, size = 5)
hist_plot <- ggplot(cohort, aes(x=qdhf_sglt2_benefit*100)) +
geom_histogram(aes(y = after_stat(count / sum(count))*100), binwidth=0.1, alpha=0.5, position="identity") +
guides(fill = FALSE) +
theme(legend.title = element_blank(), panel.background = element_rect( fill = "white",color = "grey50")) +
scale_x_continuous(limits=c(0,4.5),breaks=c(seq(0,4.5,by=1))) +
xlab(expression(paste("Predicted 5-year SGLT2i benefit (%)"))) +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(color="grey50"),
text = element_text(size = 18),
plot.margin = unit(c(0.3, 0, 0, 0), "cm"))
setwd("/slade/CPRD_data/Katie SGLT2/Plots/")
tiff("calibration_benefits_no_interaction.tiff", width=8, height=8, units = "in", res=800)
plot_grid(cal_plot, hist_plot, ncol = 1,align = 'v',
rel_heights = c(1,0.5), rel_widths = c(1,1))
dev.off()
# ### with studydrug*qdhf interaction - not using
#
#
# ## Observed - adjusted and weighted with overlap weights, with studydrug*decile interaction
#
# ps.formula <- formula(paste0("studydrug ~ ", return_cov_set("weight")))
#
# overlap <- SumStat(ps.formula=ps.formula, data=as.data.frame(cohort), weight="overlap")
# cohort$overlap_weights <- overlap$ps.weights$overlap
#
# # Remove constant variables otherwise datadist has issues
# cohort <- cohort %>% select(patid, studydrug, qdiabeteshf_5yr_score, dstartdate_age, ethnicity_qrisk2_decoded, malesex, dstartdate_dm_dur_all, imd2015_10, drugline_all, ncurrtx_cat, INS, initiation_year, prebmi, prehba1c2yrs, presbp, qrisk2_smoking_cat, hypertension, hosp_admission_prev_year_count, hf_censtime_yrs, hf_censvar, overlap_weights, predicted_benefit_decile, predrug_af)
#
# ddist <- datadist(cohort)
# options(datadist='ddist')
#
# f_adjusted <- as.formula(paste0("Surv(hf_censtime_yrs, hf_censvar) ~ studydrug*rcs(qdiabeteshf_5yr_score,5) + ", return_cov_set("adjust")))
#
# model <- cph(f_adjusted, data=cohort, weights=overlap_weights, x=T, y=T, surv=TRUE)
#
# cohort.t <- data.frame(cohort) %>% mutate(studydrug="DPP4SU")
# DPP4SU.pred = survest(model,newdata=cohort.t,times=5,se.fit=F)
# cohort.t <- data.frame(cohort) %>% mutate(studydrug="SGLT2")
# SGLT2.pred = survest(model,newdata=cohort.t,times=5,se.fit=F)
#
# cohort$DPP4SU.pred <- DPP4SU.pred$surv
# cohort$SGLT2.pred <- SGLT2.pred$surv
#
# obs <- cohort %>%
# mutate(obs=(SGLT2.pred-DPP4SU.pred)*100) %>%
# group_by(predicted_benefit_decile) %>%
# dplyr::summarise(
# obs.gp = median(obs,na.rm=T),
# l_iqr=quantile(obs,0.25,na.rm=T),
# u_iqr=quantile(obs,0.75,na.rm=T)
# )
#
#
# predicted.p <- predicted %>%
# mutate(pred=pred.median*100) %>%
# select(predicted_benefit_decile,pred)
# plot.data <- merge(obs,predicted.p,by="predicted_benefit_decile")
#
#
# setwd("/slade/CPRD_data/Katie SGLT2/Plots/")
# tiff("calibration_benefits_qdhf_interaction.tiff", width=7, height=6, units = "in", res=800)
#
# ggplot(data=plot.data,aes(x=pred,y=obs.gp)) +
# geom_point(alpha=1) + theme_bw() +
# geom_errorbar(aes(ymin=l_iqr, ymax=u_iqr), colour="black", width=.1) +
# ylab("Observed 5-year SGLT2i benefit (%)") + xlab("Predicted 5-year SGLT2i benefit (%)") +
# scale_x_continuous(limits=c(0,4.5),breaks=c(seq(0,4.5,by=1))) +
# scale_y_continuous(limits=c(0,5.5),breaks=c(seq(0,5.5,by=1))) +
# theme_base() +
# theme(plot.background = element_blank()) +
# geom_abline(intercept=0,slope=1, color="red", lwd=0.75) + ggtitle("") +
# geom_vline(xintercept=0, linetype="dashed", color = "grey60") + geom_hline(yintercept=0, linetype="dashed", color = "grey60")
#
# dev.off()