1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-09 04:42:22 +02:00

styled, unit test fix

This commit is contained in:
2022-08-28 10:31:50 +02:00
parent 4cb1db4554
commit 4d050aef7c
147 changed files with 10897 additions and 8169 deletions

View File

@ -58,7 +58,7 @@ With using \code{collapse}, this function will return a \link{character}:\cr
}
}
\examples{
# mind the bad spelling of amoxicillin in this line,
# mind the bad spelling of amoxicillin in this line,
# straight from a true health care record:
ab_from_text("28/03/2020 regular amoxicilliin 500mg po tds")
@ -73,22 +73,28 @@ abx <- ab_from_text("500 mg amoxi po and 400mg cipro iv")
ab_group(abx[[1]])
if (require("dplyr")) {
tibble(clinical_text = c("given 400mg cipro and 500 mg amox",
"started on doxy iv today")) \%>\%
mutate(abx_codes = ab_from_text(clinical_text),
abx_doses = ab_from_text(clinical_text, type = "doses"),
abx_admin = ab_from_text(clinical_text, type = "admin"),
abx_coll = ab_from_text(clinical_text, collapse = "|"),
abx_coll_names = ab_from_text(clinical_text,
collapse = "|",
translate_ab = "name"),
abx_coll_doses = ab_from_text(clinical_text,
type = "doses",
collapse = "|"),
abx_coll_admin = ab_from_text(clinical_text,
type = "admin",
collapse = "|"))
tibble(clinical_text = c(
"given 400mg cipro and 500 mg amox",
"started on doxy iv today"
)) \%>\%
mutate(
abx_codes = ab_from_text(clinical_text),
abx_doses = ab_from_text(clinical_text, type = "doses"),
abx_admin = ab_from_text(clinical_text, type = "admin"),
abx_coll = ab_from_text(clinical_text, collapse = "|"),
abx_coll_names = ab_from_text(clinical_text,
collapse = "|",
translate_ab = "name"
),
abx_coll_doses = ab_from_text(clinical_text,
type = "doses",
collapse = "|"
),
abx_coll_admin = ab_from_text(clinical_text,
type = "admin",
collapse = "|"
)
)
}
}
}

View File

@ -109,36 +109,38 @@ All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/S
\examples{
# all properties:
ab_name("AMX") # "Amoxicillin"
ab_atc("AMX") # "J01CA04" (ATC code from the WHO)
ab_cid("AMX") # 33613 (Compound ID from PubChem)
ab_synonyms("AMX") # a list with brand names of amoxicillin
ab_name("AMX") # "Amoxicillin"
ab_atc("AMX") # "J01CA04" (ATC code from the WHO)
ab_cid("AMX") # 33613 (Compound ID from PubChem)
ab_synonyms("AMX") # a list with brand names of amoxicillin
ab_tradenames("AMX") # same
ab_group("AMX") # "Beta-lactams/penicillins"
ab_group("AMX") # "Beta-lactams/penicillins"
ab_atc_group1("AMX") # "Beta-lactam antibacterials, penicillins"
ab_atc_group2("AMX") # "Penicillins with extended spectrum"
ab_url("AMX") # link to the official WHO page
ab_url("AMX") # link to the official WHO page
# smart lowercase tranformation
ab_name(x = c("AMC", "PLB")) # "Amoxicillin/clavulanic acid" "Polymyxin B"
ab_name(x = c("AMC", "PLB"),
tolower = TRUE) # "amoxicillin/clavulanic acid" "polymyxin B"
ab_name(x = c("AMC", "PLB")) # "Amoxicillin/clavulanic acid" "Polymyxin B"
ab_name(
x = c("AMC", "PLB"),
tolower = TRUE
) # "amoxicillin/clavulanic acid" "polymyxin B"
# defined daily doses (DDD)
ab_ddd("AMX", "oral") # 1.5
ab_ddd("AMX", "oral") # 1.5
ab_ddd_units("AMX", "oral") # "g"
ab_ddd("AMX", "iv") # 3
ab_ddd_units("AMX", "iv") # "g"
ab_ddd("AMX", "iv") # 3
ab_ddd_units("AMX", "iv") # "g"
ab_info("AMX") # all properties as a list
ab_info("AMX") # all properties as a list
# all ab_* functions use as.ab() internally, so you can go from 'any' to 'any':
ab_atc("AMP") # ATC code of AMP (ampicillin)
ab_group("J01CA01") # Drug group of ampicillins ATC code
ab_loinc("ampicillin") # LOINC codes of ampicillin
ab_name("21066-6") # "Ampicillin" (using LOINC)
ab_name(6249) # "Ampicillin" (using CID)
ab_name("J01CA01") # "Ampicillin" (using ATC)
ab_atc("AMP") # ATC code of AMP (ampicillin)
ab_group("J01CA01") # Drug group of ampicillins ATC code
ab_loinc("ampicillin") # LOINC codes of ampicillin
ab_name("21066-6") # "Ampicillin" (using LOINC)
ab_name(6249) # "Ampicillin" (using CID)
ab_name("J01CA01") # "Ampicillin" (using ATC)
# spelling from different languages and dyslexia are no problem
ab_atc("ceftriaxon")
@ -155,24 +157,24 @@ if (require("dplyr")) {
example_isolates \%>\%
set_ab_names() \%>\%
head()
# this does the same:
example_isolates \%>\%
rename_with(set_ab_names)\%>\%
rename_with(set_ab_names) \%>\%
head()
# set_ab_names() works with any AB property:
example_isolates \%>\%
set_ab_names(property = "atc")\%>\%
set_ab_names(property = "atc") \%>\%
head()
example_isolates \%>\%
set_ab_names(where(is.rsi)) \%>\%
colnames()
example_isolates \%>\%
set_ab_names(NIT:VAN) \%>\%
colnames()
example_isolates \%>\%
set_ab_names(where(is.rsi)) \%>\%
colnames()
example_isolates \%>\%
set_ab_names(NIT:VAN) \%>\%
colnames()
}
}
}

View File

@ -62,10 +62,12 @@ if (require("dplyr")) {
filter(mo == as.mo("Escherichia coli")) \%>\%
group_by(age_group = age_groups(age)) \%>\%
select(age_group, CIP) \%>\%
ggplot_rsi(x = "age_group",
minimum = 0,
x.title = "Age Group",
title = "Ciprofloxacin resistance per age group")
ggplot_rsi(
x = "age_group",
minimum = 0,
x.title = "Age Group",
title = "Ciprofloxacin resistance per age group"
)
}
}
}

View File

@ -217,101 +217,92 @@ if (require("dplyr")) {
# get AMR for all aminoglycosides e.g., per ward:
example_isolates \%>\%
group_by(ward) \%>\%
group_by(ward) \%>\%
summarise(across(aminoglycosides(), resistance))
}
if (require("dplyr")) {
# You can combine selectors with '&' to be more specific:
example_isolates \%>\%
select(penicillins() & administrable_per_os())
}
if (require("dplyr")) {
# get AMR for only drugs that matter - no intrinsic resistance:
example_isolates \%>\%
filter(mo_genus() \%in\% c("Escherichia", "Klebsiella")) \%>\%
group_by(ward) \%>\%
filter(mo_genus() \%in\% c("Escherichia", "Klebsiella")) \%>\%
group_by(ward) \%>\%
summarise(across(not_intrinsic_resistant(), resistance))
}
if (require("dplyr")) {
# get susceptibility for antibiotics whose name contains "trim":
example_isolates \%>\%
filter(first_isolate()) \%>\%
group_by(ward) \%>\%
filter(first_isolate()) \%>\%
group_by(ward) \%>\%
summarise(across(ab_selector(name \%like\% "trim"), susceptibility))
}
if (require("dplyr")) {
# this will select columns 'IPM' (imipenem) and 'MEM' (meropenem):
example_isolates \%>\%
example_isolates \%>\%
select(carbapenems())
}
if (require("dplyr")) {
# this will select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB':
example_isolates \%>\%
select(mo, aminoglycosides())
}
if (require("dplyr")) {
# any() and all() work in dplyr's filter() too:
example_isolates \%>\%
filter(any(aminoglycosides() == "R"),
all(cephalosporins_2nd() == "R"))
}
if (require("dplyr")) {
# also works with c():
example_isolates \%>\%
filter(any(c(carbapenems(), aminoglycosides()) == "R"))
}
if (require("dplyr")) {
# not setting any/all will automatically apply all():
example_isolates \%>\%
filter(aminoglycosides() == "R")
}
if (require("dplyr")) {
# this will select columns 'mo' and all antimycobacterial drugs ('RIF'):
example_isolates \%>\%
select(mo, ab_class("mycobact"))
}
if (require("dplyr")) {
# get bug/drug combinations for only glycopeptides in Gram-positives:
example_isolates \%>\%
filter(mo_is_gram_positive()) \%>\%
select(mo, glycopeptides()) \%>\%
bug_drug_combinations() \%>\%
format()
}
if (require("dplyr")) {
data.frame(some_column = "some_value",
J01CA01 = "S") \%>\% # ATC code of ampicillin
select(penicillins()) # only the 'J01CA01' column will be selected
# this will select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB':
example_isolates \%>\%
select(mo, aminoglycosides())
}
if (require("dplyr")) {
# any() and all() work in dplyr's filter() too:
example_isolates \%>\%
filter(
any(aminoglycosides() == "R"),
all(cephalosporins_2nd() == "R")
)
}
if (require("dplyr")) {
# also works with c():
example_isolates \%>\%
filter(any(c(carbapenems(), aminoglycosides()) == "R"))
}
if (require("dplyr")) {
# not setting any/all will automatically apply all():
example_isolates \%>\%
filter(aminoglycosides() == "R")
}
if (require("dplyr")) {
# this will select columns 'mo' and all antimycobacterial drugs ('RIF'):
example_isolates \%>\%
select(mo, ab_class("mycobact"))
}
if (require("dplyr")) {
# get bug/drug combinations for only glycopeptides in Gram-positives:
example_isolates \%>\%
filter(mo_is_gram_positive()) \%>\%
select(mo, glycopeptides()) \%>\%
bug_drug_combinations() \%>\%
format()
}
if (require("dplyr")) {
data.frame(
some_column = "some_value",
J01CA01 = "S"
) \%>\% # ATC code of ampicillin
select(penicillins()) # only the 'J01CA01' column will be selected
}
if (require("dplyr")) {
# with recent versions of dplyr this is all equal:
x <- example_isolates[carbapenems() == "R", ]
y <- example_isolates \%>\% filter(carbapenems() == "R")
z <- example_isolates \%>\% filter(if_all(carbapenems(), ~.x == "R"))
z <- example_isolates \%>\% filter(if_all(carbapenems(), ~ .x == "R"))
identical(x, y) && identical(y, z)
}
}

View File

@ -74,26 +74,25 @@ as.ab(" eryt 123")
as.ab("ERYT")
as.ab("ERY")
as.ab("eritromicine") # spelled wrong, yet works
as.ab("Erythrocin") # trade name
as.ab("Romycin") # trade name
as.ab("Erythrocin") # trade name
as.ab("Romycin") # trade name
# spelling from different languages and dyslexia are no problem
ab_atc("ceftriaxon")
ab_atc("cephtriaxone") # small spelling error
ab_atc("cephthriaxone") # or a bit more severe
ab_atc("cephtriaxone") # small spelling error
ab_atc("cephthriaxone") # or a bit more severe
ab_atc("seephthriaaksone") # and even this works
# use ab_* functions to get a specific properties (see ?ab_property);
# they use as.ab() internally:
ab_name("J01FA01") # "Erythromycin"
ab_name("eryt") # "Erythromycin"
ab_name("J01FA01") # "Erythromycin"
ab_name("eryt") # "Erythromycin"
\donttest{
if (require("dplyr")) {
# you can quickly rename <rsi> columns using dplyr >= 1.0.0:
example_isolates \%>\%
rename_with(as.ab, where(is.rsi))
}
}
}

View File

@ -35,11 +35,13 @@ Interpret disk values as RSI values with \code{\link[=as.rsi]{as.rsi()}}. It sup
}
\examples{
# transform existing disk zones to the `disk` class (using base R)
df <- data.frame(microorganism = "Escherichia coli",
AMP = 20,
CIP = 14,
GEN = 18,
TOB = 16)
df <- data.frame(
microorganism = "Escherichia coli",
AMP = 20,
CIP = 14,
GEN = 18,
TOB = 16
)
df[, 2:5] <- lapply(df[, 2:5], as.disk)
str(df)
@ -51,10 +53,12 @@ if (require("dplyr")) {
}
# interpret disk values, see ?as.rsi
as.rsi(x = as.disk(18),
mo = "Strep pneu", # `mo` will be coerced with as.mo()
ab = "ampicillin", # and `ab` with as.ab()
guideline = "EUCAST")
as.rsi(
x = as.disk(18),
mo = "Strep pneu", # `mo` will be coerced with as.mo()
ab = "ampicillin", # and `ab` with as.ab()
guideline = "EUCAST"
)
# interpret whole data set, pretend to be all from urinary tract infections:
as.rsi(df, uti = TRUE)

View File

@ -91,14 +91,18 @@ quantile(mic_data)
all(mic_data < 512)
# interpret MIC values
as.rsi(x = as.mic(2),
mo = as.mo("Streptococcus pneumoniae"),
ab = "AMX",
guideline = "EUCAST")
as.rsi(x = as.mic(c(0.01, 2, 4, 8)),
mo = as.mo("Streptococcus pneumoniae"),
ab = "AMX",
guideline = "EUCAST")
as.rsi(
x = as.mic(2),
mo = as.mo("Streptococcus pneumoniae"),
ab = "AMX",
guideline = "EUCAST"
)
as.rsi(
x = as.mic(c(0.01, 2, 4, 8)),
mo = as.mo("Streptococcus pneumoniae"),
ab = "AMX",
guideline = "EUCAST"
)
# plot MIC values, see ?plot
plot(mic_data)

View File

@ -188,9 +188,9 @@ as.mo("S aureus")
as.mo("Staphylococcus aureus")
as.mo("Staphylococcus aureus (MRSA)")
as.mo("Zthafilokkoockus oureuz") # handles incorrect spelling
as.mo("MRSA") # Methicillin Resistant S. aureus
as.mo("VISA") # Vancomycin Intermediate S. aureus
as.mo("VRSA") # Vancomycin Resistant S. aureus
as.mo("MRSA") # Methicillin Resistant S. aureus
as.mo("VISA") # Vancomycin Intermediate S. aureus
as.mo("VRSA") # Vancomycin Resistant S. aureus
as.mo(115329001) # SNOMED CT code
# Dyslexia is no problem - these all work:
@ -203,15 +203,15 @@ as.mo("Streptococcus group A")
as.mo("GAS") # Group A Streptococci
as.mo("GBS") # Group B Streptococci
as.mo("S. epidermidis") # will remain species: B_STPHY_EPDR
as.mo("S. epidermidis", Becker = TRUE) # will not remain species: B_STPHY_CONS
as.mo("S. epidermidis") # will remain species: B_STPHY_EPDR
as.mo("S. epidermidis", Becker = TRUE) # will not remain species: B_STPHY_CONS
as.mo("S. pyogenes") # will remain species: B_STRPT_PYGN
as.mo("S. pyogenes") # will remain species: B_STRPT_PYGN
as.mo("S. pyogenes", Lancefield = TRUE) # will not remain species: B_STRPT_GRPA
# All mo_* functions use as.mo() internally too (see ?mo_property):
mo_genus("E. coli") # returns "Escherichia"
mo_gramstain("E. coli") # returns "Gram negative"
mo_genus("E. coli") # returns "Escherichia"
mo_gramstain("E. coli") # returns "Gram negative"
mo_is_intrinsic_resistant("E. coli", "vanco") # returns TRUE
}
}

View File

@ -164,27 +164,33 @@ example_isolates
summary(example_isolates) # see all R/SI results at a glance
# For INTERPRETING disk diffusion and MIC values -----------------------
# a whole data set, even with combined MIC values and disk zones
df <- data.frame(microorganism = "Escherichia coli",
AMP = as.mic(8),
CIP = as.mic(0.256),
GEN = as.disk(18),
TOB = as.disk(16),
NIT = as.mic(32),
ERY = "R")
df <- data.frame(
microorganism = "Escherichia coli",
AMP = as.mic(8),
CIP = as.mic(0.256),
GEN = as.disk(18),
TOB = as.disk(16),
NIT = as.mic(32),
ERY = "R"
)
as.rsi(df)
# for single values
as.rsi(x = as.mic(2),
mo = as.mo("S. pneumoniae"),
ab = "AMP",
guideline = "EUCAST")
as.rsi(
x = as.mic(2),
mo = as.mo("S. pneumoniae"),
ab = "AMP",
guideline = "EUCAST"
)
as.rsi(x = as.disk(18),
mo = "Strep pneu", # `mo` will be coerced with as.mo()
ab = "ampicillin", # and `ab` with as.ab()
guideline = "EUCAST")
as.rsi(
x = as.disk(18),
mo = "Strep pneu", # `mo` will be coerced with as.mo()
ab = "ampicillin", # and `ab` with as.ab()
guideline = "EUCAST"
)
\donttest{
# the dplyr way
@ -194,23 +200,27 @@ if (require("dplyr")) {
df \%>\% mutate(across(where(is.mic), as.rsi))
df \%>\% mutate_at(vars(AMP:TOB), as.rsi)
df \%>\% mutate(across(AMP:TOB, as.rsi))
df \%>\%
mutate_at(vars(AMP:TOB), as.rsi, mo = .$microorganism)
# to include information about urinary tract infections (UTI)
data.frame(mo = "E. coli",
NIT = c("<= 2", 32),
from_the_bladder = c(TRUE, FALSE)) \%>\%
data.frame(
mo = "E. coli",
NIT = c("<= 2", 32),
from_the_bladder = c(TRUE, FALSE)
) \%>\%
as.rsi(uti = "from_the_bladder")
data.frame(mo = "E. coli",
NIT = c("<= 2", 32),
specimen = c("urine", "blood")) \%>\%
data.frame(
mo = "E. coli",
NIT = c("<= 2", 32),
specimen = c("urine", "blood")
) \%>\%
as.rsi() # automatically determines urine isolates
df \%>\%
mutate_at(vars(AMP:NIT), as.rsi, mo = "E. coli", uti = TRUE)
mutate_at(vars(AMP:NIT), as.rsi, mo = "E. coli", uti = TRUE)
}
# For CLEANING existing R/SI values ------------------------------------
@ -219,22 +229,22 @@ as.rsi(c("S", "I", "R", "A", "B", "C"))
as.rsi("<= 0.002; S") # will return "S"
rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370)))
is.rsi(rsi_data)
plot(rsi_data) # for percentages
plot(rsi_data) # for percentages
barplot(rsi_data) # for frequencies
# the dplyr way
if (require("dplyr")) {
example_isolates \%>\%
mutate_at(vars(PEN:RIF), as.rsi)
# same:
# same:
example_isolates \%>\%
as.rsi(PEN:RIF)
# fastest way to transform all columns with already valid AMR results to class `rsi`:
example_isolates \%>\%
mutate_if(is.rsi.eligible, as.rsi)
# since dplyr 1.0.0, this can also be:
# since dplyr 1.0.0, this can also be:
# example_isolates \%>\%
# mutate(across(where(is.rsi.eligible), as.rsi))
}

View File

@ -71,7 +71,7 @@ Abbreviations of return values when using \code{property = "U"} (unit):
}
\examples{
\donttest{
if (requireNamespace("curl") && requireNamespace("rvest") && requireNamespace("xml2")) {
if (requireNamespace("curl") && requireNamespace("rvest") && requireNamespace("xml2")) {
# oral DDD (Defined Daily Dose) of amoxicillin
atc_online_property("J01CA04", "DDD", "O")
atc_online_ddd(ab_atc("amox"))

View File

@ -70,12 +70,17 @@ head(x)
format(x, translate_ab = "name (atc)")
# Use FUN to change to transformation of microorganism codes
bug_drug_combinations(example_isolates,
FUN = mo_gramstain)
bug_drug_combinations(example_isolates,
FUN = function(x) ifelse(x == as.mo("Escherichia coli"),
"E. coli",
"Others"))
FUN = mo_gramstain
)
bug_drug_combinations(example_isolates,
FUN = function(x) {
ifelse(x == as.mo("Escherichia coli"),
"E. coli",
"Others"
)
}
)
}
}

View File

@ -129,9 +129,9 @@ Using \code{only_all_tested} has no impact when only using one antibiotic as inp
# run ?example_isolates for more info.
# base R ------------------------------------------------------------
count_resistant(example_isolates$AMX) # counts "R"
count_resistant(example_isolates$AMX) # counts "R"
count_susceptible(example_isolates$AMX) # counts "S" and "I"
count_all(example_isolates$AMX) # counts "S", "I" and "R"
count_all(example_isolates$AMX) # counts "S", "I" and "R"
# be more specific
count_S(example_isolates$AMX)
@ -156,36 +156,38 @@ susceptibility(example_isolates$AMX) * n_rsi(example_isolates$AMX)
if (require("dplyr")) {
example_isolates \%>\%
group_by(ward) \%>\%
summarise(R = count_R(CIP),
I = count_I(CIP),
S = count_S(CIP),
n1 = count_all(CIP), # the actual total; sum of all three
n2 = n_rsi(CIP), # same - analogous to n_distinct
total = n()) # NOT the number of tested isolates!
summarise(
R = count_R(CIP),
I = count_I(CIP),
S = count_S(CIP),
n1 = count_all(CIP), # the actual total; sum of all three
n2 = n_rsi(CIP), # same - analogous to n_distinct
total = n()
) # NOT the number of tested isolates!
# Number of available isolates for a whole antibiotic class
# (i.e., in this data set columns GEN, TOB, AMK, KAN)
example_isolates \%>\%
group_by(ward) \%>\%
summarise(across(aminoglycosides(), n_rsi))
# Count co-resistance between amoxicillin/clav acid and gentamicin,
# so we can see that combination therapy does a lot more than mono therapy.
# Please mind that `susceptibility()` calculates percentages right away instead.
example_isolates \%>\% count_susceptible(AMC) # 1433
example_isolates \%>\% count_all(AMC) # 1879
example_isolates \%>\% count_all(AMC) # 1879
example_isolates \%>\% count_susceptible(GEN) # 1399
example_isolates \%>\% count_all(GEN) # 1855
example_isolates \%>\% count_all(GEN) # 1855
example_isolates \%>\% count_susceptible(AMC, GEN) # 1764
example_isolates \%>\% count_all(AMC, GEN) # 1936
example_isolates \%>\% count_all(AMC, GEN) # 1936
# Get number of S+I vs. R immediately of selected columns
example_isolates \%>\%
select(AMX, CIP) \%>\%
count_df(translate = FALSE)
# It also supports grouping variables
example_isolates \%>\%
select(ward, AMX, CIP) \%>\%

View File

@ -109,19 +109,24 @@ It is possible to define antibiotic groups instead of single antibiotics for the
}
\examples{
x <- custom_eucast_rules(AMC == "R" & genus == "Klebsiella" ~ aminopenicillins == "R",
AMC == "I" & genus == "Klebsiella" ~ aminopenicillins == "I")
x <- custom_eucast_rules(
AMC == "R" & genus == "Klebsiella" ~ aminopenicillins == "R",
AMC == "I" & genus == "Klebsiella" ~ aminopenicillins == "I"
)
x
# run the custom rule set (verbose = TRUE will return a logbook instead of the data set):
eucast_rules(example_isolates,
rules = "custom",
custom_rules = x,
info = FALSE,
verbose = TRUE)
rules = "custom",
custom_rules = x,
info = FALSE,
verbose = TRUE
)
# combine rule sets
x2 <- c(x,
custom_eucast_rules(TZP == "R" ~ carbapenems == "R"))
x2 <- c(
x,
custom_eucast_rules(TZP == "R" ~ carbapenems == "R")
)
x2
}

View File

@ -136,19 +136,23 @@ All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/S
\examples{
\donttest{
a <- data.frame(mo = c("Staphylococcus aureus",
"Enterococcus faecalis",
"Escherichia coli",
"Klebsiella pneumoniae",
"Pseudomonas aeruginosa"),
VAN = "-", # Vancomycin
AMX = "-", # Amoxicillin
COL = "-", # Colistin
CAZ = "-", # Ceftazidime
CXM = "-", # Cefuroxime
PEN = "S", # Benzylpenicillin
FOX = "S", # Cefoxitin
stringsAsFactors = FALSE)
a <- data.frame(
mo = c(
"Staphylococcus aureus",
"Enterococcus faecalis",
"Escherichia coli",
"Klebsiella pneumoniae",
"Pseudomonas aeruginosa"
),
VAN = "-", # Vancomycin
AMX = "-", # Amoxicillin
COL = "-", # Colistin
CAZ = "-", # Ceftazidime
CXM = "-", # Cefuroxime
PEN = "S", # Benzylpenicillin
FOX = "S", # Cefoxitin
stringsAsFactors = FALSE
)
head(a)

View File

@ -177,22 +177,20 @@ if (require("dplyr")) {
# filter on first isolates using dplyr:
example_isolates \%>\%
filter(first_isolate())
}
if (require("dplyr")) {
# short-hand version:
example_isolates \%>\%
filter_first_isolate(info = FALSE)
}
if (require("dplyr")) {
# flag the first isolates per group:
example_isolates \%>\%
group_by(ward) \%>\%
mutate(first = first_isolate()) \%>\%
select(ward, date, patient, mo, first)
# flag the first isolates per group:
example_isolates \%>\%
group_by(ward) \%>\%
mutate(first = first_isolate()) \%>\%
select(ward, date, patient, mo, first)
}
}
}

View File

@ -37,68 +37,72 @@ The \code{dplyr} package is not required for these functions to work, but these
# See ?example_isolates
df <- example_isolates[sample(seq_len(2000), size = 200), ]
get_episode(df$date, episode_days = 60) # indices
get_episode(df$date, episode_days = 60) # indices
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
# filter on results from the third 60-day episode only, using base R
df[which(get_episode(df$date, 60) == 3), ]
# the functions also work for less than a day, e.g. to include one per hour:
get_episode(c(Sys.time(),
Sys.time() + 60 * 60),
episode_days = 1/24)
get_episode(c(
Sys.time(),
Sys.time() + 60 * 60
),
episode_days = 1 / 24
)
\donttest{
if (require("dplyr")) {
# is_new_episode() can also be used in dplyr verbs to determine patient
# episodes based on any (combination of) grouping variables:
df \%>\%
mutate(condition = sample(x = c("A", "B", "C"),
size = 200,
replace = TRUE)) \%>\%
mutate(condition = sample(
x = c("A", "B", "C"),
size = 200,
replace = TRUE
)) \%>\%
group_by(condition) \%>\%
mutate(new_episode = is_new_episode(date, 365)) \%>\%
select(patient, date, condition, new_episode)
}
if (require("dplyr")) {
df \%>\%
group_by(ward, patient) \%>\%
transmute(date,
patient,
new_index = get_episode(date, 60),
new_logical = is_new_episode(date, 60))
transmute(date,
patient,
new_index = get_episode(date, 60),
new_logical = is_new_episode(date, 60)
)
}
if (require("dplyr")) {
df \%>\%
group_by(ward) \%>\%
summarise(n_patients = n_distinct(patient),
n_episodes_365 = sum(is_new_episode(date, episode_days = 365)),
n_episodes_60 = sum(is_new_episode(date, episode_days = 60)),
n_episodes_30 = sum(is_new_episode(date, episode_days = 30)))
group_by(ward) \%>\%
summarise(
n_patients = n_distinct(patient),
n_episodes_365 = sum(is_new_episode(date, episode_days = 365)),
n_episodes_60 = sum(is_new_episode(date, episode_days = 60)),
n_episodes_30 = sum(is_new_episode(date, episode_days = 30))
)
}
if (require("dplyr")) {
# grouping on patients and microorganisms leads to the same
# results as first_isolate() when using 'episode-based':
x <- df \%>\%
filter_first_isolate(include_unknown = TRUE,
method = "episode-based")
filter_first_isolate(
include_unknown = TRUE,
method = "episode-based"
)
y <- df \%>\%
group_by(patient, mo) \%>\%
filter(is_new_episode(date, 365)) \%>\%
ungroup()
identical(x, y)
}
if (require("dplyr")) {
# but is_new_episode() has a lot more flexibility than first_isolate(),
# since you can now group on anything that seems relevant:
df \%>\%

View File

@ -114,24 +114,26 @@ The colours for labels and points can be changed by adding another scale layer f
\donttest{
if (require("dplyr")) {
# calculate the resistance per group first
resistance_data <- example_isolates \%>\%
group_by(order = mo_order(mo), # group on anything, like order
genus = mo_genus(mo)) \%>\% # and genus as we do here;
filter(n() >= 30) \%>\% # filter on only 30 results per group
summarise_if(is.rsi, resistance) # then get resistance of all drugs
# calculate the resistance per group first
resistance_data <- example_isolates \%>\%
group_by(
order = mo_order(mo), # group on anything, like order
genus = mo_genus(mo)
) \%>\% # and genus as we do here;
filter(n() >= 30) \%>\% # filter on only 30 results per group
summarise_if(is.rsi, resistance) # then get resistance of all drugs
# now conduct PCA for certain antimicrobial agents
pca_result <- resistance_data \%>\%
pca(AMC, CXM, CTX, CAZ, GEN, TOB, TMP, SXT)
pca_result <- resistance_data \%>\%
pca(AMC, CXM, CTX, CAZ, GEN, TOB, TMP, SXT)
summary(pca_result)
# old base R plotting method:
biplot(pca_result)
# new ggplot2 plotting method using this package:
ggplot_pca(pca_result)
if (require("ggplot2")) {
ggplot_pca(pca_result) +
scale_colour_viridis_d() +

View File

@ -143,14 +143,13 @@ At default, the names of antibiotics will be shown on the plots using \code{\lin
\examples{
\donttest{
if (require("ggplot2") && require("dplyr")) {
# get antimicrobial results for drugs against a UTI:
ggplot(example_isolates \%>\% select(AMX, NIT, FOS, TMP, CIP)) +
geom_rsi()
}
if (require("ggplot2") && require("dplyr")) {
# prettify the plot using some additional functions:
df <- example_isolates \%>\% select(AMX, NIT, FOS, TMP, CIP)
ggplot(df) +
@ -159,35 +158,33 @@ if (require("ggplot2") && require("dplyr")) {
scale_rsi_colours() +
labels_rsi_count() +
theme_rsi()
}
if (require("ggplot2") && require("dplyr")) {
# or better yet, simplify this using the wrapper function - a single command:
example_isolates \%>\%
select(AMX, NIT, FOS, TMP, CIP) \%>\%
ggplot_rsi()
}
if (require("ggplot2") && require("dplyr")) {
# get only proportions and no counts:
example_isolates \%>\%
select(AMX, NIT, FOS, TMP, CIP) \%>\%
ggplot_rsi(datalabels = FALSE)
}
if (require("ggplot2") && require("dplyr")) {
# add other ggplot2 arguments as you like:
example_isolates \%>\%
select(AMX, NIT, FOS, TMP, CIP) \%>\%
ggplot_rsi(width = 0.5,
colour = "black",
size = 1,
linetype = 2,
alpha = 0.25)
ggplot_rsi(
width = 0.5,
colour = "black",
size = 1,
linetype = 2,
alpha = 0.25
)
}
if (require("ggplot2") && require("dplyr")) {
@ -195,55 +192,57 @@ if (require("ggplot2") && require("dplyr")) {
example_isolates \%>\%
select(AMX) \%>\%
ggplot_rsi(colours = c(SI = "yellow"))
}
if (require("ggplot2") && require("dplyr")) {
# but you can also use the built-in colour-blind friendly colours for
# your plots, where "S" is green, "I" is yellow and "R" is red:
data.frame(x = c("Value1", "Value2", "Value3"),
y = c(1, 2, 3),
z = c("Value4", "Value5", "Value6")) \%>\%
data.frame(
x = c("Value1", "Value2", "Value3"),
y = c(1, 2, 3),
z = c("Value4", "Value5", "Value6")
) \%>\%
ggplot() +
geom_col(aes(x = x, y = y, fill = z)) +
scale_rsi_colours(Value4 = "S", Value5 = "I", Value6 = "R")
}
if (require("ggplot2") && require("dplyr")) {
# resistance of ciprofloxacine per age group
example_isolates \%>\%
mutate(first_isolate = first_isolate()) \%>\%
filter(first_isolate == TRUE,
mo == as.mo("Escherichia coli")) \%>\%
filter(
first_isolate == TRUE,
mo == as.mo("Escherichia coli")
) \%>\%
# age_groups() is also a function in this AMR package:
group_by(age_group = age_groups(age)) \%>\%
select(age_group, CIP) \%>\%
ggplot_rsi(x = "age_group")
}
if (require("ggplot2") && require("dplyr")) {
# a shorter version which also adjusts data label colours:
example_isolates \%>\%
select(AMX, NIT, FOS, TMP, CIP) \%>\%
ggplot_rsi(colours = FALSE)
}
if (require("ggplot2") && require("dplyr")) {
# it also supports groups (don't forget to use the group var on `x` or `facet`):
example_isolates \%>\%
filter(mo_is_gram_negative(), ward != "Outpatient") \%>\%
filter(mo_is_gram_negative(), ward != "Outpatient") \%>\%
# select only UTI-specific drugs
select(ward, AMX, NIT, FOS, TMP, CIP) \%>\%
group_by(ward) \%>\%
ggplot_rsi(x = "ward",
facet = "antibiotic",
nrow = 1,
title = "AMR of Anti-UTI Drugs Per Ward",
x.title = "Ward",
datalabels = FALSE)
ggplot_rsi(
x = "ward",
facet = "antibiotic",
nrow = 1,
title = "AMR of Anti-UTI Drugs Per Ward",
x.title = "Ward",
datalabels = FALSE
)
}
}
}

View File

@ -30,8 +30,10 @@ This tries to find a column name in a data set based on information from the \li
You can look for an antibiotic (trade) name or abbreviation and it will search \code{x} and the \link{antibiotics} data set for any column containing a name or code of that antibiotic. \strong{Longer columns names take precedence over shorter column names.}
}
\examples{
df <- data.frame(amox = "S",
tetr = "R")
df <- data.frame(
amox = "S",
tetr = "R"
)
guess_ab_col(df, "amoxicillin")
# [1] "amox"
@ -43,8 +45,10 @@ guess_ab_col(df, "J01AA07", verbose = TRUE)
# [1] "tetr"
# WHONET codes
df <- data.frame(AMP_ND10 = "R",
AMC_ED20 = "S")
df <- data.frame(
AMP_ND10 = "R",
AMC_ED20 = "S"
)
guess_ab_col(df, "ampicillin")
# [1] "AMP_ND10"
guess_ab_col(df, "J01CR02")
@ -53,8 +57,10 @@ guess_ab_col(df, as.ab("augmentin"))
# [1] "AMC_ED20"
# Longer names take precendence:
df <- data.frame(AMP_ED2 = "S",
AMP_ED20 = "S")
df <- data.frame(
AMP_ED2 = "S",
AMP_ED20 = "S"
)
guess_ab_col(df, "ampicillin")
# [1] "AMP_ED20"
}

View File

@ -47,12 +47,18 @@ If the \code{dplyr} package is installed, their join functions will be used. Oth
left_join_microorganisms(as.mo("K. pneumoniae"))
left_join_microorganisms("B_KLBSL_PNMN")
df <- data.frame(date = seq(from = as.Date("2018-01-01"),
to = as.Date("2018-01-07"),
by = 1),
bacteria = as.mo(c("S. aureus", "MRSA", "MSSA", "STAAUR",
"E. coli", "E. coli", "E. coli")),
stringsAsFactors = FALSE)
df <- data.frame(
date = seq(
from = as.Date("2018-01-01"),
to = as.Date("2018-01-07"),
by = 1
),
bacteria = as.mo(c(
"S. aureus", "MRSA", "MSSA", "STAAUR",
"E. coli", "E. coli", "E. coli"
)),
stringsAsFactors = FALSE
)
colnames(df)
df_joined <- left_join_microorganisms(df, "bacteria")
@ -61,7 +67,7 @@ colnames(df_joined)
\donttest{
if (require("dplyr")) {
example_isolates \%>\%
left_join_microorganisms() \%>\%
left_join_microorganisms() \%>\%
colnames()
}
}

View File

@ -133,7 +133,7 @@ if (require("dplyr")) {
# and first WEIGHTED isolates
first_weighted = first_isolate(col_keyantimicrobials = "keyab")
)
# Check the difference in this data set, 'weighted' results in more isolates:
sum(my_patients$first_regular, na.rm = TRUE)
sum(my_patients$first_weighted, na.rm = TRUE)

View File

@ -53,7 +53,7 @@ b \%like\% a
# also supports multiple patterns
a <- c("Test case", "Something different", "Yet another thing")
b <- c( "case", "diff", "yet")
b <- c("case", "diff", "yet")
a \%like\% b
a \%unlike\% b

View File

@ -137,7 +137,7 @@ You can print the rules set in the console for an overview. Colours will help re
#> 1. CIP is "R" and age is higher than 60 -> Elderly Type A
#> 2. ERY is "R" and age is higher than 60 -> Elderly Type B
#> 3. Otherwise -> Negative
#>
#>
#> Unmatched rows will return NA.
}\if{html}{\out{</div>}}
@ -153,10 +153,10 @@ table(x)
Rules can also be combined with other custom rules by using \code{\link[=c]{c()}}:
\if{html}{\out{<div class="sourceCode">}}\preformatted{x <- mdro(example_isolates,
guideline = c(custom,
guideline = c(custom,
custom_mdro_guideline(ERY == "R" & age > 50 ~ "Elderly Type C")))
table(x)
#> Negative Elderly Type A Elderly Type B Elderly Type C
#> Negative Elderly Type A Elderly Type B Elderly Type C
#> 961 198 732 109
}\if{html}{\out{</div>}}
@ -193,8 +193,11 @@ str(out)
table(out)
out <- mdro(example_isolates,
guideline = custom_mdro_guideline(AMX == "R" ~ "Custom MDRO 1",
VAN == "R" ~ "Custom MDRO 2"))
guideline = custom_mdro_guideline(
AMX == "R" ~ "Custom MDRO 1",
VAN == "R" ~ "Custom MDRO 2"
)
)
table(out)
\donttest{
@ -202,13 +205,12 @@ if (require("dplyr")) {
example_isolates \%>\%
mdro() \%>\%
table()
# no need to define `x` when used inside dplyr verbs:
example_isolates \%>\%
mutate(MDRO = mdro()) \%>\%
pull(MDRO) \%>\%
table()
}
}
}

View File

@ -48,8 +48,10 @@ All data sets in this \code{AMR} package (about microorganisms, antibiotics, R/S
as.mo("E. coli")
mo_uncertainties()
mo_matching_score(x = "E. coli",
n = c("Escherichia coli", "Entamoeba coli"))
mo_matching_score(
x = "E. coli",
n = c("Escherichia coli", "Entamoeba coli")
)
}
\author{
Dr Matthijs Berends

View File

@ -263,11 +263,13 @@ mo_type("Klebsiella pneumoniae")
mo_type("Klebsiella pneumoniae")
mo_fullname("S. pyogenes",
Lancefield = TRUE,
language = "de")
Lancefield = TRUE,
language = "de"
)
mo_fullname("S. pyogenes",
Lancefield = TRUE,
language = "nl")
Lancefield = TRUE,
language = "nl"
)
# other --------------------------------------------------------------------
@ -278,7 +280,7 @@ mo_is_yeast(c("Candida", "Trichophyton", "Klebsiella"))
if (require("dplyr")) {
example_isolates \%>\%
filter(mo_is_gram_positive())
example_isolates \%>\%
filter(mo_is_intrinsic_resistant(ab = "vanco"))
}

View File

@ -51,7 +51,7 @@ We save it as \code{"home/me/ourcodes.xlsx"}. Now we have to set it as a source:
\if{html}{\out{<div class="sourceCode">}}\preformatted{set_mo_source("home/me/ourcodes.xlsx")
#> NOTE: Created mo_source file '/Users/me/mo_source.rds' (0.3 kB) from
#> '/Users/me/Documents/ourcodes.xlsx' (9 kB), columns
#> '/Users/me/Documents/ourcodes.xlsx' (9 kB), columns
#> "Organisation XYZ" and "mo"
}\if{html}{\out{</div>}}
@ -88,7 +88,7 @@ If we edit the Excel file by, let's say, adding row 4 like this:
...any new usage of an MO function in this package will update your data file:
\if{html}{\out{<div class="sourceCode">}}\preformatted{as.mo("lab_mo_ecoli")
#> NOTE: Updated mo_source file '/Users/me/mo_source.rds' (0.3 kB) from
#> NOTE: Updated mo_source file '/Users/me/mo_source.rds' (0.3 kB) from
#> '/Users/me/Documents/ourcodes.xlsx' (9 kB), columns
#> "Organisation XYZ" and "mo"
#> Class <mo>

View File

@ -65,25 +65,27 @@ The result of the \code{\link[=pca]{pca()}} function is a \link{prcomp} object,
\donttest{
if (require("dplyr")) {
# calculate the resistance per group first
resistance_data <- example_isolates \%>\%
group_by(order = mo_order(mo), # group on anything, like order
genus = mo_genus(mo)) \%>\% # and genus as we do here;
filter(n() >= 30) \%>\% # filter on only 30 results per group
summarise_if(is.rsi, resistance) # then get resistance of all drugs
# calculate the resistance per group first
resistance_data <- example_isolates \%>\%
group_by(
order = mo_order(mo), # group on anything, like order
genus = mo_genus(mo)
) \%>\% # and genus as we do here;
filter(n() >= 30) \%>\% # filter on only 30 results per group
summarise_if(is.rsi, resistance) # then get resistance of all drugs
# now conduct PCA for certain antimicrobial agents
pca_result <- resistance_data \%>\%
pca(AMC, CXM, CTX, CAZ, GEN, TOB, TMP, SXT)
pca_result <- resistance_data \%>\%
pca(AMC, CXM, CTX, CAZ, GEN, TOB, TMP, SXT)
pca_result
summary(pca_result)
# old base R plotting method:
biplot(pca_result)
# new ggplot2 plotting method using this package:
ggplot_pca(pca_result)
if (require("ggplot2")) {
ggplot_pca(pca_result) +
scale_colour_viridis_d() +

View File

@ -144,7 +144,7 @@ This AMR package honours this (new) insight. Use \code{\link[=susceptibility]{su
# run ?example_isolates for more info.
# base R ------------------------------------------------------------
resistance(example_isolates$AMX) # determines \%R
resistance(example_isolates$AMX) # determines \%R
susceptibility(example_isolates$AMX) # determines \%S+I
# be more specific
@ -159,55 +159,65 @@ proportion_R(example_isolates$AMX)
if (require("dplyr")) {
example_isolates \%>\%
group_by(ward) \%>\%
summarise(r = resistance(CIP),
n = n_rsi(CIP)) # n_rsi works like n_distinct in dplyr, see ?n_rsi
summarise(
r = resistance(CIP),
n = n_rsi(CIP)
) # n_rsi works like n_distinct in dplyr, see ?n_rsi
example_isolates \%>\%
group_by(ward) \%>\%
summarise(R = resistance(CIP, as_percent = TRUE),
SI = susceptibility(CIP, as_percent = TRUE),
n1 = count_all(CIP), # the actual total; sum of all three
n2 = n_rsi(CIP), # same - analogous to n_distinct
total = n()) # NOT the number of tested isolates!
summarise(
R = resistance(CIP, as_percent = TRUE),
SI = susceptibility(CIP, as_percent = TRUE),
n1 = count_all(CIP), # the actual total; sum of all three
n2 = n_rsi(CIP), # same - analogous to n_distinct
total = n()
) # NOT the number of tested isolates!
# Calculate co-resistance between amoxicillin/clav acid and gentamicin,
# so we can see that combination therapy does a lot more than mono therapy:
example_isolates \%>\% susceptibility(AMC) # \%SI = 76.3\%
example_isolates \%>\% count_all(AMC) # n = 1879
example_isolates \%>\% susceptibility(GEN) # \%SI = 75.4\%
example_isolates \%>\% count_all(GEN) # n = 1855
example_isolates \%>\% susceptibility(AMC) # \%SI = 76.3\%
example_isolates \%>\% count_all(AMC) # n = 1879
example_isolates \%>\% susceptibility(GEN) # \%SI = 75.4\%
example_isolates \%>\% count_all(GEN) # n = 1855
example_isolates \%>\% susceptibility(AMC, GEN) # \%SI = 94.1\%
example_isolates \%>\% count_all(AMC, GEN) # n = 1939
example_isolates \%>\% count_all(AMC, GEN) # n = 1939
# See Details on how `only_all_tested` works. Example:
example_isolates \%>\%
summarise(numerator = count_susceptible(AMC, GEN),
denominator = count_all(AMC, GEN),
proportion = susceptibility(AMC, GEN))
summarise(
numerator = count_susceptible(AMC, GEN),
denominator = count_all(AMC, GEN),
proportion = susceptibility(AMC, GEN)
)
example_isolates \%>\%
summarise(numerator = count_susceptible(AMC, GEN, only_all_tested = TRUE),
denominator = count_all(AMC, GEN, only_all_tested = TRUE),
proportion = susceptibility(AMC, GEN, only_all_tested = TRUE))
summarise(
numerator = count_susceptible(AMC, GEN, only_all_tested = TRUE),
denominator = count_all(AMC, GEN, only_all_tested = TRUE),
proportion = susceptibility(AMC, GEN, only_all_tested = TRUE)
)
example_isolates \%>\%
group_by(ward) \%>\%
summarise(cipro_p = susceptibility(CIP, as_percent = TRUE),
cipro_n = count_all(CIP),
genta_p = susceptibility(GEN, as_percent = TRUE),
genta_n = count_all(GEN),
combination_p = susceptibility(CIP, GEN, as_percent = TRUE),
combination_n = count_all(CIP, GEN))
summarise(
cipro_p = susceptibility(CIP, as_percent = TRUE),
cipro_n = count_all(CIP),
genta_p = susceptibility(GEN, as_percent = TRUE),
genta_n = count_all(GEN),
combination_p = susceptibility(CIP, GEN, as_percent = TRUE),
combination_n = count_all(CIP, GEN)
)
# Get proportions S/I/R immediately of all rsi columns
example_isolates \%>\%
select(AMX, CIP) \%>\%
proportion_df(translate = FALSE)
# It also supports grouping variables
# (use rsi_df to also include the count)
example_isolates \%>\%

View File

@ -42,12 +42,12 @@ random_rsi(25)
\donttest{
# make the random generation more realistic by setting a bug and/or drug:
random_mic(25, "Klebsiella pneumoniae") # range 0.0625-64
random_mic(25, "Klebsiella pneumoniae", "meropenem") # range 0.0625-16
random_mic(25, "Klebsiella pneumoniae") # range 0.0625-64
random_mic(25, "Klebsiella pneumoniae", "meropenem") # range 0.0625-16
random_mic(25, "Streptococcus pneumoniae", "meropenem") # range 0.0625-4
random_disk(25, "Klebsiella pneumoniae") # range 8-50
random_disk(25, "Klebsiella pneumoniae", "ampicillin") # range 11-17
random_disk(25, "Klebsiella pneumoniae") # range 8-50
random_disk(25, "Klebsiella pneumoniae", "ampicillin") # range 11-17
random_disk(25, "Streptococcus pneumoniae", "ampicillin") # range 12-27
}
}

View File

@ -126,10 +126,11 @@ This AMR package honours this (new) insight. Use \code{\link[=susceptibility]{su
}
\examples{
x <- resistance_predict(example_isolates,
col_ab = "AMX",
year_min = 2010,
model = "binomial")
x <- resistance_predict(example_isolates,
col_ab = "AMX",
year_min = 2010,
model = "binomial"
)
plot(x)
\donttest{
if (require("ggplot2")) {
@ -151,14 +152,15 @@ if (require("dplyr")) {
# create nice plots with ggplot2 yourself
if (require("dplyr") && require("ggplot2")) {
data <- example_isolates \%>\%
filter(mo == as.mo("E. coli")) \%>\%
resistance_predict(col_ab = "AMX",
col_date = "date",
model = "binomial",
info = FALSE,
minimum = 15)
resistance_predict(
col_ab = "AMX",
col_date = "date",
model = "binomial",
info = FALSE,
minimum = 15
)
head(data)
autoplot(data)
}