AMR/tests/testthat/test-count.R

89 lines
4.0 KiB
R
Raw Normal View History

# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# 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) #
# #
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-08-23 00:40:36 +02:00
context("count.R")
test_that("counts work", {
2019-05-10 16:44:59 +02:00
# AMX resistance in `septic_patients`
expect_equal(count_R(septic_patients$AMX), 683)
expect_equal(count_I(septic_patients$AMX), 3)
expect_equal(count_S(septic_patients$AMX), 543)
expect_equal(count_R(septic_patients$AMX) + count_I(septic_patients$AMX),
count_IR(septic_patients$AMX))
expect_equal(count_S(septic_patients$AMX) + count_I(septic_patients$AMX),
count_SI(septic_patients$AMX))
2018-08-23 00:40:36 +02:00
2018-10-12 16:35:18 +02:00
library(dplyr)
2019-05-10 16:44:59 +02:00
expect_equal(septic_patients %>% count_S(AMC), 1342)
expect_equal(septic_patients %>% count_S(AMC, GEN), 1660)
expect_equal(septic_patients %>% count_all(AMC, GEN), 1798)
expect_identical(septic_patients %>% count_all(AMC, GEN),
septic_patients %>% count_S(AMC, GEN) +
septic_patients %>% count_IR(AMC, GEN))
2018-08-23 00:40:36 +02:00
# count of cases
expect_equal(septic_patients %>%
group_by(hospital_id) %>%
2019-05-10 16:44:59 +02:00
summarise(cipro = count_S(CIP),
genta = count_S(GEN),
combination = count_S(CIP, GEN)) %>%
2018-08-23 00:40:36 +02:00
pull(combination),
2018-09-24 23:33:29 +02:00
c(192, 446, 184, 474))
2018-08-23 00:40:36 +02:00
2018-10-16 09:59:31 +02:00
# count_df
expect_equal(
2019-05-10 16:44:59 +02:00
septic_patients %>% select(AMX) %>% count_df() %>% pull(Value),
2019-05-13 10:10:16 +02:00
c(septic_patients$AMX %>% count_SI(),
2019-05-10 16:44:59 +02:00
septic_patients$AMX %>% count_R())
2018-10-16 09:59:31 +02:00
)
expect_equal(
2019-05-10 16:44:59 +02:00
septic_patients %>% select(AMX) %>% count_df(combine_IR = TRUE) %>% pull(Value),
c(septic_patients$AMX %>% count_S(),
septic_patients$AMX %>% count_IR())
2018-10-16 09:59:31 +02:00
)
2019-05-13 10:10:16 +02:00
expect_equal(
septic_patients %>% select(AMX) %>% count_df(combine_SI = FALSE) %>% pull(Value),
c(septic_patients$AMX %>% count_S(),
septic_patients$AMX %>% count_I(),
septic_patients$AMX %>% count_R())
)
2018-08-23 01:01:50 +02:00
2018-08-23 00:40:36 +02:00
# warning for speed loss
2019-05-10 16:44:59 +02:00
expect_warning(count_R(as.character(septic_patients$AMC)))
expect_warning(count_I(as.character(septic_patients$AMC)))
expect_warning(count_S(as.character(septic_patients$AMC,
septic_patients$GEN)))
expect_warning(count_S(septic_patients$AMC,
as.character(septic_patients$GEN)))
2018-08-23 00:40:36 +02:00
# check for errors
expect_error(count_IR("test", minimum = "test"))
expect_error(count_IR("test", as_percent = "test"))
expect_error(count_I("test", minimum = "test"))
expect_error(count_I("test", as_percent = "test"))
expect_error(count_S("test", minimum = "test"))
expect_error(count_S("test", as_percent = "test"))
2018-09-16 22:11:17 +02:00
expect_error(count_df(c("A", "B", "C")))
2018-09-17 21:51:21 +02:00
expect_error(count_df(septic_patients[,"date"]))
2018-09-16 22:11:17 +02:00
2018-08-23 00:40:36 +02:00
})