generated from opensafely/research-template
/
measures_carehome.py
144 lines (113 loc) · 3.78 KB
/
measures_carehome.py
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
###################################################
# This script creates monthly counts/rates of opioid
# prescribing for any opioid prescribing, new opioid prescribing,
# and high dose/long-acting prescribing
###################################################
from ehrql import case, when, months, INTERVAL, Measures
from ehrql.tables.tpp import (
patients,
addresses,
practice_registrations,
clinical_events)
import codelists
from dataset_definition import make_dataset_opioids
##########
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--start-date", type=str)
parser.add_argument("--intervals", type=int)
args = parser.parse_args()
start_date = args.start_date
intervals = args.intervals
##########
index_date = INTERVAL.start_date
dataset = make_dataset_opioids(index_date=index_date, end_date=INTERVAL.end_date)
##########
## Define relevant variables
# In care home based on primis codes/TPP address match
carehome_primis = clinical_events.where(
clinical_events.snomedct_code.is_in(codelists.carehome_primis_codes)
).where(
clinical_events.date.is_on_or_before(index_date)
).exists_for_patient()
carehome_tpp = addresses.for_patient_on(index_date).care_home_is_potential_match
carehome = case(
when(carehome_primis).then(True),
when(carehome_tpp).then(True),
otherwise=False
)
age = patients.age_on(index_date)
age_group = case(
when(age < 30).then("18-29"),
when(age < 40).then("30-39"),
when(age < 50).then("40-49"),
when(age < 60).then("50-59"),
when(age < 70).then("60-69"),
when(age < 80).then("70-79"),
when(age < 90).then("80-89"),
when(age >= 90).then("90+"),
otherwise="missing",
)
######
measures = Measures()
measures.configure_disclosure_control(enabled=False)
## Opioid prescribing to people in care homes
# Total denominator - people in care home
denominator = (
(patients.age_on(index_date) >= 18)
& (patients.age_on(index_date) < 110)
& ((patients.sex == "male") | (patients.sex == "female"))
& (patients.date_of_death.is_after(index_date) | patients.date_of_death.is_null())
& (practice_registrations.for_patient_on(index_date).exists_for_patient())
& carehome
)
measures.define_defaults(intervals=months(intervals).starting_on(start_date))
# By care home status
measures.define_measure(
name="opioid_any",
numerator=dataset.opioid_any,
denominator=denominator,
)
measures.define_measure(
name="hi_opioid_any",
numerator=dataset.hi_opioid_any,
denominator=denominator,
)
measures.define_measure(
name="opioid_new",
numerator=dataset.opioid_new,
denominator=denominator & dataset.opioid_naive,
)
# By admin route
measures.define_measure(
name="oral_opioid",
numerator=dataset.oral_opioid_any,
denominator=denominator,
)
measures.define_measure(
name="trans_opioid",
numerator=dataset.trans_opioid_any,
denominator=denominator,
)
measures.define_measure(
name="par_opioid",
numerator=dataset.par_opioid_any,
denominator=denominator,
)
## Sensitivity analysis by care home residence
# Total denominator - restrict to >=65 years
denominator_sens = (
(patients.age_on(index_date) >= 60)
& (patients.age_on(index_date) < 110)
& ((patients.sex == "male") | (patients.sex == "female"))
& (patients.date_of_death.is_after(index_date) | patients.date_of_death.is_null())
& (practice_registrations.for_patient_on(index_date).exists_for_patient())
)
measures.define_measure(
name="opioid_any_carehome_age",
numerator=dataset.opioid_any,
denominator=denominator_sens,
group_by={
"age_group": age_group,
"carehome": carehome}
)