generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
303_competing_events_time_stratified.do
148 lines (97 loc) · 3.84 KB
/
303_competing_events_time_stratified.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
********************************************************************************
*
* 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_time_strat_age_sex.txt, replace t
tempname measures
postfile `measures' ///
str20(comparator) str20(outcome) str25(analysis) str10(adjustment) str10(time) hr lc uc ///
using $tabfigdir/fine_gray_summary_time_strat_age_sex, replace
foreach an in pneumonia gen_population {
*foreach an in pneumonia {
use $outdir/combined_covid_`an'.dta, replace
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 {
noi di "Starting analysis for `v' Outcome ..."
preserve
local out `v'_cens_gp
local end_date `v'_cens_gp_end_date
* Apply exclusion for AKI and diabetes outcomes
if "`v'" == "t2dm" {
drop if previous_diabetes == 1
local out `v'
local end_date `v'_end_date
}
if "`v'" == "aki" {
drop if aki_exclusion_flag == 1
local out `v'_cens_gp
local end_date `v'_cens_gp_end_date
}
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')
* STSPLIT
stsplit time , at(30(30)120)
foreach adjust in age_sex {
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
}
forvalues t = 0(30)120 {
stcox $`adjust' if time == `t' , vce(robust)
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'") ("`t'") ///
(`hr') (`lc') (`uc')
}
}
restore
}
}
postclose `measures'
* Change postfiles to csv
use $tabfigdir/fine_gray_summary_time_strat_age_sex, replace
export delimited using $tabfigdir/fine_gray_summary_time_strat_age_sex.csv, replace
log close