generated from opensafely/research-template
/
305_ps_model.do
467 lines (384 loc) · 19.6 KB
/
305_ps_model.do
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
********************************************************************************
*
* Do-file: propensity score cox model
* Project: Sotrovimab-Paxlovid-Molnupiravir
* Date: 24/4/23
* Programmed by: Katie Bechman
* Description: create and run ps model
* Data used: main.dta
* Data created: psoutput
* Other output: logfiles, printed to folder $Logdir
* User installed ado: (place .ado file(s) in analysis folder)
****************************************************************************************************************
**Set filepaths
// global projectdir "C:\Users\k1635179\OneDrive - King's College London\Katie\OpenSafely\Safety mAB and antivirals\Safety-Sotrovimab-Paxlovid-Molnupiravir"
global projectdir `c(pwd)'
di "$projectdir"
capture mkdir "$projectdir/output/data"
capture mkdir "$projectdir/output/figures"
capture mkdir "$projectdir/output/tables"
global logdir "$projectdir/logs"
di "$logdir"
* Open a log file
cap log close
log using "$logdir/ps_model.log", replace
*Set Ado file path
adopath + "$projectdir/analysis/ado"
* SET Index date
global indexdate = "01/03/2020"
use "$projectdir/output/data/sensitivity_analysis.dta", clear
* Models
global agesex age i.sex
global adj age i.sex i.region_nhs i.imdq5 i.ethnicity 1b.bmi_group ///
paxlovid_contraindicated i.vaccination_status diabetes chronic_cardiac_disease chronic_respiratory_disease hypertension ///
downs_syndrome solid_cancer haem_disease renal_disease liver_disease imid_on_drug immunosuppression hiv_aids organ_transplant rare_neuro
global adj2 age i.sex i.region_nhs i.imdq5 i.ethnicity 1b.bmi_group
global adj3 age i.sex i.region_nhs i.imdq5 i.ethnicity 1b.bmi_group ///
paxlovid_contraindicated i.vaccination_status diabetes chronic_cardiac_disease chronic_respiratory_disease hypertension
global adj4 age i.sex i.region_nhs i.imdq5 White 1b.bmi_group ///
paxlovid_contraindicated i.vaccination_status diabetes chronic_cardiac_disease chronic_respiratory_disease hypertension ///
dementia care_home serious_mental_illness
* Balance
global bal_agesex age sex
global bal_adj age sex region_nhs imdq5 ethnicity bmi_group ///
paxlovid_contraindicated vaccination_status diabetes chronic_cardiac_disease chronic_respiratory_disease hypertension ///
downs_syndrome solid_cancer haem_disease renal_disease liver_disease imid_on_drug immunosuppression hiv_aids organ_transplant rare_neuro
global bal_adj2 age sex region_nhs imdq5 ethnicity bmi_group
global bal_adj3 age sex region_nhs imdq5 ethnicity bmi_group ///
paxlovid_contraindicated vaccination_status diabetes chronic_cardiac_disease chronic_respiratory_disease hypertension
global bal_adj4 age sex region_nhs imdq5 ethnicity bmi_group ///
paxlovid_contraindicated vaccination_status diabetes chronic_cardiac_disease chronic_respiratory_disease hypertension ///
dementia care_home serious_mental_illness
* Outcome
global ae_disease ae_diverticulitis ///
ae_diarrhoea ///
ae_taste ///
ae_rash ///
ae_bronchospasm ///
ae_contactderm ///
ae_dizziness ///
ae_nausea_vomit ///
ae_headache ///
ae_anaphylaxis ///
ae_drugreaction ///
ae_ra ///
ae_sle ///
ae_psorasis ///
ae_psa ///
ae_axspa ///
ae_ibd
global ae_disease_serious ae_diverticulitis_serious ///
ae_diarrhoea_serious ///
ae_taste_serious ///
ae_rash_serious ///
ae_contactderm_serious ///
ae_dizziness_serious ///
ae_nausea_vomit_serious ///
ae_headache_serious ///
ae_anaphylaxis_serious ///
ae_drugreaction_serious ///
ae_ra_serious ///
ae_sle_serious ///
ae_psorasis_serious ///
ae_psa_serious ///
ae_axspa_serious ///
ae_ibd_serious
global ae_combined ae_all ///
ae_all_serious ///
ae_spc_all ///
ae_spc_serious ///
ae_drug_all ///
ae_drug_serious ///
ae_imae_all ///
ae_imae_serious
** Hazard of summary - Main analysis *
* Treatment group start date = treat date *
* Control group start date = delayed by median time between test and treat in treat group *
use "$projectdir/output/data/sensitivity_analysis.dta", clear
tab pre_drug_test drug
drop if covid_test_5d!=1 & drug >0
tab pre_drug_test drug
tab drug paxlovid_contraindicated
** Start day for treatment group is date treated & start date for control group is date test + 2 days
count if start_date!=covid_test_positive_date & drug==0
count if start_date!=date_treated & drug>0
tab median_delay_all
gen start_date_delay = start_date + median_delay_all if drug==0
replace start_date_delay = start_date if drug>0
****************************************************************************************************************
*1. Individual drug V No Drug
// SOTROVIMAB //
tempname coxoutput_propensity_sot
postfile `coxoutput_propensity_sot' str20(model) str20(fail) ///
hr_sot lc_sot uc_sot ///
using "$projectdir/output/tables/cox_propensity_sot", replace
preserve
keep if drug==0 | drug==1
foreach model in agesex adj adj2 adj3 adj4 {
logistic drug $`model'
predict p_drug_`model'
twoway (histogram p_drug_`model' if drug==1, color(green)) (histogram p_drug_`model' if drug==0, fcolor(none) lcolor(black)), ///
legend(order(1 "No Drug" 2 "Sotrovimab")) name(histogram_sot`model', replace) saving("$projectdir/output/figures/histogram_sot_`model'", replace)
gen iptw_`model' = 1/p_drug_`model' if drug==1
replace iptw_`model' = 1/(1-p_drug_`model') if drug==0
foreach fail in $ae_combined {
stset stop_`fail' [pw=iptw_`model'], id(patient_id) origin(time start_date_delay) enter(time start_date_delay) failure(fail_`fail'==1)
stcox i.drug, vce(robust)
matrix b = r(table)
local hr_sot = b[1,2]
local lc_sot = b[5,2]
local uc_sot = b[6,2]
post `coxoutput_propensity_sot' ("`fail'") ("`model'") (`hr_sot') (`lc_sot') (`uc_sot')
}
}
pbalchk drug $bal_agesex
pbalchk drug $bal_agesex, wt(iptw_agesex) graph
graph save "$projectdir/output/figures/sot_match_agesex", replace
pbalchk drug $bal_adj
pbalchk drug $bal_adj, wt(iptw_adj) graph
graph save "$projectdir/output/figures/sot_match_adj", replace
pbalchk drug $bal_adj2
pbalchk drug $bal_adj2, wt(iptw_adj2) graph
graph save "$projectdir/output/figures/sot_match_adj2", replace
pbalchk drug $bal_adj3
pbalchk drug $bal_adj3, wt(iptw_adj3) graph
graph save "$projectdir/output/figures/sot_match_adj3", replace
pbalchk drug $bal_adj4
pbalchk drug $bal_adj4, wt(iptw_adj4) graph
graph save "$projectdir/output/figures/sot_match_adj4", replace
restore
postclose `coxoutput_propensity_sot'
// PAXLOVID //
tempname coxoutput_propensity_pax
postfile `coxoutput_propensity_pax' str20(model) str20(fail) ///
hr_pax lc_pax uc_pax ///
using "$projectdir/output/tables/cox_propensity_pax", replace
preserve
keep if drug==0 | drug==2
replace drug=1 if drug==2
lab define drug 0 "Control" 1 "Paxlovid", replace
label values drug drug
foreach model in agesex adj adj2 adj3 adj4 {
logistic drug $`model'
predict p_drug_`model'
twoway (histogram p_drug_`model' if drug==1, color(green)) (histogram p_drug_`model' if drug==0, fcolor(none) lcolor(black)), ///
legend(order(1 "No Drug" 2 "Paxlovid")) name(histogram_pax_`model', replace) saving("$projectdir/output/figures/histogram_pax_`model'", replace)
graph export "$projectdir/output/figures/histogram_pax_`model'.svg", as(svg) replace
gen iptw_`model' = 1/p_drug_`model' if drug==1
replace iptw_`model' = 1/(1-p_drug_`model') if drug==0
foreach fail in $ae_combined {
stset stop_`fail' [pw=iptw_`model'], id(patient_id) origin(time start_date_delay) enter(time start_date_delay) failure(fail_`fail'==1)
stcox i.drug, vce(robust)
matrix b = r(table)
local hr_pax = b[1,2]
local lc_pax = b[5,2]
local uc_pax = b[6,2]
post `coxoutput_propensity_pax' ("`fail'") ("`model'") (`hr_pax') (`lc_pax') (`uc_pax')
}
}
pbalchk drug $bal_agesex
pbalchk drug $bal_agesex, wt(iptw_agesex) graph
graph save "$projectdir/output/figures/pax_match_agesex", replace
pbalchk drug $bal_adj
pbalchk drug $bal_adj, wt(iptw_adj) graph
graph save "$projectdir/output/figures/pax_match_adj", replace
pbalchk drug $bal_adj2
pbalchk drug $bal_adj2, wt(iptw_adj2) graph
graph save "$projectdir/output/figures/pax_match_adj2", replace
pbalchk drug $bal_adj3
pbalchk drug $bal_adj3, wt(iptw_adj3) graph
graph save "$projectdir/output/figures/pax_match_adj3", replace
pbalchk drug $bal_adj4
pbalchk drug $bal_adj4, wt(iptw_adj4) graph
graph save "$projectdir/output/figures/pax_match_adj4", replace
restore
postclose `coxoutput_propensity_pax'
// MOLNUPAVIR //
tempname coxoutput_propensity_mol
postfile `coxoutput_propensity_mol' str20(model) str20(fail) ///
hr_mol lc_mol uc_mol ///
using "$projectdir/output/tables/cox_propensity_mol", replace
preserve
keep if drug==0 | drug==3
replace drug=1 if drug==3
lab define drug 0 "Control" 1 "Molnupavir", replace
label values drug drug
foreach model in agesex adj adj2 adj3 adj4 {
logistic drug $`model'
predict p_drug_`model'
twoway (histogram p_drug_`model' if drug==1, color(green)) (histogram p_drug_`model' if drug==0, fcolor(none) lcolor(black)), ///
legend(order(1 "No Drug" 2 "Molnupavir")) name(histogram_mol_`model', replace) saving("$projectdir/output/figures/histogram_mol_`model'", replace)
graph export "$projectdir/output/figures/histogram_mol_`model'.svg", as(svg) replace
gen iptw_`model' = 1/p_drug_`model' if drug==1
replace iptw_`model' = 1/(1-p_drug_`model') if drug==0
foreach fail in $ae_combined {
stset stop_`fail' [pw=iptw_`model'], id(patient_id) origin(time start_date_delay) enter(time start_date_delay) failure(fail_`fail'==1)
stcox i.drug, vce(robust)
matrix b = r(table)
local hr_mol = b[1,2]
local lc_mol = b[5,2]
local uc_mol = b[6,2]
post `coxoutput_propensity_mol' ("`fail'") ("`model'") (`hr_mol') (`lc_mol') (`uc_mol')
}
}
pbalchk drug $bal_agesex
pbalchk drug $bal_agesex, wt(iptw_agesex) graph
graph save "$projectdir/output/figures/mol_match_agesex", replace
pbalchk drug $bal_adj
pbalchk drug $bal_adj, wt(iptw_adj) graph
graph save "$projectdir/output/figures/mol_match_adj", replace
pbalchk drug $bal_adj2
pbalchk drug $bal_adj2, wt(iptw_adj2) graph
graph save "$projectdir/output/figures/mol_match_adj2", replace
pbalchk drug $bal_adj3
pbalchk drug $bal_adj3, wt(iptw_adj3) graph
graph save "$projectdir/output/figures/mol_match_adj3", replace
pbalchk drug $bal_adj4
pbalchk drug $bal_adj4, wt(iptw_adj4) graph
graph save "$projectdir/output/figures/mol_match_adj4", replace
restore
postclose `coxoutput_propensity_mol'
** Histogram figures
graph combine "$projectdir/output/figures/histogram_sot_agesex" "$projectdir/output/figures/histogram_pax_agesex" "$projectdir/output/figures/histogram_mol_agesex", ///
title("histogram agesex adjusted model") saving("$projectdir/output/figures/histogram_agesex", replace)
graph export "$projectdir/output/figures/histogram_agesex.svg", as(svg) replace
graph combine "$projectdir/output/figures/histogram_sot_adj" "$projectdir/output/figures/histogram_pax_adj" "$projectdir/output/figures/histogram_mol_adj", ///
title("histogram adjusted model") saving("$projectdir/output/figures/histogram_adj", replace)
graph export "$projectdir/output/figures/histogram_adj.svg", as(svg) replace
graph combine "$projectdir/output/figures/histogram_sot_adj2" "$projectdir/output/figures/histogram_pax_adj2" "$projectdir/output/figures/histogram_mol_adj2", ///
title("histogram adjusted model 2") saving("$projectdir/output/figures/histogram_adj2", replace)
graph export "$projectdir/output/figures/histogram_adj2.svg", as(svg) replace
graph combine "$projectdir/output/figures/histogram_sot_adj3" "$projectdir/output/figures/histogram_pax_adj3" "$projectdir/output/figures/histogram_mol_adj3", ///
title("histogram adjusted model 3") saving("$projectdir/output/figures/histogram_adj3", replace)
graph export "$projectdir/output/figures/histogram_adj3.svg", as(svg) replace
graph combine "$projectdir/output/figures/histogram_sot_adj4" "$projectdir/output/figures/histogram_pax_adj4" "$projectdir/output/figures/histogram_mol_adj4", ///
title("histogram adjusted model 4") saving("$projectdir/output/figures/histogram_adj4", replace)
graph export "$projectdir/output/figures/histogram_adj4.svg", as(svg) replace
** Balancing figures
graph combine "$projectdir/output/figures/sot_match_agesex.gph" "$projectdir/output/figures/pax_match_agesex.gph" "$projectdir/output/figures/mol_match_agesex.gph" ///
, title("PS balancing agesex model") saving("$projectdir/output/figures/balance_agesex", replace)
graph export "$projectdir/output/figures/balance_agesex.svg", as(svg) replace
graph combine "$projectdir/output/figures/sot_match_adj.gph" "$projectdir/output/figures/pax_match_adj.gph" "$projectdir/output/figures/mol_match_adj.gph" ///
, title("PS balancing adjusted model") saving("$projectdir/output/figures/balance_adj", replace)
graph export "$projectdir/output/figures/balance_adj.svg", as(svg) replace
graph combine "$projectdir/output/figures/sot_match_adj2.gph" "$projectdir/output/figures/pax_match_adj2.gph" "$projectdir/output/figures/mol_match_adj2.gph" ///
, title("PS balancing adjusted model 2") saving("$projectdir/output/figures/balance_adj2", replace)
graph export "$projectdir/output/figures/balance_adj2.svg", as(svg) replace
graph combine "$projectdir/output/figures/sot_match_adj3.gph" "$projectdir/output/figures/pax_match_adj3.gph" "$projectdir/output/figures/mol_match_adj3.gph" ///
, title("PS balancing adjusted model 3") saving("$projectdir/output/figures/balance_adj3", replace)
graph export "$projectdir/output/figures/balance_adj3.svg", as(svg) replace
graph combine "$projectdir/output/figures/sot_match_adj4.gph" "$projectdir/output/figures/pax_match_adj4.gph" "$projectdir/output/figures/mol_match_adj4.gph" ///
, title("PS balancing adjusted model 4") saving("$projectdir/output/figures/balance_adj4", replace)
graph export "$projectdir/output/figures/balance_adj4.svg", as(svg) replace
*******************************************************************************
use "$projectdir/output/tables/cox_propensity_sot", clear
merge 1:1 model fail using "$projectdir/output/tables/cox_propensity_pax", nogenerate
merge 1:1 model fail using "$projectdir/output/tables/cox_propensity_mol", nogenerate
foreach var of varlist hr* lc* uc* {
format `var' %3.2f
}
save "$projectdir/output/tables/cox_propensity", replace
export delimited using "$projectdir/output/tables/cox_propensity.csv", replace
****************************************************************************************************************
** Hazard of disease Main analysis
* Treatment group start date = treat date *
* Control group start date = delayed by median time between test and treat in treat group *
use "$projectdir/output/data/sensitivity_analysis.dta", clear
tab pre_drug_test drug
drop if covid_test_5d!=1 & drug >0
tab pre_drug_test drug
tab drug paxlovid_contraindicated
** Start day for treatment group is date treated & start date for control group is date test + 2 days
count if start_date!=covid_test_positive_date & drug==0
count if start_date!=date_treated & drug>0
tab median_delay_all
gen start_date_delay = start_date + median_delay_all if drug==0
replace start_date_delay = start_date if drug>0
// SOTROVIMAB //
tempname coxoutput_propensity_sot_dis
postfile `coxoutput_propensity_sot_dis' str20(model) str20(fail) ///
hr_sot lc_sot uc_sot ///
using "$projectdir/output/tables/cox_propensity_sot_dis", replace
preserve
keep if drug==0 | drug==1
foreach model in agesex adj adj2 adj3 adj4 {
logistic drug $`model'
predict p_drug_`model'
gen iptw_`model' = 1/p_drug_`model' if drug==1
replace iptw_`model' = 1/(1-p_drug_`model') if drug==0
foreach fail in $ae_disease $ae_disease_serious {
stset stop_`fail' [pw=iptw_`model'], id(patient_id) origin(time start_date_delay) enter(time start_date_delay) failure(fail_`fail'==1)
stcox i.drug, vce(robust)
matrix b = r(table)
local hr_sot = b[1,2]
local lc_sot = b[5,2]
local uc_sot = b[6,2]
post `coxoutput_propensity_sot_dis' ("`fail'") ("`model'") (`hr_sot') (`lc_sot') (`uc_sot')
}
}
restore
postclose `coxoutput_propensity_sot_dis'
// PAXLOVID //
tempname coxoutput_propensity_pax_dis
postfile `coxoutput_propensity_pax_dis' str20(model) str20(fail) ///
hr_pax lc_pax uc_pax ///
using "$projectdir/output/tables/cox_propensity_pax_dis", replace
preserve
keep if drug==0 | drug==2
replace drug=1 if drug==2
lab define drug 0 "Control" 1 "Paxlovid", replace
label values drug drug
foreach model in agesex adj adj2 adj3 adj4 {
logistic drug $`model'
predict p_drug_`model'
gen iptw_`model' = 1/p_drug_`model' if drug==1
replace iptw_`model' = 1/(1-p_drug_`model') if drug==0
foreach fail in $ae_disease $ae_disease_serious {
stset stop_`fail' [pw=iptw_`model'], id(patient_id) origin(time start_date_delay) enter(time start_date_delay) failure(fail_`fail'==1)
stcox i.drug, vce(robust)
matrix b = r(table)
local hr_pax = b[1,2]
local lc_pax = b[5,2]
local uc_pax = b[6,2]
post `coxoutput_propensity_pax_dis' ("`fail'") ("`model'") (`hr_pax') (`lc_pax') (`uc_pax')
}
}
restore
postclose `coxoutput_propensity_pax_dis'
// MOLNUPAVIR //
tempname coxoutput_propensity_mol_dis
postfile `coxoutput_propensity_mol_dis' str20(model) str20(fail) ///
hr_mol lc_mol uc_mol ///
using "$projectdir/output/tables/cox_propensity_mol_dis", replace
preserve
keep if drug==0 | drug==3
replace drug=1 if drug==3
lab define drug 0 "Control" 1 "Molnupavir", replace
label values drug drug
foreach model in agesex adj adj2 adj3 adj4 {
logistic drug $`model'
predict p_drug_`model'
gen iptw_`model' = 1/p_drug_`model' if drug==1
replace iptw_`model' = 1/(1-p_drug_`model') if drug==0
foreach fail in $ae_disease $ae_disease_serious {
stset stop_`fail' [pw=iptw_`model'], id(patient_id) origin(time start_date_delay) enter(time start_date_delay) failure(fail_`fail'==1)
stcox i.drug, vce(robust)
matrix b = r(table)
local hr_mol = b[1,2]
local lc_mol = b[5,2]
local uc_mol = b[6,2]
post `coxoutput_propensity_mol_dis' ("`fail'") ("`model'") (`hr_mol') (`lc_mol') (`uc_mol')
}
}
restore
postclose `coxoutput_propensity_mol_dis'
*******************************************************************************
use "$projectdir/output/tables/cox_propensity_sot_dis", clear
merge 1:1 model fail using "$projectdir/output/tables/cox_propensity_pax_dis", nogenerate
merge 1:1 model fail using "$projectdir/output/tables/cox_propensity_mol_dis", nogenerate
foreach var of varlist hr* lc* uc* {
format `var' %3.2f
}
save "$projectdir/output/tables/cox_propensity_disease", replace
export delimited using "$projectdir/output/tables/cox_propensity_disease.csv", replace
log close