-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmark Setup.R
288 lines (251 loc) · 13.1 KB
/
Benchmark Setup.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
###################################################################
# Set a random seed
###################################################################
set.seed(948756)
###################################################################
# Identifying drugs that were dispensed but not prescribed
###################################################################
setwd("/Volumes/SLS Mock Data")
rm(list=ls()) # remove all variables from workspace
library(readr)
library(dplyr)
library(lubridate)
library(stringr)
library(MASS)
library(survival)
library(survminer)
library(dplyr)
## Drug type keywords
med_salbutamol<-toupper(c("Salbutamol","Albuterol","Ventolin","Airomir",
"Salamol","AirSalb","Salapin","Ventmax","Asmasal",
"Esi-Breathe","Salbulin","Salipraneb","Ipramol","Combivent"))
med_bambuterol<-toupper(c("Bambec","Bambuterol"))
med_formoterol<-toupper(c("Atimos","Formoterol","Foradil","Fostair","Symbicort",
"Flutiform","SPIROMAX","oxis"))
med_salmeterol<-toupper(c("Salmeterol","Neovent","Serevent","Seretide",
"SIRDUPLA","AIRFLUSAL"))
med_terbutaline<-toupper(c("Bricanyl","Terbutaline"))
med_tiotropium<-toupper(c("SPIRIVA","TIOTROPIUM"))
med_vilanterol<-toupper(c("vilanterol","relvar", "vilenterol"))
med_glycopyrronium_bromide<-toupper(c("SEEBRI"))
med_ipratropium<-toupper(c("Ipratropium","Atrovent","Respontin","IPRAVENT","Salipraneb",
"Ipramol","Combivent"))
med_theophylline<-toupper(c("Theophylline","Neulin","Slo-Phyllin","Uniphyllin"))
med_aminophylline<-toupper(c("Aminophylline","Phyllocontin"))
med_beclometasone<-toupper(c("Beclometasone","Asmabec","Becodisks","Clenil","Qvar",
"Fostair"))
med_ciclesonide<-toupper(c("ciclesonide","alvesco"))
med_budesonide<-toupper(c("Budesonide","Budelin","Pulmicort","Symbicort","SPIROMAX"))
med_fluticasone<-toupper(c("fluticasone","Flixotide","Flutiform","Seretide",
"SIRDUPLA","AIRFLUSAL","relvar"))
med_mometasone<-toupper(c("Twisthaler","Asmanex","MOMETASONE"))
med_montelukast<-toupper(c("Singulair","Montelukast"))
med_zafirlukast<-toupper(c("Zafirlukast","Accolate"))
med_zileuton<-toupper(c("Zileuton","Zyflo"))
med_nedocromil<-toupper(c("Nedocromil","Tilade"))
med_cromolyn<-toupper(c("Cromoglicate","Cromoglycate","Intal"))
med_omalizumab<-toupper(c("Omalizumab","Xolair"))
med_prednisolone<-toupper(c("Prednisolone"))
med_methotrexate<-toupper(c("Methotrexate","Maxtrex","Metoject","Methofill",
"Nordimet", "Zlatal"))
med_ciclosporin<-toupper(c("Ciclosporin", "Capimune", "Capsorin", "Deximune","Neoral",
"Sandimmun"))
med_azathioprine<-toupper(c("Azathioprine", "Imuran"))
# List of all medicine keywords
med_list<-substr(ls()[which(substr(ls(),1,3)=="med")],5,30)
## Drug Class Keywords
key_SABA<-c("salbutamol","bambuterol")
key_LABA<-c("formoterol","salmeterol","terbutaline","tiotropium","vilanterol")
key_LAMA<-c("glycopyrronium_bromide","ipratropium")
key_theophyllines<-c("theophylline", "aminophylline")
key_ICS<-c("beclometasone","ciclesonide","budesonide","fluticasone","mometasone")
key_LTRA<-c("montelukast","zafirlukast","zileuton")
key_cromoglicate<-c("nedocromil","cromolyn")
key_steroid<-c("omalizumab","prednisolone")
key_immuno<-c("methotrexate","ciclosporin","azathioprine")
# List of all drug class keywords
key_list<-substr(ls()[which(substr(ls(),1,3)=="key")],5,30)
# List of all brand names
brands<-toupper(c("Ventolin","Airomir","Salamol","AirSalb","Salapin","Ventmax","Asmasal",
"Esi-Breathe","Salbulin","Salipraneb","Ipramol","Combivent","Bambec",
"Atimos","Foradil","Fostair","Symbicort",
"Flutiform","SPIROMAX","oxis","Neovent","Serevent","Seretide",
"SIRDUPLA","AIRFLUSAL","Bricanyl","SPIRIVA","relvar", "SEEBRI",
"Atrovent","Respontin","IPRAVENT","Neulin","Slo-Phyllin","Uniphyllin","Asmabec",
"Becodisks","Clenil","Qvar","alvesco","Budelin","Pulmicort","Flixotide",
"Twisthaler","Asmanex","Singulair","Accolate","Zyflo","Tilade","Intal","Xolair",
"Maxtrex","Metoject","Methofill", "Nordimet", "Zlatal", "Capimune", "Capsorin",
"Deximune","Neoral","Sandimmun", "Imuran"))
###################################################################
# Importing Data
###################################################################
PRESCR1F_asthma <- read_csv("PRESCR1F.csv")
temp<-read_csv("PRESCR1F_12APR2019.csv")
PRESCR1F_asthma<-rbind(PRESCR1F_asthma,temp)
temp<-read_csv("PRESCR1F_15JUL2019.csv")
PRESCR1F_asthma<-rbind(PRESCR1F_asthma,temp)
DISPENSF_asthma <- read_csv("DISPENSF.csv")
temp<-read_csv("DISPENSF_12APR2019.csv")
DISPENSF_asthma<-rbind(DISPENSF_asthma,temp)
temp<-read_csv("DISPENSF_15JUL2019.csv")
DISPENSF_asthma<-rbind(DISPENSF_asthma,temp)
rm(temp)
###################################################################
# Benchmark matching
###################################################################
PRESCR1F_asthma$DRUGDESC<-ifelse(is.na(PRESCR1F_asthma$DRUGDESC) | PRESCR1F_asthma$DRUGDESC=="",
PRESCR1F_asthma$CMDRGSYN,
toupper(PRESCR1F_asthma$DRUGDESC))
DISPENSF_asthma$DRUGDESC<-ifelse(is.na(DISPENSF_asthma$DRUGDESC) | DISPENSF_asthma$DRUGDESC=="",
DISPENSF_asthma$CMDRGSYN,
toupper(DISPENSF_asthma$DRUGDESC))
recode_CMDRGSYN<-unique(as.data.frame(c(PRESCR1F_asthma$DRUGDESC,DISPENSF_asthma$DRUGDESC)))
names(recode_CMDRGSYN)<-"DRUGDESC"
recode_CMDRGSYN<-recode_CMDRGSYN %>%
filter(!is.na(recode_CMDRGSYN$DRUGDESC))
# Assigning the medication type
for (med in med_list) {
filter<- vapply(get(paste0("med_",med)),
function(x) str_detect(recode_CMDRGSYN$DRUGDESC, x),
logical(nrow(recode_CMDRGSYN)))
recode_CMDRGSYN[,med] <- as.logical(apply(filter, 1, sum))
rm(filter)
}
# Assign the drug class key flags, and the overall keyword
recode_CMDRGSYN$CMDRGSYN<-""
for (key in key_list) {
recode_CMDRGSYN[,key] <- 0
for (med in med_list) {
if(med %in% get(paste0("key_",key))) {
recode_CMDRGSYN[,key] <- recode_CMDRGSYN[,key]+recode_CMDRGSYN[,med]
temp <- recode_CMDRGSYN[,med]==T
recode_CMDRGSYN[temp, "CMDRGSYN"] <- paste(recode_CMDRGSYN[temp, "CMDRGSYN"],med,sep="_")
rm(temp)
}
}
}
recode_CMDRGSYN$CMDRGSYN<-substr(recode_CMDRGSYN$CMDRGSYN,2,30)
### How many flags does each prescription have?
recode_CMDRGSYN$flag_sum<-0
for (key in key_list) {
temp <- recode_CMDRGSYN[,key]==T
recode_CMDRGSYN[temp, "flag_sum"] <- recode_CMDRGSYN[temp, "flag_sum"]+1
rm(temp)
}
# Drop the ones with no flags
recode_CMDRGSYN<-recode_CMDRGSYN[which(recode_CMDRGSYN$flag_sum!=0),]
### code the drug class in cases with only one flag
recode_CMDRGSYN$drug_class<-NA
for (key in key_list) {
temp <- recode_CMDRGSYN[,key]==T & recode_CMDRGSYN$flag_sum==1
recode_CMDRGSYN[temp, "drug_class"] <- key
rm(temp)
}
# Drop records based on keywords
keywords<-c("NASAL","NOSE","NOSTRIL","NASULE","HAYFEVER",
"EYE","EAR","DROP","TONGUE",
"FOAM","ENEMA","RECTAL",
"GASTRO","MODIFIED",
"CREAM", "APPLY","SKIN","ULCER","OINTMENT","PATCH",
"CAPSULE", "SACHET", "SPRAY",
"AZELASTINE","NASONEX","FLIXONASE","ANORA ELLIPTA",
"SUMATRIPTAN","AVAMYS","RHINOCORT","NASOBEC","NASOFAN")
for (keyword in keywords) {
recode_CMDRGSYN<-recode_CMDRGSYN[which(str_detect(recode_CMDRGSYN$DRUGDESC,keyword)==F |
(recode_CMDRGSYN$drug_class %in% c("steroid","theophyllines") &
keyword %in% c("GASTRO","MODIFIED","CAPSULE")) |
(recode_CMDRGSYN$CMDRGSYN %in% c("tiotropium","glycopyrronium_bromide") &
keyword=="CAPSULE")),]
}
recode_CMDRGSYN<-recode_CMDRGSYN[,c("DRUGDESC","CMDRGSYN")]
PRESCR1F_asthma<-inner_join(dplyr::select(PRESCR1F_asthma,-c("CMDRGSYN")),recode_CMDRGSYN)
DISPENSF_asthma<-inner_join(dplyr::select(DISPENSF_asthma,-c("CMDRGSYN")),recode_CMDRGSYN)
###################################################################
# Benchmark matching
###################################################################
DISPENSF_asthma<-DISPENSF_asthma %>%
dplyr::select(SUBJID,CMDRGSYN,DISPDT,DOSETX,QUANTITY,DOSEDIRC) %>%
mutate(DISPDT=as.Date(DISPDT,"%d%b%Y")) %>%
arrange(SUBJID,CMDRGSYN,DISPDT) %>%
group_by(SUBJID,CMDRGSYN) %>%
mutate(DID = row_number())
PRESCR1F_asthma<-PRESCR1F_asthma %>%
dplyr::select(SUBJID,CMDRGSYN,PRESCDT,DOSETX,QUANTITY,DOSEDIRC) %>%
mutate(PRESCDT=as.Date(PRESCDT,"%d%b%Y")) %>%
arrange(SUBJID,CMDRGSYN,PRESCDT) %>%
group_by(SUBJID,CMDRGSYN) %>%
mutate(PID = row_number())
rm(list=setdiff(ls(),c("DISPENSF_asthma","PRESCR1F_asthma",ls()[which(substr(ls(),1,5)=="check")])))
merge_benchmark<-merge(DISPENSF_asthma, PRESCR1F_asthma, all = TRUE)
merge_benchmark<-merge_benchmark[which(!is.na(merge_benchmark$DISPDT) & !is.na(merge_benchmark$PRESCDT)),]
merge_benchmark<-merge_benchmark[which(merge_benchmark$DISPDT>=merge_benchmark$PRESCDT),]
merge_benchmark<-merge_benchmark[which((merge_benchmark$PRESCDT %m+% months(6))>=merge_benchmark$DISPDT),]
# matching
for (i in unique(merge_benchmark$DID)) {
merge_benchmark<-merge_benchmark %>%
group_by(SUBJID,CMDRGSYN,DID) %>%
mutate(match = ifelse(DID==i,PID,0)) %>%
group_by(SUBJID,CMDRGSYN) %>%
mutate(flag = max(match))
# Get rid of the unmatched records for the prescription we just matched,
# and the unmatched records for the dispensing we just matched
merge_benchmark<-merge_benchmark[which((merge_benchmark$DID==i & merge_benchmark$PID==merge_benchmark$flag) |
(merge_benchmark$DID!=i & merge_benchmark$PID!=merge_benchmark$flag)),]
merge_benchmark<-dplyr::select(merge_benchmark,-c("match","flag"))
}
# Second version, without dose directions
DISPENSF_asthma<-DISPENSF_asthma %>%
rename(DOSEDIRC_d=DOSEDIRC)
merge_benchmark2<-merge(DISPENSF_asthma, PRESCR1F_asthma, all = TRUE)
merge_benchmark2<-merge_benchmark2[which(!is.na(merge_benchmark2$DISPDT) & !is.na(merge_benchmark2$PRESCDT)),]
merge_benchmark2<-merge_benchmark2[which(merge_benchmark2$DISPDT>=merge_benchmark2$PRESCDT),]
merge_benchmark2<-merge_benchmark2[which((merge_benchmark2$PRESCDT %m+% months(6))>=merge_benchmark2$DISPDT),]
# matching
for (i in unique(merge_benchmark2$DID)) {
merge_benchmark2<-merge_benchmark2 %>%
group_by(SUBJID,CMDRGSYN,DID) %>%
mutate(match = ifelse(DID==i,PID,0)) %>%
group_by(SUBJID,CMDRGSYN) %>%
mutate(flag = max(match))
# Get rid of the unmatched records for the prescription we just matched,
# and the unmatched records for the dispensing we just matched
merge_benchmark2<-merge_benchmark2[which((merge_benchmark2$DID==i & merge_benchmark2$PID==merge_benchmark2$flag) |
(merge_benchmark2$DID!=i & merge_benchmark2$PID!=merge_benchmark2$flag)),]
merge_benchmark2<-dplyr::select(merge_benchmark2,-c("match","flag"))
}
prescriptions<-nrow(PRESCR1F_asthma)
dispensings<-nrow(DISPENSF_asthma)
# Third version, complete cases only
DISPENSF_asthma<-DISPENSF_asthma[complete.cases(DISPENSF_asthma),] %>%
group_by(SUBJID,CMDRGSYN) %>%
mutate(DID = row_number())
PRESCR1F_asthma<-PRESCR1F_asthma[complete.cases(PRESCR1F_asthma),] %>%
group_by(SUBJID,CMDRGSYN) %>%
mutate(PID = row_number())
merge_benchmark3<-merge(DISPENSF_asthma, PRESCR1F_asthma, all = TRUE)
merge_benchmark3<-merge_benchmark3[which(!is.na(merge_benchmark3$DISPDT) & !is.na(merge_benchmark3$PRESCDT)),]
merge_benchmark3<-merge_benchmark3[which(merge_benchmark3$DISPDT>=merge_benchmark3$PRESCDT),]
merge_benchmark3<-merge_benchmark3[which((merge_benchmark3$PRESCDT %m+% months(6))>=merge_benchmark3$DISPDT),]
# matching
for (i in unique(merge_benchmark3$DID)) {
merge_benchmark3<-merge_benchmark3 %>%
group_by(SUBJID,CMDRGSYN,DID) %>%
mutate(match = ifelse(DID==i,PID,0)) %>%
group_by(SUBJID,CMDRGSYN) %>%
mutate(flag = max(match))
# Get rid of the unmatched records for the prescription we just matched,
# and the unmatched records for the dispensing we just matched
merge_benchmark3<-merge_benchmark3[which((merge_benchmark3$DID==i & merge_benchmark3$PID==merge_benchmark3$flag) |
(merge_benchmark3$DID!=i & merge_benchmark3$PID!=merge_benchmark3$flag)),]
merge_benchmark3<-dplyr::select(merge_benchmark3,-c("match","flag"))
}
prescriptions_cc<-nrow(PRESCR1F_asthma)
dispensings_cc<-nrow(DISPENSF_asthma)
first_benchmark<-nrow(merge_benchmark)
second_benchmark<-nrow(merge_benchmark2)
third_benchmark<-nrow(merge_benchmark3)
rm(list=setdiff(ls(),c("prescriptions","dispensings","prescriptions_cc","dispensings_cc",
"first_benchmark","second_benchmark","third_benchmark",
ls()[which(substr(ls(),1,5)=="check")])))
# making sure I don't overwrite the actual data from James!
save.image("results_version_final_benchmarking_internal.RData")