AMR/inst/tinytest/test-count.R

110 lines
4.9 KiB
R
Raw Normal View History

2021-05-15 21:36:22 +02:00
# ==================================================================== #
# TITLE: #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
2021-05-15 21:36:22 +02:00
# #
# SOURCE CODE: #
2021-05-15 21:36:22 +02:00
# https://github.com/msberends/AMR #
# #
# 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. #
2021-05-15 21:36:22 +02: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. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
expect_equal(count_resistant(example_isolates$AMX), count_R(example_isolates$AMX))
expect_equal(count_susceptible(example_isolates$AMX), count_SI(example_isolates$AMX))
2023-01-21 23:47:20 +01:00
expect_equal(count_all(example_isolates$AMX), n_sir(example_isolates$AMX))
2021-05-15 21:36:22 +02:00
# AMX resistance in `example_isolates`
expect_equal(count_R(example_isolates$AMX), 804)
expect_equal(count_I(example_isolates$AMX), 3)
expect_equal(suppressWarnings(count_S(example_isolates$AMX)), 543)
2022-08-28 10:31:50 +02:00
expect_equal(
count_R(example_isolates$AMX) + count_I(example_isolates$AMX),
suppressWarnings(count_IR(example_isolates$AMX))
)
expect_equal(
suppressWarnings(count_S(example_isolates$AMX)) + count_I(example_isolates$AMX),
count_SI(example_isolates$AMX)
)
2021-05-15 21:36:22 +02:00
# warning for speed loss
# expect_warning(count_resistant(as.character(example_isolates$AMC)))
2021-05-24 11:01:32 +02:00
2023-02-14 15:59:40 +01:00
# expect_warning(count_resistant(example_isolates$AMC, as.character(example_isolates$GEN)))
2021-05-15 21:36:22 +02:00
# check for errors
expect_error(count_resistant("test", minimum = "test"))
expect_error(count_resistant("test", as_percent = "test"))
expect_error(count_susceptible("test", minimum = "test"))
expect_error(count_susceptible("test", as_percent = "test"))
expect_error(count_df(c("A", "B", "C")))
2022-08-27 20:49:37 +02:00
expect_error(count_df(example_isolates[, "date", drop = TRUE]))
2021-05-15 21:36:22 +02:00
2023-02-18 14:56:06 +01:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
2021-05-15 21:36:22 +02:00
expect_equal(example_isolates %>% count_susceptible(AMC), 1433)
expect_equal(example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE), 1687)
expect_equal(example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = FALSE), 1764)
expect_equal(example_isolates %>% count_all(AMC, GEN, only_all_tested = TRUE), 1798)
expect_equal(example_isolates %>% count_all(AMC, GEN, only_all_tested = FALSE), 1936)
2022-08-28 10:31:50 +02:00
expect_identical(
example_isolates %>% count_all(AMC, GEN, only_all_tested = TRUE),
example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE) +
example_isolates %>% count_resistant(AMC, GEN, only_all_tested = TRUE)
)
2021-05-15 21:36:22 +02:00
# count of cases
2022-08-28 10:31:50 +02:00
expect_equal(
example_isolates %>%
group_by(ward) %>%
summarise(
cipro = count_susceptible(CIP),
genta = count_susceptible(GEN),
combination = count_susceptible(CIP, GEN)
) %>%
pull(combination),
2022-08-28 19:17:12 +02:00
c(946, 428, 94)
2022-08-28 10:31:50 +02:00
)
2021-05-15 21:36:22 +02:00
# count_df
expect_equal(
example_isolates %>% select(AMX) %>% count_df() %>% pull(value),
2022-08-28 10:31:50 +02:00
c(
example_isolates$AMX %>% count_susceptible(),
example_isolates$AMX %>% count_resistant()
)
2021-05-15 21:36:22 +02:00
)
expect_equal(
example_isolates %>% select(AMX) %>% count_df(combine_SI = FALSE) %>% pull(value),
2022-08-28 10:31:50 +02:00
c(
suppressWarnings(example_isolates$AMX %>% count_S()),
2021-05-15 21:36:22 +02:00
example_isolates$AMX %>% count_I(),
2022-08-28 10:31:50 +02:00
example_isolates$AMX %>% count_R()
)
2021-05-15 21:36:22 +02:00
)
2022-08-28 10:31:50 +02:00
2023-01-21 23:47:20 +01:00
# grouping in sir_calc_df() (= backbone of sir_df())
2022-08-28 10:31:50 +02:00
expect_true("ward" %in% (example_isolates %>%
group_by(ward) %>%
select(ward, AMX, CIP, gender) %>%
2023-01-21 23:47:20 +01:00
sir_df() %>%
2022-08-28 10:31:50 +02:00
colnames()))
2021-05-15 21:36:22 +02:00
}