generated from opensafely/research-template
/
201_cox_models.do
91 lines (66 loc) · 2.25 KB
/
201_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
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//
// 201_cox_models.do
//
// This program runs Cox models to perform survival analysis.
//
// Authors: Robin (based on Alex & John)
// Date: 15 Oct 2021
// Updated: 18 Oct 2021
// Input files:
// Output files:
//
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
set varabbrev off
clear
do `c(pwd)'/analysis/global.do
cap log close
log using $outdir/cox_models.txt, replace t
tempname measures
postfile `measures' ///
str20(comparator) str20(outcome) str25(analysis) str10(adjustment) ptime_covid num_events_covid rate_covid ///
ptime_comparator num_events_comparator rate_comparator hr lc uc ///
using $tabfigdir/cox_model_summary, replace
foreach an in pneumonia general_2019 general_2020 {
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
foreach v in sick_note {
noi di "Starting analysis for `v' Outcome ..."
preserve
local end_date `v'_end_date
local out `v'
noi di "$group: stset in `a'"
stset `end_date', id(new_patient_id) failure(`out') enter(indexdate) origin(indexdate)
foreach adjust in crude age_sex {
stcox $`adjust', vce(robust)
matrix b = r(table)
local hr = b[1,2]
local lc = b[5,2]
local uc = b[6,2]
stptime if case == 1
local rate_covid = `r(rate)'
local ptime_covid = `r(ptime)'
local events_covid .
if `r(failures)' == 0 | `r(failures)' > 5 local events_covid `r(failures)'
stptime if case == 0
local rate_comparator = `r(rate)'
local ptime_comparator = `r(ptime)'
local events_comparator .
if `r(failures)' == 0 | `r(failures)' > 5 local events_comparator `r(failures)'
post `measures' ("`an'") ("`v'") ("`out'") ("`adjust'") ///
(`ptime_covid') (`events_covid') (`rate_covid') (`ptime_comparator') (`events_comparator') (`rate_comparator') ///
(`hr') (`lc') (`uc')
}
restore
}
}
postclose `measures'
* Change postfiles to csv
use $tabfigdir/cox_model_summary, replace
export delimited using $tabfigdir/cox_model_summary.csv, replace
log close