AMR/R/count.R

252 lines
9.7 KiB
R
Raw Normal View History

2018-08-22 00:02:26 +02:00
# ==================================================================== #
# TITLE: #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
2018-08-22 00:02:26 +02:00
# #
# SOURCE CODE: #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2018-08-22 00:02:26 +02:00
# #
# PLEASE CITE THIS SOFTWARE AS: #
2022-10-05 09:12:22 +02:00
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C #
# (2022). AMR: An R Package for Working with Antimicrobial Resistance #
# Data. Journal of Statistical Software, 104(3), 1-31. #
2023-05-27 10:39:22 +02:00
# https://doi.org/10.18637/jss.v104.i03 #
2022-10-05 09:12:22 +02:00
# #
2022-12-27 15:16:15 +01:00
# Developed at the University of Groningen and the University Medical #
# Center Groningen in The Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
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. #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
2020-10-08 11:16:03 +02:00
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
2018-08-22 00:02:26 +02:00
# ==================================================================== #
#' Count Available Isolates
2018-08-22 00:02:26 +02:00
#'
#' @description These functions can be used to count resistant/susceptible microbial isolates. All functions support quasiquotation with pipes, can be used in `summarise()` from the `dplyr` package and also support grouped variables, see *Examples*.
2018-08-22 00:02:26 +02:00
#'
#' [count_resistant()] should be used to count resistant isolates, [count_susceptible()] should be used to count susceptible isolates.
2023-01-21 23:47:20 +01:00
#' @param ... one or more vectors (or columns) with antibiotic interpretations. They will be transformed internally with [as.sir()] if needed.
#' @inheritParams proportion
2023-01-21 23:47:20 +01:00
#' @inheritSection as.sir Interpretation of SIR
#' @details These functions are meant to count isolates. Use the [resistance()]/[susceptibility()] functions to calculate microbial resistance/susceptibility.
2022-08-28 10:31:50 +02:00
#'
#' The function [count_resistant()] is equal to the function [count_R()]. The function [count_susceptible()] is equal to the function [count_SI()].
2018-08-22 00:02:26 +02:00
#'
2023-01-21 23:47:20 +01:00
#' The function [n_sir()] is an alias of [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 `n_distinct()`. Their function is equal to `count_susceptible(...) + count_resistant(...)`.
2018-08-22 00:02:26 +02:00
#'
2023-01-21 23:47:20 +01:00
#' The function [count_df()] takes any variable from `data` that has an [`sir`] class (created with [as.sir()]) and counts the number of S's, I's and R's. It also supports grouped variables. The function [sir_df()] works exactly like [count_df()], but adds the percentage of S, I and R.
#' @inheritSection proportion Combination Therapy
#' @seealso [`proportion_*`][proportion] to calculate microbial resistance and susceptibility.
#' @return An [integer]
2018-08-22 00:02:26 +02:00
#' @rdname count
#' @name count
#' @export
#' @examples
#' # example_isolates is a data set available in the AMR package.
2022-08-21 16:37:20 +02:00
#' # run ?example_isolates for more info.
2022-08-28 10:31:50 +02:00
#'
2022-08-21 16:37:20 +02:00
#' # base R ------------------------------------------------------------
2022-08-28 10:31:50 +02:00
#' count_resistant(example_isolates$AMX) # counts "R"
#' count_susceptible(example_isolates$AMX) # counts "S" and "I"
2022-08-28 10:31:50 +02:00
#' count_all(example_isolates$AMX) # counts "S", "I" and "R"
2018-08-22 00:02:26 +02:00
#'
#' # be more specific
#' count_S(example_isolates$AMX)
#' count_SI(example_isolates$AMX)
#' count_I(example_isolates$AMX)
#' count_IR(example_isolates$AMX)
#' count_R(example_isolates$AMX)
2018-08-22 00:02:26 +02:00
#'
2018-10-12 16:35:18 +02:00
#' # Count all available isolates
#' count_all(example_isolates$AMX)
2023-01-21 23:47:20 +01:00
#' n_sir(example_isolates$AMX)
2018-10-12 16:35:18 +02:00
#'
2023-01-21 23:47:20 +01:00
#' # n_sir() is an alias of count_all().
#' # Since it counts all available isolates, you can
#' # calculate back to count e.g. susceptible isolates.
#' # These results are the same:
#' count_susceptible(example_isolates$AMX)
2023-01-21 23:47:20 +01:00
#' susceptibility(example_isolates$AMX) * n_sir(example_isolates$AMX)
2018-08-22 00:02:26 +02:00
#'
2022-08-21 16:37:20 +02:00
#' # dplyr -------------------------------------------------------------
2021-05-24 09:00:11 +02:00
#' \donttest{
2020-05-16 21:40:50 +02:00
#' if (require("dplyr")) {
2020-05-16 20:08:21 +02:00
#' example_isolates %>%
2022-08-27 20:49:37 +02:00
#' group_by(ward) %>%
2022-08-28 10:31:50 +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
2023-01-21 23:47:20 +01:00
#' n2 = n_sir(CIP), # same - analogous to n_distinct
2022-08-28 10:31:50 +02:00
#' 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 %>%
2022-08-27 20:49:37 +02:00
#' group_by(ward) %>%
2023-01-21 23:47:20 +01:00
#' summarise(across(aminoglycosides(), n_sir))
2022-08-28 10:31:50 +02:00
#'
2020-05-16 20:08:21 +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.
#' # Please mind that `susceptibility()` calculates percentages right away instead.
#' example_isolates %>% count_susceptible(AMC) # 1433
2022-08-28 10:31:50 +02:00
#' example_isolates %>% count_all(AMC) # 1879
#'
2020-05-16 20:08:21 +02:00
#' example_isolates %>% count_susceptible(GEN) # 1399
2022-08-28 10:31:50 +02:00
#' example_isolates %>% count_all(GEN) # 1855
#'
2020-05-16 20:08:21 +02:00
#' example_isolates %>% count_susceptible(AMC, GEN) # 1764
2022-08-28 10:31:50 +02:00
#' example_isolates %>% count_all(AMC, GEN) # 1936
#'
2020-05-16 20:08:21 +02:00
#' # Get number of S+I vs. R immediately of selected columns
#' example_isolates %>%
#' select(AMX, CIP) %>%
#' count_df(translate = FALSE)
2022-08-28 10:31:50 +02:00
#'
2020-05-16 20:08:21 +02:00
#' # It also supports grouping variables
#' example_isolates %>%
2022-08-27 20:49:37 +02:00
#' select(ward, AMX, CIP) %>%
#' group_by(ward) %>%
2020-05-16 20:08:21 +02:00
#' count_df(translate = FALSE)
#' }
2021-05-24 09:00:11 +02:00
#' }
count_resistant <- function(..., only_all_tested = FALSE) {
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc(...,
2022-08-28 10:31:50 +02:00
ab_result = "R",
only_all_tested = only_all_tested,
only_count = TRUE
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
}
#' @rdname count
#' @export
count_susceptible <- function(..., only_all_tested = FALSE) {
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc(...,
2022-08-28 10:31:50 +02:00
ab_result = c("S", "I"),
only_all_tested = only_all_tested,
only_count = TRUE
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
}
#' @rdname count
#' @export
count_R <- function(..., only_all_tested = FALSE) {
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc(...,
2022-08-28 10:31:50 +02:00
ab_result = "R",
only_all_tested = only_all_tested,
only_count = TRUE
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
2018-08-22 00:02:26 +02:00
}
#' @rdname count
#' @export
count_IR <- function(..., only_all_tested = FALSE) {
if (message_not_thrown_before("count_IR", entire_session = TRUE)) {
message_("Using `count_IR()` is discouraged; use `count_resistant()` instead to not consider \"I\" being resistant. This note will be shown once for this session.", as_note = FALSE)
}
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc(...,
2022-08-28 10:31:50 +02:00
ab_result = c("I", "R"),
only_all_tested = only_all_tested,
only_count = TRUE
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
2018-08-22 00:02:26 +02:00
}
#' @rdname count
#' @export
count_I <- function(..., only_all_tested = FALSE) {
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc(...,
2022-08-28 10:31:50 +02:00
ab_result = "I",
only_all_tested = only_all_tested,
only_count = TRUE
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
2018-08-22 00:02:26 +02:00
}
#' @rdname count
#' @export
count_SI <- function(..., only_all_tested = FALSE) {
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc(...,
2022-08-28 10:31:50 +02:00
ab_result = c("S", "I"),
only_all_tested = only_all_tested,
only_count = TRUE
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
2018-08-22 00:02:26 +02:00
}
#' @rdname count
#' @export
count_S <- function(..., only_all_tested = FALSE) {
if (message_not_thrown_before("count_S", entire_session = TRUE)) {
message_("Using `count_S()` is discouraged; use `count_susceptible()` instead to also consider \"I\" being susceptible. This note will be shown once for this session.", as_note = FALSE)
}
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc(...,
2022-08-28 10:31:50 +02:00
ab_result = "S",
only_all_tested = only_all_tested,
only_count = TRUE
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
2018-08-22 00:02:26 +02:00
}
2018-10-12 16:35:18 +02:00
#' @rdname count
#' @export
count_all <- function(..., only_all_tested = FALSE) {
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc(...,
2022-08-28 10:31:50 +02:00
ab_result = c("S", "I", "R"),
only_all_tested = only_all_tested,
only_count = TRUE
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
2018-10-12 16:35:18 +02:00
}
#' @rdname count
#' @export
2023-01-21 23:47:20 +01:00
n_sir <- 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_AMR_locale(),
combine_SI = TRUE) {
tryCatch(
2023-01-21 23:47:20 +01:00
sir_calc_df(
2022-08-28 10:31:50 +02:00
type = "count",
data = data,
translate_ab = translate_ab,
language = language,
combine_SI = combine_SI,
confidence_level = 0.95 # doesn't matter, will be removed
2022-08-28 10:31:50 +02:00
),
2023-01-21 23:47:20 +01:00
error = function(e) stop_(gsub("in sir_calc_df(): ", "", e$message, fixed = TRUE), call = -5)
2022-08-28 10:31:50 +02:00
)
2018-08-22 00:02:26 +02:00
}