I'm working with a large dataset of drugs and reactions using R. For now, I have the data structured as a very tall data frame that lists the report ID number, the Drug name, and the reported reactions. As you can tell, there is a one-to-many relationship between both IDs vs. drugs and drugs vs. reactions.
Keeping in mind that this dataset is MUCH larger than what I can duplicate here, I'd like to know how to find what pairs of drugs lead to what reactions and in what frequency .
Most importantly, I am interested in how to approach a problem like this. Is the data structured correctly? What concepts or libraries should I read about?
Here's a link to some real data: https://www.dropbox.com/s/kzx4mpyytbo9zil/query_result.csv
ID DRUG REACTION
1 1827 ASPIRIN CHEST PAIN
2 1827 CLARINEX CHEST PAIN
3 1827 ASPIRIN COUGH
4 1827 CLARINEX COUGH
5 1827 ASPIRIN HAEMOGLOBIN DECREASED
6 1827 CLARINEX HAEMOGLOBIN DECREASED
7 1827 ASPIRIN NEUTROPHIL COUNT INCREASED
8 1827 CLARINEX NEUTROPHIL COUNT INCREASED
9 1827 ASPIRIN PHARYNGOLARYNGEAL PAIN
10 1827 CLARINEX PHARYNGOLARYNGEAL PAIN
...
In my teeny little brain, the end result looks something like this...
Drug1 Drug2 Reaction Frequency
1 tylenol alcohol hepatic failure 298
2 advil aleve bleeding 201
3 aspirin advil renal failure 199
4 docusate senna diarrhea 146
5 senna sudafed palpitations 121
6 xanax alcohol sedation 111
7 clarinex benadryl dry mouth 96
...
569 ASPIRIN CLARINEX CHEST PAIN 2
Drug1 and Drug2 are the drug pairs with the highest frequency from the entire dataset. A "drug pair" is defined as any combination of two drugs with the same report ID. The example output above would be interpreted as, "row 1 had 298 unique report IDs for which hepatic failure was the reaction."
Ok, I try an answer - I hope I got the question correctly. The code is rather intended to give some ideas than to be elegant/final.
Please note: I intentionally used for loops instead of possible vectorisation / apply functions, to make it easier to understand (those who are familiar with apply functions will also undertand the for loop ;-)).
Please note 2: Since I don't have more than a tiny piece of data, I could not test the code for the whole dataset!
EDIT : columns based on example above - possibly different from csv data.
Key points are:
unique
, [
etc. utils::combn
to get combinations Hope that helps!
require(utils)
df <- read.table(header=TRUE,
text="LINE ID DRUG REACTION
1 1827 ASPIRIN CHEST_PAIN
2 1827 CLARINEX CHEST_PAIN
3 1827 ASPIRIN COUGH
4 1827 CLARINEX COUGH
5 1827 ASPIRIN HAEMOGLOBIN_DECREASED
6 1827 CLARINEX HAEMOGLOBIN_DECREASED
7 1827 ASPIRIN NEUTROPHIL_COUNT_INCREASED
8 1827 CLARINEX NEUTROPHIL_COUNT_INCREASED
9 1827 ASPIRIN PHARYNGOLARYNGEAL_PAIN
10 1827 CLARINEX PHARYNGOLARYNGEAL_PAIN")
# temporary object to collect if a combination is present
Results <- data.frame(Drug1=NA, Drug2=NA, Reaction=NA, Reaction.occurs=NA)
n=1 # start first line in Results object
# walk through each ID ...
for (ID in unique(df$ID)) {
# ... and each possible pair of drugs within a (report) ID ...
drug.pairs <- utils::combn(x=unique(df[df$ID == ID, "DRUG"]), m=2) # the columns
for (ii in 1:ncol(drug.pairs)) {
# ... and each reaction ...
for (reaction in unique(df$REACTION)) {
Results[n, "Drug1"] <- drug.pairs[1,ii]
Results[n, "Drug2"] <- drug.pairs[2,ii]
Results[n, "Reaction"] <- reaction
Results[n, "Reaction.occurs"] <- drug.pairs[1,ii] %in% df[df$REACTION == reaction & df$ID == ID, "DRUG"] &
drug.pairs[2,ii] %in% df[df$REACTION == reaction & df$ID == ID, "DRUG"]
n <- n+1
}
}
}
head(Results)
# then find the unique Drug1 - Drug2 -Reaction combinations, and count the TRUE values:
(Results[!duplicated(Results[,1:3]), ][,1:3])
(unique(Results[, 1:3]))
# Results2 contains only the unique combinations
Results2 <- Results[!duplicated(Results[,1:3]), ][,1:3]
# calculatethe frequencies
for (i in 1:nrow(Results2)) {
Results2[i, "Frequency"] <- sum(Results[Results$Drug1 == Results2[i, "Drug1"] &
Results$Drug2 == Results2[i, "Drug2"] &
Results$Reaction == Results2[i, "Reaction"], ]$Reaction.occurs)
}
Results2
# --- end ----
gives:
Drug1 Drug2 Reaction Frequency
1 ASPIRIN CLARINEX CHEST_PAIN 1
2 ASPIRIN CLARINEX COUGH 1
3 ASPIRIN CLARINEX HAEMOGLOBIN_DECREASED 1
4 ASPIRIN CLARINEX NEUTROPHIL_COUNT_INCREASED 1
5 ASPIRIN CLARINEX PHARYNGOLARYNGEAL_PAIN 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.