generated from opensafely/research-template
/
an_logit_models.do
474 lines (334 loc) · 12.8 KB
/
an_logit_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
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
468
469
470
471
472
473
474
********************************************************************************
*
* Do-file: an_logit_models.do
*
* Project: SGTF CFR
*
* Programmed by: Daniel Grint
*
* Data used: output/cr_analysis_dataset.dta
*
* Data created:
*
* Other output: an_logit_models.log containing:
* 1-Unadjusted OR and absolute risks for SGTF
* 2-Adjusted OR and absolute risks for SGTF
* 3-Subgroup analyses of OR and absolute risk
*
********************************************************************************
*
* Purpose: This do-file runs logit models, calculating relative (glm) and
* absolute odds (margins)
*
********************************************************************************
* Open a log file
cap log close
log using ./logs/an_logit_models, replace t
clear
use ./output/cr_analysis_new.dta
* DROP MISSING UTLA
noi di "DROPPING MISSING UTLA DATA"
drop if utla_group==""
* DROP IF NO DATA ON SGTF
noi di "DROPPING NO SGTF DATA"
drop if has_sgtf==0
noi di "SUBSETTING ON 28-DAY RISK POPULATION"
keep if risk_pop==1
tab sgtf risk_28, row
*******************
/* Unadjusted OR */
*******************
glm risk_28 i.sgtf, family(bin) link(logit) eform
* Absolute odds
margins sgtf
*********************************************************************
/* Demographically adjusted OR - age as spline, continuous hh size */
/* Not adjusting for comorbidities, obesity, or smoking status */
*********************************************************************
glm risk_28 i.sgtf i.male ib1.imd ib1.eth2 household_size i.home_bin ///
i.region ib1.rural_urban5 ib1.start_week age1 age2 age3 ///
if eth2 != 6 ///
, family(bin) link(logit) eform
*********************************************************************
/* Demographically adjusted OR - age grouped, cat hh size */
/* Not adjusting for comorbidities, obesity, or smoking status */
*********************************************************************
glm risk_28 i.sgtf i.male ib1.imd ib1.eth2 ib1.hh_total_cat i.home_bin ///
i.region ib1.rural_urban5 ib1.start_week ib2.agegroupA ///
if eth2 != 6 ///
, family(bin) link(logit) eform
***********************************************************
/* Fully adjusted OR - age as spline, continuous hh size */
***********************************************************
glm risk_28 i.sgtf i.male ib1.imd ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat household_size ///
i.region ib1.rural_urban5 ib0.comorb_cat ib1.start_week age1 age2 age3 i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
**************************************************
/* Fully adjusted OR - age grouped, cat hh size */
**************************************************
glm risk_28 i.sgtf ib2.agegroup6 i.male ib1.imd ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat i.region ib1.rural_urban5 ib0.comorb_cat ib1.start_week i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store fully
table agegroup6 sgtf if e(sample)==1, contents(count patient_id) by(comorb_cat male)
table male comorb_cat if e(sample)==1
* Adjusted absolute risks
margins comorb_cat#male#agegroup6 if sgtf==0, post asobserved
* Save risk estimates
matrix est = e(b)
matrix inv_est = est'
svmat inv_est
* Save SE estimates
matrix var = e(V)
matrix diag_var = vecdiag(var)
matrix inv_var = diag_var'
svmat inv_var
gen sq_var = sqrt(inv_var1)
noi disp "CHECK MARGINS ARE CORRECTLY CALCULATED TO MATCH ABOVE"
list inv_est1 sq_var in 1/36
* Re-Calculate CI
gen risk0 = inv_est1*100
gen lb0 = (inv_est1 - invnormal(0.975)*sq_var)*100
gen ub0 = (inv_est1 + invnormal(0.975)*sq_var)*100
order lb ub, after(risk0)
est restore fully
* Adjusted absolute risks
margins comorb_cat#male#agegroup6 if sgtf==1, post asobserved
* Save risk estimates
matrix est1 = e(b)
matrix inv_estx = est1'
svmat inv_estx
* Save SE estimates
matrix var1 = e(V)
matrix diag_var1 = vecdiag(var1)
matrix inv_varx = diag_var1'
svmat inv_varx
gen sq_varx = sqrt(inv_varx1)
noi disp "CHECK MARGINS ARE CORRECTLY CALCULATED TO MATCH ABOVE"
list inv_estx1 sq_varx in 1/36
* Re-Calculate CI
gen risk1 = inv_estx1*100
gen lb1 = (inv_estx1 - invnormal(0.975)*sq_varx)*100
gen ub1 = (inv_estx1 + invnormal(0.975)*sq_varx)*100
order lb1 ub1, after(risk1)
* Adjusted risk difference
est restore fully
margins comorb_cat#male#agegroup6, dydx(sgtf) post asobserved
* Save risk estimates
matrix diff = e(b)
matrix diff = diff[1, 37..72]
matrix inv_diff = diff'
svmat inv_diff
* Save SE estimates
matrix dvar = e(V)
matrix diag_dvar = vecdiag(dvar)
matrix inv_dvar = diag_dvar'
matrix inv_dvar = inv_dvar[37..72,1]
svmat inv_dvar
gen sq_dvar = sqrt(inv_dvar1)
noi disp "CHECK MARGINS ARE CORRECTLY CALCULATED TO MATCH ABOVE"
list inv_diff1 sq_dvar in 1/36
* Re-Calculate CI
gen diff1 = inv_diff1*100
gen dlb = (inv_diff1 - invnormal(0.975)*sq_dvar)*100
gen dub = (inv_diff1 + invnormal(0.975)*sq_dvar)*100
order dlb dub, after(diff1)
gen risk_labels = "Female: 0-<40" in 1
replace risk_labels = "40-<55" in 2
replace risk_labels = "55-<65" in 3
replace risk_labels = "65-<75" in 4
replace risk_labels = "75-<85" in 5
replace risk_labels = "85+" in 6
replace risk_labels = "Male: 0-<40" in 7
replace risk_labels = "40-<55" in 8
replace risk_labels = "55-<65" in 9
replace risk_labels = "65-<75" in 10
replace risk_labels = "75-<85" in 11
replace risk_labels = "85+" in 12
replace risk_labels = "Female: 0-<40" in 13
replace risk_labels = "40-<55" in 14
replace risk_labels = "55-<65" in 15
replace risk_labels = "65-<75" in 16
replace risk_labels = "75-<85" in 17
replace risk_labels = "85+" in 18
replace risk_labels = "Male: 0-<40" in 19
replace risk_labels = "40-<55" in 20
replace risk_labels = "55-<65" in 21
replace risk_labels = "65-<75" in 22
replace risk_labels = "75-<85" in 23
replace risk_labels = "85+" in 24
replace risk_labels = "Female: 0-<40" in 25
replace risk_labels = "40-<55" in 26
replace risk_labels = "55-<65" in 27
replace risk_labels = "65-<75" in 28
replace risk_labels = "75-<85" in 29
replace risk_labels = "85+" in 30
replace risk_labels = "Male: 0-<40" in 31
replace risk_labels = "40-<55" in 32
replace risk_labels = "55-<65" in 33
replace risk_labels = "65-<75" in 34
replace risk_labels = "75-<85" in 35
replace risk_labels = "85+" in 36
***********************************
/* Output table of absolute risk */
***********************************
cap file close tablecontent
file open tablecontent using ./output/table3_abs_risk.txt, write text replace
file write tablecontent ("Table 3: Absolute risk of death by 28-days") _n _n
file write tablecontent ("Comorbidities/Sex/Age group") _tab ///
("non-VOC (95% CI)") _tab ///
("VOC (95% CI)") _tab ///
("Diff (95% CI)") _n
forvalues i=1/36 {
preserve
keep if _n == `i'
if inlist(`i',7,19,31) {
file write tablecontent _n
}
if `i'==1 {
file write tablecontent _n ("No Comorbidities") _n
}
if `i'==13 {
file write tablecontent _n ("1 Comorbidity") _n
}
if `i'==25 {
file write tablecontent _n ("2+ Comorbidities") _n
}
file write tablecontent %9s (risk_labels) _tab %4.2f (risk0) (" (") %4.2f (lb0) ("-") %4.2f (ub0) (")") _tab %4.2f (risk1) (" (") %4.2f (lb1) ("-") %4.2f (ub1) (")") _tab %4.2f (diff1) (" (") %4.2f (dlb) ("-") %4.2f (dub) (")") _n
restore
}
file close tablecontent
/* Risks as balanced
est restore fully
margins comorb_cat#male#agegroupA, post asbalanced over(sgtf) grand
est restore fully
margins comorb_cat#male#agegroupA, post over(sgtf) grand
*/
***********************
/* Subgroup analyses */
***********************
/*
/* Fully adjusted OR - age grouped, cat hh size */
glm risk_28 i.sgtf ib2.agegroupA i.male ib1.imd ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat i.region ib1.rural_urban5 ib0.comorb_cat ib1.start_week i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store e_no_int
* Constant VOC effect over subgroups
lincom 1.sgtf, eform
/* Age group */
glm risk_28 i.sgtf##ib2.agegroupA i.male ib1.imd ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat i.region ib1.rural_urban5 ib0.comorb_cat ib1.start_week i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store e_ageX
lrtest e_no_int e_ageX
* Age group VOC vs. non-VOC OR
lincom 1.sgtf, eform // 0-<65
lincom 1.sgtf + 1.sgtf#2.agegroupA, eform // 65-<75
lincom 1.sgtf + 1.sgtf#3.agegroupA, eform // 75-<85
lincom 1.sgtf + 1.sgtf#4.agegroupA, eform // 85+
/* Ethnicity */
glm risk_28 i.sgtf##ib1.eth2 ib0.comorb_cat ib2.agegroupA i.male ib1.imd ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat i.region ib1.rural_urban5 ib1.start_week i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store e_ethX
* Test for interaction
lrtest e_no_int e_ethX
* Ethnicity VOC vs. non-VOC OR
lincom 1.sgtf, eform // White
*lincom 1.sgtf + 1.sgtf#2.eth5, eform // S Asian
*lincom 1.sgtf + 1.sgtf#3.eth5, eform // Black
*lincom 1.sgtf + 1.sgtf#4.eth5, eform // Mixed
lincom 1.sgtf + 1.sgtf#5.eth2, eform // Other
*lincom 1.sgtf + 1.sgtf#6.eth2, eform // Missing
/* Comorbidities */
glm risk_28 i.sgtf##ib0.comorb_cat ib2.agegroupA i.male ib1.imd ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat i.region ib1.rural_urban5 ib1.start_week i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store e_comorbX
* Test for interaction
lrtest e_no_int e_comorbX
* Comorbidities VOC vs. non-VOC OR
lincom 1.sgtf, eform // no comorbs
lincom 1.sgtf + 1.sgtf#1.comorb_cat, eform // 1 comorb
lincom 1.sgtf + 1.sgtf#2.comorb_cat, eform // 2+ comorbs
/* IMD */
glm risk_28 i.sgtf##ib1.imd ib2.agegroupA i.male ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat i.region ib1.rural_urban5 ib0.comorb_cat ib1.start_week i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store e_imdX
* Test for interaction
lrtest e_no_int e_imdX
* IMD VOC vs. non-VOC OR
lincom 1.sgtf, eform // 1 least deprived
lincom 1.sgtf + 1.sgtf#2.imd, eform // 2
lincom 1.sgtf + 1.sgtf#3.imd, eform // 3
lincom 1.sgtf + 1.sgtf#4.imd, eform // 4
lincom 1.sgtf + 1.sgtf#5.imd, eform // 5 most deprived
/* Epi week */
glm risk_28 i.sgtf ib2.agegroupA i.male ib1.imd ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat i.region ib1.rural_urban5 ib0.comorb_cat ib2.start_weekA i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store e_week
glm risk_28 i.sgtf##ib2.start_weekA ib2.agegroupA i.male ib1.imd ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat i.region ib1.rural_urban5 ib0.comorb_cat i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store e_weekX
* Test for interaction
lrtest e_no_int e_weekX
* Epi week VOC vs. non-VOC OR
lincom 1.sgtf + 1.sgtf#2.start_week, eform // week 1/2
lincom 1.sgtf + 1.sgtf#3.start_week, eform // week 3
lincom 1.sgtf + 1.sgtf#4.start_week, eform // week 4
lincom 1.sgtf + 1.sgtf#5.start_week, eform // week 5
lincom 1.sgtf + 1.sgtf#6.start_week, eform // week 6
lincom 1.sgtf + 1.sgtf#7.start_week, eform // week 7
lincom 1.sgtf + 1.sgtf#8.start_week, eform // week 8
/* NHS region */
/*
glm risk_28 i.sgtf i.agegroupA i.male ib1.imd ib1.eth5 ib1.smoke_nomiss ib1.obese4cat ///
ib1.hh_total_cat ib1.rural_urban5 ib0.comorb_cat ib1.start_week i.region, ///
family(bin) link(log) eform
est store e_region
*/
glm risk_28 i.sgtf##i.region ib2.agegroupA i.male ib1.imd ib1.eth2 ib1.smoke_nomiss2 ib1.obese4cat ///
ib1.hh_total_cat ib1.rural_urban5 ib0.comorb_cat ib1.start_week i.home_bin ///
if eth2 != 6 ///
, family(bin) link(logit) eform
est store e_regionX
* Test for interaction
lrtest e_no_int e_regionX
* NHS region VOC vs. non-VOC OR
lincom 1.sgtf, eform // East
lincom 1.sgtf + 1.sgtf#1.region, eform // East mids
lincom 1.sgtf + 1.sgtf#2.region, eform // London
lincom 1.sgtf + 1.sgtf#3.region, eform // North E
lincom 1.sgtf + 1.sgtf#4.region, eform // North W
lincom 1.sgtf + 1.sgtf#5.region, eform // South E
lincom 1.sgtf + 1.sgtf#6.region, eform // South W
lincom 1.sgtf + 1.sgtf#7.region, eform // West mids
lincom 1.sgtf + 1.sgtf#8.region, eform // Yorks & Hum
*********************************************************************
/* Causal min adjustment set - age as spline, comorbidities, */
/* deprivation index, and smoking status */
*********************************************************************
glm risk_28 i.sgtf i.comorb_cat ib1.imd i.smoke_nomiss2 age1 age2 age3 i.home_bin, ///
family(bin) link(logit) eform
* Age grouped
glm risk_28 i.sgtf i.comorb_cat ib1.imd i.smoke_nomiss2 ib2.agegroupA i.home_bin, ///
family(bin) link(logit) eform
* With region
glm risk_28 i.sgtf i.comorb_cat ib1.imd i.smoke_nomiss2 ib2.agegroupA i.home_bin i.region, ///
family(bin) link(logit) eform
*/
log close
insheet using ./output/table3_abs_risk.txt, clear
export excel using ./output/table3_abs_risk.xlsx, replace