2018-12-16 22:45:12 +01:00
|
|
|
# ==================================================================== #
|
|
|
|
# TITLE #
|
|
|
|
# Antimicrobial Resistance (AMR) Analysis #
|
|
|
|
# #
|
2019-01-02 23:24:07 +01:00
|
|
|
# SOURCE #
|
|
|
|
# https://gitlab.com/msberends/AMR #
|
2018-12-16 22:45:12 +01:00
|
|
|
# #
|
|
|
|
# LICENCE #
|
2019-01-02 23:24:07 +01:00
|
|
|
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
|
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. #
|
|
|
|
# #
|
|
|
|
# This R package was created for academic research and was publicly #
|
|
|
|
# released in the hope that it will be useful, but it comes WITHOUT #
|
|
|
|
# ANY WARRANTY OR LIABILITY. #
|
2019-04-05 18:47:39 +02:00
|
|
|
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
|
2018-12-16 22:45:12 +01:00
|
|
|
# ==================================================================== #
|
|
|
|
|
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-02-18 02:33:37 +01:00
|
|
|
# first isolates
|
2018-04-02 16:05:09 +02:00
|
|
|
expect_equal(
|
2018-06-19 10:05:38 +02:00
|
|
|
sum(
|
2019-08-27 16:45:42 +02:00
|
|
|
first_isolate(x = example_isolates,
|
2018-04-02 16:05:09 +02:00
|
|
|
col_date = "date",
|
|
|
|
col_patient_id = "patient_id",
|
2018-08-31 13:36:19 +02:00
|
|
|
col_mo = "mo",
|
2018-07-25 14:17:04 +02:00
|
|
|
info = TRUE),
|
2018-07-02 09:34:20 +02:00
|
|
|
na.rm = TRUE),
|
2019-05-31 14:25:11 +02:00
|
|
|
1317)
|
2018-06-19 10:05:38 +02:00
|
|
|
|
2019-02-18 02:33:37 +01:00
|
|
|
# first *weighted* isolates
|
2018-06-19 10:05:38 +02:00
|
|
|
expect_equal(
|
|
|
|
suppressWarnings(
|
|
|
|
sum(
|
2019-08-27 16:45:42 +02:00
|
|
|
first_isolate(x = example_isolates %>% mutate(keyab = key_antibiotics(.)),
|
2018-12-14 09:31:53 +01:00
|
|
|
# let syntax determine these automatically:
|
|
|
|
# col_date = "date",
|
|
|
|
# col_patient_id = "patient_id",
|
|
|
|
# col_mo = "mo",
|
|
|
|
# col_keyantibiotics = "keyab",
|
|
|
|
type = "keyantibiotics",
|
|
|
|
info = TRUE),
|
|
|
|
na.rm = TRUE)),
|
2019-05-31 14:25:11 +02:00
|
|
|
1413)
|
2018-12-14 09:31:53 +01:00
|
|
|
# should be same for tibbles
|
|
|
|
expect_equal(
|
|
|
|
suppressWarnings(
|
|
|
|
sum(
|
2019-08-27 16:45:42 +02:00
|
|
|
first_isolate(x = example_isolates %>% dplyr::as_tibble() %>% mutate(keyab = key_antibiotics(.)),
|
2018-12-14 07:48:12 +01:00
|
|
|
# let syntax determine these automatically:
|
|
|
|
# col_date = "date",
|
|
|
|
# col_patient_id = "patient_id",
|
|
|
|
# col_mo = "mo",
|
|
|
|
# col_keyantibiotics = "keyab",
|
2018-06-19 10:05:38 +02:00
|
|
|
type = "keyantibiotics",
|
|
|
|
info = TRUE),
|
|
|
|
na.rm = TRUE)),
|
2019-05-31 14:25:11 +02:00
|
|
|
1413)
|
2019-02-18 02:33:37 +01:00
|
|
|
# when not ignoring I
|
2018-08-29 16:25:57 +02:00
|
|
|
expect_equal(
|
|
|
|
suppressWarnings(
|
|
|
|
sum(
|
2019-08-27 16:45:42 +02:00
|
|
|
first_isolate(x = example_isolates %>% mutate(keyab = key_antibiotics(.)),
|
2018-08-29 16:25:57 +02:00
|
|
|
col_date = "date",
|
|
|
|
col_patient_id = "patient_id",
|
2018-08-31 13:36:19 +02:00
|
|
|
col_mo = "mo",
|
2018-08-29 16:25:57 +02:00
|
|
|
col_keyantibiotics = "keyab",
|
|
|
|
ignore_I = FALSE,
|
|
|
|
type = "keyantibiotics",
|
|
|
|
info = TRUE),
|
|
|
|
na.rm = TRUE)),
|
2019-05-31 14:25:11 +02:00
|
|
|
1436)
|
2019-02-18 02:33:37 +01:00
|
|
|
# when using points
|
2018-07-02 09:34:20 +02:00
|
|
|
expect_equal(
|
|
|
|
suppressWarnings(
|
|
|
|
sum(
|
2019-08-27 16:45:42 +02:00
|
|
|
first_isolate(x = example_isolates %>% mutate(keyab = key_antibiotics(.)),
|
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_keyantibiotics = "keyab",
|
|
|
|
type = "points",
|
|
|
|
info = TRUE),
|
|
|
|
na.rm = TRUE)),
|
2019-05-31 14:25:11 +02:00
|
|
|
1417)
|
2018-04-03 11:08:31 +02:00
|
|
|
|
2019-02-18 02:33:37 +01:00
|
|
|
# first non-ICU isolates
|
2018-04-20 13:45:34 +02:00
|
|
|
expect_equal(
|
|
|
|
sum(
|
2019-08-27 16:45:42 +02:00
|
|
|
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),
|
2019-05-31 14:25:11 +02:00
|
|
|
1163)
|
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)
|
2018-06-19 10:05:38 +02:00
|
|
|
expect_lt(
|
|
|
|
sum(
|
2019-08-27 16:45:42 +02:00
|
|
|
first_isolate(x = mutate(example_isolates,
|
2018-06-19 10:05:38 +02:00
|
|
|
specimen = if_else(row_number() %in% random_rows,
|
|
|
|
"Urine",
|
2018-07-25 14:17:04 +02:00
|
|
|
"Other")),
|
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(
|
2019-08-27 16:45:42 +02:00
|
|
|
first_isolate(x = mutate(example_isolates,
|
2018-07-02 09:34:20 +02:00
|
|
|
specimen = if_else(row_number() %in% random_rows,
|
|
|
|
"Urine",
|
2018-07-25 14:17:04 +02:00
|
|
|
"Other")),
|
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
|
|
|
|
2018-08-29 16:25:57 +02:00
|
|
|
# "No isolates found"
|
2019-08-27 16:45:42 +02:00
|
|
|
expect_message(example_isolates %>%
|
2018-08-23 21:27:15 +02:00
|
|
|
mutate(specimen = "test") %>%
|
|
|
|
mutate(first = first_isolate(., "date", "patient_id",
|
2018-08-31 13:36:19 +02:00
|
|
|
col_mo = "mo",
|
2018-08-29 16:25:57 +02:00
|
|
|
col_specimen = "specimen",
|
2018-12-22 22:39:34 +01:00
|
|
|
filter_specimen = "something_unexisting")))
|
2018-08-29 16:25:57 +02:00
|
|
|
|
|
|
|
# printing of exclusion message
|
2019-08-27 16:45:42 +02:00
|
|
|
expect_output(example_isolates %>%
|
2018-12-22 22:39:34 +01:00
|
|
|
first_isolate(col_date = "date",
|
|
|
|
col_mo = "mo",
|
|
|
|
col_patient_id = "patient_id",
|
|
|
|
col_testcode = "gender",
|
|
|
|
testcodes_exclude = "M"))
|
2018-08-29 16:25:57 +02:00
|
|
|
|
|
|
|
# errors
|
2018-08-31 13:36:19 +02:00
|
|
|
expect_error(first_isolate("date", "patient_id", col_mo = "mo"))
|
2019-08-27 16:45:42 +02:00
|
|
|
expect_error(first_isolate(example_isolates,
|
2018-08-29 16:25:57 +02:00
|
|
|
col_date = "non-existing col",
|
2018-08-31 13:36:19 +02:00
|
|
|
col_mo = "mo"))
|
2018-08-29 16:25:57 +02:00
|
|
|
|
2018-10-23 11:15:05 +02:00
|
|
|
# look for columns itself
|
2019-08-27 16:45:42 +02:00
|
|
|
expect_message(first_isolate(example_isolates))
|
|
|
|
expect_message(first_isolate(example_isolates %>%
|
2018-12-22 22:39:34 +01:00
|
|
|
mutate(mo = as.character(mo)) %>%
|
|
|
|
left_join_microorganisms()))
|
2018-10-23 11:15:05 +02:00
|
|
|
|
2018-09-01 21:19:46 +02:00
|
|
|
# if mo is not an mo class, result should be the same
|
2019-08-27 16:45:42 +02:00
|
|
|
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"),
|
2019-08-27 16:45:42 +02:00
|
|
|
example_isolates %>%
|
2018-09-01 21:19:46 +02:00
|
|
|
first_isolate(col_date = "date",
|
|
|
|
col_mo = "mo",
|
|
|
|
col_patient_id = "patient_id"))
|
2018-08-29 16:25:57 +02:00
|
|
|
|
2019-05-31 14:25:11 +02:00
|
|
|
# missing dates should be no problem
|
2019-08-27 16:45:42 +02:00
|
|
|
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),
|
2019-05-31 14:25:11 +02:00
|
|
|
1322)
|
2019-08-08 22:39:42 +02:00
|
|
|
|
|
|
|
# unknown MOs
|
2019-08-27 16:45:42 +02:00
|
|
|
expect_equal(example_isolates %>%
|
2019-09-18 15:46:09 +02:00
|
|
|
mutate(mo = ifelse(mo == "B_ESCHR_COLI", "UNKNOWN", mo)) %>%
|
2019-08-08 22:39:42 +02:00
|
|
|
mutate(first = first_isolate(., include_unknown = FALSE)) %>%
|
|
|
|
.$first %>%
|
|
|
|
sum(),
|
|
|
|
1062)
|
2019-08-27 16:45:42 +02:00
|
|
|
expect_equal(example_isolates %>%
|
2019-09-18 15:46:09 +02:00
|
|
|
mutate(mo = ifelse(mo == "B_ESCHR_COLI", "UNKNOWN", mo)) %>%
|
2019-08-08 22:39:42 +02:00
|
|
|
mutate(first = first_isolate(., include_unknown = TRUE)) %>%
|
|
|
|
.$first %>%
|
|
|
|
sum(),
|
|
|
|
1529)
|
2019-08-27 16:45:42 +02:00
|
|
|
expect_equal(example_isolates %>%
|
2019-09-18 15:46:09 +02:00
|
|
|
mutate(mo = ifelse(mo == "B_ESCHR_COLI", NA, mo)) %>%
|
2019-08-08 22:39:42 +02:00
|
|
|
mutate(first = first_isolate(.)) %>%
|
|
|
|
.$first %>%
|
|
|
|
sum(),
|
|
|
|
1062)
|
2019-05-13 14:56:23 +02:00
|
|
|
|
2018-03-27 17:43:42 +02:00
|
|
|
})
|