2018-12-16 22:45:12 +01:00
|
|
|
# ==================================================================== #
|
|
|
|
# TITLE #
|
2021-02-02 23:57:35 +01:00
|
|
|
# Antimicrobial Resistance (AMR) Data Analysis for R #
|
2018-12-16 22:45:12 +01:00
|
|
|
# #
|
2019-01-02 23:24:07 +01:00
|
|
|
# SOURCE #
|
2020-07-08 14:48:06 +02:00
|
|
|
# https://github.com/msberends/AMR #
|
2018-12-16 22:45:12 +01:00
|
|
|
# #
|
|
|
|
# LICENCE #
|
2020-12-27 00:30:28 +01:00
|
|
|
# (c) 2018-2021 Berends MS, Luz CF et al. #
|
2020-10-08 11:16:03 +02:00
|
|
|
# Developed at the University of Groningen, the Netherlands, in #
|
|
|
|
# collaboration with non-profit organisations Certe Medical #
|
|
|
|
# Diagnostics & Advice, and University Medical Center Groningen. #
|
2018-12-16 22:45:12 +01: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. #
|
2020-01-05 17:22:09 +01:00
|
|
|
# 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 #
|
2021-02-02 23:57:35 +01:00
|
|
|
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
|
2018-12-16 22:45:12 +01:00
|
|
|
# ==================================================================== #
|
|
|
|
|
2018-08-23 00:40:36 +02:00
|
|
|
context("count.R")
|
|
|
|
|
|
|
|
test_that("counts work", {
|
2020-07-30 15:15:52 +02:00
|
|
|
skip_on_cran()
|
|
|
|
|
2019-11-10 12:16:56 +01:00
|
|
|
expect_equal(count_resistant(example_isolates$AMX), count_R(example_isolates$AMX))
|
|
|
|
expect_equal(count_susceptible(example_isolates$AMX), count_SI(example_isolates$AMX))
|
|
|
|
expect_equal(count_all(example_isolates$AMX), n_rsi(example_isolates$AMX))
|
|
|
|
|
2019-08-27 16:45:42 +02:00
|
|
|
# AMX resistance in `example_isolates`
|
2020-09-24 00:50:23 +02:00
|
|
|
expect_equal(count_R(example_isolates$AMX), 804)
|
2020-06-22 12:18:53 +02:00
|
|
|
expect_equal(count_I(example_isolates$AMX), 3)
|
|
|
|
expect_equal(suppressWarnings(count_S(example_isolates$AMX)), 543)
|
|
|
|
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))
|
2019-11-10 12:16:56 +01:00
|
|
|
|
2020-09-26 16:26:01 +02:00
|
|
|
library(dplyr, warn.conflicts = FALSE)
|
2019-11-10 12:16:56 +01: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)
|
2019-08-27 16:45:42 +02:00
|
|
|
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)
|
|
|
|
expect_identical(example_isolates %>% count_all(AMC, GEN, only_all_tested = TRUE),
|
2019-11-10 12:16:56 +01:00
|
|
|
example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE) +
|
|
|
|
example_isolates %>% count_resistant(AMC, GEN, only_all_tested = TRUE))
|
2020-06-22 12:18:53 +02:00
|
|
|
|
2018-08-23 00:40:36 +02:00
|
|
|
# count of cases
|
2019-08-27 16:45:42 +02:00
|
|
|
expect_equal(example_isolates %>%
|
2018-08-23 00:40:36 +02:00
|
|
|
group_by(hospital_id) %>%
|
2019-11-10 12:16:56 +01:00
|
|
|
summarise(cipro = count_susceptible(CIP),
|
|
|
|
genta = count_susceptible(GEN),
|
|
|
|
combination = count_susceptible(CIP, GEN)) %>%
|
2018-08-23 00:40:36 +02:00
|
|
|
pull(combination),
|
2019-07-01 14:03:15 +02:00
|
|
|
c(253, 465, 192, 558))
|
2020-05-16 13:05:47 +02:00
|
|
|
|
2018-10-16 09:59:31 +02:00
|
|
|
# count_df
|
|
|
|
expect_equal(
|
2019-08-27 16:45:42 +02:00
|
|
|
example_isolates %>% select(AMX) %>% count_df() %>% pull(value),
|
2019-11-10 12:16:56 +01:00
|
|
|
c(example_isolates$AMX %>% count_susceptible(),
|
|
|
|
example_isolates$AMX %>% count_resistant())
|
2018-10-16 09:59:31 +02:00
|
|
|
)
|
|
|
|
expect_equal(
|
2019-08-27 16:45:42 +02:00
|
|
|
example_isolates %>% select(AMX) %>% count_df(combine_IR = TRUE) %>% pull(value),
|
2019-11-10 12:16:56 +01:00
|
|
|
c(suppressWarnings(example_isolates$AMX %>% count_S()),
|
|
|
|
suppressWarnings(example_isolates$AMX %>% count_IR()))
|
2018-10-16 09:59:31 +02:00
|
|
|
)
|
2019-05-13 10:10:16 +02:00
|
|
|
expect_equal(
|
2019-08-27 16:45:42 +02:00
|
|
|
example_isolates %>% select(AMX) %>% count_df(combine_SI = FALSE) %>% pull(value),
|
2019-11-10 12:16:56 +01:00
|
|
|
c(suppressWarnings(example_isolates$AMX %>% count_S()),
|
2019-08-27 16:45:42 +02:00
|
|
|
example_isolates$AMX %>% count_I(),
|
|
|
|
example_isolates$AMX %>% count_R())
|
2019-05-13 10:10:16 +02:00
|
|
|
)
|
2019-11-10 12:16:56 +01:00
|
|
|
|
2018-08-23 00:40:36 +02:00
|
|
|
# warning for speed loss
|
2020-12-27 00:07:00 +01:00
|
|
|
reset_all_thrown_messages()
|
2019-11-10 12:16:56 +01:00
|
|
|
expect_warning(count_resistant(as.character(example_isolates$AMC)))
|
2020-12-27 00:07:00 +01:00
|
|
|
reset_all_thrown_messages()
|
2019-11-10 12:16:56 +01:00
|
|
|
expect_warning(count_resistant(example_isolates$AMC,
|
|
|
|
as.character(example_isolates$GEN)))
|
|
|
|
|
2018-08-23 00:40:36 +02:00
|
|
|
# check for errors
|
2019-11-10 12:16:56 +01:00
|
|
|
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"))
|
2020-06-22 12:18:53 +02:00
|
|
|
|
2018-09-16 22:11:17 +02:00
|
|
|
expect_error(count_df(c("A", "B", "C")))
|
2019-10-11 17:21:02 +02:00
|
|
|
expect_error(count_df(example_isolates[, "date"]))
|
2020-06-22 12:18:53 +02:00
|
|
|
|
2020-12-13 20:44:32 +01:00
|
|
|
# grouping in rsi_calc_df() (= backbone of rsi_df())
|
|
|
|
expect_true("hospital_id" %in% (example_isolates %>%
|
|
|
|
group_by(hospital_id) %>%
|
|
|
|
select(hospital_id, AMX, CIP, gender) %>%
|
|
|
|
rsi_df() %>%
|
|
|
|
colnames()))
|
|
|
|
|
2018-08-23 00:40:36 +02:00
|
|
|
})
|