1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-08 20:41:58 +02:00

(v0.8.0.9027) adding susceptibility() and resistance()

This commit is contained in:
2019-11-10 12:16:56 +01:00
parent 228a4245cb
commit 59ededa8dc
85 changed files with 1526 additions and 932 deletions

View File

@ -174,7 +174,6 @@ Function Bootstrap {
Progress "Downloading and installing travis_tool.sh"
cp "tests\appveyor\travis_tool.sh" "..\travis_tool.sh"
# Invoke-WebRequest https://gitlab.com/msberends/AMR/raw/master/tests/appveyor/travis_tool.sh -OutFile "..\travis_tool.sh"
echo '@bash.exe ../travis_tool.sh %*' | Out-File -Encoding ASCII .\travis_tool.sh.cmd
cat .\travis_tool.sh.cmd
bash -c "( echo; echo '^travis_tool\.sh\.cmd$' ) >> .Rbuildignore"

View File

@ -22,67 +22,67 @@
context("count.R")
test_that("counts work", {
expect_equal(count_resistant(example_isolates$AMX), count_R(example_isolates$AMX))
expect_equal(count_susceptible(example_isolates$AMX), count_SI(example_isolates$AMX))
expect_equal(count_all(example_isolates$AMX), n_rsi(example_isolates$AMX))
# AMX resistance in `example_isolates`
expect_equal(count_R(example_isolates$AMX), 683)
expect_equal(count_I(example_isolates$AMX), 3)
expect_equal(count_S(example_isolates$AMX), 543)
expect_equal(count_R(example_isolates$AMX) + count_I(example_isolates$AMX),
count_IR(example_isolates$AMX))
expect_equal(count_S(example_isolates$AMX) + count_I(example_isolates$AMX),
count_SI(example_isolates$AMX))
expect_equal(count_R(example_isolates$AMX), 683)
expect_equal(count_I(example_isolates$AMX), 3)
expect_equal(suppressWarnings(count_S(example_isolates$AMX)), 543)
expect_equal(count_R(example_isolates$AMX) + count_I(example_isolates$AMX),
suppressWarnings(count_IR(example_isolates$AMX)))
expect_equal(suppressWarnings(count_S(example_isolates$AMX)) + count_I(example_isolates$AMX),
count_SI(example_isolates$AMX))
library(dplyr)
expect_equal(example_isolates %>% count_S(AMC), 1342)
expect_equal(example_isolates %>% count_S(AMC, GEN, only_all_tested = TRUE), 1660)
expect_equal(example_isolates %>% count_S(AMC, GEN, only_all_tested = FALSE), 1728)
expect_equal(example_isolates %>% count_susceptible(AMC), 1433)
expect_equal(example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE), 1687)
expect_equal(example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = FALSE), 1764)
expect_equal(example_isolates %>% count_all(AMC, GEN, only_all_tested = TRUE), 1798)
expect_equal(example_isolates %>% count_all(AMC, GEN, only_all_tested = FALSE), 1936)
expect_identical(example_isolates %>% count_all(AMC, GEN, only_all_tested = TRUE),
example_isolates %>% count_S(AMC, GEN, only_all_tested = TRUE) +
example_isolates %>% count_IR(AMC, GEN, only_all_tested = TRUE))
example_isolates %>% count_susceptible(AMC, GEN, only_all_tested = TRUE) +
example_isolates %>% count_resistant(AMC, GEN, only_all_tested = TRUE))
# count of cases
expect_equal(example_isolates %>%
group_by(hospital_id) %>%
summarise(cipro = count_SI(CIP),
genta = count_SI(GEN),
combination = count_SI(CIP, GEN)) %>%
summarise(cipro = count_susceptible(CIP),
genta = count_susceptible(GEN),
combination = count_susceptible(CIP, GEN)) %>%
pull(combination),
c(253, 465, 192, 558))
# count_df
expect_equal(
example_isolates %>% select(AMX) %>% count_df() %>% pull(value),
c(example_isolates$AMX %>% count_SI(),
example_isolates$AMX %>% count_R())
c(example_isolates$AMX %>% count_susceptible(),
example_isolates$AMX %>% count_resistant())
)
expect_equal(
example_isolates %>% select(AMX) %>% count_df(combine_IR = TRUE) %>% pull(value),
c(example_isolates$AMX %>% count_S(),
example_isolates$AMX %>% count_IR())
c(suppressWarnings(example_isolates$AMX %>% count_S()),
suppressWarnings(example_isolates$AMX %>% count_IR()))
)
expect_equal(
example_isolates %>% select(AMX) %>% count_df(combine_SI = FALSE) %>% pull(value),
c(example_isolates$AMX %>% count_S(),
c(suppressWarnings(example_isolates$AMX %>% count_S()),
example_isolates$AMX %>% count_I(),
example_isolates$AMX %>% count_R())
)
# warning for speed loss
expect_warning(count_R(as.character(example_isolates$AMC)))
expect_warning(count_I(as.character(example_isolates$AMC)))
expect_warning(count_S(as.character(example_isolates$AMC,
example_isolates$GEN)))
expect_warning(count_S(example_isolates$AMC,
as.character(example_isolates$GEN)))
expect_warning(count_resistant(as.character(example_isolates$AMC)))
expect_warning(count_resistant(example_isolates$AMC,
as.character(example_isolates$GEN)))
# 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"))
expect_error(count_resistant("test", minimum = "test"))
expect_error(count_resistant("test", as_percent = "test"))
expect_error(count_susceptible("test", minimum = "test"))
expect_error(count_susceptible("test", as_percent = "test"))
expect_error(count_df(c("A", "B", "C")))
expect_error(count_df(example_isolates[, "date"]))

View File

@ -22,20 +22,12 @@
context("deprecated.R")
test_that("deprecated functions work", {
# first 5 chars of official name
expect_equal(suppressWarnings(as.character(as.atc(c("nitro", "cipro")))),
c("J01XE01", "J01MA02"))
# EARS-Net
expect_equal(suppressWarnings(as.character(as.atc("AMX"))),
"J01CA04")
expect_equal(suppressWarnings(guess_ab_col(data.frame(AMP_ND10 = "R",
AMC_ED20 = "S"),
as.atc("augmentin"))),
"AMC_ED20")
expect_identical(suppressWarnings(p.symbol(seq(0, 1, 0.001))),
p_symbol(seq(0, 1, 0.001)))
expect_equal(suppressWarnings(portion_S(example_isolates$AMX)), proportion_S(example_isolates$AMX))
expect_equal(suppressWarnings(portion_SI(example_isolates$AMX)), proportion_SI(example_isolates$AMX))
expect_equal(suppressWarnings(portion_I(example_isolates$AMX)), proportion_I(example_isolates$AMX))
expect_equal(suppressWarnings(portion_IR(example_isolates$AMX)), proportion_IR(example_isolates$AMX))
expect_equal(suppressWarnings(portion_R(example_isolates$AMX)), proportion_R(example_isolates$AMX))
})

View File

@ -30,26 +30,26 @@ test_that("ggplot_rsi works", {
# data should be equal
expect_equal(
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi())$data %>% summarise_all(portion_IR) %>% as.double(),
example_isolates %>% select(AMC, CIP) %>% summarise_all(portion_IR) %>% as.double()
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi())$data %>% summarise_all(resistance) %>% as.double(),
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
)
print(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "interpretation", facet = "antibiotic"))
print(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))
expect_equal(
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "interpretation", facet = "antibiotic"))$data %>% summarise_all(portion_IR) %>% as.double(),
example_isolates %>% select(AMC, CIP) %>% summarise_all(portion_IR) %>% as.double()
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "interpretation", facet = "antibiotic"))$data %>% summarise_all(resistance) %>% as.double(),
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
)
expect_equal(
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))$data %>% summarise_all(portion_IR) %>% as.double(),
example_isolates %>% select(AMC, CIP) %>% summarise_all(portion_IR) %>% as.double()
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))$data %>% summarise_all(resistance) %>% as.double(),
example_isolates %>% select(AMC, CIP) %>% summarise_all(resistance) %>% as.double()
)
expect_equal(
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))$data %>% summarise_all(count_IR) %>% as.double(),
example_isolates %>% select(AMC, CIP) %>% summarise_all(count_IR) %>% as.double()
(example_isolates %>% select(AMC, CIP) %>% ggplot_rsi(x = "antibiotic", facet = "interpretation"))$data %>% summarise_all(count_resistant) %>% as.double(),
example_isolates %>% select(AMC, CIP) %>% summarise_all(count_resistant) %>% as.double()
)
# support for scale_type ab and mo

View File

@ -1,121 +0,0 @@
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# LICENCE #
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
# #
# 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. #
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
# ==================================================================== #
context("portion.R")
test_that("portions works", {
# AMX resistance in `example_isolates`
expect_equal(portion_R(example_isolates$AMX), 0.5557364, tolerance = 0.0001)
expect_equal(portion_I(example_isolates$AMX), 0.002441009, tolerance = 0.0001)
expect_equal(1 - portion_R(example_isolates$AMX) - portion_I(example_isolates$AMX),
portion_S(example_isolates$AMX))
expect_equal(portion_R(example_isolates$AMX) + portion_I(example_isolates$AMX),
portion_IR(example_isolates$AMX))
expect_equal(portion_S(example_isolates$AMX) + portion_I(example_isolates$AMX),
portion_SI(example_isolates$AMX))
expect_equal(example_isolates %>% portion_SI(AMC),
0.7626397,
tolerance = 0.0001)
expect_equal(example_isolates %>% portion_SI(AMC, GEN),
0.9408,
tolerance = 0.0001)
expect_equal(example_isolates %>% portion_SI(AMC, GEN, only_all_tested = TRUE),
0.9382647,
tolerance = 0.0001)
# percentages
expect_equal(example_isolates %>%
group_by(hospital_id) %>%
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),
total = n()) %>%
pull(n) %>%
sum(),
1409)
# count of cases
expect_equal(example_isolates %>%
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),
combination_n = n_rsi(CIP, GEN)) %>%
pull(combination_n),
c(305, 617, 241, 711))
expect_warning(portion_R(as.character(example_isolates$AMC)))
expect_warning(portion_S(as.character(example_isolates$AMC)))
expect_warning(portion_S(as.character(example_isolates$AMC,
example_isolates$GEN)))
expect_warning(n_rsi(as.character(example_isolates$AMC,
example_isolates$GEN)))
expect_equal(suppressWarnings(n_rsi(as.character(example_isolates$AMC,
example_isolates$GEN))),
1879)
# check for errors
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))
# check too low amount of isolates
expect_identical(suppressWarnings(portion_R(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA)
expect_identical(suppressWarnings(portion_I(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA)
expect_identical(suppressWarnings(portion_S(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA)
# warning for speed loss
expect_warning(portion_R(as.character(example_isolates$GEN)))
expect_warning(portion_I(as.character(example_isolates$GEN)))
expect_warning(portion_S(example_isolates$AMC, as.character(example_isolates$GEN)))
# portion_df
expect_equal(
example_isolates %>% select(AMX) %>% portion_df() %>% pull(value),
c(example_isolates$AMX %>% portion_SI(),
example_isolates$AMX %>% portion_R())
)
expect_equal(
example_isolates %>% select(AMX) %>% portion_df(combine_IR = TRUE) %>% pull(value),
c(example_isolates$AMX %>% portion_S(),
example_isolates$AMX %>% portion_IR())
)
expect_equal(
example_isolates %>% select(AMX) %>% portion_df(combine_SI = FALSE) %>% pull(value),
c(example_isolates$AMX %>% portion_S(),
example_isolates$AMX %>% portion_I(),
example_isolates$AMX %>% portion_R())
)
expect_error(portion_df(c("A", "B", "C")))
expect_error(portion_df(example_isolates[, "date"]))
})

124
tests/testthat/test-proportion.R Executable file
View File

@ -0,0 +1,124 @@
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# LICENCE #
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
# #
# 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. #
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
# ==================================================================== #
context("proportion.R")
test_that("proportions works", {
expect_equal(proportion_R(example_isolates$AMX), resistance(example_isolates$AMX))
expect_equal(proportion_SI(example_isolates$AMX), susceptibility(example_isolates$AMX))
# AMX resistance in `example_isolates`
expect_equal(proportion_R(example_isolates$AMX), 0.5557364, tolerance = 0.0001)
expect_equal(proportion_I(example_isolates$AMX), 0.002441009, tolerance = 0.0001)
expect_equal(1 - proportion_R(example_isolates$AMX) - proportion_I(example_isolates$AMX),
proportion_S(example_isolates$AMX))
expect_equal(proportion_R(example_isolates$AMX) + proportion_I(example_isolates$AMX),
proportion_IR(example_isolates$AMX))
expect_equal(proportion_S(example_isolates$AMX) + proportion_I(example_isolates$AMX),
proportion_SI(example_isolates$AMX))
expect_equal(example_isolates %>% proportion_SI(AMC),
0.7626397,
tolerance = 0.0001)
expect_equal(example_isolates %>% proportion_SI(AMC, GEN),
0.9408,
tolerance = 0.0001)
expect_equal(example_isolates %>% proportion_SI(AMC, GEN, only_all_tested = TRUE),
0.9382647,
tolerance = 0.0001)
# percentages
expect_equal(example_isolates %>%
group_by(hospital_id) %>%
summarise(R = proportion_R(CIP, as_percent = TRUE),
I = proportion_I(CIP, as_percent = TRUE),
S = proportion_S(CIP, as_percent = TRUE),
n = n_rsi(CIP),
total = n()) %>%
pull(n) %>%
sum(),
1409)
# count of cases
expect_equal(example_isolates %>%
group_by(hospital_id) %>%
summarise(cipro_p = proportion_SI(CIP, as_percent = TRUE),
cipro_n = n_rsi(CIP),
genta_p = proportion_SI(GEN, as_percent = TRUE),
genta_n = n_rsi(GEN),
combination_p = proportion_SI(CIP, GEN, as_percent = TRUE),
combination_n = n_rsi(CIP, GEN)) %>%
pull(combination_n),
c(305, 617, 241, 711))
expect_warning(proportion_R(as.character(example_isolates$AMC)))
expect_warning(proportion_S(as.character(example_isolates$AMC)))
expect_warning(proportion_S(as.character(example_isolates$AMC,
example_isolates$GEN)))
expect_warning(n_rsi(as.character(example_isolates$AMC,
example_isolates$GEN)))
expect_equal(suppressWarnings(n_rsi(as.character(example_isolates$AMC,
example_isolates$GEN))),
1879)
# check for errors
expect_error(proportion_IR("test", minimum = "test"))
expect_error(proportion_IR("test", as_percent = "test"))
expect_error(proportion_I("test", minimum = "test"))
expect_error(proportion_I("test", as_percent = "test"))
expect_error(proportion_S("test", minimum = "test"))
expect_error(proportion_S("test", as_percent = "test"))
expect_error(proportion_S("test", also_single_tested = TRUE))
# check too low amount of isolates
expect_identical(suppressWarnings(proportion_R(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA)
expect_identical(suppressWarnings(proportion_I(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA)
expect_identical(suppressWarnings(proportion_S(example_isolates$AMX, minimum = nrow(example_isolates) + 1)),
NA)
# warning for speed loss
expect_warning(proportion_R(as.character(example_isolates$GEN)))
expect_warning(proportion_I(as.character(example_isolates$GEN)))
expect_warning(proportion_S(example_isolates$AMC, as.character(example_isolates$GEN)))
# proportion_df
expect_equal(
example_isolates %>% select(AMX) %>% proportion_df() %>% pull(value),
c(example_isolates$AMX %>% proportion_SI(),
example_isolates$AMX %>% proportion_R())
)
expect_equal(
example_isolates %>% select(AMX) %>% proportion_df(combine_IR = TRUE) %>% pull(value),
c(example_isolates$AMX %>% proportion_S(),
example_isolates$AMX %>% proportion_IR())
)
expect_equal(
example_isolates %>% select(AMX) %>% proportion_df(combine_SI = FALSE) %>% pull(value),
c(example_isolates$AMX %>% proportion_S(),
example_isolates$AMX %>% proportion_I(),
example_isolates$AMX %>% proportion_R())
)
expect_error(proportion_df(c("A", "B", "C")))
expect_error(proportion_df(example_isolates[, "date"]))
})