2021-05-15 21:36:22 +02:00
|
|
|
# ==================================================================== #
|
2023-07-08 17:30:05 +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
|
|
|
# #
|
2023-07-08 17:30:05 +02:00
|
|
|
# SOURCE CODE: #
|
2021-05-15 21:36:22 +02:00
|
|
|
# https://github.com/msberends/AMR #
|
|
|
|
# #
|
2023-07-08 17:30:05 +02:00
|
|
|
# PLEASE CITE THIS SOFTWARE AS: #
|
2024-07-16 14:51:57 +02:00
|
|
|
# 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/ #
|
|
|
|
# ==================================================================== #
|
|
|
|
|
|
|
|
# all four methods
|
2022-08-28 10:31:50 +02:00
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(x = example_isolates, method = "isolate-based", info = TRUE), na.rm = TRUE),
|
|
|
|
1984
|
|
|
|
)
|
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(x = example_isolates, method = "patient-based", info = TRUE), na.rm = TRUE),
|
|
|
|
1265
|
|
|
|
)
|
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(x = example_isolates, method = "episode-based", info = TRUE), na.rm = TRUE),
|
|
|
|
1300
|
|
|
|
)
|
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(x = example_isolates, method = "phenotype-based", info = TRUE), na.rm = TRUE),
|
2023-10-20 14:51:48 +02:00
|
|
|
1373
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
2023-10-20 15:45:00 +02:00
|
|
|
# for phenotype determination
|
|
|
|
expect_equal(AMR:::duplicated_antibiogram("SSSS", points_threshold = 2, ignore_I = TRUE, type = "points"),
|
|
|
|
FALSE)
|
|
|
|
expect_equal(AMR:::duplicated_antibiogram(c("RRR", "SSS"),
|
|
|
|
points_threshold = 2, ignore_I = TRUE, type = "points"),
|
|
|
|
c(FALSE, FALSE))
|
|
|
|
expect_equal(AMR:::duplicated_antibiogram(c("RRR", "RRR", "SSS"),
|
|
|
|
points_threshold = 2, ignore_I = TRUE, type = "points"),
|
|
|
|
c(FALSE, TRUE, FALSE))
|
|
|
|
expect_equal(AMR:::duplicated_antibiogram(c("RRR", "RSS", "SSS", "RSS", "RRR", "RRR", "SSS", "RSS", "RSR", "RRR"),
|
|
|
|
points_threshold = 2, ignore_I = TRUE, type = "points"),
|
|
|
|
c(FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE))
|
|
|
|
|
2021-05-15 21:36:22 +02:00
|
|
|
# Phenotype-based, using key antimicrobials
|
2022-08-28 10:31:50 +02:00
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(
|
|
|
|
x = example_isolates,
|
|
|
|
method = "phenotype-based",
|
|
|
|
type = "keyantimicrobials",
|
|
|
|
antifungal = NULL, info = TRUE
|
|
|
|
), na.rm = TRUE),
|
2023-10-20 14:51:48 +02:00
|
|
|
1376
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(
|
|
|
|
x = example_isolates,
|
|
|
|
method = "phenotype-based",
|
|
|
|
type = "keyantimicrobials",
|
|
|
|
antifungal = NULL, info = TRUE, ignore_I = FALSE
|
|
|
|
), na.rm = TRUE),
|
2023-10-20 14:51:48 +02:00
|
|
|
1396
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
# first non-ICU isolates
|
2022-12-21 09:12:53 +01:00
|
|
|
expect_true(
|
|
|
|
sum(
|
2021-05-15 21:36:22 +02:00
|
|
|
first_isolate(example_isolates,
|
2022-08-28 10:31:50 +02:00
|
|
|
col_mo = "mo",
|
|
|
|
col_date = "date",
|
|
|
|
col_patient_id = "patient",
|
|
|
|
col_icu = example_isolates$ward == "ICU",
|
|
|
|
info = TRUE,
|
|
|
|
icu_exclude = TRUE
|
2022-12-21 09:12:53 +01:00
|
|
|
), na.rm = TRUE) < 950
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
# set 1500 random observations to be of specimen type 'Urine'
|
|
|
|
random_rows <- sample(x = 1:2000, size = 1500, replace = FALSE)
|
|
|
|
x <- example_isolates
|
|
|
|
x$specimen <- "Other"
|
|
|
|
x[random_rows, "specimen"] <- "Urine"
|
|
|
|
expect_true(
|
2022-08-28 10:31:50 +02:00
|
|
|
sum(first_isolate(
|
|
|
|
x = x,
|
|
|
|
col_date = "date",
|
|
|
|
col_patient_id = "patient",
|
|
|
|
col_mo = "mo",
|
|
|
|
col_specimen = "specimen",
|
|
|
|
filter_specimen = "Urine",
|
|
|
|
info = TRUE
|
2023-10-20 14:51:48 +02:00
|
|
|
), na.rm = TRUE) < 1400
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
# same, but now exclude ICU
|
|
|
|
expect_true(
|
2022-08-28 10:31:50 +02:00
|
|
|
sum(first_isolate(
|
|
|
|
x = x,
|
|
|
|
col_date = "date",
|
|
|
|
col_patient_id = "patient",
|
|
|
|
col_mo = "mo",
|
|
|
|
col_specimen = "specimen",
|
|
|
|
filter_specimen = "Urine",
|
|
|
|
col_icu = x$ward == "ICU",
|
|
|
|
icu_exclude = TRUE,
|
|
|
|
info = TRUE
|
2023-10-20 14:51:48 +02:00
|
|
|
), na.rm = TRUE) < 1000
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
# "No isolates found"
|
|
|
|
test_iso <- example_isolates
|
|
|
|
test_iso$specimen <- "test"
|
2022-08-28 10:31:50 +02:00
|
|
|
expect_message(first_isolate(test_iso,
|
|
|
|
"date",
|
|
|
|
"patient",
|
|
|
|
col_mo = "mo",
|
|
|
|
col_specimen = "specimen",
|
|
|
|
filter_specimen = "something_unexisting",
|
|
|
|
info = TRUE
|
|
|
|
))
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
# printing of exclusion message
|
|
|
|
expect_message(first_isolate(example_isolates,
|
2022-08-28 10:31:50 +02:00
|
|
|
col_date = "date",
|
|
|
|
col_mo = "mo",
|
|
|
|
col_patient_id = "patient",
|
|
|
|
col_testcode = "gender",
|
|
|
|
testcodes_exclude = "M",
|
|
|
|
info = TRUE
|
|
|
|
))
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
# errors
|
2022-08-27 20:49:37 +02:00
|
|
|
expect_error(first_isolate("date", "patient", col_mo = "mo"))
|
2021-05-15 21:36:22 +02:00
|
|
|
expect_error(first_isolate(example_isolates,
|
2022-08-28 10:31:50 +02:00
|
|
|
col_date = "non-existing col",
|
|
|
|
col_mo = "mo"
|
|
|
|
))
|
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
|
|
|
# if mo is not an mo class, result should be the same
|
2022-08-28 10:31:50 +02:00
|
|
|
expect_identical(
|
|
|
|
example_isolates %>%
|
|
|
|
mutate(mo = as.character(mo)) %>%
|
|
|
|
first_isolate(
|
|
|
|
col_date = "date",
|
|
|
|
col_mo = "mo",
|
|
|
|
col_patient_id = "patient",
|
|
|
|
info = FALSE
|
|
|
|
),
|
|
|
|
example_isolates %>%
|
|
|
|
first_isolate(
|
|
|
|
col_date = "date",
|
|
|
|
col_mo = "mo",
|
|
|
|
col_patient_id = "patient",
|
|
|
|
info = FALSE
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-05-15 21:36:22 +02:00
|
|
|
# support for WHONET
|
|
|
|
expect_message(example_isolates %>%
|
2022-08-28 19:17:12 +02:00
|
|
|
select(-patient) %>%
|
2022-08-28 10:31:50 +02:00
|
|
|
mutate(
|
|
|
|
`First name` = "test",
|
|
|
|
`Last name` = "test",
|
|
|
|
Sex = "Female"
|
|
|
|
) %>%
|
|
|
|
first_isolate(info = TRUE))
|
|
|
|
|
2021-05-15 21:36:22 +02:00
|
|
|
# groups
|
2022-08-28 10:31:50 +02:00
|
|
|
x <- example_isolates %>%
|
|
|
|
group_by(ward) %>%
|
|
|
|
mutate(first = first_isolate())
|
|
|
|
y <- example_isolates %>%
|
|
|
|
group_by(ward) %>%
|
|
|
|
mutate(first = first_isolate(.))
|
2021-05-15 21:36:22 +02:00
|
|
|
expect_identical(x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
# missing dates should be no problem
|
|
|
|
df <- example_isolates
|
|
|
|
df[1:100, "date"] <- NA
|
|
|
|
expect_equal(
|
|
|
|
sum(
|
2022-08-28 10:31:50 +02:00
|
|
|
first_isolate(
|
|
|
|
x = df,
|
|
|
|
col_date = "date",
|
|
|
|
col_patient_id = "patient",
|
|
|
|
col_mo = "mo",
|
|
|
|
info = TRUE
|
|
|
|
),
|
|
|
|
na.rm = TRUE
|
|
|
|
),
|
2023-10-20 14:51:48 +02:00
|
|
|
1376
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
# unknown MOs
|
|
|
|
test_unknown <- example_isolates
|
|
|
|
test_unknown$mo <- ifelse(test_unknown$mo == "B_ESCHR_COLI", "UNKNOWN", test_unknown$mo)
|
2022-08-28 10:31:50 +02:00
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(test_unknown, include_unknown = FALSE)),
|
2023-10-20 14:51:48 +02:00
|
|
|
1106
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(test_unknown, include_unknown = TRUE)),
|
2023-10-20 14:51:48 +02:00
|
|
|
1589
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
test_unknown$mo <- ifelse(test_unknown$mo == "UNKNOWN", NA, test_unknown$mo)
|
2022-08-28 10:31:50 +02:00
|
|
|
expect_equal(
|
|
|
|
sum(first_isolate(test_unknown)),
|
2023-10-20 14:51:48 +02:00
|
|
|
1106
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
2023-01-21 23:47:20 +01:00
|
|
|
# empty sir results
|
2022-08-28 10:31:50 +02:00
|
|
|
expect_equal(
|
2023-01-21 23:47:20 +01:00
|
|
|
sum(first_isolate(example_isolates, include_untested_sir = FALSE)),
|
2023-10-20 14:51:48 +02:00
|
|
|
1360
|
2022-08-28 10:31:50 +02:00
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
# shortcuts
|
2022-08-28 10:31:50 +02:00
|
|
|
expect_identical(
|
|
|
|
filter_first_isolate(example_isolates),
|
|
|
|
subset(example_isolates, first_isolate(example_isolates))
|
|
|
|
)
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
# notice that all mo's are distinct, so all are TRUE
|
2023-02-09 13:07:39 +01:00
|
|
|
expect_true(all(first_isolate(AMR:::pm_distinct(example_isolates, mo, .keep_all = TRUE), info = TRUE) == TRUE))
|
2021-05-15 21:36:22 +02:00
|
|
|
|
|
|
|
# only one isolate, so return fast
|
|
|
|
expect_true(first_isolate(data.frame(mo = "Escherichia coli", date = Sys.Date(), patient = "patient"), info = TRUE))
|