2018-08-22 00:02:26 +02:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
# https://gitlab.com/msberends/AMR #
2018-08-22 00:02:26 +02:00
# #
# LICENCE #
2019-01-02 23:24:07 +01:00
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
2018-08-22 00:02:26 +02:00
# #
2019-01-02 23:24:07 +01:00
# 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. #
2019-04-05 18:47:39 +02:00
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
2018-08-22 00:02:26 +02:00
# ==================================================================== #
#' Count isolates
#'
2018-08-23 00:40:36 +02:00
#' @description These functions can be used to count resistant/susceptible microbial isolates. All functions support quasiquotation with pipes, can be used in \code{dplyr}s \code{\link[dplyr]{summarise}} and support grouped variables, see \emph{Examples}.
2018-08-22 00:02:26 +02:00
#'
#' \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
2018-10-12 16:35:18 +02:00
#' @param ... one or more vectors (or columns) with antibiotic interpretations. They will be transformed internally with \code{\link{as.rsi}} if needed.
2018-08-22 00:02:26 +02:00
#' @inheritParams portion
2019-05-13 10:10:16 +02:00
#' @inheritSection as.rsi Interpretation of S, I and R
2018-10-12 16:35:18 +02:00
#' @details These functions are meant to count isolates. Use the \code{\link{portion}_*} functions to calculate microbial resistance.
2018-08-22 00:02:26 +02:00
#'
2019-06-13 14:28:46 +02:00
#' The function \code{n_rsi} is an alias of \code{count_all}. They can be used to count all available isolates, i.e. where all input antibiotics have an available result (S, I or R). Their use is equal to \code{\link{n_distinct}}. Their function is equal to \code{count_S(...) + count_IR(...)}.
2018-08-22 00:02:26 +02:00
#'
2019-06-13 14:28:46 +02:00
#' The function \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 S, I and R. 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"}.
#'
2019-06-16 21:42:40 +02:00
#' The function \code{rsi_df} works exactly like \code{count_df}, but adds the percentage of S, I and R.
2019-07-01 14:03:15 +02:00
#' @inheritSection portion Combination therapy
2018-08-22 00:02:26 +02:00
#' @source Wickham H. \strong{Tidy Data.} The Journal of Statistical Software, vol. 59, 2014. \url{http://vita.had.co.nz/papers/tidy-data.html}
2018-10-12 16:35:18 +02:00
#' @seealso \code{\link{portion}_*} to calculate microbial resistance and susceptibility.
2018-08-22 00:02:26 +02:00
#' @keywords resistance susceptibility rsi antibiotics isolate isolates
#' @return Integer
#' @rdname count
#' @name count
#' @export
2019-01-02 23:24:07 +01:00
#' @inheritSection AMR Read more on our website!
2018-08-22 00:02:26 +02:00
#' @examples
2019-08-27 16:45:42 +02:00
#' # example_isolates is a data set available in the AMR package.
#' ?example_isolates
2018-08-22 00:02:26 +02:00
#'
#' # Count resistant isolates
2019-08-27 16:45:42 +02:00
#' count_R(example_isolates$AMX)
#' count_IR(example_isolates$AMX)
2018-08-22 00:02:26 +02:00
#'
2018-12-14 07:48:12 +01:00
#' # Or susceptible isolates
2019-08-27 16:45:42 +02:00
#' count_S(example_isolates$AMX)
#' count_SI(example_isolates$AMX)
2018-08-22 00:02:26 +02:00
#'
2018-10-12 16:35:18 +02:00
#' # Count all available isolates
2019-08-27 16:45:42 +02:00
#' count_all(example_isolates$AMX)
#' n_rsi(example_isolates$AMX)
2018-10-12 16:35:18 +02:00
#'
2018-08-22 00:02:26 +02:00
#' # Since n_rsi counts available isolates, you can
#' # calculate back to count e.g. non-susceptible isolates.
#' # This results in the same:
2019-08-27 16:45:42 +02:00
#' count_SI(example_isolates$AMX)
#' portion_SI(example_isolates$AMX) * n_rsi(example_isolates$AMX)
2018-08-22 00:02:26 +02:00
#'
#' library(dplyr)
2019-08-27 16:45:42 +02:00
#' example_isolates %>%
2018-08-22 00:02:26 +02:00
#' group_by(hospital_id) %>%
2019-05-10 16:44:59 +02:00
#' 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
2019-03-26 14:24:03 +01:00
#' total = n()) # NOT the number of tested isolates!
2018-08-22 00:02:26 +02:00
#'
#' # Count co-resistance between amoxicillin/clav acid and gentamicin,
#' # so we can see that combination therapy does a lot more than mono therapy.
2019-07-01 14:03:15 +02:00
#' # Please mind that `portion_SI` calculates percentages right away instead.
2019-08-27 16:45:42 +02:00
#' count_SI(example_isolates$AMC) # 1433
#' count_all(example_isolates$AMC) # 1879
2018-08-22 00:02:26 +02:00
#'
2019-08-27 16:45:42 +02:00
#' count_SI(example_isolates$GEN) # 1399
#' count_all(example_isolates$GEN) # 1855
2018-08-22 00:02:26 +02:00
#'
2019-08-27 16:45:42 +02:00
#' with(example_isolates,
2019-07-01 14:03:15 +02:00
#' count_SI(AMC, GEN)) # 1764
2019-08-27 16:45:42 +02:00
#' with(example_isolates,
2019-07-01 14:03:15 +02:00
#' n_rsi(AMC, GEN)) # 1936
2018-08-22 00:02:26 +02:00
#'
#' # Get portions S/I/R immediately of all rsi columns
2019-08-27 16:45:42 +02:00
#' example_isolates %>%
2019-05-10 16:44:59 +02:00
#' select(AMX, CIP) %>%
2018-08-22 00:02:26 +02:00
#' count_df(translate = FALSE)
#'
#' # It also supports grouping variables
2019-08-27 16:45:42 +02:00
#' example_isolates %>%
2019-05-10 16:44:59 +02:00
#' select(hospital_id, AMX, CIP) %>%
2018-08-22 00:02:26 +02:00
#' group_by(hospital_id) %>%
#' count_df(translate = FALSE)
#'
2019-07-01 14:03:15 +02:00
count_R <- function ( ... , only_all_tested = FALSE ) {
2018-08-23 00:40:36 +02:00
rsi_calc ( ... ,
2019-07-01 14:03:15 +02:00
ab_result = " R" ,
only_all_tested = only_all_tested ,
2018-08-22 00:02:26 +02:00
only_count = TRUE )
}
#' @rdname count
#' @export
2019-07-01 14:03:15 +02:00
count_IR <- function ( ... , only_all_tested = FALSE ) {
2018-08-23 00:40:36 +02:00
rsi_calc ( ... ,
2019-07-01 14:03:15 +02:00
ab_result = c ( " I" , " R" ) ,
only_all_tested = only_all_tested ,
2018-08-22 00:02:26 +02:00
only_count = TRUE )
}
#' @rdname count
#' @export
2019-07-01 14:03:15 +02:00
count_I <- function ( ... , only_all_tested = FALSE ) {
2018-08-23 00:40:36 +02:00
rsi_calc ( ... ,
2019-07-01 14:03:15 +02:00
ab_result = " I" ,
only_all_tested = only_all_tested ,
2018-08-22 00:02:26 +02:00
only_count = TRUE )
}
#' @rdname count
#' @export
2019-07-01 14:03:15 +02:00
count_SI <- function ( ... , only_all_tested = FALSE ) {
2018-08-23 00:40:36 +02:00
rsi_calc ( ... ,
2019-07-01 14:03:15 +02:00
ab_result = c ( " S" , " I" ) ,
only_all_tested = only_all_tested ,
2018-08-22 00:02:26 +02:00
only_count = TRUE )
}
#' @rdname count
#' @export
2019-07-01 14:03:15 +02:00
count_S <- function ( ... , only_all_tested = FALSE ) {
2018-08-23 00:40:36 +02:00
rsi_calc ( ... ,
2019-07-01 14:03:15 +02:00
ab_result = " S" ,
only_all_tested = only_all_tested ,
2018-08-22 00:02:26 +02:00
only_count = TRUE )
}
2018-10-12 16:35:18 +02:00
#' @rdname count
#' @export
2019-07-01 14:03:15 +02:00
count_all <- function ( ... , only_all_tested = FALSE ) {
rsi_calc ( ... ,
ab_result = c ( " S" , " I" , " R" ) ,
only_all_tested = only_all_tested ,
only_count = TRUE )
2018-10-12 16:35:18 +02:00
}
#' @rdname count
#' @export
2019-10-11 17:21:02 +02:00
n_rsi <- count_all
2018-10-12 16:35:18 +02:00
2018-08-22 00:02:26 +02:00
#' @rdname count
#' @export
count_df <- function ( data ,
2019-05-10 16:44:59 +02:00
translate_ab = " name" ,
language = get_locale ( ) ,
2019-05-13 10:10:16 +02:00
combine_SI = TRUE ,
2018-10-16 09:59:31 +02:00
combine_IR = FALSE ) {
2018-08-22 00:02:26 +02:00
2019-05-13 10:10:16 +02:00
rsi_calc_df ( type = " count" ,
data = data ,
translate_ab = translate_ab ,
language = language ,
combine_SI = combine_SI ,
combine_IR = combine_IR ,
combine_SI_missing = missing ( combine_SI ) )
2018-08-22 00:02:26 +02:00
}