generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
302_competing_events.do
257 lines (184 loc) · 6.16 KB
/
302_competing_events.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
********************************************************************************
*
* Do-file: 302_competing_events.do
*
* Programmed by: John
*
* Data used: None
*
* Data created: None
*
* Other output: None
*
********************************************************************************
*
* Purpose:
*
* Note:
********************************************************************************
clear
do `c(pwd)'/analysis/global.do
cap log close
log using $outdir/competing_events.txt, replace t
tempname measures
postfile `measures' ///
str20(comparator) str20(outcome) str25(analysis) str10(adjustment) hr lc uc ///
using $tabfigdir/fine_gray_summary, replace
foreach an in pneumonia gen_population {
*foreach an in pneumonia {
use $outdir/combined_covid_`an'.dta, replace
if "`an'" == "pneumonia" {
label define caseLab 1 "COVID-19" 0 "Pnuemonia"
label values case caseLab
}
if "`an'" == "gen_population" {
label define caseLab 1 "COVID-19" 0 "General Population"
label values case caseLab
}
drop patient_id
gen new_patient_id = _n
global crude i.case
global age_sex i.case i.male age1 age2 age3
global full i.case i.male age1 age2 age3 i.stp i.ethnicity i.imd i.obese4cat_withmiss ///
i.smoke htdiag chronic_respiratory_disease i.asthmacat chronic_cardiac_disease ///
i.diabcat i.cancer_exhaem_cat i.cancer_haem_cat i.reduced_kidney_function_cat2 ///
i.chronic_liver_disease i.dementia i.other_neuro i.organ_transplant i.spleen ///
i.ra_sle_psoriasis i.other_immunosuppression i.hist_dvt i.hist_pe i.hist_stroke i.hist_mi i.hist_aki i.hist_heart_failure
foreach v in stroke dvt pe heart_failure mi aki t2dm {
if "`v'" == "stroke" {
local lab = "Stroke"
}
if "`v'" == "dvt" {
local lab = "DVT"
}
if "`v'" == "pe" {
local lab = "PE"
}
if "`v'" == "heart_failure" {
local lab = "Heart Failure"
}
if "`v'" == "mi" {
local lab = "MI"
}
if "`v'" == "aki" {
local lab = "AKI"
}
if "`v'" == "t2dm" {
local lab = "T2DM"
}
noi di "Starting analysis for `v' Outcome ..."
forvalues i = 1/3 {
preserve
local skip_1 = 0
local skip_2 = 0
local skip_3 = 0
* Apply exclusion for AKI and diabetes outcomes
if "`v'" == "t1dm" | "`v'" == "t2dm" {
drop if previous_diabetes == 1
local skip_2 = 1
local skip_3 = 1
}
if "`v'" == "aki" {
drop if aki_exclusion_flag == 1
}
if `i' == 1 {
local out `v'
local end_date `v'_end_date
}
if `i' == 2 {
local out `v'_no_gp
local end_date `v'_no_gp_end_date
}
if `i' == 3 {
local out `v'_cens_gp
local end_date `v'_cens_gp_end_date
}
if `skip_`i'' == 0 {
noi di "$group: stset in `a'"
/* Replace outcome = 2 if censoring is non-outcome death
gen act_end_date = `end_date' - 1
replace `out' = 2 if (`out' == 0) & (died_date_ons_date == act_end_date)
stset `end_date', id(new_patient_id) enter(indexdate) origin(indexdate) failure(`out'==1) */
gen act_end_date = `end_date' - 1
gen `end_date'2= `end_date'
replace `end_date'2 = td(01/02/2021) if case == 1 & act_end_date == died_date_ons_date & died_date_ons_date!= `v'_ons
replace `end_date'2 = td(01/02/2020) if case == 0 & act_end_date == died_date_ons_date & died_date_ons_date!= `v'_ons
format %td `end_date'2
gen `out'2 = `out'
replace `out'2 = 2 if (`out' == 0) & (died_date_ons_date == act_end_date) & (died_date_ons_date!= `v'_ons)
stset `end_date'2, id(new_patient_id) enter(indexdate) origin(indexdate) failure(`out')
foreach adjust in crude age_sex full {
if "`adjust'" == "full" & "`v'" == "t2dm" {
* remove diabetes
global full i.case i.male age1 age2 age3 i.stp i.ethnicity i.imd i.obese4cat_withmiss ///
i.smoke htdiag chronic_respiratory_disease i.asthmacat chronic_cardiac_disease ///
i.cancer_exhaem_cat i.cancer_haem_cat i.reduced_kidney_function_cat2 ///
i.chronic_liver_disease i.dementia i.other_neuro i.organ_transplant i.spleen ///
i.ra_sle_psoriasis i.other_immunosuppression i.hist_dvt i.hist_pe i.hist_stroke i.hist_mi i.hist_aki i.hist_heart_failure
}
*stcrreg $`adjust', compete(`out'==2) vce(robust)
stcox $`adjust', vce(robust)
/* Matrix for stcrreg...
matrix b = r(table)
local hr= b[1,1]
local lc = b[5,1]
local uc = b[6,1]
*/
* Matrix for stcox
matrix b = r(table)
local hr= b[1,2]
local lc = b[5,2]
local uc = b[6,2]
estat phtest, detail
post `measures' ("`an'") ("`v'") ("`out'") ("`adjust'") (`hr') (`lc') (`uc')
}
if `i' == 3 {
stset `end_date', id(new_patient_id) enter(indexdate) origin(indexdate) failure(`out'2==1)
* Adjusted cuminc
stcompet cuminc = ci, by(case) compet1(2)
gen cumInc = cuminc if `out'2==1 // cumaltive inc of outcome accounting death as competing risk
separate cumInc, by(case) veryshortlabel
drop cuminc
* Plot cumulative incidence functions for exp groups (accounting for death as a competing risk)
#delimit ;
twoway (line cumInc0 _t, sort c(J))
(line cumInc1 _t, sort c(J)) ,
ylabel(,angle(horizontal))
plotregion(color(white))
graphregion(color(white))
ytitle("Cumulative Incidence")
xtitle("Time (Days)")
;
#delimit cr
graph export "$tabfigdir/cumInc_`out'_`an'.svg", as(svg) replace
}
if "`adjust'" == "full" & "`v'" == "t2dm" {
stset `end_date', id(new_patient_id) enter(indexdate) origin(indexdate) failure(`out'2==1)
* Adjusted cuminc
stcompet cuminc = ci, by(case) compet1(2)
gen cumInc = cuminc if `out'2==1 // cumaltive inc of outcome accounting death as competing risk
separate cumInc, by(case) veryshortlabel
drop cuminc
* Plot cumulative incidence functions for exp groups (accounting for death as a competing risk)
#delimit ;
twoway (line cumInc0 _t, sort c(J))
(line cumInc1 _t, sort c(J)) ,
ylabel(,angle(horizontal))
plotregion(color(white))
graphregion(color(white))
ytitle("Cumulative Incidence")
xtitle("Time (Days)")
;
#delimit cr
graph export "$tabfigdir/cumInc_`out'_`an'.svg", as(svg) replace
}
}
restore
}
}
}
postclose `measures'
* Change postfiles to csv
use $tabfigdir/fine_gray_summary, replace
export delimited using $tabfigdir/fine_gray_summary.csv, replace
log close