AMR/tests/testthat/test-first_isolate.R

195 lines
7.9 KiB
R
Raw Normal View History

# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Data Analysis for R #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
# #
# 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. #
# #
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. #
# 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 #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
2018-07-23 14:14:03 +02:00
context("first_isolate.R")
2018-03-27 17:43:42 +02:00
test_that("first isolates work", {
2019-10-15 14:35:23 +02:00
skip_on_cran()
# all four methods
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)
# Phenotype-based, using key antimicrobials
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)
2019-02-18 02:33:37 +01:00
# first non-ICU isolates
2018-04-20 13:45:34 +02:00
expect_equal(
sum(
first_isolate(example_isolates,
2018-08-31 13:36:19 +02:00
col_mo = "mo",
2018-06-19 10:05:38 +02:00
col_date = "date",
col_patient_id = "patient_id",
col_icu = "ward_icu",
info = TRUE,
icu_exclude = TRUE),
na.rm = TRUE),
941)
2018-04-20 13:45:34 +02:00
2018-04-03 11:08:31 +02:00
# set 1500 random observations to be of specimen type 'Urine'
random_rows <- sample(x = 1:2000, size = 1500, replace = FALSE)
2020-05-16 13:05:47 +02:00
x <- example_isolates
x$specimen <- "Other"
x[random_rows, "specimen"] <- "Urine"
2018-06-19 10:05:38 +02:00
expect_lt(
sum(
2020-05-16 13:05:47 +02:00
first_isolate(x = x,
2018-06-19 10:05:38 +02:00
col_date = "date",
col_patient_id = "patient_id",
2018-08-31 13:36:19 +02:00
col_mo = "mo",
2018-06-19 10:05:38 +02:00
col_specimen = "specimen",
filter_specimen = "Urine",
info = TRUE),
na.rm = TRUE),
2018-04-03 11:08:31 +02:00
1501)
2018-07-02 09:34:20 +02:00
# same, but now exclude ICU
expect_lt(
sum(
2020-05-16 13:05:47 +02:00
first_isolate(x = x,
2018-07-02 09:34:20 +02:00
col_date = "date",
col_patient_id = "patient_id",
2018-08-31 13:36:19 +02:00
col_mo = "mo",
2018-07-02 09:34:20 +02:00
col_specimen = "specimen",
filter_specimen = "Urine",
col_icu = "ward_icu",
icu_exclude = TRUE,
info = TRUE),
na.rm = TRUE),
1501)
2018-08-23 21:27:15 +02:00
# "No isolates found"
2020-05-16 13:05:47 +02:00
test_iso <- example_isolates
test_iso$specimen <- "test"
expect_message(first_isolate(test_iso,
"date",
"patient_id",
col_mo = "mo",
col_specimen = "specimen",
filter_specimen = "something_unexisting",
info = TRUE))
# printing of exclusion message
2020-05-16 13:05:47 +02:00
expect_message(first_isolate(example_isolates,
col_date = "date",
2018-12-22 22:39:34 +01:00
col_mo = "mo",
col_patient_id = "patient_id",
col_testcode = "gender",
testcodes_exclude = "M",
info = TRUE))
# errors
2018-08-31 13:36:19 +02:00
expect_error(first_isolate("date", "patient_id", col_mo = "mo"))
expect_error(first_isolate(example_isolates,
col_date = "non-existing col",
2018-08-31 13:36:19 +02:00
col_mo = "mo"))
2020-12-13 20:44:32 +01:00
require("dplyr")
2018-09-01 21:19:46 +02:00
# if mo is not an mo class, result should be the same
expect_identical(example_isolates %>%
2018-12-22 22:39:34 +01:00
mutate(mo = as.character(mo)) %>%
first_isolate(col_date = "date",
col_mo = "mo",
col_patient_id = "patient_id",
info = FALSE),
example_isolates %>%
2018-09-01 21:19:46 +02:00
first_isolate(col_date = "date",
col_mo = "mo",
col_patient_id = "patient_id",
info = FALSE))
2020-12-13 20:44:32 +01:00
# support for WHONET
expect_message(example_isolates %>%
select(-patient_id) %>%
mutate(`First name` = "test",
`Last name` = "test",
Sex = "Female") %>%
first_isolate(info = TRUE))
# missing dates should be no problem
df <- example_isolates
2019-05-13 14:56:23 +02:00
df[1:100, "date"] <- NA
expect_equal(
sum(
first_isolate(x = df,
col_date = "date",
col_patient_id = "patient_id",
col_mo = "mo",
info = TRUE),
na.rm = TRUE),
1382)
# unknown MOs
2020-05-16 13:05:47 +02:00
test_unknown <- example_isolates
test_unknown$mo <- ifelse(test_unknown$mo == "B_ESCHR_COLI", "UNKNOWN", test_unknown$mo)
expect_equal(sum(first_isolate(test_unknown, include_unknown = FALSE)),
1108)
2020-05-16 13:05:47 +02:00
expect_equal(sum(first_isolate(test_unknown, include_unknown = TRUE)),
1591)
2020-05-16 13:05:47 +02:00
test_unknown$mo <- ifelse(test_unknown$mo == "UNKNOWN", NA, test_unknown$mo)
expect_equal(sum(first_isolate(test_unknown)),
1108)
2020-05-18 10:30:53 +02:00
# empty rsi results
expect_equal(sum(first_isolate(example_isolates, include_untested_rsi = FALSE)),
1366)
2020-05-18 10:30:53 +02:00
# shortcuts
expect_identical(filter_first_isolate(example_isolates),
subset(example_isolates, first_isolate(example_isolates, method = "episode-based")))
2021-04-29 17:16:30 +02:00
2020-05-18 10:30:53 +02:00
2020-11-17 16:57:41 +01:00
# notice that all mo's are distinct, so all are TRUE
expect_true(all(example_isolates %pm>%
pm_distinct(mo, .keep_all = TRUE) %pm>%
2020-12-13 20:44:32 +01:00
first_isolate(info = TRUE) == TRUE))
# only one isolate, so return fast
expect_true(first_isolate(data.frame(mo = "Escherichia coli", date = Sys.Date(), patient = "patient"), info = TRUE))
2020-11-17 16:57:41 +01:00
# groups
x <- example_isolates %>% group_by(ward_icu) %>% mutate(first = first_isolate())
y <- example_isolates %>% group_by(ward_icu) %>% mutate(first = first_isolate(.))
expect_identical(x, y)
2018-03-27 17:43:42 +02:00
})