generated from opensafely/research-template
/
an_cox_models.do
246 lines (147 loc) · 7.34 KB
/
an_cox_models.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
/*================================================================================
Do-file: an_cox_models.do
Project: Non-specific-immunity
Programmed by: Daniel Grint
Data used: cr_analysis_dataset.dta
Data created:
Other output: an_cox_models.log
================================================================================
Purpose: This do file:
Runs Cox models of increasing complexity for the association
between exposure to an RTI and outcome of COVID diagnosis,
hospital admission, and death.
================================================================================*/
set linesize 100
* Open a log file
cap log close
log using ./logs/an_cox_models, replace t
clear
*use "C:\Users\EIDEDGRI\Documents\GitHub\non-specific-immunity-research\analysis\cr_analysis_dataset"
use ./analysis/cr_analysis_dataset.dta
/* COVID DIAGNOSIS OUTCOME DATE DEFINED IN cr_analysis_dataset.do
== CONSIDER MOVING OUTCOME DEFINITION TO THIS DO FILE?
== cox_covid_date is date of covid diagnosis
*/
/* EXPOSURE DEFINITION */
* Using first RTI
gen first_rti = min_rti
* Administrative censor
replace first_rti = date("01dec2099", "DMY") if first_rti == .
format %td first_rti
/* COVID DIAGNOSIS AS OUTCOME MODELS */
stset cox_covid_date, failure(covid_diag) entry(enter_date) exit(censor_date) origin(enter_date) id(patient_id) scale(1)
strate , per(100)
/* Split time on exposure status */
* 3 months after exposure (4 weeks each)
stsplit t_rti, after(first_rti) at(0,28,56,84)
recode t_rti (-1 = 1 "pre-RTI") (0 = 2 "Month 1") (28 = 3 "Month 2") (56 = 4 "Month 3") (84 = 5 "Month 4+"), gen(exp_time)
strate exp_time, per(100)
* 12 weeks after exposure
stsplit w_rti, after(first_rti) at(0,7,14,21,28,35,42,49,56,63,70,77,84)
recode w_rti (-1 = 1 "pre-RTI") (0 = 2 "Week 1") (7 = 3 "Week 2") (14 = 4 "Week 3") (21 = 5 "Week 4") (28 = 6 "Week 5") ///
(35 = 7 "Week 6") (42 = 8 "Week 7") (49 = 9 "Week 8") (56 = 10 "Week 9") (63 = 11 "Week 10") (70 = 12 "Week 11") ///
(77 = 13 "Week 12") (84 = 14 "Week 13+"), gen(exp_week)
/* UNADJUSTED COX MODEL */
/*
stcox ib1.exp_time, base
sts graph , haz by(exp_time) width(5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(label(1 "pre-RTI") label(2 "Month 1") label(3 "Month 2") label(4 "Month 3") label(5 "Month 4+")) ///
name(unadj_cox)
graph export ./output/unadj_cox_haz.svg, name(unadj_cox) as(svg) replace
*/
* With regional stratification
* Months
stcox ib1.exp_time, base strata(utla_group)
sts graph , haz by(exp_time) width(5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(label(1 "pre-RTI") label(2 "Month 1") label(3 "Month 2") label(4 "Month 3") label(5 "Month 4+")) ///
name(unadj_utla_cox)
graph export ./output/unadj_utla_cox_haz.svg, name(unadj_utla_cox) as(svg) replace
sts graph , haz by(utla_group) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(off) ///
name(utla_haz)
graph export ./output/utla_haz.svg, name(utla_haz) as(svg) replace
* Weeks
stcox ib1.exp_week, base strata(utla_group)
sts graph , haz by(exp_week) width(5 5 5 5 5 5 5 5 5 5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(label(1 "pre-RTI") label(2 "Week 1") label(3 "Week 2") label(4 "Week 3") label(5 "Week 4") ///
label(6 "Week 5") label(7 "Week 6") label(8 "Week 7") label(9 "Week 8") label(10 "Week 9") ///
label(11 "Week 10") label(12 "Week 11") label(13 "Week 12") label(14 "Week 13+") rows(3)) ///
name(unadj_utla_cox_week)
graph export ./output/unadj_utla_cox_week.svg, name(unadj_utla_cox_week) as(svg) replace
sts graph , haz by(exp_week) width(5 5 5 5 5 5 5 5 5 5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(off) ///
name(unadj_utla_cox_week1)
graph export ./output/unadj_utla_cox_week_nl.svg, name(unadj_utla_cox_week1) as(svg) replace
* With adjustment for region
encode utla_group, gen(utla_num)
* Months
stcox ib1.exp_time i.utla_num, base
sts graph , haz by(exp_time) width(5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(label(1 "pre-RTI") label(2 "Month 1") label(3 "Month 2") label(4 "Month 3") label(5 "Month 4+")) ///
name(adj_utla_cox)
graph export ./output/reg_utla_cox_haz.svg, name(adj_utla_cox) as(svg) replace
/* ========================================== */
/* == Using SGSS to define covid diagnosis == */
/* ========================================== */
use ./analysis/cr_analysis_dataset.dta, clear
/* COVID DIAGNOSIS OUTCOME DATE DEFINED IN cr_analysis_dataset.do
== CONSIDER MOVING OUTCOME DEFINITION TO THIS DO FILE?
== cox_sgss_date is date of covid diagnosis
*/
/* EXPOSURE DEFINITION */
* Using first RTI
gen first_rti = min_rti
* Administrative censor
replace first_rti = date("01dec2099", "DMY") if first_rti == .
format %td first_rti
/* COVID DIAGNOSIS AS OUTCOME MODELS */
stset cox_sgss_date, failure(sgss_diag) entry(enter_date) exit(censor_date) origin(enter_date) id(patient_id) scale(1)
strate , per(100)
/* Split time on exposure status */
* 3 months after exposure (4 weeks each)
stsplit t_rti, after(first_rti) at(0,28,56,84)
recode t_rti (-1 = 1 "pre-RTI") (0 = 2 "Month 1") (28 = 3 "Month 2") (56 = 4 "Month 3") (84 = 5 "Month 4+"), gen(exp_time)
strate exp_time, per(100)
* 12 weeks after exposure
stsplit w_rti, after(first_rti) at(0,7,14,21,28,35,42,49,56,63,70,77,84)
recode w_rti (-1 = 1 "pre-RTI") (0 = 2 "Week 1") (7 = 3 "Week 2") (14 = 4 "Week 3") (21 = 5 "Week 4") (28 = 6 "Week 5") ///
(35 = 7 "Week 6") (42 = 8 "Week 7") (49 = 9 "Week 8") (56 = 10 "Week 9") (63 = 11 "Week 10") (70 = 12 "Week 11") ///
(77 = 13 "Week 12") (84 = 14 "Week 13+"), gen(exp_week)
/* UNADJUSTED COX MODEL */
/*
stcox ib1.exp_time, base
sts graph , haz by(exp_time) width(5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(label(1 "pre-RTI") label(2 "Month 1") label(3 "Month 2") label(4 "Month 3") label(5 "Month 4+")) ///
name(unadj_cox)
graph export ./output/unadj_cox_haz.svg, name(unadj_cox) as(svg) replace
*/
* With regional stratification
* Months
stcox ib1.exp_time, base strata(utla_group)
sts graph , haz by(exp_time) width(5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(label(1 "pre-RTI") label(2 "Month 1") label(3 "Month 2") label(4 "Month 3") label(5 "Month 4+")) ///
name(unadj_utla_sgss)
graph export ./output/unadj_utla_sgss_haz.svg, name(unadj_utla_sgss) as(svg) replace
* Weeks
stcox ib1.exp_week, base strata(utla_group)
sts graph , haz by(exp_week) width(5 5 5 5 5 5 5 5 5 5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(label(1 "pre-RTI") label(2 "Week 1") label(3 "Week 2") label(4 "Week 3") label(5 "Week 4") ///
label(6 "Week 5") label(7 "Week 6") label(8 "Week 7") label(9 "Week 8") label(10 "Week 9") ///
label(11 "Week 10") label(12 "Week 11") label(13 "Week 12") label(14 "Week 13+") rows(3)) ///
name(unadj_utla_sgss_week)
graph export ./output/unadj_utla_sgss_week.svg, name(unadj_utla_sgss_week) as(svg) replace
sts graph , haz by(exp_week) width(5 5 5 5 5 5 5 5 5 5 5 5 5 5) ///
xlabel(1 "01SEP" 31 "01OCT" 62 "01NOV" 91 "01DEC") ///
legend(off) ///
name(unadj_utla_sgss_week1)
graph export ./output/unadj_utla_sgss_week_nl.svg, name(unadj_utla_sgss_week1) as(svg) replace
log close