generated from opensafely/research-template
/
105_analysis_plots.do
329 lines (276 loc) · 14.3 KB
/
105_analysis_plots.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
/*==============================================================================
DO FILE NAME: 105_analysis.do.do
DATE: 07/07/2023
AUTHOR: Ruth Costello (adapted from Christopher Rentsch)
DESCRIPTION OF FILE: Produce cumulative incidence plots
==============================================================================*/
adopath + ./analysis/ado
cap mkdir ./output/graphs
cap mkdir ./output/tables
* Open a log file
cap log using ./logs/analysis.log, replace
* Open file to write results to
file open tablecontent using ./output/tables/cum_incidence.txt, write text replace
file write tablecontent ("Outcome") _tab ("Exposure") _tab ("Cumulative incidence") _tab ("95% confidence interval") _n
* plot for each outcome - age and sex adjusted only
foreach outcome in hosp_any composite_any {
use ./output/an_dataset_`outcome', clear
drop if stp==""
describe
gen index_date = date("01/03/2020", "DMY")
bys patient_id (start): gen last = _N==_n
tab died_liver_any `outcome'_flag if last==1
* Update stop date of last record to end of study period if died of liver disease
replace stop = date("31/12/2022", "DMY") if last==1 & died_liver_any==1
stset stop, fail(`outcome'_flag) id(patient_id) enter(index_date) origin(index_date)
* Add cumulative incidence plots
* Not stratifying by stp as cannot use tvc and stratify
* Age and sex adjusted model
* Setting df (degrees of freedom for restricted cubic splines) as 3 as this is default
* Setting dftvc (degrees of freedom for time-dependent effects) as 1 = linear effect of log time
stpm2 udca age_tv male, tvc(udca) dftvc(1) df(2) scale(hazard) eform
summ _t
local tmax=r(max)
local tmaxplus1=r(max)+1
range days 0 `tmax' `tmaxplus1'
stpm2_standsurv if udca == 1, at1(udca 0) at2(udca 1) timevar(days) ci contrast(difference) fail
gen date = d(1/3/2020)+ days
format date %tddd_Month
for var _at1 _at2 _at1_lci _at1_uci _at2_lci _at2_uci _contrast2_1 _contrast2_1_lci _contrast2_1_uci: replace X=100*X
*cumulative mortality at last day of follow-up - write to file
file write tablecontent ("`outcome'") _tab ("No UDCA") _tab
* cumulative outcome - no UDCA
sum _at1 if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at1_lci if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at1_uci if days==`tmax'
file write tablecontent (r(mean)) _tab _n
* cumulative outcome - UDCA
file write tablecontent ("`outcome'") _tab ("UDCA") _tab
sum _at2 if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at2_lci if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at2_uci if days==`tmax'
file write tablecontent (r(mean)) _tab _n
*l date days _at1 _at1_lci _at1_uci _at2 _at2_lci _at2_uci if days<.
twoway (rarea _at1_lci _at1_uci days, color(red%25)) ///
(rarea _at2_lci _at2_uci days, color(blue%25)) ///
(line _at1 days, sort lcolor(red)) ///
(line _at2 days, sort lcolor(blue) lpattern(dash)) ///
, legend(order(1 "No UDCA" 2 "UDCA") ring(0) cols(1) pos(11) region(lwidth(none))) ///
title("Time to `outcome'", justification(left) size(med) ) ///
yscale(range(0, 1)) ///
ylabel(0 (1) 10, angle(0) format(%4.1f) labsize(small)) ///
xlabel(0 (200) 1035, labsize(small)) ///
ytitle("Cumulative outcomes (%)", size(medsmall)) ///
xtitle("Days since 1 Mar 2020", size(medsmall)) ///
graphregion(fcolor(white)) saving(adjcurv_`outcome', replace)
graph export "./output/graphs/adjcurv_`outcome'.svg", as(svg) replace
* Close window
graph close
}
use ./output/an_dataset_died_covid_any, clear
drop if stp==""
describe
gen index_date = date("01/03/2020", "DMY")
bys patient_id (start): gen last = _N==_n
tab died_liver_any died_covid_any_flag if last==1
* Update stop date of last record to end of study period if died of liver disease
replace stop = date("31/12/2022", "DMY") if last==1 & died_liver_any==1
stset stop, fail(died_covid_any_flag) id(patient_id) enter(index_date) origin(index_date)
* Add cumulative incidence plots
* Not stratifying by stp as cannot use tvc and stratify
* Age and sex adjusted model
* Setting df (degrees of freedom for restricted cubic splines) as 3 as this is default
* Setting dftvc (degrees of freedom for time-dependent effects) as 1 = linear effect of log time
stpm2 udca age_tv male, tvc(udca age_tv) dftvc(1) df(2) scale(hazard) eform
summ _t
local tmax=r(max)
local tmaxplus1=r(max)+1
range days 0 `tmax' `tmaxplus1'
stpm2_standsurv if udca == 1, at1(udca 0) at2(udca 1) timevar(days) ci contrast(difference) fail
gen date = d(1/3/2020)+ days
format date %tddd_Month
for var _at1 _at2 _at1_lci _at1_uci _at2_lci _at2_uci _contrast2_1 _contrast2_1_lci _contrast2_1_uci: replace X=100*X
*cumulative mortality at last day of follow-up - write to file
file write tablecontent ("Death") _tab ("No UDCA") _tab
* cumulative outcome - no UDCA
sum _at1 if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at1_lci if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at1_uci if days==`tmax'
file write tablecontent (r(mean)) _tab _n
* cumulative outcome - UDCA
file write tablecontent ("Death") _tab ("UDCA") _tab
sum _at2 if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at2_lci if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at2_uci if days==`tmax'
file write tablecontent (r(mean)) _tab _n
*l date days _at1 _at1_lci _at1_uci _at2 _at2_lci _at2_uci if days<.
twoway (rarea _at1_lci _at1_uci days, color(red%25)) ///
(rarea _at2_lci _at2_uci days, color(blue%25)) ///
(line _at1 days, sort lcolor(red)) ///
(line _at2 days, sort lcolor(blue) lpattern(dash)) ///
, legend(order(1 "No UDCA" 2 "UDCA") ring(0) cols(1) pos(11) region(lwidth(none))) ///
title("Time to COVID-19 death", justification(left) size(med) ) ///
yscale(range(0, 1)) ///
ylabel(0 (0.2) 2, angle(0) format(%4.1f) labsize(small)) ///
xlabel(0 (200) 1035, labsize(small)) ///
ytitle("Cumulative mortality (%)", size(medsmall)) ///
xtitle("Days since 1 Mar 2020", size(medsmall)) ///
graphregion(fcolor(white)) saving(adjcurv_`outcome', replace)
graph export "./output/graphs/adjcurv_died_covid_any.svg", as(svg) replace
* Close window
graph close
* plot for each outcome - fully adjusted
foreach outcome in hosp_any composite_any {
use ./output/an_dataset_`outcome', clear
drop if stp==""
describe
gen index_date = date("01/03/2020", "DMY")
bys patient_id (start): gen last = _N==_n
tab died_liver_any `outcome'_flag if last==1
* Update stop date of last record to end of study period if died of liver disease
replace stop = date("31/12/2022", "DMY") if last==1 & died_liver_any==1
stset stop, fail(`outcome'_flag) id(patient_id) enter(index_date) origin(index_date)
* Add cumulative incidence plots
* Not stratifying by stp as cannot use tvc and stratify
* Fully adjusted model (though not including wave as not right in main model)
* Setting df (degrees of freedom for restricted cubic splines) as 3 as this is default
* Setting dftvc (degrees of freedom for time-dependent effects) as 1 = linear effect of log time
* stpm2_standsurv cannot use factor variables so creating dummy variables:
tab imd, gen(imd)
tab bmi_cat, gen(bmi_cat)
tab smoking, gen(smoking)
* Check all binary
foreach vars in udca male any_high_risk_condition eth_bin imd1 imd2 imd3 imd4 imd5 bmi_cat1 bmi_cat2 bmi_cat3 ///
bmi_cat4 bmi_cat5 smoking1 smoking2 smoking3 severe_disease covid_vacc_first liver_trans {
tab `vars', m nolabel
}
* fully adjusted model to fit
stpm2 udca male age_tv any_high_risk_condition eth_bin imd2 imd3 imd4 imd5 bmi_cat1 bmi_cat3 ///
bmi_cat4 bmi_cat5 smoking2 smoking3 severe_disease covid_vacc_first liver_trans, ///
tvc(udca severe_disease covid_vacc_first liver_trans age_tv) dftvc(1) df(2) scale(hazard) eform
summ _t
local tmax=r(max)
local tmaxplus1=r(max)+1
range days 0 `tmax' `tmaxplus1'
stpm2_standsurv if udca == 1, at1(udca 0) at2(udca 1) timevar(days) ci contrast(difference) fail
gen date = d(1/3/2020)+ days
format date %tddd_Month
for var _at1 _at2 _at1_lci _at1_uci _at2_lci _at2_uci _contrast2_1 _contrast2_1_lci _contrast2_1_uci: replace X=100*X
*cumulative mortality at last day of follow-up
list _at1* if days==`tmax', noobs
list _at2* if days==`tmax', noobs
list _contrast* if days==`tmax', noobs
*cumulative mortality at last day of follow-up - write to file
file write tablecontent ("`outcome' fully adjusted") _tab ("No UDCA") _tab
* cumulative outcome - no UDCA
sum _at1 if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at1_lci if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at1_uci if days==`tmax'
file write tablecontent (r(mean)) _tab _n
* cumulative outcome - UDCA
file write tablecontent ("`outcome'") _tab ("UDCA") _tab
sum _at2 if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at2_lci if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at2_uci if days==`tmax'
file write tablecontent (r(mean)) _tab _n
*l date days _at1 _at1_lci _at1_uci _at2 _at2_lci _at2_uci if days<.
twoway (rarea _at1_lci _at1_uci days, color(red%25)) ///
(rarea _at2_lci _at2_uci days, color(blue%25)) ///
(line _at1 days, sort lcolor(red)) ///
(line _at2 days, sort lcolor(blue) lpattern(dash)) ///
, legend(order(1 "No UDCA" 2 "UDCA") ring(0) cols(1) pos(11) region(lwidth(none))) ///
title("Time to `outcome'", justification(left) size(med) ) ///
yscale(range(0, 1)) ///
ylabel(0 (1) 10, angle(0) format(%4.1f) labsize(small)) ///
xlabel(0 (200) 1035, labsize(small)) ///
ytitle("Cumulative outcomes (%)", size(medsmall)) ///
xtitle("Days since 1 Mar 2020", size(medsmall)) ///
graphregion(fcolor(white)) saving(adjcurv_f_`outcome', replace)
graph export "./output/graphs/adjcurv_f_`outcome'.svg", as(svg) replace
* Close window
graph close
}
use ./output/an_dataset_died_covid_any, clear
drop if stp==""
describe
gen index_date = date("01/03/2020", "DMY")
bys patient_id (start): gen last = _N==_n
tab died_liver_any died_covid_any_flag if last==1
* Update stop date of last record to end of study period if died of liver disease
replace stop = date("31/12/2022", "DMY") if last==1 & died_liver_any==1
stset stop, fail(died_covid_any_flag) id(patient_id) enter(index_date) origin(index_date)
* Add cumulative incidence plots
* Not stratifying by stp as cannot use tvc and stratify
* Fully adjusted model - currently not running
* Setting df (degrees of freedom for restricted cubic splines) as 3 as this is default
* Setting dftvc (degrees of freedom for time-dependent effects) as 1 = linear effect of log time
* stpm2_standsurv cannot use factor variables so creating dummy variables:
tab imd, gen(imd)
tab bmi_cat, gen(bmi_cat)
tab smoking, gen(smoking)
* Check all binary
foreach vars in udca male any_high_risk_condition eth_bin imd1 imd2 imd3 imd4 imd5 bmi_cat1 bmi_cat2 bmi_cat3 ///
bmi_cat4 bmi_cat5 smoking1 smoking2 smoking3 severe_disease covid_vacc_first liver_trans {
tab `vars', m nolabel
}
stpm2 udca male age_tv any_high_risk_condition eth_bin imd2 imd3 imd4 imd5 bmi_cat1 bmi_cat3 ///
bmi_cat4 bmi_cat5 smoking2 smoking3 severe_disease covid_vacc_first liver_trans, ///
tvc(udca severe_disease covid_vacc_first liver_trans age_tv) dftvc(1) df(2) scale(hazard) eform
summ _t
local tmax=r(max)
local tmaxplus1=r(max)+1
range days 0 `tmax' `tmaxplus1'
stpm2_standsurv if udca == 1, at1(udca 0) at2(udca 1) timevar(days) ci contrast(difference) fail
gen date = d(1/3/2020)+ days
format date %tddd_Month
for var _at1 _at2 _at1_lci _at1_uci _at2_lci _at2_uci _contrast2_1 _contrast2_1_lci _contrast2_1_uci: replace X=100*X
*cumulative mortality at last day of follow-up
list _at1* if days==`tmax', noobs
list _at2* if days==`tmax', noobs
list _contrast* if days==`tmax', noobs
*cumulative mortality at last day of follow-up - write to file
file write tablecontent ("Death fully adjusted") _tab ("No UDCA") _tab
* cumulative outcome - no UDCA
sum _at1 if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at1_lci if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at1_uci if days==`tmax'
file write tablecontent (r(mean)) _tab _n
* cumulative outcome - UDCA
file write tablecontent ("Death") _tab ("UDCA") _tab
sum _at2 if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at2_lci if days==`tmax'
file write tablecontent (r(mean)) _tab
sum _at2_uci if days==`tmax'
file write tablecontent (r(mean)) _tab _n
*l date days _at1 _at1_lci _at1_uci _at2 _at2_lci _at2_uci if days<.
twoway (rarea _at1_lci _at1_uci days, color(red%25)) ///
(rarea _at2_lci _at2_uci days, color(blue%25)) ///
(line _at1 days, sort lcolor(red)) ///
(line _at2 days, sort lcolor(blue) lpattern(dash)) ///
, legend(order(1 "No UDCA" 2 "UDCA") ring(0) cols(1) pos(11) region(lwidth(none))) ///
title("Time to `outcome'", justification(left) size(med) ) ///
yscale(range(0, 1)) ///
ylabel(0 (0.2) 2, angle(0) format(%4.1f) labsize(small)) ///
xlabel(0 (200) 1035, labsize(small)) ///
ytitle("Cumulative mortality (%)", size(medsmall)) ///
xtitle("Days since 1 Mar 2020", size(medsmall)) ///
graphregion(fcolor(white)) saving(adjcurv_`outcome', replace)
graph export "./output/graphs/adjcurv_f_died_covid_any.svg", as(svg) replace
* Close window
graph close
file close tablecontent