diff --git a/DESCRIPTION b/DESCRIPTION index 4784b50f..a0092870 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 0.5.0.9011 -Date: 2019-01-21 +Version: 0.5.0.9012 +Date: 2019-01-25 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/NEWS.md b/NEWS.md index fe9f3c73..50dcadd1 100755 --- a/NEWS.md +++ b/NEWS.md @@ -35,6 +35,8 @@ * New vignettes about how to conduct AMR analysis, predict antimicrobial resistance, use the *G*-test and more. These are also available (and even easier readable) on our website: https://msberends.gitlab.io/AMR. #### Changed +* Added 65 antibiotics to the `antibiotics` data set, from the [Pharmaceuticals Community Register](http://ec.europa.eu/health/documents/community-register/html/atc.htm) of the European Commission +* Removed columns `atc_group1_nl` and `atc_group2_nl` from the `antibiotics` data set * Function `eucast_rules()`: * Updated EUCAST Clinical breakpoints to [version 9.0 of 1 January 2019](http://www.eucast.org/clinical_breakpoints/) * Fixed a critical bug where some rules that depend on previous applied rules would not be applied adequately @@ -49,6 +51,7 @@ * Manual now contains more info about the algorithms * Progress bar will be shown when it takes more than 3 seconds to get results * Support for formatted console text + * Console will return the percentage of uncoercable input * Function `first_isolate()`: * Fixed a bug where distances between dates would not be calculated right - in the `septic_patients` data set this yielded a difference of 0.15% more isolates * Will now use a column named like "patid" for the patient ID (parameter `col_patientid`), when this parameter was left blank diff --git a/R/abname.R b/R/abname.R index ff868239..a561ae68 100755 --- a/R/abname.R +++ b/R/abname.R @@ -29,6 +29,7 @@ #' @details \strong{The \code{\link{ab_property}} functions are faster and more concise}, but do not support concatenated strings, like \code{abname("AMCL+GENT"}. #' @keywords ab antibiotics #' @source \code{\link{antibiotics}} +#' @inheritSection WHOCC WHOCC #' @export #' @importFrom dplyr %>% pull #' @inheritSection AMR Read more on our website! diff --git a/R/age.R b/R/age.R index 265b1c9e..421f9626 100755 --- a/R/age.R +++ b/R/age.R @@ -29,6 +29,9 @@ #' @importFrom dplyr if_else #' @inheritSection AMR Read more on our website! #' @export +#' @examples +#' df <- data.frame(birth_date = Sys.Date() - runif(100) * 25000) +#' df$age <- age(df$birth_date) age <- function(x, reference = Sys.Date()) { if (length(x) != length(reference)) { if (length(reference) == 1) { @@ -39,17 +42,21 @@ age <- function(x, reference = Sys.Date()) { } x <- base::as.POSIXlt(x) reference <- base::as.POSIXlt(reference) - if (any(reference < x)) { - stop("`reference` cannot be lower (older) than `x`.") - } - years_gap <- reference$year - x$year + # from https://stackoverflow.com/a/25450756/4575331 + years_gap <- reference$year - x$year ages <- if_else(reference$mon < x$mon | (reference$mon == x$mon & reference$mday < x$mday), as.integer(years_gap - 1), as.integer(years_gap)) - if (any(ages > 120)) { + + if (any(ages < 0, na.rm = TRUE)) { + warning("NAs introduced for ages below 0.") + ages[ages < 0] <- NA_integer_ + } + if (any(ages > 120, na.rm = TRUE)) { warning("Some ages are > 120.") } + ages } diff --git a/R/atc.R b/R/atc.R index 672b5226..23cd1d1d 100755 --- a/R/atc.R +++ b/R/atc.R @@ -26,6 +26,7 @@ #' @rdname as.atc #' @aliases atc #' @keywords atc +#' @inheritSection WHOCC WHOCC #' @export #' @importFrom dplyr %>% filter slice pull #' @details Use the \code{\link{ab_property}} functions to get properties based on the returned ATC code, see Examples. diff --git a/R/data.R b/R/data.R index 2b2838f6..3793064e 100755 --- a/R/data.R +++ b/R/data.R @@ -19,10 +19,10 @@ # Visit our website for more info: https://msberends.gitab.io/AMR. # # ==================================================================== # -#' Data set with 423 antibiotics +#' Data set with ~500 antibiotics #' #' A data set containing all antibiotics with a J0 code and some other antimicrobial agents, with their DDDs. Except for trade names and abbreviations, all properties were downloaded from the WHO, see Source. -#' @format A \code{\link{data.frame}} with 423 observations and 18 variables: +#' @format A \code{\link{data.frame}} with 488 observations and 16 variables: #' \describe{ #' \item{\code{atc}}{ATC code, like \code{J01CR02}} #' \item{\code{certe}}{Certe code, like \code{amcl}} @@ -38,12 +38,14 @@ #' \item{\code{iv_units}}{Units of \code{iv_ddd}} #' \item{\code{atc_group1}}{ATC group, like \code{"Macrolides, lincosamides and streptogramins"}} #' \item{\code{atc_group2}}{Subgroup of \code{atc_group1}, like \code{"Macrolides"}} -#' \item{\code{atc_group1_nl}}{ATC group in Dutch, like \code{"Macroliden, lincosamiden en streptograminen"}} -#' \item{\code{atc_group2_nl}}{Subgroup of \code{atc_group1} in Dutch, like \code{"Macroliden"}} #' \item{\code{useful_gramnegative}}{\code{FALSE} if not useful according to EUCAST, \code{NA} otherwise (see Source)} #' \item{\code{useful_grampositive}}{\code{FALSE} if not useful according to EUCAST, \code{NA} otherwise (see Source)} #' } -#' @source - World Health Organization: \url{https://www.whocc.no/atc_ddd_index/} \cr - EUCAST - Expert rules intrinsic exceptional V3.1 \cr - MOLIS (LIS of Certe): \url{https://www.certe.nl} \cr - GLIMS (LIS of UMCG): \url{https://www.umcg.nl} +#' @source - World Health Organization (WHO) Collaborating Centre for Drug Statistics Methodology: \url{https://www.whocc.no/atc_ddd_index/} +#' +#' EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes Tables. Version 3.1, 2016: \url{http://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/Expert_rules_intrinsic_exceptional_V3.1.pdf} +#' +#' European Commission Public Health PHARMACEUTICALS - COMMUNITY REGISTER: \url{http://ec.europa.eu/health/documents/community-register/html/atc.htm} #' @inheritSection AMR Read more on our website! #' @seealso \code{\link{microorganisms}} # use this later to further fill AMR::antibiotics @@ -124,9 +126,9 @@ # "antibiotics" -#' Data set with taxonomic data from ITIS +#' Data set with ~20,000 microorganisms #' -#' A data set containing the complete microbial taxonomy of the kingdoms Bacteria, Fungi and Protozoa. MO codes can be looked up using \code{\link{as.mo}}. +#' A data set containing the complete microbial taxonomy of the kingdoms Bacteria, Fungi and Protozoa from ITIS. MO codes can be looked up using \code{\link{as.mo}}. #' @inheritSection ITIS ITIS #' @format A \code{\link{data.frame}} with 18,833 observations and 15 variables: #' \describe{ @@ -146,12 +148,12 @@ #' \item{\code{prevalence}}{An integer based on estimated prevalence of the microorganism in humans. Used internally by \code{\link{as.mo}}, otherwise quite meaningless. It has a value of 25 for manually added items and a value of 1000 for all unprevalent microorganisms whose genus was somewhere in the top 250 (with another species).} #' \item{\code{ref}}{Author(s) and year of concerning publication as found in ITIS, see Source} #' } -#' @source [3] Integrated Taxonomic Information System (ITIS) on-line database, \url{https://www.itis.gov}. +#' @source Integrated Taxonomic Information System (ITIS) public online database, \url{https://www.itis.gov}. #' @inheritSection AMR Read more on our website! #' @seealso \code{\link{as.mo}} \code{\link{mo_property}} \code{\link{microorganisms.codes}} "microorganisms" -#' Data set with old taxonomic data from ITIS +#' Data set with previously accepted taxonomic names #' #' A data set containing old (previously valid or accepted) taxonomic names according to ITIS. This data set is used internally by \code{\link{as.mo}}. #' @inheritSection as.mo ITIS @@ -179,7 +181,7 @@ #' @seealso \code{\link{as.mo}} \code{\link{microorganisms}} "microorganisms.codes" -#' Data set with 2000 blood culture isolates of septic patients +#' Data set with 2,000 blood culture isolates from septic patients #' #' An anonymised data set containing 2,000 microbial blood culture isolates with their full antibiograms found in septic patients in 4 different hospitals in the Netherlands, between 2001 and 2017. It is true, genuine data. This \code{data.frame} can be used to practice AMR analysis. For examples, please read \href{https://msberends.gitlab.io/AMR/articles/AMR.html}{the tutorial on our website}. #' @format A \code{\link{data.frame}} with 2,000 observations and 49 variables: diff --git a/R/eucast_rules.R b/R/eucast_rules.R index 459db3cf..059c1604 100755 --- a/R/eucast_rules.R +++ b/R/eucast_rules.R @@ -222,7 +222,7 @@ eucast_rules <- function(tbl, trsu = guess_ab_col(), vanc = guess_ab_col()) { - EUCAST_VERSION_BREAKPOINTS <- "8.1, 2018" + EUCAST_VERSION_BREAKPOINTS <- "9.0, 2019" EUCAST_VERSION_EXPERT_RULES <- "3.1, 2016" if (!is.data.frame(tbl)) { diff --git a/R/itis.R b/R/itis.R index f1e74752..d8f189cd 100755 --- a/R/itis.R +++ b/R/itis.R @@ -23,10 +23,10 @@ #' #' All taxonomic names of all microorganisms are included in this package, using the authoritative Integrated Taxonomic Information System (ITIS). #' @section ITIS: -#' \if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr} +#' \if{html}{\figure{logo_itis.jpg}{options: height=60px style=margin-bottom:5px} \cr} #' This package contains the \strong{complete microbial taxonomic data} (with all nine taxonomic ranks - from kingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, \url{https://www.itis.gov}). #' -#' All ~20,000 (sub)species from \strong{the taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package}, as well as all ~2,500 previously accepted names known to ITIS. Furthermore, the responsible authors and year of publication are available. This allows users to use authoritative taxonomic information for their data analysis on any microorganism, not only human pathogens. It also helps to quickly determine the Gram stain of bacteria, since all bacteria are classified into subkingdom Negibacteria or Posibacteria. +#' All ~20,000 (sub)species from \strong{the taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package}, as well as all their ~2,500 previously accepted names known to ITIS. Furthermore, the responsible authors and year of publication are available. This allows users to use authoritative taxonomic information for their data analysis on any microorganism, not only human pathogens. It also helps to quickly determine the Gram stain of bacteria, since all bacteria are classified into subkingdom Negibacteria or Posibacteria. #' #' ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. #' @inheritSection AMR Read more on our website! diff --git a/R/mo.R b/R/mo.R index e38004a3..22d78454 100755 --- a/R/mo.R +++ b/R/mo.R @@ -707,7 +707,11 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, if (n_distinct(failures) > 1) { plural <- "s" } - msg <- paste0("\n", n_distinct(failures), " unique value", plural, " could not be coerced to a valid MO code") + total_failures <- length(x_input[x_input %in% failures & !x_input %in% c(NA, NULL, NaN)]) + total_n <- length(x_input[!x_input %in% c(NA, NULL, NaN)]) + msg <- paste0("\n", n_distinct(failures), " unique value", plural, + " (^= ", percent(total_failures / total_n, round = 1, force_zero = TRUE), + ") could not be coerced to a valid MO code") if (n_distinct(failures) <= 10) { msg <- paste0(msg, ": ", paste('"', unique(failures), '"', sep = "", collapse = ', ')) } diff --git a/R/read.4d.R b/R/read.4d.R index c7b792ec..d6f7bb4e 100755 --- a/R/read.4d.R +++ b/R/read.4d.R @@ -124,7 +124,6 @@ read.4D <- function(file, } if ("date_birth" %in% colnames(data_4D)) { data_4D$date_birth <- to_date_4D(data_4D$date_birth) - } if ("date_received" %in% colnames(data_4D)) { data_4D$date_received <- to_date_4D(data_4D$date_received) diff --git a/R/whocc.R b/R/whocc.R new file mode 100755 index 00000000..96200cd9 --- /dev/null +++ b/R/whocc.R @@ -0,0 +1,40 @@ +# ==================================================================== # +# TITLE # +# Antimicrobial Resistance (AMR) Analysis # +# # +# SOURCE # +# https://gitlab.com/msberends/AMR # +# # +# LICENCE # +# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) # +# # +# This R package is free software; you can freely use and distribute # +# it for both personal and commercial purposes under the terms of the # +# GNU General Public License version 2.0 (GNU GPL-2), as published by # +# the Free Software Foundation. # +# # +# This R package was created for academic research and was publicly # +# released in the hope that it will be useful, but it comes WITHOUT # +# ANY WARRANTY OR LIABILITY. # +# Visit our website for more info: https://msberends.gitab.io/AMR. # +# ==================================================================== # + +#' WHO Collaborating Centre for Drug Statistics Methodology +#' +#' All antimicrobial drugs and their official names, ATC codes, ATC groups and defined daily dose (DDD) are included in this package, using the WHO Collaborating Centre for Drug Statistics Methodology. +#' @section WHOCC: +#' \if{html}{\figure{logo_who.png}{options: height=60px style=margin-bottom:5px} \cr} +#' This package contains \strong{all ~500 antimicrobial drugs and their Anatomical Therapeutic Chemical (ATC) codes, ATC groups and Defined Daily Dose (DDD)} from the World Health Organization Collaborating Centre for Drug Statistics Methodology (WHOCC, \url{https://www.whocc.no}) and the Pharmaceuticals Community Register of the European Commission (\url{http://ec.europa.eu/health/documents/community-register/html/atc.htm}). +#' +#' These have become the gold standard for international drug utilisation monitoring and research. +#' +#' The WHOCC is located in Oslo at the Norwegian Institute of Public Health and funded by the Norwegian government. The European Commission is the executive of the European Union and promotes its general interest. +#' @inheritSection AMR Read more on our website! +#' @name WHOCC +#' @rdname WHOCC +#' @examples +#' as.atc("meropenem") +#' ab_name("J01DH02") +#' +#' ab_tradenames("flucloxacillin") +NULL diff --git a/_pkgdown.yml b/_pkgdown.yml index db7d835f..8c169cd7 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -81,6 +81,7 @@ reference: contents: - '`AMR`' - '`ITIS`' + - '`WHOCC`' - title: 'Cleaning your data' desc: > Functions for cleaning and optimising your data, to be able to add diff --git a/data/antibiotics.rda b/data/antibiotics.rda index ce7b7d6b..f5015913 100755 Binary files a/data/antibiotics.rda and b/data/antibiotics.rda differ diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index a2bbd152..a3db6251 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -78,7 +78,7 @@
diff --git a/docs/articles/AMR.html b/docs/articles/AMR.html index 79ab6ab5..2929e674 100644 --- a/docs/articles/AMR.html +++ b/docs/articles/AMR.html @@ -40,7 +40,7 @@ @@ -178,7 +178,7 @@AMR.Rmd
Note: values on this page will change with every website update since they are based on randomly created values and the page was written in RMarkdown. However, the methodology remains unchanged. This page was generated on 12 January 2019.
+Note: values on this page will change with every website update since they are based on randomly created values and the page was written in RMarkdown. However, the methodology remains unchanged. This page was generated on 25 January 2019.
As with many uses in R, we need some additional packages for AMR analysis. The most important one is dplyr
, which tremendously improves the way we work with data - it allows for a very natural way of writing syntaxes in R. Another important dependency is ggplot2
. This package can be used to create beautiful plots in R.
Our AMR
package depends on these packages and even extends their use and functions.
library(dplyr) # the data science package
-library(AMR) # this package, to simplify and automate AMR analysis
-library(ggplot2) # for appealing plots
library(dplyr) # the data science package
+library(AMR) # this package, to simplify and automate AMR analysis
+library(ggplot2) # for appealing plots
To start with patients, we need a unique list of patients.
- +The LETTERS
object is available in R - it’s a vector with 26 characters: A
to Z
. The patients
object we just created is now a vector of length 260, with values (patient IDs) varying from A1
to Z10
. Now we we also set the gender of our patients, by putting the ID and the gender in a table:
patients_table <- data.frame(patient_id = patients,
- gender = c(rep("M", 135),
- rep("F", 125)))
The first 135 patient IDs are now male, the other 125 are female.
Let’s pretend that our data consists of blood cultures isolates from 1 January 2010 until 1 January 2018.
- +This dates
object now contains all days in our date range.
For this tutorial, we will uses four different microorganisms: Escherichia coli, Staphylococcus aureus, Streptococcus pneumoniae, and Klebsiella pneumoniae:
-bacteria <- c("Escherichia coli", "Staphylococcus aureus",
- "Streptococcus pneumoniae", "Klebsiella pneumoniae")
For completeness, we can also add the hospital where the patients was admitted and we need to define valid antibmicrobial results for our randomisation:
- +Using the sample()
function, we can randomly select items from all objects we defined earlier. To let our fake data reflect reality a bit, we will also approximately define the probabilities of bacteria and the antibiotic results with the prob
parameter.
data <- data.frame(date = sample(dates, 5000, replace = TRUE),
- patient_id = sample(patients, 5000, replace = TRUE),
- hospital = sample(hospitals, 5000, replace = TRUE, prob = c(0.30, 0.35, 0.15, 0.20)),
- bacteria = sample(bacteria, 5000, replace = TRUE, prob = c(0.50, 0.25, 0.15, 0.10)),
- amox = sample(ab_interpretations, 5000, replace = TRUE, prob = c(0.60, 0.05, 0.35)),
- amcl = sample(ab_interpretations, 5000, replace = TRUE, prob = c(0.75, 0.10, 0.15)),
- cipr = sample(ab_interpretations, 5000, replace = TRUE, prob = c(0.80, 0.00, 0.20)),
- gent = sample(ab_interpretations, 5000, replace = TRUE, prob = c(0.92, 0.00, 0.08))
- )
Using the left_join()
function from the dplyr
package, we can ‘map’ the gender to the patient ID using the patients_table
object we created earlier:
data <- data %>% left_join(patients_table)
Using the sample()
function, we can randomly select items from all objects we defined earlier. To let our fake data reflect reality a bit, we will also approximately define the probabilities of bacteria and the antibiotic results with the prob
parameter.
data <- data.frame(date = sample(dates, 5000, replace = TRUE),
+ patient_id = sample(patients, 5000, replace = TRUE),
+ hospital = sample(hospitals, 5000, replace = TRUE, prob = c(0.30, 0.35, 0.15, 0.20)),
+ bacteria = sample(bacteria, 5000, replace = TRUE, prob = c(0.50, 0.25, 0.15, 0.10)),
+ amox = sample(ab_interpretations, 5000, replace = TRUE, prob = c(0.60, 0.05, 0.35)),
+ amcl = sample(ab_interpretations, 5000, replace = TRUE, prob = c(0.75, 0.10, 0.15)),
+ cipr = sample(ab_interpretations, 5000, replace = TRUE, prob = c(0.80, 0.00, 0.20)),
+ gent = sample(ab_interpretations, 5000, replace = TRUE, prob = c(0.92, 0.00, 0.08))
+ )
Using the left_join()
function from the dplyr
package, we can ‘map’ the gender to the patient ID using the patients_table
object we created earlier:
The resulting data set contains 5,000 blood culture isolates. With the head()
function we can preview the first 6 values of this data set:
head(data)
date | @@ -303,8 +303,63 @@ ab_interpretations <- -2013-07-18 | +2013-03-25 | +O5 | +Hospital B | +Streptococcus pneumoniae | +S | +S | +R | +S | +F | +
---|---|---|---|---|---|---|---|---|---|---|
2014-05-07 | +W10 | +Hospital A | +Klebsiella pneumoniae | +R | +S | +S | +S | +F | +||
2014-08-13 | +L5 | +Hospital D | +Escherichia coli | +S | +S | +S | +S | +M | +||
2014-07-10 | +Z2 | +Hospital D | +Escherichia coli | +R | +S | +S | +S | +F | +||
2014-09-18 | Z9 | +Hospital D | +Staphylococcus aureus | +S | +S | +S | +S | +F | +||
2015-04-27 | +Q3 | Hospital C | Escherichia coli | S | @@ -313,61 +368,6 @@ ab_interpretations <- SF | |||||
2010-02-24 | -P1 | -Hospital D | -Escherichia coli | -R | -S | -S | -S | -F | -||
2014-04-02 | -A10 | -Hospital B | -Streptococcus pneumoniae | -S | -S | -S | -S | -M | -||
2016-05-30 | -A7 | -Hospital B | -Escherichia coli | -R | -R | -R | -S | -M | -||
2017-11-22 | -O6 | -Hospital D | -Klebsiella pneumoniae | -R | -I | -S | -S | -F | -||
2017-03-08 | -L4 | -Hospital B | -Escherichia coli | -R | -R | -S | -S | -M | -
Now, let’s start the cleaning and the analysis!
@@ -377,7 +377,7 @@ ab_interpretations <- Cleaning the dataUse the frequency table function freq()
to look specifically for unique values in any variable. For example, for the gender
variable:
data %>% freq(gender) # this would be the same: freq(data$gender)
# Frequency table of `gender` # Class: factor (numeric) # Levels: F, M @@ -386,67 +386,67 @@ ab_interpretations <- mutate()
function of thedplyr
package makes this really easy: - -We also want to transform the antibiotics, because in real life data we don’t know if they are really clean. The
- +as.rsi()
function ensures reliability and reproducibility in these kind of variables. Themutate_at()
will run theas.rsi()
function on defined variables:The data is already quite clean, but we still need to transform some variables. The
+ +bacteria
column now consists of text, and we want to add more variables based on microbial IDs later on. So, we will transform this column to valid IDs. Themutate()
function of thedplyr
package makes this really easy:We also want to transform the antibiotics, because in real life data we don’t know if they are really clean. The
+as.rsi()
function ensures reliability and reproducibility in these kind of variables. Themutate_at()
will run theas.rsi()
function on defined variables:Finally, we will apply EUCAST rules on our antimicrobial results. In Europe, most medical microbiological laboratories already apply these rules. Our package features their latest insights on intrinsic resistance and exceptional phenotypes. Moreover, the
eucast_rules()
function can also apply additional rules, like forcingampicillin = R whenamoxicillin/clavulanic acid = R.Because the amoxicillin (column
-amox
) and amoxicillin/clavulanic acid (columnamcl
) in our data were generated randomly, some rows will undoubtedly contain amox = S and amcl = R, which is technically impossible. Theeucast_rules()
fixes this:+data <- eucast_rules(data, col_mo = "bacteria") -# -# Rules by the European Committee on Antimicrobial Susceptibility Testing (EUCAST) -# -# EUCAST Clinical Breakpoints (v8.1, 2018) -# Enterobacteriales (Order) (no changes) -# Staphylococcus (no changes) -# Enterococcus (no changes) -# Streptococcus groups A, B, C, G (no changes) -# Streptococcus pneumoniae (no changes) -# Viridans group streptococci (no changes) -# Haemophilus influenzae (no changes) -# Moraxella catarrhalis (no changes) -# Anaerobic Gram positives (no changes) -# Anaerobic Gram negatives (no changes) -# Pasteurella multocida (no changes) -# Campylobacter jejuni and C. coli (no changes) -# Aerococcus sanguinicola and A. urinae (no changes) -# Kingella kingae (no changes) -# -# EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes (v3.1, 2016) -# Table 1: Intrinsic resistance in Enterobacteriaceae (307 changes) -# Table 2: Intrinsic resistance in non-fermentative Gram-negative bacteria (no changes) -# Table 3: Intrinsic resistance in other Gram-negative bacteria (no changes) -# Table 4: Intrinsic resistance in Gram-positive bacteria (708 changes) -# Table 8: Interpretive rules for B-lactam agents and Gram-positive cocci (no changes) -# Table 9: Interpretive rules for B-lactam agents and Gram-negative rods (no changes) -# Table 10: Interpretive rules for B-lactam agents and other Gram-negative bacteria (no changes) -# Table 11: Interpretive rules for macrolides, lincosamides, and streptogramins (no changes) -# Table 12: Interpretive rules for aminoglycosides (no changes) -# Table 13: Interpretive rules for quinolones (no changes) -# -# Other rules -# Non-EUCAST: ampicillin = R where amoxicillin/clav acid = R (no changes) -# Non-EUCAST: piperacillin = R where piperacillin/tazobactam = R (no changes) -# Non-EUCAST: trimethoprim = R where trimethoprim/sulfa = R (no changes) -# Non-EUCAST: amoxicillin/clav acid = S where ampicillin = S (no changes) -# Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no changes) -# Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no changes) -# -# => EUCAST rules affected 1,871 out of 5,000 rows -> changed 1,015 test results.
data <- eucast_rules(data, col_mo = "bacteria") +# +# Rules by the European Committee on Antimicrobial Susceptibility Testing (EUCAST) +# +# EUCAST Clinical Breakpoints (v9.0, 2019) +# Enterobacteriales (Order) (no changes) +# Staphylococcus (no changes) +# Enterococcus (no changes) +# Streptococcus groups A, B, C, G (no changes) +# Streptococcus pneumoniae (no changes) +# Viridans group streptococci (no changes) +# Haemophilus influenzae (no changes) +# Moraxella catarrhalis (no changes) +# Anaerobic Gram positives (no changes) +# Anaerobic Gram negatives (no changes) +# Pasteurella multocida (no changes) +# Campylobacter jejuni and C. coli (no changes) +# Aerococcus sanguinicola and A. urinae (no changes) +# Kingella kingae (no changes) +# +# EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes (v3.1, 2016) +# Table 1: Intrinsic resistance in Enterobacteriaceae (324 changes) +# Table 2: Intrinsic resistance in non-fermentative Gram-negative bacteria (no changes) +# Table 3: Intrinsic resistance in other Gram-negative bacteria (no changes) +# Table 4: Intrinsic resistance in Gram-positive bacteria (672 changes) +# Table 8: Interpretive rules for B-lactam agents and Gram-positive cocci (no changes) +# Table 9: Interpretive rules for B-lactam agents and Gram-negative rods (no changes) +# Table 10: Interpretive rules for B-lactam agents and other Gram-negative bacteria (no changes) +# Table 11: Interpretive rules for macrolides, lincosamides, and streptogramins (no changes) +# Table 12: Interpretive rules for aminoglycosides (no changes) +# Table 13: Interpretive rules for quinolones (no changes) +# +# Other rules +# Non-EUCAST: ampicillin = R where amoxicillin/clav acid = R (no changes) +# Non-EUCAST: piperacillin = R where piperacillin/tazobactam = R (no changes) +# Non-EUCAST: trimethoprim = R where trimethoprim/sulfa = R (no changes) +# Non-EUCAST: amoxicillin/clav acid = S where ampicillin = S (no changes) +# Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no changes) +# Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no changes) +# +# => EUCAST rules affected 1,808 out of 5,000 rows -> changed 996 test results.
Now that we have the microbial ID, we can add some taxonomic properties:
-data <- data %>%
- mutate(gramstain = mo_gramstain(bacteria),
- genus = mo_genus(bacteria),
- species = mo_species(bacteria))
data <- data %>%
+ mutate(gramstain = mo_gramstain(bacteria),
+ genus = mo_genus(bacteria),
+ species = mo_species(bacteria))
This AMR
package includes this methodology with the first_isolate()
function. It adopts the episode of a year (can be changed by user) and it starts counting days after every selected isolate. This new variable can easily be added to our data:
data <- data %>%
- mutate(first = first_isolate(.))
-# NOTE: Using column `bacteria` as input for `col_mo`.
-# NOTE: Using column `date` as input for `col_date`.
-# NOTE: Using column `patient_id` as input for `col_patient_id`.
-# => Found 2,951 first isolates (59.0% of total)
So only 59% is suitable for resistance analysis! We can now filter on is with the filter()
function, also from the dplyr
package:
data_1st <- data %>%
- filter(first == TRUE)
data <- data %>%
+ mutate(first = first_isolate(.))
+# NOTE: Using column `bacteria` as input for `col_mo`.
+# NOTE: Using column `date` as input for `col_date`.
+# NOTE: Using column `patient_id` as input for `col_patient_id`.
+# => Found 2,928 first isolates (58.6% of total)
So only 58.6% is suitable for resistance analysis! We can now filter on is with the filter()
function, also from the dplyr
package:
For future use, the above two syntaxes can be shortened with the filter_first_isolate()
function:
data_1st <- data %>%
- filter_first_isolate()
Only 3 isolates are marked as ‘first’ according to CLSI guideline. But when reviewing the antibiogram, it is obvious that some isolates are absolutely different strains and show be included too. This is why we weigh isolates, based on their antibiogram. The key_antibiotics()
function adds a vector with 18 key antibiotics: 6 broad spectrum ones, 6 small spectrum for Gram negatives and 6 small spectrum for Gram positives. These can be defined by the user.
Only 4 isolates are marked as ‘first’ according to CLSI guideline. But when reviewing the antibiogram, it is obvious that some isolates are absolutely different strains and show be included too. This is why we weigh isolates, based on their antibiogram. The key_antibiotics()
function adds a vector with 18 key antibiotics: 6 broad spectrum ones, 6 small spectrum for Gram negatives and 6 small spectrum for Gram positives. These can be defined by the user.
If a column exists with a name like ‘key(…)ab’ the first_isolate()
function will automatically use it and determine the first weighted isolates. Mind the NOTEs in below output:
data <- data %>%
- mutate(keyab = key_antibiotics(.)) %>%
- mutate(first_weighted = first_isolate(.))
-# NOTE: Using column `bacteria` as input for `col_mo`.
-# amox amcl cipr gent
-# "amox" "amcl" "cipr" "gent"
-# NOTE: Using column `bacteria` as input for `col_mo`.
-# NOTE: Using column `date` as input for `col_date`.
-# NOTE: Using column `patient_id` as input for `col_patient_id`.
-# NOTE: Using column `keyab` as input for `col_keyantibiotics`. Use col_keyantibiotics = FALSE to prevent this.
-# [Criterion] Inclusion based on key antibiotics, ignoring I.
-# => Found 4,431 first weighted isolates (88.6% of total)
data <- data %>%
+ mutate(keyab = key_antibiotics(.)) %>%
+ mutate(first_weighted = first_isolate(.))
+# NOTE: Using column `bacteria` as input for `col_mo`.
+# NOTE: Using column `bacteria` as input for `col_mo`.
+# NOTE: Using column `date` as input for `col_date`.
+# NOTE: Using column `patient_id` as input for `col_patient_id`.
+# NOTE: Using column `keyab` as input for `col_keyantibiotics`. Use col_keyantibiotics = FALSE to prevent this.
+# [Criterion] Inclusion based on key antibiotics, ignoring I.
+# => Found 4,422 first weighted isolates (88.4% of total)
isolate | @@ -629,8 +627,8 @@ ab_interpretations <-1 | -2010-03-01 | -Y4 | +2010-03-08 | +V6 | B_ESCHR_COL | S | S | @@ -641,58 +639,58 @@ ab_interpretations <-2 | -2010-11-02 | -Y4 | +2011-02-03 | +V6 | B_ESCHR_COL | +R | S | S | -S | -S | -FALSE | +R | FALSE | +TRUE | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3 | -2011-06-12 | -Y4 | +2011-12-31 | +V6 | B_ESCHR_COL | -R | S | -R | +I | +S | S | TRUE | TRUE | ||||||||||||
4 | -2011-09-03 | -Y4 | +2012-10-20 | +V6 | B_ESCHR_COL | S | S | S | S | FALSE | -TRUE | +FALSE | |||||||||||||
5 | -2011-09-07 | -Y4 | +2012-12-17 | +V6 | B_ESCHR_COL | S | S | -R | +S | S | FALSE | -TRUE | +FALSE | ||||||||||||
6 | -2012-06-11 | -Y4 | +2013-08-27 | +V6 | B_ESCHR_COL | -S | +R | S | S | S | @@ -701,44 +699,32 @@ ab_interpretations <-7 | -2012-08-16 | -Y4 | +2013-09-21 | +V6 | B_ESCHR_COL | +I | S | S | -S | -S | -FALSE | +R | FALSE | +TRUE |
8 | -2012-11-24 | -Y4 | +2014-07-19 | +V6 | B_ESCHR_COL | S | -I | S | -R | +S | +S | FALSE | TRUE | ||||||||||||
9 | -2012-12-12 | -Y4 | -B_ESCHR_COL | -S | -S | -S | -R | -FALSE | -FALSE | -||||||||||||||||
10 | -2013-02-02 | -Y4 | +2014-07-24 | +V6 | B_ESCHR_COL | R | S | @@ -747,21 +733,32 @@ ab_interpretations <- FALSETRUE | |||||||||||||||||
10 | +2014-11-23 | +V6 | +B_ESCHR_COL | +S | +S | +S | +S | +TRUE | +TRUE | +
Instead of 3, now 7 isolates are flagged. In total, 88.6% of all isolates are marked ‘first weighted’ - 147.6% more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.
+Instead of 4, now 8 isolates are flagged. In total, 88.4% of all isolates are marked ‘first weighted’ - 147% more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.
As with filter_first_isolate()
, there’s a shortcut for this new algorithm too:
data_1st <- data %>%
- filter_first_weighted_isolate()
So we end up with 4,431 isolates for analysis.
+ +So we end up with 4,422 isolates for analysis.
We can remove unneeded columns:
- +Now our data looks like:
-head(data_1st)
date | patient_id | hospital | @@ -778,25 +775,53 @@ ab_interpretations <- -1 | -2013-07-18 | -Z9 | -Hospital C | +2013-03-25 | +O5 | +Hospital B | +B_STRPTC_PNE | +S | +S | +R | +R | +F | +Gram positive | +Streptococcus | +pneumoniae | +TRUE | +|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2014-05-07 | +W10 | +Hospital A | +B_KLBSL_PNE | +R | +S | +S | +S | +F | +Gram negative | +Klebsiella | +pneumoniae | +TRUE | +||||||||
2014-08-13 | +L5 | +Hospital D | B_ESCHR_COL | S | S | S | S | -F | +M | Gram negative | Escherichia | coli | TRUE | |||||||
2 | -2010-02-24 | -P1 | +2014-07-10 | +Z2 | Hospital D | B_ESCHR_COL | R | @@ -810,58 +835,9 @@ ab_interpretations <- TRUE|||||||||||||
3 | -2014-04-02 | -A10 | -Hospital B | -B_STRPTC_PNE | -S | -S | -S | -R | -M | -Gram positive | -Streptococcus | -pneumoniae | -TRUE | -|||||||
4 | -2016-05-30 | -A7 | -Hospital B | -B_ESCHR_COL | -R | -R | -R | -S | -M | -Gram negative | -Escherichia | -coli | -TRUE | -|||||||
6 | -2017-03-08 | -L4 | -Hospital B | -B_ESCHR_COL | -R | -R | -S | -S | -M | -Gram negative | -Escherichia | -coli | -TRUE | -|||||||
7 | -2014-06-24 | -O7 | -Hospital B | +2014-09-18 | +Z9 | +Hospital D | B_STPHY_AUR | S | S | @@ -873,6 +849,21 @@ ab_interpretations <- aureusTRUE | ||||||||||
2015-04-27 | +Q3 | +Hospital C | +B_ESCHR_COL | +S | +S | +S | +S | +F | +Gram negative | +Escherichia | +coli | +TRUE | +
Time for the analysis!
@@ -883,12 +874,12 @@ ab_interpretations <- Analysing the dataYou might want to start by getting an idea of how the data is distributed. It’s an important start, because it also decides how you will continue your analysis. ## Dispersion of species To just get an idea how the species are distributed, create a frequency table with our freq()
function. We created the genus
and species
column earlier based on the microbial ID. With paste()
, we can concatenate them together.
The freq()
function can be used like the base R language was intended:
Or can be used like the dplyr
way, which is easier readable:
data_1st %>% freq(genus, species)
Frequency table of genus
and species
Columns: 2
-Length: 4,431 (of which NA: 0 = 0.00%)
+Length: 4,422 (of which NA: 0 = 0.00%)
Unique: 4
Shortest: 16
Longest: 24
The functions portion_R
, portion_RI
, portion_I
, portion_IS
and portion_S
can be used to determine the portion of a specific antimicrobial outcome. They can be used on their own:
data_1st %>% portion_IR(amox)
-# [1] 0.460167
Or can be used in conjuction with group_by()
and summarise()
, both from the dplyr
package:
data_1st %>%
- group_by(hospital) %>%
- summarise(amoxicillin = portion_IR(amox))
Or can be used in conjuction with group_by()
and summarise()
, both from the dplyr
package:
hospital | @@ -954,27 +945,27 @@ Longest: 24||
---|---|---|
Hospital A | -0.4626060 | +0.4980784 |
Hospital B | -0.4652956 | +0.4516332 |
Hospital C | -0.4336283 | +0.4705882 |
Hospital D | -0.4677778 | +0.4767837 |
Of course it would be very convenient to know the number of isolates responsible for the percentages. For that purpose the n_rsi()
can be used, which works exactly like n_distinct()
from the dplyr
package. It counts all isolates available for every group (i.e. values S, I or R):
data_1st %>%
- group_by(hospital) %>%
- summarise(amoxicillin = portion_IR(amox),
- available = n_rsi(amox))
Of course it would be very convenient to know the number of isolates responsible for the percentages. For that purpose the n_rsi()
can be used, which works exactly like n_distinct()
from the dplyr
package. It counts all isolates available for every group (i.e. values S, I or R):
data_1st %>%
+ group_by(hospital) %>%
+ summarise(amoxicillin = portion_IR(amox),
+ available = n_rsi(amox))
hospital | @@ -984,32 +975,32 @@ Longest: 24||||
---|---|---|---|---|
Hospital A | -0.4626060 | -1297 | +0.4980784 | +1301 |
Hospital B | -0.4652956 | -1556 | +0.4516332 | +1592 |
Hospital C | -0.4336283 | -678 | +0.4705882 | +646 |
Hospital D | -0.4677778 | -900 | +0.4767837 | +883 |
These functions can also be used to get the portion of multiple antibiotics, to calculate co-resistance very easily:
-data_1st %>%
- group_by(genus) %>%
- summarise(amoxicillin = portion_S(amcl),
- gentamicin = portion_S(gent),
- "amox + gent" = portion_S(amcl, gent))
data_1st %>%
+ group_by(genus) %>%
+ summarise(amoxicillin = portion_S(amcl),
+ gentamicin = portion_S(gent),
+ "amox + gent" = portion_S(amcl, gent))
genus | @@ -1020,94 +1011,94 @@ Longest: 24||||||
---|---|---|---|---|---|---|
Escherichia | -0.7312179 | -0.9094727 | -0.9752683 | +0.7498855 | +0.9079249 | +0.9775538 |
Klebsiella | -0.7750557 | -0.9064588 | -0.9866370 | +0.7505519 | +0.9006623 | +0.9713024 |
Staphylococcus | -0.7469027 | -0.9247788 | -0.9743363 | +0.7437500 | +0.9196429 | +0.9767857 |
Streptococcus | -0.7602257 | +0.7357357 | 0.0000000 | -0.7602257 | +0.7357357 |
To make a transition to the next part, let’s see how this difference could be plotted:
-data_1st %>%
- group_by(genus) %>%
- summarise("1. Amoxicillin" = portion_S(amcl),
- "2. Gentamicin" = portion_S(gent),
- "3. Amox + gent" = portion_S(amcl, gent)) %>%
- tidyr::gather("Antibiotic", "S", -genus) %>%
- ggplot(aes(x = genus,
- y = S,
- fill = Antibiotic)) +
- geom_col(position = "dodge2")
data_1st %>%
+ group_by(genus) %>%
+ summarise("1. Amoxicillin" = portion_S(amcl),
+ "2. Gentamicin" = portion_S(gent),
+ "3. Amox + gent" = portion_S(amcl, gent)) %>%
+ tidyr::gather("Antibiotic", "S", -genus) %>%
+ ggplot(aes(x = genus,
+ y = S,
+ fill = Antibiotic)) +
+ geom_col(position = "dodge2")
To show results in plots, most R users would nowadays use the ggplot2
package. This package lets you create plots in layers. You can read more about it on their website. A quick example would look like these syntaxes:
ggplot(data = a_data_set,
- mapping = aes(x = year,
- y = value)) +
- geom_col() +
- labs(title = "A title",
- subtitle = "A subtitle",
- x = "My X axis",
- y = "My Y axis")
-
-ggplot(a_data_set,
- aes(year, value) +
- geom_bar()
ggplot(data = a_data_set,
+ mapping = aes(x = year,
+ y = value)) +
+ geom_col() +
+ labs(title = "A title",
+ subtitle = "A subtitle",
+ x = "My X axis",
+ y = "My Y axis")
+
+ggplot(a_data_set,
+ aes(year, value) +
+ geom_bar()
The AMR
package contains functions to extend this ggplot2
package, for example geom_rsi()
. It automatically transforms data with count_df()
or portion_df()
and show results in stacked bars. Its simplest and shortest example:
Omit the translate_ab = FALSE
to have the antibiotic codes (amox, amcl, cipr, gent) translated to official WHO names (amoxicillin, amoxicillin and betalactamase inhibitor, ciprofloxacin, gentamicin).
If we group on e.g. the genus
column and add some additional functions from our package, we can create this:
# group the data on `genus`
-ggplot(data_1st %>% group_by(genus)) +
- # create bars with genus on x axis
- # it looks for variables with class `rsi`,
- # of which we have 4 (earlier created with `as.rsi`)
- geom_rsi(x = "genus") +
- # split plots on antibiotic
- facet_rsi(facet = "Antibiotic") +
- # make R red, I yellow and S green
- scale_rsi_colours() +
- # show percentages on y axis
- scale_y_percent(breaks = 0:4 * 25) +
- # turn 90 degrees, make it bars instead of columns
- coord_flip() +
- # add labels
- labs(title = "Resistance per genus and antibiotic",
- subtitle = "(this is fake data)") +
- # and print genus in italic to follow our convention
- # (is now y axis because we turned the plot)
- theme(axis.text.y = element_text(face = "italic"))
# group the data on `genus`
+ggplot(data_1st %>% group_by(genus)) +
+ # create bars with genus on x axis
+ # it looks for variables with class `rsi`,
+ # of which we have 4 (earlier created with `as.rsi`)
+ geom_rsi(x = "genus") +
+ # split plots on antibiotic
+ facet_rsi(facet = "Antibiotic") +
+ # make R red, I yellow and S green
+ scale_rsi_colours() +
+ # show percentages on y axis
+ scale_y_percent(breaks = 0:4 * 25) +
+ # turn 90 degrees, make it bars instead of columns
+ coord_flip() +
+ # add labels
+ labs(title = "Resistance per genus and antibiotic",
+ subtitle = "(this is fake data)") +
+ # and print genus in italic to follow our convention
+ # (is now y axis because we turned the plot)
+ theme(axis.text.y = element_text(face = "italic"))
To simplify this, we also created the ggplot_rsi()
function, which combines almost all above functions:
data_1st %>%
- group_by(genus) %>%
- ggplot_rsi(x = "genus",
- facet = "Antibiotic",
- breaks = 0:4 * 25,
- datalabels = FALSE) +
- coord_flip()
We can transform the data and apply the test in only a couple of lines:
-septic_patients %>%
- filter(hospital_id %in% c("A", "D")) %>% # filter on only hospitals A and D
- select(hospital_id, fosf) %>% # select the hospitals and fosfomycin
- group_by(hospital_id) %>% # group on the hospitals
- count_df(combine_IR = TRUE) %>% # count all isolates per group (hospital_id)
- tidyr::spread(hospital_id, Value) %>% # transform output so A and D are columns
- select(A, D) %>% # and select these only
- as.matrix() %>% # transform to good old matrix for fisher.test()
- fisher.test() # do Fisher's Exact Test
-#
-# Fisher's Exact Test for Count Data
-#
-# data: .
-# p-value = 0.03104
-# alternative hypothesis: true odds ratio is not equal to 1
-# 95 percent confidence interval:
-# 1.054283 4.735995
-# sample estimates:
-# odds ratio
-# 2.228006
septic_patients %>%
+ filter(hospital_id %in% c("A", "D")) %>% # filter on only hospitals A and D
+ select(hospital_id, fosf) %>% # select the hospitals and fosfomycin
+ group_by(hospital_id) %>% # group on the hospitals
+ count_df(combine_IR = TRUE) %>% # count all isolates per group (hospital_id)
+ tidyr::spread(hospital_id, Value) %>% # transform output so A and D are columns
+ select(A, D) %>% # and select these only
+ as.matrix() %>% # transform to good old matrix for fisher.test()
+ fisher.test() # do Fisher's Exact Test
+#
+# Fisher's Exact Test for Count Data
+#
+# data: .
+# p-value = 0.03104
+# alternative hypothesis: true odds ratio is not equal to 1
+# 95 percent confidence interval:
+# 1.054283 4.735995
+# sample estimates:
+# odds ratio
+# 2.228006
As can be seen, the p value is 0.03, which means that the fosfomycin resistances found in hospital A and D are really different.
EUCAST.Rmd
G_test.Rmd
Predict.Rmd
ab_property.Rmd
benchmarks.Rmd
library(dplyr)
# take 500,000 random MO codes from the septic_patients data set
x = septic_patients %>%
- sample_n(500000, replace = TRUE) %>%
- pull(mo)
+ sample_n(500000, replace = TRUE) %>%
+ pull(mo)
# got the right length?
length(x)
# [1] 500000
# and how many unique values do we have?
-n_distinct(x)
+n_distinct(x)
# [1] 96
# only 96, but distributed in 500,000 results. now let's see:
diff --git a/docs/articles/freq.html b/docs/articles/freq.html
index a294b649..8156c10b 100644
--- a/docs/articles/freq.html
+++ b/docs/articles/freq.html
@@ -40,7 +40,7 @@
freq.Rmd
To only show and quickly review the content of one variable, you can just select this variable in various ways. Let’s say we want to get the frequencies of the gender
variable of the septic_patients
dataset:
septic_patients %>% freq(gender)
Frequency table of gender