mirror of
https://github.com/msberends/AMR.git
synced 2025-07-09 17:41:59 +02:00
Welcome C++!
This commit is contained in:
@ -30,3 +30,4 @@ as.mic("<=0.002; R") # will return <=0.002
|
||||
plot(mic_data)
|
||||
barplot(mic_data)
|
||||
}
|
||||
\keyword{mic}
|
||||
|
@ -29,3 +29,4 @@ as.rsi("<= 0.002; R") # will return R
|
||||
plot(rsi_data) # for percentages
|
||||
barplot(rsi_data) # for frequencies
|
||||
}
|
||||
\keyword{rsi}
|
||||
|
@ -4,11 +4,15 @@
|
||||
\alias{g.test}
|
||||
\title{\emph{G}-test for Count Data}
|
||||
\source{
|
||||
This code is almost perfectly equal to \code{\link{chisq.test}}; only the calculation of the statistic was changed to \code{2 * sum(x * log(x / E))} and Yates' continuity correction was deleted.
|
||||
This code is almost identical to \code{\link{chisq.test}}, except that:
|
||||
\itemize{
|
||||
\item{The calculation of the statistic was changed to \code{2 * sum(x * log(x / E))}}
|
||||
\item{Yates' continuity correction was removed as it does not apply to a \emph{G}-test}
|
||||
\item{The possibility to simulate p values with \code{simulate.p.value} was removed}
|
||||
}
|
||||
}
|
||||
\usage{
|
||||
g.test(x, y = NULL, p = rep(1/length(x), length(x)), rescale.p = FALSE,
|
||||
simulate.p.value = FALSE, B = 2000)
|
||||
g.test(x, y = NULL, p = rep(1/length(x), length(x)), rescale.p = FALSE)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{a numeric vector or matrix. \code{x} and \code{y} can also
|
||||
@ -23,12 +27,6 @@ g.test(x, y = NULL, p = rep(1/length(x), length(x)), rescale.p = FALSE,
|
||||
\item{rescale.p}{a logical scalar; if TRUE then \code{p} is rescaled
|
||||
(if necessary) to sum to 1. If \code{rescale.p} is FALSE, and
|
||||
\code{p} does not sum to 1, an error is given.}
|
||||
|
||||
\item{simulate.p.value}{a logical indicating whether to compute
|
||||
p-values by Monte Carlo simulation.}
|
||||
|
||||
\item{B}{an integer specifying the number of replicates used in the
|
||||
Monte Carlo test.}
|
||||
}
|
||||
\value{
|
||||
A list with class \code{"htest"} containing the following
|
||||
@ -58,7 +56,7 @@ If \code{x} is a matrix with one row or column, or if \code{x} is a vector and \
|
||||
|
||||
If \code{x} is a matrix with at least two rows and columns, it is taken as a two-dimensional contingency table: the entries of \code{x} must be non-negative integers. Otherwise, \code{x} and \code{y} must be vectors or factors of the same length; cases with missing values are removed, the objects are coerced to factors, and the contingency table is computed from these. Then Pearson's chi-squared test is performed of the null hypothesis that the joint distribution of the cell counts in a 2-dimensional contingency table is the product of the row and column marginals.
|
||||
|
||||
If \code{simulate.p.value} is \code{FALSE}, the p-value is computed from the asymptotic chi-squared distribution of the test statistic. Otherwise the p-value is computed for a Monte Carlo test (Hope, 1968) with \code{B} replicates.
|
||||
The p-value is computed from the asymptotic chi-squared distribution of the test statistic.
|
||||
|
||||
In the contingency table case simulation is done by random sampling from the set of all contingency tables with given marginals, and works only if the marginals are strictly positive. Note that this is not the usual sampling situation assumed for a chi-squared test (like the \emph{G}-test) but rather that for Fisher's exact test.
|
||||
|
||||
|
108
man/resistance.Rd
Normal file
108
man/resistance.Rd
Normal file
@ -0,0 +1,108 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/resistance.R
|
||||
\name{resistance}
|
||||
\alias{resistance}
|
||||
\alias{susceptibility}
|
||||
\alias{n_rsi}
|
||||
\alias{rsi}
|
||||
\title{Calculate resistance of isolates}
|
||||
\usage{
|
||||
resistance(ab, include_I = TRUE, minimum = 30, as_percent = FALSE)
|
||||
|
||||
susceptibility(ab1, ab2 = NULL, include_I = FALSE, minimum = 30,
|
||||
as_percent = FALSE)
|
||||
|
||||
n_rsi(ab1, ab2 = NULL)
|
||||
|
||||
rsi(ab1, ab2 = NULL, interpretation = "IR", minimum = 30,
|
||||
as_percent = FALSE)
|
||||
}
|
||||
\arguments{
|
||||
\item{ab, ab1, ab2}{vector of antibiotic interpretations, they will be transformed internally with \code{\link{as.rsi}}}
|
||||
|
||||
\item{include_I}{logical to indicate whether antimicrobial interpretations of "I" should be included}
|
||||
|
||||
\item{minimum}{minimal amount of available isolates. Any number lower than \code{minimum} will return \code{NA}.}
|
||||
|
||||
\item{as_percent}{logical to indicate whether the output must be returned as percent (text), will else be a double}
|
||||
|
||||
\item{interpretation}{antimicrobial interpretation}
|
||||
}
|
||||
\value{
|
||||
Double or, when \code{as_percent = TRUE}, a character.
|
||||
}
|
||||
\description{
|
||||
These functions can be used to calculate the (co-)resistance of microbial isolates (i.e. percentage S, SI, I, IR or R). All functions can be used in \code{dplyr}s \code{\link[dplyr]{summarise}} and support grouped variables, see \emph{Examples}.
|
||||
}
|
||||
\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.
|
||||
|
||||
All return values are calculated using hybrid evaluation (i.e. using C++), which makes these functions 60-65 times faster than in \code{AMR} v0.2.0 and below. The \code{rsi} function is available for backwards compatibility and deprecated. It now uses the \code{resistance} and \code{susceptibility} functions internally, based on the \code{interpretation} parameter.
|
||||
\if{html}{
|
||||
\cr
|
||||
To calculate the probability (\emph{p}) of susceptibility of one antibiotic, we use this formula:
|
||||
\out{<div style="text-align: center">}\figure{mono_therapy.png}\out{</div>}
|
||||
To calculate the probability (\emph{p}) of susceptibility of more antibiotics (i.e. combination therapy), we need to check whether one of them has a susceptible result (as numerator) and count all cases where all antibiotics were tested (as denominator). \cr
|
||||
\cr
|
||||
For two antibiotics:
|
||||
\out{<div style="text-align: center">}\figure{combi_therapy_2.png}\out{</div>}
|
||||
\cr
|
||||
Theoretically for three antibiotics:
|
||||
\out{<div style="text-align: center">}\figure{combi_therapy_3.png}\out{</div>}
|
||||
}
|
||||
}
|
||||
\examples{
|
||||
library(dplyr)
|
||||
|
||||
septic_patients \%>\%
|
||||
group_by(hospital_id) \%>\%
|
||||
summarise(p = susceptibility(cipr),
|
||||
n = n_rsi(cipr)) # n_rsi works like n_distinct in dplyr
|
||||
|
||||
septic_patients \%>\%
|
||||
group_by(hospital_id) \%>\%
|
||||
summarise(cipro_p = susceptibility(cipr, as_percent = TRUE),
|
||||
cipro_n = n_rsi(cipr),
|
||||
genta_p = susceptibility(gent, as_percent = TRUE),
|
||||
genta_n = n_rsi(gent),
|
||||
combination_p = susceptibility(cipr, gent, as_percent = TRUE),
|
||||
combination_n = n_rsi(cipr, gent))
|
||||
|
||||
|
||||
# Calculate resistance
|
||||
resistance(septic_patients$amox)
|
||||
rsi(septic_patients$amox, interpretation = "IR") # deprecated
|
||||
|
||||
# Or susceptibility
|
||||
susceptibility(septic_patients$amox)
|
||||
rsi(septic_patients$amox, interpretation = "S") # deprecated
|
||||
|
||||
|
||||
# Calculate co-resistance between amoxicillin/clav acid and gentamicin,
|
||||
# so we can see that combination therapy does a lot more than mono therapy:
|
||||
susceptibility(septic_patients$amcl) # p = 67.8\%
|
||||
n_rsi(septic_patients$amcl) # n = 1641
|
||||
|
||||
susceptibility(septic_patients$gent) # p = 69.1\%
|
||||
n_rsi(septic_patients$gent) # n = 1863
|
||||
|
||||
with(septic_patients,
|
||||
susceptibility(amcl, gent)) # p = 90.6\%
|
||||
with(septic_patients,
|
||||
n_rsi(amcl, gent)) # n = 1580
|
||||
|
||||
\dontrun{
|
||||
# calculate current empiric combination therapy of Helicobacter gastritis:
|
||||
my_table \%>\%
|
||||
filter(first_isolate == TRUE,
|
||||
genus == "Helicobacter") \%>\%
|
||||
summarise(p = susceptibility(amox, metr), # amoxicillin with metronidazole
|
||||
n = n_rsi(amox, metr))
|
||||
}
|
||||
}
|
||||
\keyword{antibiotics}
|
||||
\keyword{isolate}
|
||||
\keyword{isolates}
|
||||
\keyword{resistance}
|
||||
\keyword{rsi_df}
|
||||
\keyword{susceptibility}
|
12
man/rsi_predict.Rd → man/resistance_predict.Rd
Executable file → Normal file
12
man/rsi_predict.Rd → man/resistance_predict.Rd
Executable file → Normal file
@ -1,9 +1,15 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/rsi_analysis.R
|
||||
\name{rsi_predict}
|
||||
% Please edit documentation in R/resistance.R
|
||||
\name{resistance_predict}
|
||||
\alias{resistance_predict}
|
||||
\alias{rsi_predict}
|
||||
\title{Predict antimicrobial resistance}
|
||||
\usage{
|
||||
resistance_predict(tbl, col_ab, col_date,
|
||||
year_max = as.integer(format(as.Date(Sys.Date()), "\%Y")) + 15,
|
||||
year_every = 1, model = "binomial", I_as_R = TRUE,
|
||||
preserve_measurements = TRUE, info = TRUE)
|
||||
|
||||
rsi_predict(tbl, col_ab, col_date,
|
||||
year_max = as.integer(format(as.Date(Sys.Date()), "\%Y")) + 15,
|
||||
year_every = 1, model = "binomial", I_as_R = TRUE,
|
||||
@ -74,5 +80,5 @@ septic_patients \%>\%
|
||||
|
||||
}
|
||||
\seealso{
|
||||
\code{\link{lm}} \cr \code{\link{glm}}
|
||||
\code{\link{resistance}} \cr \code{\link{lm}} \code{\link{glm}}
|
||||
}
|
96
man/rsi.Rd
96
man/rsi.Rd
@ -1,96 +0,0 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/rsi_analysis.R
|
||||
\name{rsi}
|
||||
\alias{rsi}
|
||||
\alias{rsi_df}
|
||||
\alias{n_rsi}
|
||||
\title{Resistance of isolates}
|
||||
\usage{
|
||||
rsi(ab1, ab2 = NA, interpretation = "IR", minimum = 30,
|
||||
as_percent = FALSE, info = FALSE, warning = TRUE)
|
||||
|
||||
rsi_df(tbl, ab, interpretation = "IR", minimum = 30, as_percent = FALSE,
|
||||
info = TRUE, warning = TRUE)
|
||||
|
||||
n_rsi(ab1, ab2 = NA)
|
||||
}
|
||||
\arguments{
|
||||
\item{ab1, ab2}{vector of antibiotic interpretations, they will be transformed internally with \code{\link{as.rsi}}}
|
||||
|
||||
\item{interpretation}{antimicrobial interpretation of which the portion must be calculated. Valid values are \code{"S"}, \code{"SI"}, \code{"I"}, \code{"IR"} or \code{"R"}.}
|
||||
|
||||
\item{minimum}{minimal amount of available isolates. Any number lower than \code{minimum} will return \code{NA} with a warning (when \code{warning = TRUE}).}
|
||||
|
||||
\item{as_percent}{return output as percent (text), will else (at default) be a double}
|
||||
|
||||
\item{info}{calculate the amount of available isolates and print it, like \code{n = 423}}
|
||||
|
||||
\item{warning}{show a warning when the available amount of isolates is below \code{minimum}}
|
||||
|
||||
\item{tbl}{\code{data.frame} containing columns with antibiotic interpretations.}
|
||||
|
||||
\item{ab}{character vector with 1, 2 or 3 antibiotics that occur as column names in \code{tbl}, like \code{ab = c("amox", "amcl")}}
|
||||
}
|
||||
\value{
|
||||
Double or, when \code{as_percent = TRUE}, a character.
|
||||
}
|
||||
\description{
|
||||
This functions can be used to calculate the (co-)resistance of isolates (i.e. percentage S, SI, I, IR or R [of a vector] of isolates). The functions \code{rsi} and \code{n_rsi} can be used in \code{dplyr}s \code{\link[dplyr]{summarise}} and support grouped variables, see \emph{Examples}.
|
||||
}
|
||||
\details{
|
||||
Remember that you should filter your table to let it contain \strong{only first isolates}!
|
||||
\if{html}{
|
||||
\cr \cr
|
||||
To calculate the probability (\emph{p}) of susceptibility of one antibiotic, we use this formula:
|
||||
\out{<div style="text-align: center">}\figure{mono_therapy.png}\out{</div>}
|
||||
To calculate the probability (\emph{p}) of susceptibility of more antibiotics (i.e. combination therapy), we need to check whether one of them has a susceptible result (as numerator) and count all cases where all antibiotics were tested (as denominator). \cr
|
||||
For two antibiotics:
|
||||
\out{<div style="text-align: center">}\figure{combi_therapy_2.png}\out{</div>}
|
||||
\cr
|
||||
For three antibiotics:
|
||||
\out{<div style="text-align: center">}\figure{combi_therapy_3.png}\out{</div>}
|
||||
}
|
||||
}
|
||||
\examples{
|
||||
library(dplyr)
|
||||
|
||||
septic_patients \%>\%
|
||||
group_by(hospital_id) \%>\%
|
||||
summarise(cipro_susceptibility = rsi(cipr, interpretation = "S"),
|
||||
n = n_rsi(cipr)) # n_rsi works like n_distinct in dplyr
|
||||
|
||||
septic_patients \%>\%
|
||||
group_by(hospital_id) \%>\%
|
||||
summarise(cipro_S = rsi(cipr, interpretation = "S",
|
||||
as_percent = TRUE, warning = FALSE),
|
||||
cipro_n = n_rsi(cipr),
|
||||
genta_S = rsi(gent, interpretation = "S",
|
||||
as_percent = TRUE, warning = FALSE),
|
||||
genta_n = n_rsi(gent),
|
||||
combination_S = rsi(cipr, gent, interpretation = "S",
|
||||
as_percent = TRUE, warning = FALSE),
|
||||
combination_n = n_rsi(cipr, gent))
|
||||
|
||||
# calculate resistance
|
||||
rsi(septic_patients$amox)
|
||||
# or susceptibility
|
||||
rsi(septic_patients$amox, interpretation = "S")
|
||||
|
||||
# calculate co-resistance between amoxicillin/clav acid and gentamicin,
|
||||
# so we can review that combination therapy does a lot more than mono therapy:
|
||||
septic_patients \%>\% rsi_df(ab = "amcl", interpretation = "S") # = 67.8\%
|
||||
septic_patients \%>\% rsi_df(ab = "gent", interpretation = "S") # = 69.1\%
|
||||
septic_patients \%>\% rsi_df(ab = c("amcl", "gent"), interpretation = "S") # = 90.6\%
|
||||
|
||||
\dontrun{
|
||||
# calculate current empiric combination therapy of Helicobacter gastritis:
|
||||
my_table \%>\%
|
||||
filter(first_isolate == TRUE,
|
||||
genus == "Helicobacter") \%>\%
|
||||
rsi_df(ab = c("amox", "metr")) # amoxicillin with metronidazole
|
||||
}
|
||||
}
|
||||
\keyword{antibiotics}
|
||||
\keyword{isolate}
|
||||
\keyword{isolates}
|
||||
\keyword{rsi}
|
Reference in New Issue
Block a user