From 2af7b6686030ee77babfa8837c5a3c579a9807a8 Mon Sep 17 00:00:00 2001 From: LS31 Date: Wed, 11 Nov 2020 18:06:37 +0100 Subject: [PATCH] Improved support for NA. Added testing for renal function. --- R/blood_pressure.R | 19 ++-- R/bmi.R | 13 ++- R/bsa.R | 10 +- R/electrolytes.R | 24 ++--- R/metabolic_syndrome.R | 6 +- R/pneumonia.R | 6 +- R/qtc.R | 18 +++- R/renal_function.R | 134 +++++++++++++++------------ R/sepsis.R | 5 +- tests/testthat/test-blood_pressure.R | 26 ++++-- tests/testthat/test-bmi.R | 19 ++-- tests/testthat/test-bsa.R | 12 +-- tests/testthat/test-electrolytes.R | 9 +- tests/testthat/test-pneumonia.R | 19 ++++ tests/testthat/test-qtc.R | 12 +-- tests/testthat/test-renal_function.R | 53 +++++++++++ 16 files changed, 264 insertions(+), 121 deletions(-) create mode 100644 tests/testthat/test-pneumonia.R create mode 100644 tests/testthat/test-renal_function.R diff --git a/R/blood_pressure.R b/R/blood_pressure.R index 32c7f9f..c377171 100644 --- a/R/blood_pressure.R +++ b/R/blood_pressure.R @@ -9,12 +9,16 @@ #' #' @param sbp Systolic blood pressure (mmHg). #' @param dbp Diastolic blood pressure (mmHg). -#' @return MAP (mmHg). +#' @return MAP (mmHg), or `NA` if any parameters are `NA`. #' @export #' @seealso \code{\link[units]{set_units}}, \code{\link[units]{drop_units}} estimate_map <- function(sbp, dbp) { - assertthat::assert_that(assertthat::is.number(sbp) | is.na(sbp)) - assertthat::assert_that(assertthat::is.number(dbp) | is.na(dbp)) + if (anyNA(c(sbp, dbp))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(sbp)) + assertthat::assert_that(assertthat::is.number(dbp)) + (dbp + (1/3 * (sbp - dbp))) %>% units::set_units("mmHg", mode = "standard") } @@ -30,12 +34,15 @@ estimate_map <- function(sbp, dbp) { #' #' @param sbp Systolic blood pressure (mmHg). #' @param dbp Diastolic blood pressure (mmHg). -#' @return Pulse pressure (mmHg). +#' @return Pulse pressure (mmHg), or `NA` if any parameters are `NA`. #' @export #' @seealso \code{\link[units]{set_units}}, \code{\link[units]{drop_units}} calculate_pulse_pressure <- function(sbp, dbp) { - assertthat::assert_that(assertthat::is.number(sbp) | is.na(sbp)) - assertthat::assert_that(assertthat::is.number(dbp) | is.na(dbp)) + if (anyNA(c(sbp, dbp))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(sbp)) + assertthat::assert_that(assertthat::is.number(dbp)) (sbp - dbp) %>% units::set_units("mmHg", mode = "standard") } diff --git a/R/bmi.R b/R/bmi.R index 43d4c07..14ca1d7 100644 --- a/R/bmi.R +++ b/R/bmi.R @@ -9,14 +9,18 @@ #' #' @param weight Weight (kg). #' @param height Height (cm). -#' @return BMI (\eqn{\text{kg} m^{-2}}). +#' @return BMI (\eqn{\text{kg} m^{-2}}), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] calculate_bmi <- function(weight, height) { + if (anyNA(c(weight, height))) { + return(NA) + } assertthat::assert_that(assertthat::is.number(weight) | is.na(weight)) assertthat::assert_that(assertthat::is.number(height) | is.na(height)) + (weight / ((height / 100) ^ 2)) %>% units::set_units("kg1 m-2", mode = "standard") } @@ -35,11 +39,14 @@ calculate_bmi <- function(weight, height) { #' @param height Height (cm). #' @param is_female `TRUE` if patient is female, `FALSE` if patient is #' male. -#' @return IBW (kg). +#' @return IBW (kg), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] estimate_ibw <- function(height, is_female) { - assertthat::assert_that(assertthat::is.number(height) | is.na(height)) + if (anyNA(c(height, is_female))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(height)) assertthat::assert_that(assertthat::is.flag(is_female)) if(is_female) { diff --git a/R/bsa.R b/R/bsa.R index 27726ce..1861b74 100644 --- a/R/bsa.R +++ b/R/bsa.R @@ -10,7 +10,7 @@ #' @param height Height (cm). #' @param method Formula to estimate BSA. Options are: "Monsteller" (default), #' "Du Bois". -#' @return BSA (\eqn{m^2}). +#' @return BSA (\eqn{m^2}), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] estimate_bsa <- function(weight, height, method = "Monsteller") { @@ -25,10 +25,14 @@ estimate_bsa <- function(weight, height, method = "Monsteller") { #' @describeIn estimate_bsa Estimate the body surface area (BSA) according to Monsteller. #' @noRd estimate_bsa_monsteller <- function(weight, height) { + if (anyNA(c(weight, height))) { + return(NA) + } assertthat::assert_that(assertthat::is.number(weight) | is.na(weight)) assertthat::assert_that(assertthat::is.number(height) | is.na(height)) + ((weight * height / 3600) ^ (0.5)) %>% units::set_units("m-2", mode = "standard") } @@ -36,10 +40,14 @@ estimate_bsa_monsteller <- function(weight, height) { #' @describeIn estimate_bsa Estimate the body surface area (BSA) according to Du Bois #' @noRd estimate_bsa_du_bois <- function(weight, height) { + if (anyNA(c(weight, height))) { + return(NA) + } assertthat::assert_that(assertthat::is.number(weight) | is.na(weight)) assertthat::assert_that(assertthat::is.number(height) | is.na(height)) + (0.007184 * (weight ^ 0.425) * (height ^ 0.725)) %>% units::set_units("m-2", mode = "standard") } diff --git a/R/electrolytes.R b/R/electrolytes.R index 04eaeab..c7f3a8b 100644 --- a/R/electrolytes.R +++ b/R/electrolytes.R @@ -16,15 +16,16 @@ #' @param calcium Measured total calcium level (mmol/l). #' @param albumin Measured albumin level (g/l). #' @param normal_albumin Normal value of albumin (g/l) (40 by default). -#' @return Albumin-adjusted calcium level (mmol/l). +#' @return Albumin-adjusted calcium level (mmol/l), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] adjust_calcium_for_albumin <- function(calcium, albumin, normal_albumin = 40) { - assertthat::assert_that(assertthat::is.number(calcium) | - is.na(calcium)) - assertthat::assert_that(assertthat::is.number(albumin) | - is.na(albumin)) + if (anyNA(c(calcium, albumin, normal_albumin))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(calcium)) + assertthat::assert_that(assertthat::is.number(albumin)) (calcium + 0.025 * (normal_albumin - albumin)) %>% units::set_units("mmol1 l-1", mode = "standard") @@ -53,22 +54,23 @@ adjust_calcium_for_albumin <- #' @param sodium Measured sodium level (mmol/l). #' @param glucose Measured glucose level (mmol/l). #' @param method Formula. Options are: "Hillier" (default), "Katz". -#' @return Glucose-adjusted sodium level (mmol/l). +#' @return Glucose-adjusted sodium level (mmol/l), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] adjust_sodium_for_glucose <- function(sodium, glucose, method = "Hillier") { - assertthat::assert_that(assertthat::is.number(sodium) | - is.na(sodium)) - assertthat::assert_that(assertthat::is.number(glucose) | - is.na(glucose)) + if (anyNA(c(sodium, glucose, method))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(sodium)) + assertthat::assert_that(assertthat::is.number(glucose)) assertthat::assert_that(assertthat::is.string(method)) xkappa <- switch( method, "Hillier" = 2.4, "Katz" = 1.6, - stop("Illegal value for argument method in adjust_sodium_for_glucose().") + stop(paste0("Error: `method` must be `Hillier` or `Katz`.\n You've supplied method `", method, "`.")) ) (sodium + xkappa * ((glucose-5.6)/5.6)) %>% diff --git a/R/metabolic_syndrome.R b/R/metabolic_syndrome.R index 1330aa8..c3c11af 100644 --- a/R/metabolic_syndrome.R +++ b/R/metabolic_syndrome.R @@ -44,7 +44,7 @@ #' @param has_glucose_drug `TRUE` if patient is on drug treatment for #' elevated glucose, otherwise `FALSE` (default). #' @return `TRUE` if patient has the metabolic syndrome, `FALSE` is -#' not, `NA` if indetermined. +#' not, `NA` if indetermined or `is_female` was `NA`. #' @export has_metabolic_syndrome_atpiii <- function(is_female, waist_circumference = NA, @@ -57,6 +57,10 @@ has_metabolic_syndrome_atpiii <- function(is_female, has_antihypertensive_drug = FALSE, has_lipid_drug = FALSE, has_glucose_drug = FALSE) { + if(is.na(is_female)) { + warning("Warning: `is_female` was `NA`, therefore presence of the metabolic syndrome could not be determined. `NA` was returned.") + return(NA) + } assertthat::assert_that(assertthat::is.flag(is_female)) assertthat::assert_that(assertthat::is.number(waist_circumference) | is.na(waist_circumference)) assertthat::assert_that(assertthat::is.number(sbp) | is.na(sbp)) diff --git a/R/pneumonia.R b/R/pneumonia.R index 12b47f9..6840e75 100644 --- a/R/pneumonia.R +++ b/R/pneumonia.R @@ -13,7 +13,7 @@ #' @param sbp Systolic blood pressure (mmHg). #' @param dbp Diastolic blood pressure (mmHg). #' @param age Age (years). -#' @return CURB-65 score (points). +#' @return CURB-65 score (points), or `NA` if any parameters are `NA`. #' @export score_curb65 <- function(confusion, urea, @@ -21,7 +21,11 @@ score_curb65 <- function(confusion, sbp, dbp, age) { + if(anyNA(c(confusion, urea, respiratory_rate, sbp, dbp, age))){ + return(NA) + } assertthat::assert_that(assertthat::is.flag(confusion)) + assertthat::assert_that(assertthat::is.number(urea)) assertthat::assert_that(assertthat::is.number(respiratory_rate)) assertthat::assert_that(assertthat::is.number(sbp)) assertthat::assert_that(assertthat::is.number(dbp)) diff --git a/R/qtc.R b/R/qtc.R index 8ddc2aa..e0ff30b 100644 --- a/R/qtc.R +++ b/R/qtc.R @@ -7,7 +7,7 @@ #' @param heart_rate Heart rate (/min). #' @param method Formula for QTc. Options are: "Fridericia" (default), #' "Bazzett". -#' @return QTc (ms). +#' @return QTc (ms), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] calculate_qtc <- function(qt, heart_rate, method = "Fridericia") { @@ -20,8 +20,12 @@ calculate_qtc <- function(qt, heart_rate, method = "Fridericia") { #' @describeIn calculate_qtc QTc according to Bazett. #' @noRd calculate_qtc_bazett <- function(qt, heart_rate) { - assertthat::assert_that(assertthat::is.number(qt) | is.na(qt)) - assertthat::assert_that(assertthat::is.number(heart_rate) | is.na(heart_rate)) + if (anyNA(c(qt, heart_rate))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(qt)) + assertthat::assert_that(assertthat::is.number(heart_rate)) + qt <- qt / 1000 rr <- 60 / heart_rate (qt / (rr ^ (1 / 2)) * 1000) %>% @@ -31,8 +35,12 @@ calculate_qtc_bazett <- function(qt, heart_rate) { #' @describeIn calculate_qtc QTc according to Fridericia. #' @noRd calculate_qtc_fridericia <- function(qt, heart_rate) { - assertthat::assert_that(assertthat::is.number(qt) | is.na(qt)) - assertthat::assert_that(assertthat::is.number(heart_rate) | is.na(heart_rate)) + if (anyNA(c(qt, heart_rate))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(qt)) + assertthat::assert_that(assertthat::is.number(heart_rate)) + qt <- qt / 1000 rr <- 60 / heart_rate (qt / (rr ^ (1 / 3)) * 1000) %>% diff --git a/R/renal_function.R b/R/renal_function.R index 8e865de..9a7f4fd 100644 --- a/R/renal_function.R +++ b/R/renal_function.R @@ -12,27 +12,28 @@ #' @param is_female `TRUE` if patient is female, `FALSE` if patient is #' male. #' @param weight Weight (kg). -#' @return eGFR (\eqn{\text{ml} \text{min}^{-1}}). +#' @return eGFR (\eqn{\text{ml} \text{min}^{-1}}), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] -estimate_gfr_cockcroft <- function(creatinine, age, is_female, weight) { - assertthat::assert_that(assertthat::is.number(creatinine) | - is.na(creatinine)) - assertthat::assert_that(assertthat::is.number(age) | - is.na(age)) - assertthat::assert_that(assertthat::is.flag(is_female)) - assertthat::assert_that(assertthat::is.number(weight) | - is.na(weight)) - - egfr <- ((140 - age) * weight) / (0.81 * creatinine) - - if (is_female) { - egfr <- egfr * 0.85 - } +estimate_gfr_cockcroft <- + function(creatinine, age, is_female, weight) { + if (anyNA(c(creatinine, age, is_female, weight))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(creatinine)) + assertthat::assert_that(assertthat::is.number(age)) + assertthat::assert_that(assertthat::is.flag(is_female)) + assertthat::assert_that(assertthat::is.number(weight)) - egfr %>% - units::set_units("ml1 min-1", mode = "standard") -} + egfr <- ((140 - age) * weight) / (0.81 * creatinine) + + if (is_female) { + egfr <- egfr * 0.85 + } + + egfr %>% + units::set_units("ml1 min-1", mode = "standard") + } #' Estimate creatinine clearance and glomerular filtration rate using the MDRD formula #' @@ -48,28 +49,36 @@ estimate_gfr_cockcroft <- function(creatinine, age, is_female, weight) { #' @param is_female `TRUE` if patient is female, `FALSE` if patient is #' male. #' @param is_african_american `TRUE` if patient is African-American, `FALSE` if patient is not. -#' @return eGFR (\eqn{\text{ml} \text{min}^{-1} 1.73 \text{m}^{-2}}). +#' @return eGFR (\eqn{\text{ml} \text{min}^{-1} 1.73 \text{m}^{-2}}), or `NA` if any parameters are `NA`. + #' @export #' @seealso [units::set_units()], [units::drop_units()] -estimate_gfr_mdrd <- function(creatinine, age, is_female, is_african_american) { - assertthat::assert_that(assertthat::is.number(creatinine) | is.na(creatinine)) - assertthat::assert_that(assertthat::is.number(age) | is.na(age)) - assertthat::assert_that(assertthat::is.flag(is_female)) - assertthat::assert_that(assertthat::is.flag(is_african_american)) +estimate_gfr_mdrd <- + function(creatinine, + age, + is_female, + is_african_american) { + if (anyNA(c(creatinine, age, is_female, is_african_american))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(creatinine)) + assertthat::assert_that(assertthat::is.number(age)) + assertthat::assert_that(assertthat::is.flag(is_female)) + assertthat::assert_that(assertthat::is.flag(is_african_american)) - egfr <- 175 * (creatinine / 88.4) ^ -1.154 * (age)^-0.203 + egfr <- 175 * (creatinine / 88.4) ^ -1.154 * (age) ^ -0.203 - if (is_female) { - egfr <- egfr * 0.742 - } + if (is_female) { + egfr <- egfr * 0.742 + } - if (is_african_american) { - egfr <- egfr * 1.212 - } + if (is_african_american) { + egfr <- egfr * 1.212 + } - egfr %>% - units::set_units("ml1 min-1", mode = "standard") -} + egfr %>% + units::set_units("ml1 min-1", mode = "standard") + } #' Estimate creatinine clearance and glomerular filtration rate using the Schwartz formula #' @@ -82,12 +91,16 @@ estimate_gfr_mdrd <- function(creatinine, age, is_female, is_african_american) { #' #' @param creatinine Creatinine (\eqn{\text{μmol} \text{l}^{-1}}). #' @param height Height (cm). -#' @return eGFR (\eqn{\text{ml} \text{min}^{-1} 1.73 \text{m}^{-2}}). +#' @return eGFR (\eqn{\text{ml} \text{min}^{-1} 1.73 \text{m}^{-2}}), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] estimate_gfr_schwartz <- function(creatinine, height) { - assertthat::assert_that(assertthat::is.number(creatinine) | is.na(creatinine)) - assertthat::assert_that(assertthat::is.number(height) | is.na(height)) + if (anyNA(c(creatinine, height))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(creatinine)) + assertthat::assert_that(assertthat::is.number(height)) + (36.2 * height) / creatinine %>% units::set_units("ml1 min-1", mode = "standard") } @@ -120,30 +133,37 @@ estimate_gfr_schwartz <- function(creatinine, height) { #' male. #' @param is_african_american `TRUE` if patient is African-American, #' `FALSE` if patient is not. -#' @return eGFR (\eqn{\text{ml} \text{min}^{-1} 1.73 \text{m}^{-2}}). +#' @return eGFR (\eqn{\text{ml} \text{min}^{-1} 1.73 \text{m}^{-2}}), or `NA` if any parameters are `NA`. #' @export #' @seealso [units::set_units()], [units::drop_units()] -estimate_gfr_ckdepi <- function(creatinine, age, is_female, is_african_american) { - assertthat::assert_that(assertthat::is.number(creatinine) | is.na(creatinine)) - assertthat::assert_that(assertthat::is.number(age) | is.na(age)) - assertthat::assert_that(assertthat::is.flag(is_female)) - assertthat::assert_that(assertthat::is.flag(is_african_american)) +estimate_gfr_ckdepi <- + function(creatinine, + age, + is_female, + is_african_american) { + if (anyNA(c(creatinine, age, is_female, is_african_american))) { + return(NA) + } + assertthat::assert_that(assertthat::is.number(creatinine)) + assertthat::assert_that(assertthat::is.number(age)) + assertthat::assert_that(assertthat::is.flag(is_female)) + assertthat::assert_that(assertthat::is.flag(is_african_american)) - xkappa <- ifelse(is_female, 61.9, 79.6) - xalpha <- ifelse(is_female, -0.329, -0.411) - xmin <- ifelse(creatinine/xkappa < 1, creatinine/xkappa, 1) - xmax <- ifelse(creatinine/xkappa > 1, creatinine/xkappa, 1) + xkappa <- ifelse(is_female, 61.9, 79.6) + xalpha <- ifelse(is_female,-0.329,-0.411) + xmin <- ifelse(creatinine / xkappa < 1, creatinine / xkappa, 1) + xmax <- ifelse(creatinine / xkappa > 1, creatinine / xkappa, 1) - egfr = 141 * xmin^xalpha * xmax^-1.209 * 0.993^age + egfr = 141 * xmin ^ xalpha * xmax ^ -1.209 * 0.993 ^ age - if (is_female) { - egfr <- egfr * 1.018 - } + if (is_female) { + egfr <- egfr * 1.018 + } - if (is_african_american) { - egfr <- egfr * 1.159 - } + if (is_african_american) { + egfr <- egfr * 1.159 + } - egfr %>% - units::set_units("ml1 min-1", mode = "standard") -} + egfr %>% + units::set_units("ml1 min-1", mode = "standard") + } diff --git a/R/sepsis.R b/R/sepsis.R index 78a41a4..29cb2ef 100644 --- a/R/sepsis.R +++ b/R/sepsis.R @@ -15,11 +15,14 @@ #' @param gcs Glasgow Coma Scale. #' @param respiratory_rate Respiratory rate (/min). #' @param sbp Systolic blood pressure (mmHg). -#' @return qSOFA score (points). +#' @return qSOFA score (points), or `NA` if any parameters are `NA`. #' @export score_qsofa <- function(gcs, respiratory_rate, sbp) { + if (anyNA(c(gcs, respiratory_rate, sbp))) { + return(NA) + } assertthat::assert_that(assertthat::is.number(gcs)) assertthat::assert_that(assertthat::is.number(respiratory_rate)) assertthat::assert_that(assertthat::is.number(sbp)) diff --git a/tests/testthat/test-blood_pressure.R b/tests/testthat/test-blood_pressure.R index 6ac6732..2c7752e 100644 --- a/tests/testthat/test-blood_pressure.R +++ b/tests/testthat/test-blood_pressure.R @@ -1,13 +1,27 @@ context("Blood pressure") test_that("MAP for missing DBP or SBP is missing", { - expect_equal(estimate_map(NA, 80), NA_integer_) - expect_equal(estimate_map(120, NA), NA_integer_) - expect_equal(estimate_map(NA, NA), NA_integer_) + expect_equal(estimate_map(NA, 80), NA) + expect_equal(estimate_map(120, NA), NA) + expect_equal(estimate_map(NA, NA), NA) +}) + +test_that("MAP for wrong input of DBP or SBP gives error", { + expect_error(estimate_map(TRUE, 80)) + expect_error(estimate_map("A", 80)) + expect_error(estimate_map(120, TRUE)) + expect_error(estimate_map(120, "A")) }) test_that("Pulse pressure for missing DBP or SBP is missing", { - expect_equal(calculate_pulse_pressure(NA, 80), NA_integer_) - expect_equal(calculate_pulse_pressure(120, NA), NA_integer_) - expect_equal(calculate_pulse_pressure(NA, NA), NA_integer_) + expect_equal(calculate_pulse_pressure(NA, 80), NA) + expect_equal(calculate_pulse_pressure(120, NA), NA) + expect_equal(calculate_pulse_pressure(NA, NA), NA) +}) + +test_that("Pulse pressure for wrong input of DBP or SBP gives error", { + expect_error(calculate_pulse_pressure(TRUE, 80)) + expect_error(calculate_pulse_pressure("A", 80)) + expect_error(calculate_pulse_pressure(120, TRUE)) + expect_error(calculate_pulse_pressure(120, "A")) }) diff --git a/tests/testthat/test-bmi.R b/tests/testthat/test-bmi.R index 2ca883a..e4f6d01 100644 --- a/tests/testthat/test-bmi.R +++ b/tests/testthat/test-bmi.R @@ -1,18 +1,13 @@ context("BMI and IBW") test_that("BMI for missing height or weight is missing", { - expect_equal(calculate_bmi(NA, 180), NA_integer_) - expect_equal(calculate_bmi(80, NA), NA_integer_) - expect_equal(calculate_bmi(NA, NA), NA_integer_) + expect_equal(calculate_bmi(NA, 180), NA) + expect_equal(calculate_bmi(80, NA), NA) + expect_equal(calculate_bmi(NA, NA), NA) }) -test_that("IBW for missing height is missing", { - expect_equal(estimate_ibw(NA, TRUE), NA_integer_) -}) - - -test_that("IBW for missing sex raises error", { - expect_error(estimate_ibw(180, "bla")) - expect_error(estimate_ibw(180, NA)) - expect_error(estimate_ibw(NA, NA)) +test_that("IBW for missing height or sex is missing", { + expect_equal(estimate_ibw(NA, TRUE), NA) + expect_equal(estimate_ibw(180, NA), NA) + expect_equal(estimate_ibw(NA, NA), NA) }) diff --git a/tests/testthat/test-bsa.R b/tests/testthat/test-bsa.R index 8e1fa21..a561b23 100644 --- a/tests/testthat/test-bsa.R +++ b/tests/testthat/test-bsa.R @@ -1,12 +1,12 @@ context("BSA") test_that("BSA for missing height or weight is missing", { - expect_equal(estimate_bsa_monsteller(NA, 180), NA_integer_) - expect_equal(estimate_bsa_monsteller(80, NA), NA_integer_) - expect_equal(estimate_bsa_monsteller(NA, NA), NA_integer_) - expect_equal(estimate_bsa_du_bois(NA, 180), NA_integer_) - expect_equal(estimate_bsa_du_bois(80, NA), NA_integer_) - expect_equal(estimate_bsa_du_bois(NA, NA), NA_integer_) + expect_equal(estimate_bsa_monsteller(NA, 180), NA) + expect_equal(estimate_bsa_monsteller(80, NA), NA) + expect_equal(estimate_bsa_monsteller(NA, NA), NA) + expect_equal(estimate_bsa_du_bois(NA, 180), NA) + expect_equal(estimate_bsa_du_bois(80, NA), NA) + expect_equal(estimate_bsa_du_bois(NA, NA), NA) }) test_that("BSA crash on illegal arguments", { diff --git a/tests/testthat/test-electrolytes.R b/tests/testthat/test-electrolytes.R index 4ce3831..3f71345 100644 --- a/tests/testthat/test-electrolytes.R +++ b/tests/testthat/test-electrolytes.R @@ -1,8 +1,8 @@ context("Electrolytes") test_that("Albumin-corrected calcium for missing values is missing", { - expect_equal(adjust_calcium_for_albumin(3, NA), NA_integer_) - expect_equal(adjust_calcium_for_albumin(NA, 23), NA_integer_) + expect_equal(adjust_calcium_for_albumin(3, NA), NA) + expect_equal(adjust_calcium_for_albumin(NA, 23), NA) }) test_that("Albumin-corrected calcium crash on illegal arguments", { @@ -11,14 +11,13 @@ test_that("Albumin-corrected calcium crash on illegal arguments", { }) test_that("Glucose-corrected sodium for missing values is missing", { - expect_equal(adjust_sodium_for_glucose(120, NA), NA_integer_) - expect_equal(adjust_sodium_for_glucose(NA, 25), NA_integer_) + expect_equal(adjust_sodium_for_glucose(120, NA), NA) + expect_equal(adjust_sodium_for_glucose(NA, 25), NA) }) test_that("Glucose-corrected sodium crash on illegal arguments", { expect_error(adjust_sodium_for_glucose("A", 25)) expect_error(adjust_sodium_for_glucose(120, "A")) expect_error(adjust_sodium_for_glucose(120, 25, "A")) - expect_error(adjust_sodium_for_glucose(120, 25, NA)) }) diff --git a/tests/testthat/test-pneumonia.R b/tests/testthat/test-pneumonia.R new file mode 100644 index 0000000..4013235 --- /dev/null +++ b/tests/testthat/test-pneumonia.R @@ -0,0 +1,19 @@ +context("Pneumonia") + +test_that("CURB65 for missing values gives missing", { + expect_equal(score_curb65(NA, 10, 10, 10, 10, 10), NA) + expect_equal(score_curb65(TRUE, NA, 10, 10, 10, 10), NA) + expect_equal(score_curb65(TRUE, 10, NA, 10, 10, 10), NA) + expect_equal(score_curb65(TRUE, 10, 10, NA, 10, 10), NA) + expect_equal(score_curb65(TRUE, 10, 10, 10, NA, 10), NA) + expect_equal(score_curb65(TRUE, 10, 10, 10, 10, NA), NA) +}) + +test_that("CURB65 for wrong values gives error (no guesses)", { + expect_error(score_curb65("A", 10, 10, 10, 10, 10)) + expect_error(score_curb65(TRUE, "A", 10, 10, 10, 10)) + expect_error(score_curb65(TRUE, 10, "A", 10, 10, 10)) + expect_error(score_curb65(TRUE, 10, 10, "A", 10, 10)) + expect_error(score_curb65(TRUE, 10, 10, 10, "A", 10)) + expect_error(score_curb65(TRUE, 10, 10, 10, 10, "A")) +}) diff --git a/tests/testthat/test-qtc.R b/tests/testthat/test-qtc.R index 7ba4cf1..f421d11 100644 --- a/tests/testthat/test-qtc.R +++ b/tests/testthat/test-qtc.R @@ -1,12 +1,12 @@ context("QTc") test_that("QTc for missing QT or heart rate is missing", { - expect_equal(calculate_qtc_bazett(NA, 100), NA_integer_) - expect_equal(calculate_qtc_bazett(400, NA), NA_integer_) - expect_equal(calculate_qtc_bazett(NA, NA), NA_integer_) - expect_equal(calculate_qtc_fridericia(NA, 100), NA_integer_) - expect_equal(calculate_qtc_fridericia(400, NA), NA_integer_) - expect_equal(calculate_qtc_fridericia(NA, NA), NA_integer_) + expect_equal(calculate_qtc_bazett(NA, 100), NA) + expect_equal(calculate_qtc_bazett(400, NA), NA) + expect_equal(calculate_qtc_bazett(NA, NA), NA) + expect_equal(calculate_qtc_fridericia(NA, 100), NA) + expect_equal(calculate_qtc_fridericia(400, NA), NA) + expect_equal(calculate_qtc_fridericia(NA, NA), NA) }) test_that("QTc crash on illegal arguments", { diff --git a/tests/testthat/test-renal_function.R b/tests/testthat/test-renal_function.R new file mode 100644 index 0000000..121f6d8 --- /dev/null +++ b/tests/testthat/test-renal_function.R @@ -0,0 +1,53 @@ +context("Renal function") + +test_that("Cockcroft-Gault for missing values gives missing", { + expect_equal(estimate_gfr_cockcroft(NA, 80, TRUE, 80), NA) + expect_equal(estimate_gfr_cockcroft(120, NA, TRUE, 80), NA) + expect_equal(estimate_gfr_cockcroft(120, 80, NA, 80), NA) + expect_equal(estimate_gfr_cockcroft(120, 80, TRUE, NA), NA) +}) + +test_that("Cockcroft-Gault for wrong value types gives error", { + expect_error(estimate_gfr_cockcroft("A", 80, TRUE, 80)) + expect_error(estimate_gfr_cockcroft(120, "A", TRUE, 80)) + expect_error(estimate_gfr_cockcroft(120, 80, "A", 80)) + expect_error(estimate_gfr_cockcroft(120, 80, TRUE, "A")) +}) + +test_that("MDRD for missing values gives missing", { + expect_equal(estimate_gfr_mdrd(NA, 20, TRUE, TRUE), NA) + expect_equal(estimate_gfr_mdrd(120, NA, TRUE, TRUE), NA) + expect_equal(estimate_gfr_mdrd(120, 20, NA, TRUE), NA) + expect_equal(estimate_gfr_mdrd(120, 20, TRUE, NA), NA) +}) + +test_that("MDRD for wrong value types gives error", { + expect_error(estimate_gfr_mdrd("A", 20, TRUE, TRUE)) + expect_error(estimate_gfr_mdrd(120, "A", TRUE, TRUE)) + expect_error(estimate_gfr_mdrd(120, 20, "A", TRUE)) + expect_error(estimate_gfr_mdrd(120, 20, TRUE, "A")) +}) + +test_that("Schwartz for missing values gives missing", { + expect_equal(estimate_gfr_schwartz(NA, 120), NA) + expect_equal(estimate_gfr_schwartz(120, NA), NA) +}) + +test_that("Schwartz for wrong value types gives error", { + expect_error(estimate_gfr_schwartz("A", 120)) + expect_error(estimate_gfr_schwartz(120, "A")) +}) + +test_that("CKD-EPI for missing values gives missing", { + expect_equal(estimate_gfr_ckdepi(NA, 20, TRUE, TRUE), NA) + expect_equal(estimate_gfr_ckdepi(120, NA, TRUE, TRUE), NA) + expect_equal(estimate_gfr_ckdepi(120, 20, NA, TRUE), NA) + expect_equal(estimate_gfr_ckdepi(120, 20, TRUE, NA), NA) +}) + +test_that("CKD-EPI for wrong value types gives error", { + expect_error(estimate_gfr_ckdepi("A", 20, TRUE, TRUE)) + expect_error(estimate_gfr_ckdepi(120, "A", TRUE, TRUE)) + expect_error(estimate_gfr_ckdepi(120, 20, "A", TRUE)) + expect_error(estimate_gfr_ckdepi(120, 20, TRUE, "A")) +})