1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-27 23:26:13 +01:00
AMR/tests/testthat/test-portion.R

122 lines
5.7 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-10 15:01:05 +02:00
context("portion.R")
2018-07-13 17:23:46 +02:00
2018-08-12 17:44:06 +02:00
test_that("portions works", {
2019-05-10 16:44:59 +02:00
# AMX resistance in `septic_patients`
expect_equal(portion_R(septic_patients$AMX), 0.5557364, tolerance = 0.0001)
expect_equal(portion_I(septic_patients$AMX), 0.002441009, tolerance = 0.0001)
expect_equal(1 - portion_R(septic_patients$AMX) - portion_I(septic_patients$AMX),
portion_S(septic_patients$AMX))
expect_equal(portion_R(septic_patients$AMX) + portion_I(septic_patients$AMX),
portion_IR(septic_patients$AMX))
expect_equal(portion_S(septic_patients$AMX) + portion_I(septic_patients$AMX),
portion_SI(septic_patients$AMX))
expect_equal(septic_patients %>% portion_SI(AMC),
0.7626397,
2019-02-08 16:06:54 +01:00
tolerance = 0.0001)
expect_equal(septic_patients %>% portion_SI(AMC, GEN),
0.9408,
2019-02-08 16:06:54 +01:00
tolerance = 0.0001)
expect_equal(septic_patients %>% portion_SI(AMC, GEN, only_all_tested = TRUE),
0.9382647,
2019-02-08 16:06:54 +01:00
tolerance = 0.0001)
2018-08-23 00:40:36 +02:00
2018-08-03 14:49:29 +02:00
# percentages
expect_equal(septic_patients %>%
group_by(hospital_id) %>%
2019-05-10 16:44:59 +02:00
summarise(R = portion_R(CIP, as_percent = TRUE),
I = portion_I(CIP, as_percent = TRUE),
S = portion_S(CIP, as_percent = TRUE),
n = n_rsi(CIP),
2018-08-03 14:49:29 +02:00
total = n()) %>%
pull(n) %>%
sum(),
2018-09-24 23:33:29 +02:00
1409)
2018-08-03 14:49:29 +02:00
2018-05-02 14:56:25 +02:00
# count of cases
expect_equal(septic_patients %>%
group_by(hospital_id) %>%
summarise(cipro_p = portion_SI(CIP, as_percent = TRUE),
cipro_n = n_rsi(CIP),
genta_p = portion_SI(GEN, as_percent = TRUE),
genta_n = n_rsi(GEN),
combination_p = portion_SI(CIP, GEN, as_percent = TRUE),
2019-05-10 16:44:59 +02:00
combination_n = n_rsi(CIP, GEN)) %>%
2018-05-02 14:56:25 +02:00
pull(combination_n),
c(305, 617, 241, 711))
2018-07-17 10:32:26 +02:00
2019-05-10 16:44:59 +02:00
expect_warning(portion_R(as.character(septic_patients$AMC)))
expect_warning(portion_S(as.character(septic_patients$AMC)))
expect_warning(portion_S(as.character(septic_patients$AMC,
septic_patients$GEN)))
expect_warning(n_rsi(as.character(septic_patients$AMC,
septic_patients$GEN)))
expect_equal(suppressWarnings(n_rsi(as.character(septic_patients$AMC,
septic_patients$GEN))),
2019-02-08 16:06:54 +01:00
1879)
2018-07-17 10:32:26 +02:00
2018-07-30 00:57:49 +02:00
# check for errors
2018-08-10 15:01:05 +02:00
expect_error(portion_IR("test", minimum = "test"))
expect_error(portion_IR("test", as_percent = "test"))
expect_error(portion_I("test", minimum = "test"))
expect_error(portion_I("test", as_percent = "test"))
expect_error(portion_S("test", minimum = "test"))
expect_error(portion_S("test", as_percent = "test"))
expect_error(portion_S("test", also_single_tested = TRUE))
2018-07-30 00:57:49 +02:00
# check too low amount of isolates
2019-05-10 16:44:59 +02:00
expect_identical(suppressWarnings(portion_R(septic_patients$AMX, minimum = nrow(septic_patients) + 1)),
2018-08-03 14:49:29 +02:00
NA)
2019-05-10 16:44:59 +02:00
expect_identical(suppressWarnings(portion_I(septic_patients$AMX, minimum = nrow(septic_patients) + 1)),
2018-07-30 00:57:49 +02:00
NA)
2019-05-10 16:44:59 +02:00
expect_identical(suppressWarnings(portion_S(septic_patients$AMX, minimum = nrow(septic_patients) + 1)),
2018-07-30 00:57:49 +02:00
NA)
# warning for speed loss
2019-05-10 16:44:59 +02:00
expect_warning(portion_R(as.character(septic_patients$GEN)))
expect_warning(portion_I(as.character(septic_patients$GEN)))
expect_warning(portion_S(septic_patients$AMC, as.character(septic_patients$GEN)))
2018-08-12 17:44:06 +02:00
# portion_df
expect_equal(
2019-06-13 14:28:46 +02:00
septic_patients %>% select(AMX) %>% portion_df() %>% pull(value),
2019-05-13 10:10:16 +02:00
c(septic_patients$AMX %>% portion_SI(),
2019-05-10 16:44:59 +02:00
septic_patients$AMX %>% portion_R())
2018-08-12 17:44:06 +02:00
)
2018-10-16 09:59:31 +02:00
expect_equal(
2019-06-13 14:28:46 +02:00
septic_patients %>% select(AMX) %>% portion_df(combine_IR = TRUE) %>% pull(value),
2019-05-10 16:44:59 +02:00
c(septic_patients$AMX %>% portion_S(),
septic_patients$AMX %>% portion_IR())
2018-10-16 09:59:31 +02:00
)
2019-05-13 10:10:16 +02:00
expect_equal(
2019-06-13 14:28:46 +02:00
septic_patients %>% select(AMX) %>% portion_df(combine_SI = FALSE) %>% pull(value),
2019-05-13 10:10:16 +02:00
c(septic_patients$AMX %>% portion_S(),
septic_patients$AMX %>% portion_I(),
septic_patients$AMX %>% portion_R())
)
2019-08-07 15:37:39 +02:00
expect_error(portion_df(c("A", "B", "C")))
expect_error(portion_df(septic_patients[,"date"]))
2018-07-17 11:18:36 +02:00
})