1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-08 13:21:50 +02:00

count_* functions

This commit is contained in:
2018-08-22 00:02:26 +02:00
parent e47e77febc
commit 3e87c8f409
16 changed files with 504 additions and 59 deletions

View File

@ -3,11 +3,14 @@
\name{as.rsi}
\alias{as.rsi}
\alias{is.rsi}
\alias{is.rsi.eligible}
\title{Class 'rsi'}
\usage{
as.rsi(x)
is.rsi(x)
is.rsi.eligible(x)
}
\arguments{
\item{x}{vector}
@ -18,6 +21,9 @@ Ordered factor with new class \code{rsi} and new attribute \code{package}
\description{
This transforms a vector to a new class \code{rsi}, which is an ordered factor with levels \code{S < I < R}. Invalid antimicrobial interpretations will be translated as \code{NA} with a warning.
}
\details{
The function \code{is.rsi.eligible} returns \code{TRUE} when a columns contains only valid antimicrobial interpretations (S and/or I and/or R), and \code{FALSE} otherwise.
}
\examples{
rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370)))
rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370), "A", "B", "C"))
@ -29,6 +35,12 @@ as.rsi("<= 0.002; S") # will return S
plot(rsi_data) # for percentages
barplot(rsi_data) # for frequencies
freq(rsi_data) # frequency table with informative header
# fastest way to transform all columns with already valid AB results to class `rsi`:
library(dplyr)
septic_patients \%>\%
mutate_if(is.rsi.eligible,
as.rsi)
}
\seealso{
\code{\link{as.mic}}

115
man/count.Rd Normal file
View File

@ -0,0 +1,115 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/count.R
\name{count}
\alias{count}
\alias{count_R}
\alias{count_IR}
\alias{count_I}
\alias{count_SI}
\alias{count_S}
\alias{count_df}
\title{Count isolates}
\source{
Wickham H. \strong{Tidy Data.} The Journal of Statistical Software, vol. 59, 2014. \url{http://vita.had.co.nz/papers/tidy-data.html}
}
\usage{
count_R(ab1, ab2 = NULL)
count_IR(ab1, ab2 = NULL)
count_I(ab1)
count_SI(ab1, ab2 = NULL)
count_S(ab1, ab2 = NULL)
count_df(data, translate_ab = getOption("get_antibiotic_names",
"official"))
}
\arguments{
\item{ab1}{vector of antibiotic interpretations, they will be transformed internally with \code{\link{as.rsi}} if needed}
\item{ab2}{like \code{ab}, a vector of antibiotic interpretations. Use this to calculate (the lack of) co-resistance: the probability where one of two drugs have a resistant or susceptible result. See Examples.}
\item{data}{a \code{data.frame} containing columns with class \code{rsi} (see \code{\link{as.rsi}})}
\item{translate_ab}{a column name of the \code{\link{antibiotics}} data set to translate the antibiotic abbreviations to, using \code{\link{abname}}. This can be set with \code{\link{getOption}("get_antibiotic_names")}.}
}
\value{
Integer
}
\description{
These functions can be used to count resistant/susceptible microbial isolates. All functions can be used in \code{dplyr}s \code{\link[dplyr]{summarise}} and support grouped variables, see \emph{Examples}.
\code{count_R} and \code{count_IR} can be used to count resistant isolates, \code{count_S} and \code{count_SI} can be used to count susceptible isolates.\cr
}
\details{
\strong{Remember that you should filter your table to let it contain only first isolates!} Use \code{\link{first_isolate}} to determine them in your data set.
These functions are meant to count isolates. Use the \code{\link{portion}_*} functions to calculate microbial resistance.
\code{count_df} takes any variable from \code{data} that has an \code{"rsi"} class (created with \code{\link{as.rsi}}) and counts the amounts of R, I and S. The resulting \emph{tidy data} (see Source) \code{data.frame} will have three rows (S/I/R) and a column for each variable with class \code{"rsi"}.
}
\examples{
# septic_patients is a data set available in the AMR package. It is true, genuine data.
?septic_patients
# Count resistant isolates
count_R(septic_patients$amox)
count_IR(septic_patients$amox)
# Or susceptibile isolates
count_S(septic_patients$amox)
count_SI(septic_patients$amox)
# Since n_rsi counts available isolates, you can
# calculate back to count e.g. non-susceptible isolates.
# This results in the same:
count_IR(septic_patients$amox)
portion_IR(septic_patients$amox) * n_rsi(septic_patients$amox)
library(dplyr)
septic_patients \%>\%
group_by(hospital_id) \%>\%
summarise(R = count_R(cipr),
I = count_I(cipr),
S = count_S(cipr),
n = n_rsi(cipr), # the actual total; sum of all three
total = n()) # NOT the amount of tested isolates!
# 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 `portion_S` calculates percentages right away instead.
count_S(septic_patients$amcl) # S = 1056 (67.3\%)
n_rsi(septic_patients$amcl) # n = 1570
count_S(septic_patients$gent) # S = 1363 (74.0\%)
n_rsi(septic_patients$gent) # n = 1842
with(septic_patients,
count_S(amcl, gent)) # S = 1385 (92.1\%)
with(septic_patients, # n = 1504
n_rsi(amcl, gent))
# Get portions S/I/R immediately of all rsi columns
septic_patients \%>\%
select(amox, cipr) \%>\%
count_df(translate = FALSE)
# It also supports grouping variables
septic_patients \%>\%
select(hospital_id, amox, cipr) \%>\%
group_by(hospital_id) \%>\%
count_df(translate = FALSE)
}
\seealso{
\code{\link{portion}_*} to calculate microbial resistance and susceptibility.\cr
\code{\link{n_rsi}} to count all cases where antimicrobial results are available.
}
\keyword{antibiotics}
\keyword{isolate}
\keyword{isolates}
\keyword{resistance}
\keyword{rsi}
\keyword{susceptibility}

View File

@ -9,12 +9,13 @@
\alias{theme_rsi}
\title{AMR bar plots with \code{ggplot}}
\usage{
ggplot_rsi(data, position = "stack", x = "Antibiotic",
ggplot_rsi(data, position = NULL, x = "Antibiotic",
fill = "Interpretation", facet = NULL, translate_ab = "official",
...)
fun = portion_df, ...)
geom_rsi(position = "stack", x = c("Antibiotic", "Interpretation"),
fill = "Interpretation", translate_ab = "official")
geom_rsi(position = NULL, x = c("Antibiotic", "Interpretation"),
fill = "Interpretation", translate_ab = "official",
fun = portion_df)
facet_rsi(facet = c("Interpretation", "Antibiotic"), ...)
@ -27,7 +28,7 @@ theme_rsi()
\arguments{
\item{data}{a \code{data.frame} with column(s) of class \code{"rsi"} (see \code{\link{as.rsi}})}
\item{position}{position adjustment of bars, either \code{"stack"} (default) or \code{"dodge"}}
\item{position}{position adjustment of bars, either \code{"stack"} (default when \code{fun} is \code{\link{portion_df}}) or \code{"dodge"} (default when \code{fun} is \code{\link{count_df}})}
\item{x}{variable to show on x axis, either \code{"Antibiotic"} (default) or \code{"Interpretation"} or a grouping variable}
@ -37,6 +38,8 @@ theme_rsi()
\item{translate_ab}{a column name of the \code{\link{antibiotics}} data set to translate the antibiotic abbreviations into, using \code{\link{abname}}. Default behaviour is to translate to official names according to the WHO. Use \code{translate_ab = FALSE} to disable translation.}
\item{fun}{function to transform \code{data}, either \code{\link{portion_df}} (default) or \code{\link{count_df}}}
\item{...}{other parameters passed on to \code{\link[ggplot2]{facet_wrap}}}
}
\description{
@ -46,7 +49,7 @@ Use these functions to create bar plots for antimicrobial resistance analysis. A
At default, the names of antibiotics will be shown on the plots using \code{\link{abname}}. This can be set with the option \code{get_antibiotic_names} (a logical value), so change it e.g. to \code{FALSE} with \code{options(get_antibiotic_names = FALSE)}.
\strong{The functions}\cr
\code{geom_rsi} will take any variable from the data that has an \code{rsi} class (created with \code{\link{as.rsi}}) using \code{\link{portion_df}} and will plot bars with the percentage R, I and S. The default behaviour is to have the bars stacked and to have the different antibiotics on the x axis.
\code{geom_rsi} will take any variable from the data that has an \code{rsi} class (created with \code{\link{as.rsi}}) using \code{fun} (\code{\link{portion_df}} at default, could also be \code{\link{count_df}}) and will plot bars with the percentage R, I and S. The default behaviour is to have the bars stacked and to have the different antibiotics on the x axis.
\code{facet_rsi} creates 2d plots (at default based on S/I/R) using \code{\link[ggplot2]{facet_wrap}}.
@ -79,6 +82,11 @@ ggplot(df) +
septic_patients \%>\%
select(amox, nitr, fosf, trim, cipr) \%>\%
ggplot_rsi()
# get counts instead of percentages:
septic_patients \%>\%
select(amox, nitr, fosf, trim, cipr) \%>\%
ggplot_rsi(fun = count_df)
\donttest{
# it also supports groups (don't forget to use the group on `x` or `facet`):
septic_patients \%>\%

View File

@ -26,7 +26,7 @@ portion_SI(ab1, ab2 = NULL, minimum = 30, as_percent = FALSE)
portion_S(ab1, ab2 = NULL, minimum = 30, as_percent = FALSE)
portion_df(data, translate_ab = getOption("get_antibiotic_names",
"official"))
"official"), minimum = 30, as_percent = FALSE)
}
\arguments{
\item{ab1}{vector of antibiotic interpretations, they will be transformed internally with \code{\link{as.rsi}} if needed}
@ -35,9 +35,9 @@ portion_df(data, translate_ab = getOption("get_antibiotic_names",
\item{minimum}{minimal amount of available isolates. Any number lower than \code{minimum} will return \code{NA}. The default number of \code{30} isolates is advised by the CLSI as best practice, see Source.}
\item{as_percent}{logical to indicate whether the output must be returned as percent (text), will else be a double}
\item{as_percent}{logical to indicate whether the output must be returned as a hundred fold with \% sign (a character). A value of \code{0.123456} will then be returned as \code{"12.3\%"}.}
\item{data}{a code{data.frame} containing columns with class \code{rsi} (see \code{\link{as.rsi}})}
\item{data}{a \code{data.frame} containing columns with class \code{rsi} (see \code{\link{as.rsi}})}
\item{translate_ab}{a column name of the \code{\link{antibiotics}} data set to translate the antibiotic abbreviations to, using \code{\link{abname}}. This can be set with \code{\link{getOption}("get_antibiotic_names")}.}
}
@ -52,6 +52,8 @@ These functions can be used to calculate the (co-)resistance of microbial isolat
\details{
\strong{Remember that you should filter your table to let it contain only first isolates!} Use \code{\link{first_isolate}} to determine them in your data set.
These functions are not meant to count isolates, but to calculate the portion of resistance/susceptibility. If a column has been transformed with \code{\link{as.rsi}}, just use e.g. \code{isolates[isolates == "R"]} to get the resistant ones. You could then calculate the \code{\link{length}} of it.
\code{portion_df} takes any variable from \code{data} that has an \code{"rsi"} class (created with \code{\link{as.rsi}}) and calculates the portions R, I and S. The resulting \emph{tidy data} (see Source) \code{data.frame} will have three rows (S/I/R) and a column for each variable with class \code{"rsi"}.
The old \code{\link{rsi}} function is still available for backwards compatibility but is deprecated.
@ -143,7 +145,8 @@ my_table \%>\%
}
}
\seealso{
\code{\link{n_rsi}} to count cases with antimicrobial results.
\code{\link[AMR]{count}_*} to count resistant and susceptibile isolates.\cr
\code{\link{n_rsi}} to count all cases where antimicrobial results are available.
}
\keyword{antibiotics}
\keyword{isolate}

View File

@ -16,7 +16,7 @@ rsi(ab1, ab2 = NULL, interpretation = "IR", minimum = 30,
\item{minimum}{minimal amount of available isolates. Any number lower than \code{minimum} will return \code{NA}. The default number of \code{30} isolates is advised by the CLSI as best practice, see Source.}
\item{as_percent}{logical to indicate whether the output must be returned as percent (text), will else be a double}
\item{as_percent}{logical to indicate whether the output must be returned as a hundred fold with \% sign (a character). A value of \code{0.123456} will then be returned as \code{"12.3\%"}.}
\item{...}{deprecated parameters to support usage on older versions}
}