library(openfda)
patient_ages = fda_query("/drug/event.json") %>%
fda_count("patient.patientonsetage") %>%
fda_exec()
Fetching: https://api.fda.gov/drug/event.json?search=&count=patient.patientonsetage
head(patient_ages)
term | count | |
---|---|---|
<int> | <int> | |
1 | 60 | 165574 |
2 | 65 | 158855 |
3 | 62 | 155215 |
4 | 63 | 153591 |
5 | 64 | 152798 |
6 | 61 | 150172 |
library(ggplot2)
# let's plot it with ggplot2
ggplot(data = patient_ages) +
geom_point(mapping = aes(x=term, y=count))
paxil_ages = fda_query("/drug/event.json") %>%
fda_filter("patient.drug.openfda.generic_name", "paroxetine") %>%
fda_count("patient.patientonsetage") %>%
fda_exec()
Fetching: https://api.fda.gov/drug/event.json?search=patient.drug.openfda.generic_name:paroxetine&count=patient.patientonsetage
# let's plot it with ggplot2
ggplot(data = paxil_ages) +
geom_point(mapping = aes(x=term, y=count))
library(plyr)
my_query = fda_query("/drug/event.json")
agesm = ldply(c('PREDNISONE', 'Hydroxychloroquine', 'Azathioprine'), function(drug) {
dfm = my_query %>%
fda_filter("patient.patientsex","2") %>%
fda_filter("patient.drug.openfda.generic_name", drug) %>%
fda_filter("receivedate", "[20040101+TO+20160101]") %>%
fda_filter("patient.drug.drugindication", "%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22") %>%
fda_filter("patient.patientonsetage", "[50+TO+60]") %>%
fda_count("patient.reaction.reactionmeddrapt.exact") %>%
fda_limit(5)%>%
fda_exec()
dfm$drug = drug
dfm
})
Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:PREDNISONE+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:Hydroxychloroquine+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:Azathioprine+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact
head(agesm)
term | count | drug | |
---|---|---|---|
<chr> | <int> | <chr> | |
1 | FATIGUE | 44 | PREDNISONE |
2 | PAIN | 38 | PREDNISONE |
3 | HEADACHE | 37 | PREDNISONE |
4 | SYSTEMIC LUPUS ERYTHEMATOSUS | 32 | PREDNISONE |
5 | NAUSEA | 31 | PREDNISONE |
6 | PAIN | 48 | Hydroxychloroquine |
# let's plot it with ggplot2
ggplot(data = agesm) +
geom_point(mapping = aes(x=term, y=count))
# ggplot(data = agesm) + geom_bar(aes(x=term, y=count, fill = drug))
ggplot(agesm, aes(x=term, y= count)) + geom_bar(stat='identity')
ggplot(data = agesm, aes(x = term, y = count, fill = drug )) +
geom_bar(stat = "identity", width = 0.4,
position=position_dodge(width = 0.5))+
theme_bw()
ggplot(data = agesm, aes(x = term, y = count, fill = drug )) +
geom_bar(stat = "identity", width = 0.4,
position=position_dodge(width = 0.5))+
theme_bw() +
theme(axis.text.x = element_text(angle = 90)) # Rotate axis labels
my_query = fda_query("/drug/event.json")
agesm1 = ldply(c('esomeprazole', 'lansoprazole', 'omeprazole', 'pantoprazole', 'rabeprazole'), function(drug) {
dfm = my_query %>%
fda_filter("patient.patientsex","2") %>%
fda_filter("patient.drug.openfda.generic_name", drug) %>%
fda_filter("receivedate", "[20040101+TO+20160101]") %>%
fda_filter("patient.drug.drugindication", "%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22") %>%
fda_filter("patient.patientonsetage", "[50+TO+60]") %>%
fda_count("patient.reaction.reactionmeddrapt.exact") %>%
fda_limit(5)%>%
fda_exec()
dfm$drug = drug
dfm
})
Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:esomeprazole+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:lansoprazole+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:omeprazole+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:pantoprazole+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:rabeprazole+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact
head(agesm1)
term | count | drug | |
---|---|---|---|
<chr> | <int> | <chr> | |
1 | NAUSEA | 12 | esomeprazole |
2 | HEADACHE | 10 | esomeprazole |
3 | FATIGUE | 9 | esomeprazole |
4 | ARTHRALGIA | 8 | esomeprazole |
5 | DRUG INEFFECTIVE | 8 | esomeprazole |
6 | C-REACTIVE PROTEIN INCREASED | 5 | lansoprazole |
ggplot(data = agesm1, aes(x = term, y = count, fill = drug )) +
geom_bar(stat = "identity", width = 0.4,
position=position_dodge(width = 0.5))+
theme_bw() +
theme(axis.text.x = element_text(angle = 90)) # Rotate axis labels
FDAquery¶
This code queries the FDA api based upon user search terms and cross correlates with google trends and twitter posts. Goal is to create a dashboard showing potential clusters of drug side effects. Probably won't work...but the google trends show some seriously interesting cyclical data for certain conditions.....
library("openfda")
patient_ages = fda_query("/drug/event.json") %>%
fda_count("patient.patientonsetage") %>%
fda_exec()
Fetching: https://api.fda.gov/drug/event.json?search=&count=patient.patientonsetage
library("ggplot2")
qplot(x=term, y=count, data=patient_ages)
paxil_ages = fda_query("/drug/event.json") %>%
fda_filter("patient.drug.openfda.generic_name", "paroxetine") %>%
fda_count("patient.patientonsetage") %>%
fda_exec()
Fetching: https://api.fda.gov/drug/event.json?search=patient.drug.openfda.generic_name:paroxetine&count=patient.patientonsetage
age_query = fda_query("/drug/event.json") %>%
# fda_api_key("MYKEY") %>%
fda_count("patient.patientonsetage");
paxil_ages = age_query %>% fda_filter("patient.drug.openfda.generic_name", "paroxetine") %>% fda_exec()
zoloft_ages = age_query %>% fda_filter("patient.drug.openfda.generic_name", "sertraline") %>% fda_exec()
qplot(x=term, y=count, data=paxil_ages)
qplot(x=term, y=count, data=zoloft_ages)
Fetching: https://api.fda.gov/drug/event.json?search=patient.drug.openfda.generic_name:paroxetine&count=patient.patientonsetage Fetching: https://api.fda.gov/drug/event.json?search=patient.drug.openfda.generic_name:sertraline&count=patient.patientonsetage
library("openfda")
library(ggplot2)
library("plyr")
patient_ages = fda_query("/drug/event.json") %>%
fda_count("patient.patientonsetage") %>%
fda_exec()
ggplot(data = patient_ages) +
geom_point(mapping = aes(x=term, y=count))
Fetching: https://api.fda.gov/drug/event.json?search=&count=patient.patientonsetage
my_query = fda_query("/drug/event.json")
agesm = ldply(c('PREDNISONE', 'Hydroxychloroquine', 'Azathioprine'), function(drug) {
dfm = my_query %>%
fda_filter("patient.patientsex","2") %>%
fda_filter("patient.drug.openfda.generic_name", drug) %>%
fda_filter("receivedate", "[20040101+TO+20160101]") %>%
fda_filter("patient.drug.drugindication", "%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22") %>%
fda_filter("patient.patientonsetage", "[50+TO+60]") %>%
fda_count("patient.reaction.reactionmeddrapt.exact") %>%
fda_limit(5)%>%
fda_exec()
dfm$drug = drug
dfm
})
Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:PREDNISONE+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:Hydroxychloroquine+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact Fetching: https://api.fda.gov/drug/event.json?search=patient.patientsex:2+AND+patient.drug.openfda.generic_name:Azathioprine+AND+receivedate:[20040101+TO+20160101]+AND+patient.drug.drugindication:%22SYSTEMIC+LUPUS+ERYTHEMATOSUS%22+AND+patient.patientonsetage:[50+TO+60]&limit=5&count=patient.reaction.reactionmeddrapt.exact
p <- ggplot(data = agesm, aes(x = term, y = count, fill = drug)) + geom_bar(stat="identity", position=position_dodge()) + theme(axis.text = element_text(vjust = 0, angle = -15), legend.background = element_rect(fill = "gray96"))
p
02_GGplot2_adverse_events_plots.R¶
library(stringr)
library(openfda)
library(dplyr)
library(ggplot2)
library(ggthemes)
Attaching package: ‘dplyr’ The following objects are masked from ‘package:plyr’: arrange, count, desc, failwith, id, mutate, rename, summarise, summarize The following objects are masked from ‘package:stats’: filter, lag The following objects are masked from ‘package:base’: intersect, setdiff, setequal, union
# helper functions to query the openFDA API
get_adverse <- function(gender, brand_name, age) {
fda_query("/drug/event.json") %>%
fda_filter("patient.drug.openfda.brand_name", brand_name) %>%
fda_filter("patient.patientsex", gender) %>%
fda_filter("patient.patientonsetage", age) %>%
fda_count("patient.reaction.reactionmeddrapt.exact") %>%
fda_limit(10) %>%
fda_exec()
}
create_age <- function(min, max){#
sprintf('[%d+TO+%d]', min, max)
}
# specify drug
drug <- "Tylenol"
# specify age range
age <- create_age(20,65)
# get adverse event data from the openfda API
jnk <- capture.output(male <- get_adverse("1", drug, age))
if (!is.null(male)) {
male$gender <- 'male'
}
jnk <- capture.output(female <- get_adverse("1", drug, age))
if (!is.null(female)) {
female$gender <- 'female'
}
# comnbine male and female event data
adverse <- rbind(male, female)
head(adverse)
term | count | gender | |
---|---|---|---|
<chr> | <int> | <chr> | |
1 | FATIGUE | 1363 | male |
2 | NAUSEA | 1334 | male |
3 | TOXICITY TO VARIOUS AGENTS | 1326 | male |
4 | COMPLETED SUICIDE | 1309 | male |
5 | PAIN | 1249 | male |
6 | PYREXIA | 1219 | male |
# plot all events
adverse %>%
group_by(term) %>%
summarise(count = sum(count)) %>%
ggplot() +
geom_bar(aes(reorder(term,count), count), stat = 'identity') +
coord_flip() +
labs(
title = drug,
x = NULL,
y = NULL
) +
theme_minimal()
# plot by gender
ggplot(adverse) +
geom_bar(aes(reorder(term,count), count, fill = gender), stat = 'identity') +
facet_wrap(~gender)+
coord_flip() +
labs(
title = drug,
x = NULL,
y = NULL
) +
theme_minimal() +
guides(fill = FALSE) +
scale_fill_manual(values = c("#d54a30","#4c83b6"))
Không có nhận xét nào :
Đăng nhận xét