AMR/inst/tinytest/test-proportion.R

158 lines
6.3 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: #
# Berends MS, Luz CF, Friedrich AW, et al. (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(proportion_R(example_isolates$AMX), resistance(example_isolates$AMX))
expect_equal(proportion_SI(example_isolates$AMX), susceptibility(example_isolates$AMX))
# AMX resistance in `example_isolates`
expect_equal(proportion_R(example_isolates$AMX), 0.5955556, tolerance = 0.0001)
expect_equal(proportion_I(example_isolates$AMX), 0.002222222, tolerance = 0.0001)
2023-01-21 23:47:20 +01:00
expect_equal(sir_confidence_interval(example_isolates$AMX)[1], 0.5688204, tolerance = 0.0001)
expect_equal(sir_confidence_interval(example_isolates$AMX)[2], 0.6218738, tolerance = 0.0001)
2022-08-28 10:31:50 +02:00
expect_equal(
1 - proportion_R(example_isolates$AMX) - proportion_I(example_isolates$AMX),
proportion_S(example_isolates$AMX)
)
expect_equal(
proportion_R(example_isolates$AMX) + proportion_I(example_isolates$AMX),
proportion_IR(example_isolates$AMX)
)
expect_equal(
proportion_S(example_isolates$AMX) + proportion_I(example_isolates$AMX),
proportion_SI(example_isolates$AMX)
)
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)) {
expect_equal(example_isolates %>% proportion_SI(AMC),
2022-08-28 10:31:50 +02:00
0.7626397,
tolerance = 0.0001
)
expect_equal(example_isolates %>% proportion_SI(AMC, GEN),
2022-08-28 10:31:50 +02:00
0.9408,
tolerance = 0.0001
)
expect_equal(example_isolates %>% proportion_SI(AMC, GEN, only_all_tested = TRUE),
2022-08-28 10:31:50 +02:00
0.9382647,
tolerance = 0.0001
)
2021-05-15 21:36:22 +02:00
# percentages
2022-08-28 10:31:50 +02:00
expect_equal(
example_isolates %>%
group_by(ward) %>%
summarise(
R = proportion_R(CIP, as_percent = TRUE),
I = proportion_I(CIP, as_percent = TRUE),
S = proportion_S(CIP, as_percent = TRUE),
2023-01-21 23:47:20 +01:00
n = n_sir(CIP),
2022-08-28 10:31:50 +02:00
total = n()
) %>%
pull(n) %>%
sum(),
1409
)
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_p = proportion_SI(CIP, as_percent = TRUE),
2023-01-21 23:47:20 +01:00
cipro_n = n_sir(CIP),
2022-08-28 10:31:50 +02:00
genta_p = proportion_SI(GEN, as_percent = TRUE),
2023-01-21 23:47:20 +01:00
genta_n = n_sir(GEN),
2022-08-28 10:31:50 +02:00
combination_p = proportion_SI(CIP, GEN, as_percent = TRUE),
2023-01-21 23:47:20 +01:00
combination_n = n_sir(CIP, GEN)
2022-08-28 10:31:50 +02:00
) %>%
pull(combination_n),
2022-08-28 19:17:12 +02:00
c(1181, 577, 116)
2022-08-28 10:31:50 +02:00
)
2021-05-15 21:36:22 +02:00
# proportion_df
expect_equal(
example_isolates %>% select(AMX) %>% proportion_df() %>% pull(value),
2022-08-28 10:31:50 +02:00
c(
example_isolates$AMX %>% proportion_SI(),
example_isolates$AMX %>% proportion_R()
)
2021-05-15 21:36:22 +02:00
)
expect_equal(
example_isolates %>% select(AMX) %>% proportion_df(combine_SI = FALSE) %>% pull(value),
2022-08-28 10:31:50 +02:00
c(
example_isolates$AMX %>% proportion_S(),
2021-05-15 21:36:22 +02:00
example_isolates$AMX %>% proportion_I(),
2022-08-28 10:31:50 +02:00
example_isolates$AMX %>% proportion_R()
)
2021-05-15 21:36:22 +02:00
)
# expect_warning(example_isolates %>% group_by(ward) %>% summarise(across(KAN, sir_confidence_interval)))
2021-05-15 21:36:22 +02:00
}
2021-05-24 11:01:32 +02:00
# expect_warning(proportion_R(as.character(example_isolates$AMC)))
# expect_warning(proportion_S(as.character(example_isolates$AMC)))
2023-02-14 15:59:40 +01:00
# expect_warning(proportion_S(as.character(example_isolates$AMC, example_isolates$GEN)))
2021-05-24 11:01:32 +02:00
2023-02-14 15:59:40 +01:00
# expect_warning(n_sir(as.character(example_isolates$AMC, example_isolates$GEN)))
2022-08-28 10:31:50 +02:00
expect_equal(
2023-01-21 23:47:20 +01:00
suppressWarnings(n_sir(as.character(
2022-08-28 10:31:50 +02:00
example_isolates$AMC,
example_isolates$GEN
))),
1879
)
2021-05-15 21:36:22 +02:00
# check for errors
expect_error(proportion_IR("test", minimum = "test"))
expect_error(proportion_IR("test", as_percent = "test"))
expect_error(proportion_I("test", minimum = "test"))
expect_error(proportion_I("test", as_percent = "test"))
expect_error(proportion_S("test", minimum = "test"))
expect_error(proportion_S("test", as_percent = "test"))
expect_error(proportion_S("test", also_single_tested = TRUE))
# check too low amount of isolates
2022-08-28 10:31:50 +02:00
expect_identical(
suppressWarnings(proportion_R(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA_real_
)
expect_identical(
suppressWarnings(proportion_I(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA_real_
)
expect_identical(
suppressWarnings(proportion_S(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA_real_
)
2021-05-15 21:36:22 +02:00
# warning for speed loss
# expect_warning(proportion_R(as.character(example_isolates$GEN)))
# expect_warning(proportion_I(as.character(example_isolates$GEN)))
# expect_warning(proportion_S(example_isolates$AMC, as.character(example_isolates$GEN)))
2021-05-15 21:36:22 +02:00
expect_error(proportion_df(c("A", "B", "C")))
2022-08-27 20:49:37 +02:00
expect_error(proportion_df(example_isolates[, "date", drop = TRUE]))