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 0
/
Copy path01_extract_data.R
166 lines (116 loc) · 4.38 KB
/
01_extract_data.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
###########################################################################
# Author: Patrick Rockenschaub
# Project: Preserve Antibiotics through Safe Stewardship (PASS)
# Primary Care work package 1
# Comorbidity onset analysis
#
# File: 01_extract_data.R
# Date: 11/06/2019
# Task: Extract all necessary records from the database and store them
# in local R files for reproducability
#
###########################################################################
# Path from project directory to this file
# NOTE: must be set in each programme separately
subfolder <- "05_comorb_onset"
# Initialise the workspace
source(file.path(subfolder, "00_init.R"))
source(file.path(subfolder, "00_functions.R"))
# Select practices --------------------------------------------------------
## @knitr practices
#+ practices, include = FALSE
# Obtain information on when practices provided data
def_practice <-
practice_db() %>%
filter(linked == 1L) %>%
compute(name = "practices")
practices <- collect_dt(def_practice, convert = TRUE)
# Select patient population -----------------------------------------------
## @knitr patients
#+ patients, include = FALSE
# Define start and end dates
def_patients <-
study_population_db(link = TRUE) %>%
in_date(study_start, study_end) %>%
semi_join(def_practice, by = "pracid") %>%
left_join(imd_db(), by = c("patid", "pracid"))
# Apply exclusion criteria
def_patients <-
def_patients %>%
filter(
!is.na(birth_date),
!is.na(female),
!is.na(imd)
) %>%
select(patid, pracid, female, imd, birth_date,
death_date, enter_date, leave_date)
def_patients <- compute(def_patients, name = "patients")
patients <- collect_dt(def_patients, convert = TRUE)
# Select all systemic antibiotic prescribing ------------------------------
## @knitr abx
#+ abx, include = FALSE
# Define the included and excluded BNF chapters
bnf_systemic <- "0501"
bnf_excl <- c("050109", "050110")
# Get all prescription data for the above defined study period
def_abx <-
abx_bnf_db(bnf_systemic, bnf_excl) %>%
semi_join(def_patients, by = "patid") %>%
in_date(study_start, study_end) %>%
select(patid, prescdate = eventdate, prodcode, consid,
qty, ndd, issueseq) %>%
arrange(patid, eventdate, issueseq)
abx <- collect_dt(def_abx, convert = TRUE)
# Some drugs are classified in 5.1., but are crossover-products
# Exclude those
abx_info <- antibiotics() %>% collect_dt()
atc_a02b <- abx_info[atcchapter == "A02B"]
abx %<>% .[!atc_a02b, on = "prodcode"]
remove(atc_a02b)
abx %<>% .[!(abx_info[group %in% c("Antifungal", "Antileprotic",
"Antituberculosis", "No antibiotic")]),
on = "prodcode"]
# No unique identifier of prescription exists in the database, so create
# one for this project.
#
# NOTE: This identifier might change with each new database query, so the
# identifier is only valid within the same session (and not between
# sessions)
abx <- abx[order(patid, prescdate, prodcode, qty, issueseq)]
abx[, "abx_id" := .I]
# Obtain comorbidities ----------------------------------------------------
## @knitr comorbs
#+ comorbs, include = FALSE
# Use all other comorbidity code
codes_qof <-
codes_qof_db() %>%
filter(!(subclass %in% c("ckd_stage_1", "ckd_stage_2"))) %>%
collect_dt()
# Select all records with one of those codes in the study population
# NOTE: exclude COPD (by definition) and CKD stage 1&2 (unreliable coding)
comorb <-
qof_db() %>%
records_db(codes_qof) %>%
semi_join(def_patients, by = "patid") %>%
filter(!is.na(eventdate)) %>%
select(-num_rows) %>%
collect_dt(convert = TRUE)
# Add the disease labels
comorb[codes_qof, on = "medcode", comorb := comorbidity]
# Keep the earliest for each comorbidity
comorb %<>%
split(., f = .$comorb) %>%
map_df(first_record)
# Get all consultations in the study period -------------------------------
## @knitr comorbs
#+ comorbs, include = FALSE
def_consultations <-
consult_db() %>%
in_date(study_start , study_end) %>%
semi_join(def_patients, by = "patid") %>%
select(patid, eventdate)
cons <- collect_dt(def_consultations, convert = TRUE)
cons %<>% unique()
# Save the datasets -------------------------------------------------------
mget(c("practices", "patients", "abx", "comorb", "cons")) %>%
walk2(., names(.), save_derived, compress = "gz")