1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-26 06:46:11 +01:00
AMR/inst/tinytest/test-first_isolate.R

238 lines
6.7 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 #
# https://github.com/msberends/AMR #
# #
2022-10-05 09:12:22 +02:00
# CITE AS #
# 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. #
# doi:10.18637/jss.v104.i03 #
# #
2021-05-15 21:36:22 +02:00
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
2022-08-28 10:31:50 +02:00
# Diagnostics & Advice, and University Medical Center Groningen. #
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),
1379
)
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),
1395
)
expect_equal(
sum(first_isolate(
x = example_isolates,
method = "phenotype-based",
type = "keyantimicrobials",
antifungal = NULL, info = TRUE, ignore_I = FALSE
), na.rm = TRUE),
1418
)
2021-05-15 21:36:22 +02:00
# first non-ICU isolates
expect_equal(
sum(
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
),
na.rm = TRUE
),
2022-08-28 19:17:12 +02:00
942
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
), na.rm = TRUE) < 1501
)
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
), na.rm = TRUE) < 1501
)
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
2021-10-05 09:58:08 +02:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
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
),
1382
)
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)),
1108
)
expect_equal(
sum(first_isolate(test_unknown, include_unknown = TRUE)),
1591
)
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)),
1108
)
2021-05-15 21:36:22 +02:00
# empty rsi results
2022-08-28 10:31:50 +02:00
expect_equal(
sum(first_isolate(example_isolates, include_untested_rsi = FALSE)),
1366
)
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
2021-05-16 10:50:00 +02: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))