This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
03a_derive_tables.R
457 lines (321 loc) · 15 KB
/
03a_derive_tables.R
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
###########################################################################
# Author: Patrick Rockenschaub
# Project: Preserve Antibiotics through Safe Stewardship (PASS)
# Primary Care work package 1
# COPD analysis
#
# File: 03a_derive_tables.R
# Date: 07/11/2018
# Task: Use the extracted data to create tables for analysis
#
###########################################################################
library(forcats)
# Create a table of baseline values ---------------------------------------
## @knitr baseline
#+ baseline, include = FALSE
require_tbls(char(practices, cohort, comorb, obese, smoke,
aecopd, severe_aecopd, fev, mrc, flu_vacc))
# Time ranges used to detect and remove outliers
y5 <- list(main_start %m-% years(5), main_start %m-% days(1))
m18 <- list(main_start %m-% months(18), main_start %m-% days(1))
m12 <- list(main_start %m-% months(12), main_start %m-% days(1))
# Practices with complete year follow-up
prac_main <- practices[uts <= main_start & lcd >= study_end, "pracid"]
# Age, sex, IMD
baseline <-
cohort[prac_main, on = "pracid", nomatch = 0] %>%
.[enter_date <= main_start & leave_date >= study_end,
.(patid, pracid,
female,
age = year(main_start) - year(birth_date),
imd,
leave_date,
risk = time_length(main_start %--% study_end, u = "days") + 1L)]
baseline %<>% .[age %between% list(35, 110)] # Only a handful are younger
# Comorbidities, obesity, smoking
comorb_at_base <-
comorb[, c(.(patid = patid), map(.SD, ~ . < main_start)),
.SDcols = asthma:stroke]
setorder(obese, patid, eventdate)
obese_at_base <- closest_in(obese[, .(patid, eventdate, obese)], y5)
setorder(smoke, patid, eventdate)
smoke_at_base <- closest_in(smoke[, .(patid, eventdate, smoke = status)], y5)
ever_smoked <- smoke[eventdate < study_start & status != "non", .(patid)]
smoke_at_base[ever_smoked, on = "patid", smoke := if_else(smoke == "non", "ex", smoke)]
# Baseline number of exacerbations
aecopd_at_base <- aecopd[eventdate %between% m12]
aecopd_at_base %<>% .[, .(num_aecopd = .N), by = patid]
aecopd_fu <- aecopd[eventdate %between% list(main_start, study_end)]
aecopd_fu %<>% .[, .(fu_aecopd = .N), by = patid]
sev_ae_at_base <- severe_aecopd[admidate %between% m12 |
discharged %between% m12]
sev_ae_at_base %<>% .[, .(num_sev_ae = .N), by = patid]
sev_ae_fu <- severe_aecopd[admidate %between% list(main_start, study_end) |
discharged %between% list(main_start, study_end)]
sev_ae_fu %<>% .[, .(fu_sev_ae = .N), by = patid]
# COPD severity
fev_at_base <- closest_in(fev[, !("method")], m12)
mrc_at_base <- closest_in(mrc[, !("medcode")], m12)
# Immunisation
vacc_at_base <- closest_in(flu_vacc[, !("medcode")], m12)
vacc_at_base[, vacc := TRUE]
# Combine to baseline table
baseline <-
mget(c("baseline", ls()[ls() %like% "at_base"])) %>%
reduce(merge, by = "patid", all.x = TRUE)
baseline[is.na(num_aecopd), num_aecopd := 0]
baseline[is.na(num_sev_ae), num_sev_ae := 0]
baseline[aecopd_fu, on = "patid", fu_aecopd := fu_aecopd]
baseline[is.na(fu_aecopd), fu_aecopd := 0]
baseline[sev_ae_fu, on = "patid", fu_sev_ae := fu_sev_ae]
baseline[is.na(fu_sev_ae), fu_sev_ae := 0]
# Limit to patients with a record of ever having smoked
baseline %<>% .[smoke %in% c("smoke", "ex")]
# Get all the antibiotics within the study period -------------------------
## @knitr abx_cohort
#+ abx_cohort, include = FALSE
require_tbls(char(cohort, baseline, abx, reason, presc_as))
# Limit the antibiotics to all patients in the cohort and within
# the main study period
abx_cohort <-
abx[(baseline[, .(patid)]), on = "patid", nomatch = 0] %>%
.[(cohort[, .(patid, leave_date)]),
on = "patid", nomatch = 0] %>%
.[reason, on = "abx_id",
c("system", "conditions") := .(system, conditions)] %>%
.[presc_as, on = "abx_id", as := as] %>%
.[prescdate %between% list(main_start, study_end)] %>%
.[, .(patid, abx_id, prodcode, prescdate, substance,
strength, qty, ndd, system, conditions, as, total_ddd)]
# Collapse systems and conditions
abx_cohort[, system := factor(system)]
abx_cohort[, system := fct_lump(system, 5L, other = "other")]
main_systems <- c("rt/ent", "urogenital tract", "skin and wounds")
abx_cohort[!(system %in% main_systems),
conditions := list(list(NA_character_))]
# Define the order of precedence of conditions to get a unique
# condition for each within each body system
resp_class <-
read_excel(file.path(subfolder, "resp_indication_chapters.xlsx"),
col_names = c("code", "type", "desc", "condition", "hier"),
skip = 1)
setDT(resp_class)
setorder(resp_class, type, hier)
c_order <- c(unique(resp_class$condition),
"genital tract", "urinary tract", "unspecific urogenital",
"cellulitis", "boil, cyst, abscess", "bites", "wounds",
"ingrown/infected nail", "acne", "other skin",
"unspecific")
choose_cond <- function(conditions){
# Among the list passed into the function, choose the value that is
# earliest in the vecotr c_order given above
#
# Args:
# conditions - a character vector with all conditions that apply
# to one antibiotic
#
# Result:
# a character vector of length 1
if(all(is.na(conditions))) {
return(NA_character_)
}
pos <- map_int(unique(conditions),
~ which(str_detect(c_order, str_c("^", ., "$"))))
conditions[which.min(pos)]
}
abx_cohort[!is.na(conditions),
condition := map_chr(conditions, choose_cond)]
# Calculate the health resource utilisation -------------------------------
## @knitr util
#+ util, include = FALSE
require_tbls(char(cohort, baseline, abx, cons, admission, hosp_diag, ae))
util <- cohort[, .(patid)] %>%
.[(baseline[, "patid"]), on = "patid"]
# Consultations in primary care
cons_obs <-
cons[util, on = "patid", nomatch = 0] %>%
.[, .(patid,
in_obs = eventdate %between% list(main_start,
study_end),
in_prev= eventdate %between% list(main_start %m-% months(6),
main_start %m-% days(6)))] %>%
.[, .(num_cons = sum(in_obs), num_cons_6m = sum(in_prev)), by = patid]
# Hospital admissions
admission[(hosp_diag[icd %like% "^J"]), on = "spno", resp_icd := TRUE]
admission[is.na(resp_icd), resp_icd := FALSE]
time_within <- function(activity, in_period){
# Calculate the number of days of activity that fall within another
# time interval. Overlap between intervals is compared pair-wise
#
# Args:
# activity - vector of activity intervals (e.g. hospital stay)
# in_period - vector of corresponding time windows
#
# Result:
# An integer vector with the overlap in days
#
# NOTE: The first day (i.e. start) of an activity is not counted
start <- pmax(int_start(activity), int_start(in_period))
end <- pmin(int_end(activity), int_end(in_period))
len <- time_length(start %--% end, unit = "days")
len[len < 0] <- NA
len + (int_start(activity) < int_start(in_period))
}
hosp_stays <-
admission[util, on = "patid", nomatch = 0] %>%
.[, `:=`(in_obs = time_within(admidate %--% discharged,
main_start %--% study_end),
in_prev= time_within(admidate %--% discharged,
(main_start %m-% months(6)) %--%
(main_start %m-% days(1))))]
hosp_obs <-
hosp_stays[, .(num_hosp = sum(!is.na(in_obs)),
num_hosp_resp = sum(resp_icd & !is.na(in_obs)),
day_hosp = sum(in_obs, na.rm = TRUE),
num_hosp_6m = sum(!is.na(in_prev)),
num_hosp_resp_6m = sum(resp_icd & !is.na(in_prev)),
day_hosp_6m = sum(in_prev, na.rm = TRUE)),
by = patid]
remove(hosp_stays, time_within)
# Visits to the A&E department
ae_obs <-
ae[util, on = "patid", nomatch = 0] %>%
.[, .(patid,
in_obs = arrivaldate %between% list(main_start,
study_end),
in_prev= arrivaldate %between% list(main_start %m-% months(6),
main_start %m-% days(1)))] %>%
.[, .(num_ae = sum(in_obs), num_ae_6m = sum(in_prev)), by = patid]
# Death during the observation period
death_obs <-
cohort[(baseline[, .(patid)]), on = "patid",
.(patid,
death = if_else(death_date %between% .(main_start, study_end),
"yes", "no", "no"))]
# Add together all the measures
util %<>%
list(cons_obs, hosp_obs, ae_obs, death_obs) %>%
reduce(merge, by = "patid", all.x = TRUE)
util_num <- util[, which(map_lgl(.SD, is.numeric))]
fill_na(util, names(util_num), 0)
# Create a table of baseline values for all patients ----------------------
## @knitr everyone
#+ everyone, include = FALSE
require_tbls(char(practices, patients, cohort, comorb, obese, smoke,
fev, mrc, flu_vacc))
# Update the entry date for the COPD patients
patients[cohort, on = "patid", enter_date := i.enter_date]
# Time ranges used to detect and remove outliers
y5 <- list(main_start %m-% years(5), main_start %m-% days(1))
m18 <- list(main_start %m-% months(18), main_start %m-% days(1))
m12 <- list(main_start %m-% months(12), main_start %m-% days(1))
# Practices with complete year follow-up
prac_main <- practices[uts <= main_start & lcd >= study_end, "pracid"]
# Age, sex, IMD
everyone <-
patients[prac_main, on = "pracid", nomatch = 0] %>%
.[enter_date <= main_start & leave_date >= study_end,
.(patid, pracid, leave_date,
female,
age = year(main_start) - year(birth_date),
imd,
risk = time_length(main_start %--% study_end, u = "days") + 1L)]
everyone %<>% .[age %between% list(35, 110)]
# Comorbidities, obesity, smoking
comorb_all <-
comorb[, c(.(patid = patid), map(.SD, ~ . < main_start)),
.SDcols = asthma:stroke]
setorder(obese, patid, eventdate)
obese_all <- closest_in(obese[, .(patid, eventdate, obese)], y5)
setorder(smoke, patid, eventdate)
smoke_all <- closest_in(smoke[, .(patid, eventdate, smoke = status)], y5)
ever_smoked <- smoke[eventdate < study_start & status != "non", .(patid)]
smoke_all[ever_smoked, on = "patid", smoke := if_else(smoke == "non", "ex", smoke)]
# Combine to baseline table
everyone <-
mget(c("everyone", ls()[ls() %like% "_all$"])) %>%
reduce(merge, by = "patid", all.x = TRUE)
require_tbls(char(cohort, baseline, abx, cons, admission, hosp_diag, ae))
util_eone <- everyone[, .(patid)]
# Consultations in primary care
cons_obs <-
cons[util_eone, on = "patid", nomatch = 0] %>%
.[, .(patid,
in_obs = eventdate %between% list(main_start,
study_end),
in_prev= eventdate %between% list(main_start %m-% months(6),
main_start %m-% days(6)))] %>%
.[, .(num_cons = sum(in_obs), num_cons_6m = sum(in_prev)), by = patid]
# Hospital admissions
admission[(hosp_diag[icd %like% "^J"]), on = "spno", resp_icd := TRUE]
admission[is.na(resp_icd), resp_icd := FALSE]
time_within <- function(activity, in_period){
# Calculate the number of days of activity that fall within another
# time interval. Overlap between intervals is compared pair-wise
#
# Args:
# activity - vector of activity intervals (e.g. hospital stay)
# in_period - vector of corresponding time windows
#
# Result:
# An integer vector with the overlap in days
#
# NOTE: The first day (i.e. start) of an activity is not counted
start <- pmax(int_start(activity), int_start(in_period))
end <- pmin(int_end(activity), int_end(in_period))
len <- time_length(start %--% end, unit = "days")
len[len < 0] <- NA
len + (int_start(activity) < int_start(in_period))
}
hosp_stays <-
admission[util_eone, on = "patid", nomatch = 0] %>%
.[, `:=`(in_obs = time_within(admidate %--% discharged,
main_start %--% study_end),
in_prev= time_within(admidate %--% discharged,
(main_start %m-% months(6)) %--%
(main_start %m-% days(1))))]
hosp_obs <-
hosp_stays[, .(num_hosp = sum(!is.na(in_obs)),
num_hosp_resp = sum(resp_icd & !is.na(in_obs)),
day_hosp = sum(in_obs, na.rm = TRUE),
num_hosp_6m = sum(!is.na(in_prev)),
num_hosp_resp_6m = sum(resp_icd & !is.na(in_prev)),
day_hosp_6m = sum(in_prev, na.rm = TRUE)),
by = patid]
remove(hosp_stays, time_within)
# Visits to the A&E department
ae_obs <-
ae[util_eone, on = "patid", nomatch = 0] %>%
.[, .(patid,
in_obs = arrivaldate %between% list(main_start,
study_end),
in_prev= arrivaldate %between% list(main_start %m-% months(6),
main_start %m-% days(1)))] %>%
.[, .(num_ae = sum(in_obs), num_ae_6m = sum(in_prev)), by = patid]
# Death during the observation period
death_obs <-
cohort[(baseline[, .(patid)]), on = "patid",
.(patid,
death = if_else(death_date %between% .(main_start, study_end),
"yes", "no", "no"))]
# Add together all the measures
util_eone %<>%
list(cons_obs, hosp_obs, ae_obs, death_obs) %>%
reduce(merge, by = "patid", all.x = TRUE)
util_eone_num <- util_eone[, which(map_lgl(.SD, is.numeric))]
fill_na(util_eone, names(util_eone_num), 0)
everyone[, female := factor(female, 0:1, c("male", "female"))]
age_lvls <- c("35-50" = 50, "50-60" = 60, "60-70" = 70, "70-80" = 80, ">80" = Inf)
everyone[, age_cat:= cut(age, c(0, age_lvls), names(age_lvls), right = FALSE)]
everyone[, age_scaled := scale(age)]
everyone[, imd := factor(imd, 1:5)]
everyone[baseline, on = "patid", copd := TRUE]
lgl <- char(asthma, chd, ckd, copd, dm, hf, pad, stroke)
fill_na(everyone, lgl, FALSE)
everyone[, num_com := .(asthma + chd + ckd + dm + hf + pad + stroke)]
everyone[, num_com := cut(num_com, c(-Inf, 0, 1, Inf), c("0", "1", "2"))]
everyone[, (lgl) := map(.SD, factor, c(FALSE, TRUE), c("no", "yes")), .SDcols = lgl]
fill_na(everyone, "obese", "non")
everyone[, obese := factor(obese, c("non", "obese", "severely obese"))]
everyone[, obese := fct_recode(obese, obese = "severely obese")]
everyone[, smoke := factor(smoke, levels = c("ex", "smoke", "non"))]