1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-28 04:46:11 +01:00
AMR/tests/testthat/test-portion.R

182 lines
8.0 KiB
R
Raw Normal View History

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", {
2018-08-03 14:49:29 +02:00
# amox resistance in `septic_patients`
2018-10-17 17:32:34 +02:00
expect_equal(portion_R(septic_patients$amox), 0.5827645, tolerance = 0.0001)
expect_equal(portion_I(septic_patients$amox), 0.0025597, tolerance = 0.0001)
2018-08-10 15:01:05 +02:00
expect_equal(1 - portion_R(septic_patients$amox) - portion_I(septic_patients$amox),
portion_S(septic_patients$amox))
expect_equal(portion_R(septic_patients$amox) + portion_I(septic_patients$amox),
portion_IR(septic_patients$amox))
expect_equal(portion_S(septic_patients$amox) + portion_I(septic_patients$amox),
portion_SI(septic_patients$amox))
2018-03-27 17:43:42 +02:00
2018-08-23 00:40:36 +02:00
expect_equal(septic_patients %>% portion_S(amcl),
2018-10-17 17:32:34 +02:00
0.7062363,
2018-08-23 00:40:36 +02:00
tolerance = 0.001)
expect_equal(septic_patients %>% portion_S(amcl, gent),
2018-10-17 17:32:34 +02:00
0.9210074,
2018-08-23 00:40:36 +02:00
tolerance = 0.001)
expect_equal(septic_patients %>% portion_S(amcl, gent, also_single_tested = TRUE),
0.9239669,
tolerance = 0.001)
2018-08-23 00:40:36 +02:00
# amcl+genta susceptibility around 92.1%
expect_equal(suppressWarnings(rsi(septic_patients$amcl,
2018-07-13 17:23:46 +02:00
septic_patients$gent,
interpretation = "S")),
2018-10-17 17:32:34 +02:00
0.9210074,
2018-08-23 00:40:36 +02:00
tolerance = 0.000001)
2018-05-02 14:56:25 +02:00
2018-08-03 14:49:29 +02:00
# percentages
expect_equal(septic_patients %>%
group_by(hospital_id) %>%
2018-08-10 15:01:05 +02:00
summarise(R = portion_R(cipr, as_percent = TRUE),
I = portion_I(cipr, as_percent = TRUE),
S = portion_S(cipr, as_percent = TRUE),
n = n_rsi(cipr),
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) %>%
2018-08-10 15:01:05 +02:00
summarise(cipro_p = portion_S(cipr, as_percent = TRUE),
2018-05-02 14:56:25 +02:00
cipro_n = n_rsi(cipr),
2018-08-10 15:01:05 +02:00
genta_p = portion_S(gent, as_percent = TRUE),
2018-05-02 14:56:25 +02:00
genta_n = n_rsi(gent),
2018-08-10 15:01:05 +02:00
combination_p = portion_S(cipr, gent, as_percent = TRUE),
combination_n = n_rsi(cipr, gent)) %>%
2018-05-02 14:56:25 +02:00
pull(combination_n),
2018-09-24 23:33:29 +02:00
c(202, 488, 201, 499))
2018-07-17 10:32:26 +02:00
2018-08-10 15:01:05 +02:00
expect_warning(portion_R(as.character(septic_patients$amcl)))
expect_warning(portion_S(as.character(septic_patients$amcl)))
expect_warning(portion_S(as.character(septic_patients$amcl,
2018-07-17 10:32:26 +02:00
septic_patients$gent)))
2018-08-23 00:40:36 +02:00
expect_warning(n_rsi(as.character(septic_patients$amcl,
septic_patients$gent)))
expect_equal(suppressWarnings(n_rsi(as.character(septic_patients$amcl,
septic_patients$gent))),
2018-10-17 17:32:34 +02:00
1828)
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 = "test"))
2018-07-30 00:57:49 +02:00
# check too low amount of isolates
2018-10-12 16:35:18 +02:00
expect_identical(suppressWarnings(portion_R(septic_patients$amox, minimum = nrow(septic_patients) + 1)),
2018-08-03 14:49:29 +02:00
NA)
2018-10-12 16:35:18 +02:00
expect_identical(suppressWarnings(portion_I(septic_patients$amox, minimum = nrow(septic_patients) + 1)),
2018-07-30 00:57:49 +02:00
NA)
2018-10-12 16:35:18 +02:00
expect_identical(suppressWarnings(portion_S(septic_patients$amox, minimum = nrow(septic_patients) + 1)),
2018-07-30 00:57:49 +02:00
NA)
# warning for speed loss
2018-08-10 15:01:05 +02:00
expect_warning(portion_R(as.character(septic_patients$gent)))
expect_warning(portion_I(as.character(septic_patients$gent)))
expect_warning(portion_S(septic_patients$amcl, as.character(septic_patients$gent)))
2018-07-30 00:57:49 +02:00
2018-03-27 17:43:42 +02:00
})
2018-07-17 11:18:36 +02:00
test_that("old rsi works", {
2018-10-17 17:32:34 +02:00
# amox resistance in `septic_patients` should be around 58.53%
expect_equal(suppressWarnings(rsi(septic_patients$amox)), 0.5853, tolerance = 0.0001)
expect_equal(suppressWarnings(rsi(septic_patients$amox, interpretation = "S")), 1 - 0.5853, tolerance = 0.0001)
2018-08-10 15:01:05 +02:00
2018-07-17 11:18:36 +02:00
# pita+genta susceptibility around 98.09%
2018-07-29 22:14:51 +02:00
expect_equal(suppressWarnings(rsi(septic_patients$pita,
septic_patients$gent,
interpretation = "S",
info = TRUE)),
2018-10-17 17:32:34 +02:00
0.9498886,
2018-07-17 11:18:36 +02:00
tolerance = 0.0001)
# count of cases
expect_equal(septic_patients %>%
group_by(hospital_id) %>%
2018-07-29 22:14:51 +02:00
summarise(cipro_S = suppressWarnings(rsi(cipr, interpretation = "S",
as_percent = TRUE, warning = FALSE)),
2018-07-17 11:18:36 +02:00
cipro_n = n_rsi(cipr),
2018-07-29 22:14:51 +02:00
genta_S = suppressWarnings(rsi(gent, interpretation = "S",
as_percent = TRUE, warning = FALSE)),
2018-07-17 11:18:36 +02:00
genta_n = n_rsi(gent),
2018-07-29 22:14:51 +02:00
combination_S = suppressWarnings(rsi(cipr, gent, interpretation = "S",
as_percent = TRUE, warning = FALSE)),
2018-07-17 11:18:36 +02:00
combination_n = n_rsi(cipr, gent)) %>%
pull(combination_n),
2018-09-24 23:33:29 +02:00
c(202, 488, 201, 499))
2018-08-12 17:44:06 +02:00
# portion_df
expect_equal(
2018-09-16 22:11:17 +02:00
septic_patients %>% select(amox) %>% portion_df() %>% pull(Value),
2018-08-12 17:44:06 +02:00
c(septic_patients$amox %>% portion_S(),
septic_patients$amox %>% portion_I(),
septic_patients$amox %>% portion_R())
)
2018-10-16 09:59:31 +02:00
expect_equal(
septic_patients %>% select(amox) %>% portion_df(combine_IR = TRUE) %>% pull(Value),
c(septic_patients$amox %>% portion_S(),
septic_patients$amox %>% portion_IR())
)
2018-08-12 17:44:06 +02:00
2018-07-17 11:18:36 +02:00
})
2018-03-27 17:43:42 +02:00
test_that("prediction of rsi works", {
2018-04-03 11:08:31 +02:00
amox_R <- septic_patients %>%
2018-09-24 23:33:29 +02:00
filter(mo == "B_ESCHR_COL") %>%
2018-04-03 11:08:31 +02:00
rsi_predict(col_ab = "amox",
col_date = "date",
2018-07-26 16:30:42 +02:00
minimum = 10,
2018-07-17 11:18:36 +02:00
info = TRUE) %>%
2018-07-28 09:34:03 +02:00
pull("value")
# amox resistance will increase according to data set `septic_patients`
expect_true(amox_R[3] < amox_R[20])
2018-04-03 11:08:31 +02:00
2018-09-24 23:33:29 +02:00
expect_output(rsi_predict(tbl = filter(septic_patients, mo == "B_ESCHR_COL"),
2018-04-03 11:08:31 +02:00
model = "binomial",
col_ab = "amox",
col_date = "date",
info = TRUE))
2018-09-24 23:33:29 +02:00
expect_output(rsi_predict(tbl = filter(septic_patients, mo == "B_ESCHR_COL"),
2018-04-03 11:08:31 +02:00
model = "loglin",
col_ab = "amox",
col_date = "date",
info = TRUE))
2018-09-24 23:33:29 +02:00
expect_output(rsi_predict(tbl = filter(septic_patients, mo == "B_ESCHR_COL"),
2018-04-03 11:08:31 +02:00
model = "lin",
col_ab = "amox",
col_date = "date",
info = TRUE))
2018-09-24 23:33:29 +02:00
expect_error(rsi_predict(tbl = filter(septic_patients, mo == "B_ESCHR_COL"),
2018-03-27 17:43:42 +02:00
model = "INVALID MODEL",
col_ab = "amox",
col_date = "date",
2018-07-17 11:18:36 +02:00
info = TRUE))
2018-09-24 23:33:29 +02:00
expect_error(rsi_predict(tbl = filter(septic_patients, mo == "B_ESCHR_COL"),
2018-03-27 17:43:42 +02:00
col_ab = "NOT EXISTING COLUMN",
col_date = "date",
2018-07-17 11:18:36 +02:00
info = TRUE))
2018-09-24 23:33:29 +02:00
expect_error(rsi_predict(tbl = filter(septic_patients, mo == "B_ESCHR_COL"),
2018-03-27 17:43:42 +02:00
col_ab = "amox",
col_date = "NOT EXISTING COLUMN",
2018-07-17 11:18:36 +02:00
info = TRUE))
2018-07-30 00:57:49 +02:00
# almost all E. coli are mero S in the Netherlands :)
2018-09-24 23:33:29 +02:00
expect_error(resistance_predict(tbl = filter(septic_patients, mo == "B_ESCHR_COL"),
2018-07-30 00:57:49 +02:00
col_ab = "mero",
col_date = "date",
info = TRUE))
2018-09-16 22:11:17 +02:00
expect_error(portion_df(c("A", "B", "C")))
2018-09-17 21:51:21 +02:00
expect_error(portion_df(septic_patients[,"date"]))
2018-03-27 17:43:42 +02:00
})