From 7c451d815621e46e15752a2e70c21e2146959c2a Mon Sep 17 00:00:00 2001
From: LS31
Date: Wed, 4 Nov 2020 22:06:07 +0100
Subject: [PATCH] Add adjust_sodium_for_glucose. Update some tests.
---
NAMESPACE | 1 +
R/bmi.R | 2 +-
R/bsa.R | 12 +-
R/electrolytes.R | 45 ++++
R/renal_function.R | 21 +-
README.md | 6 +-
docs/index.html | 16 +-
docs/pkgdown.yml | 2 +-
docs/reference/adjust_sodium_for_glucose.html | 193 ++++++++++++++++++
docs/reference/index.html | 6 +
docs/sitemap.xml | 3 +
man/adjust_sodium_for_glucose.Rd | 37 ++++
tests/testthat/test-blood_pressure.R | 8 +
tests/testthat/test-bmi.R | 13 +-
tests/testthat/test-electrolytes.R | 13 ++
15 files changed, 353 insertions(+), 25 deletions(-)
create mode 100644 docs/reference/adjust_sodium_for_glucose.html
create mode 100644 man/adjust_sodium_for_glucose.Rd
diff --git a/NAMESPACE b/NAMESPACE
index ecd66ec..42d8279 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -2,6 +2,7 @@
export("%>%")
export(adjust_calcium_for_albumin)
+export(adjust_sodium_for_glucose)
export(calculate_bmi)
export(calculate_pulse_pressure)
export(calculate_qtc)
diff --git a/R/bmi.R b/R/bmi.R
index 94fb559..43d4c07 100644
--- a/R/bmi.R
+++ b/R/bmi.R
@@ -39,8 +39,8 @@ calculate_bmi <- function(weight, height) {
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
estimate_ibw <- function(height, is_female) {
+ assertthat::assert_that(assertthat::is.number(height) | is.na(height))
assertthat::assert_that(assertthat::is.flag(is_female))
- assertthat::assert_that(assertthat::is.number(height))
if(is_female) {
(45.5 + 0.9 * (height - 152)) %>%
diff --git a/R/bsa.R b/R/bsa.R
index 5fb8776..27726ce 100644
--- a/R/bsa.R
+++ b/R/bsa.R
@@ -25,8 +25,10 @@ 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) {
- assertthat::assert_that(assertthat::is.number(weight))
- assertthat::assert_that(assertthat::is.number(height))
+ 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")
}
@@ -34,8 +36,10 @@ 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) {
- assertthat::assert_that(assertthat::is.number(weight))
- assertthat::assert_that(assertthat::is.number(height))
+ 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 fc8a363..04eaeab 100644
--- a/R/electrolytes.R
+++ b/R/electrolytes.R
@@ -29,3 +29,48 @@ adjust_calcium_for_albumin <-
(calcium + 0.025 * (normal_albumin - albumin)) %>%
units::set_units("mmol1 l-1", mode = "standard")
}
+
+
+#' Adjust sodium for hyperglycaemia
+#'
+#' Hyperglycaemia can lead to hypertonic hyponatriemia. Using observational
+#' data, the following formula predicts sodium levels after correction of
+#' glucose: \eqn{\text{Na}_{predicted} = \text{Na}_\text{measured} + \kappa
+#' \times \frac{\text{glucose} - 5.6}{5.6}}, where \eqn{\kappa} is 1.6 when
+#' derived from the publication by Katz and 2.4 when derived from more recent
+#' work by Hillier (default).
+#'
+#' @references [Katz, M. A. Hyperglycemia-Induced Hyponatremia — Calculation of
+#' Expected Serum Sodium Depression. N Engl J Med 289, 843–844
+#' (1973).](https://doi.org/10.1056/NEJM197310182891607) and [Hillier, T. A.,
+#' Abbott, R. D. & Barrett, E. J. Hyponatremia: evaluating the correction
+#' factor for hyperglycemia. The American Journal of Medicine 106, 399–403
+#' (1999).](https://doi.org/10.1016/s0002-9343(99)00055-8).
+#'
+#' @section Caveats: It's just a prediction, and the choice between both
+#' formulae may result in quite different predictions.
+#'
+#' @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).
+#' @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))
+ 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().")
+ )
+
+ (sodium + xkappa * ((glucose-5.6)/5.6)) %>%
+ units::set_units("mmol1 l-1", mode = "standard")
+}
diff --git a/R/renal_function.R b/R/renal_function.R
index e57fb57..8e865de 100644
--- a/R/renal_function.R
+++ b/R/renal_function.R
@@ -16,10 +16,13 @@
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
estimate_gfr_cockcroft <- function(creatinine, age, is_female, weight) {
- assertthat::assert_that(assertthat::is.number(creatinine))
- assertthat::assert_that(assertthat::is.number(age))
+ 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))
+ assertthat::assert_that(assertthat::is.number(weight) |
+ is.na(weight))
egfr <- ((140 - age) * weight) / (0.81 * creatinine)
@@ -49,8 +52,8 @@ estimate_gfr_cockcroft <- function(creatinine, age, is_female, weight) {
#' @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))
- assertthat::assert_that(assertthat::is.number(age))
+ 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))
@@ -83,8 +86,8 @@ estimate_gfr_mdrd <- function(creatinine, age, is_female, is_african_american) {
#' @export
#' @seealso [units::set_units()], [units::drop_units()]
estimate_gfr_schwartz <- function(creatinine, height) {
- assertthat::assert_that(assertthat::is.number(creatinine))
- assertthat::assert_that(assertthat::is.number(height))
+ assertthat::assert_that(assertthat::is.number(creatinine) | is.na(creatinine))
+ assertthat::assert_that(assertthat::is.number(height) | is.na(height))
(36.2 * height) / creatinine %>%
units::set_units("ml1 min-1", mode = "standard")
}
@@ -121,8 +124,8 @@ estimate_gfr_schwartz <- function(creatinine, height) {
#' @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))
- assertthat::assert_that(assertthat::is.number(age))
+ 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))
diff --git a/README.md b/README.md
index 39442b9..7a1e93b 100644
--- a/README.md
+++ b/README.md
@@ -14,12 +14,14 @@ Help and pull requests are more than welcome.
- *Anthropometry*
- Body mass index (BMI, Quetelet index).
- Body surface area (BSA).
+ - Ideal body weight (IBW).
- Mean arterial blood pressure (MAP).
- Pulse pressure.
- *Cardiology*
- Corrected QT interval (QTc).
- *Electrolytes*
- - Albumin-corrected calcium.
+ - Albumin-adjusted calcium.
+ - Glucose-adjusted sodium.
- *Infectiology*
- CURB-65 community-acquired pneumonia severity score.
- qSOFA score for sepsis.
@@ -66,7 +68,6 @@ remotes::install_github("lc31/clinicalr")
# To do
* effective osmolality?
-* glucose corrected sodium?
* anion gap?
* total body water?
* fractional excretion of sodium?
@@ -75,3 +76,4 @@ remotes::install_github("lc31/clinicalr")
* A-a gradient?
* creatinine clearance (using urine)?
* Framingham?
+* assertion tests
diff --git a/docs/index.html b/docs/index.html
index 8cd8163..70f4853 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -87,6 +87,7 @@
Body mass index (BMI, Quetelet index).
Body surface area (BSA).
+Ideal body weight (IBW).
Mean arterial blood pressure (MAP).
Pulse pressure.
@@ -100,7 +101,8 @@
Electrolytes
-Albumin-corrected calcium.
+Albumin-adjusted calcium.
+Glucose-adjusted sodium.
@@ -173,22 +175,21 @@
How to install
Clinicalr is not (yet) available on CRAN. Install devtools. Then use devtools to install clinicalr directly from GitHub using the remotes package.
-
+
To do
effective osmolality?
-glucose corrected sodium?
anion gap?
total body water?
fractional excretion of sodium?
@@ -197,6 +198,7 @@
A-a gradient?
creatinine clearance (using urine)?
Framingham?
+assertion tests
diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml
index 46eb26b..473d82e 100644
--- a/docs/pkgdown.yml
+++ b/docs/pkgdown.yml
@@ -2,7 +2,7 @@ pandoc: 2.7.3
pkgdown: 1.6.1
pkgdown_sha: ~
articles: {}
-last_built: 2020-11-04T11:39Z
+last_built: 2020-11-04T21:04Z
urls:
reference: https://ls31.github.io/clinicalr//reference
article: https://ls31.github.io/clinicalr//articles
diff --git a/docs/reference/adjust_sodium_for_glucose.html b/docs/reference/adjust_sodium_for_glucose.html
new file mode 100644
index 0000000..8448912
--- /dev/null
+++ b/docs/reference/adjust_sodium_for_glucose.html
@@ -0,0 +1,193 @@
+
+
+
+
+
+
+
+
+Adjust sodium for hyperglycaemia — adjust_sodium_for_glucose • clinicalr
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Hyperglycaemia can lead to hypertonic hyponatriemia. Using observational
+data, the following formula predicts sodium levels after correction of
+glucose: \(\text{Na}_{predicted} = \text{Na}_\text{measured} + \kappa
+\times \frac{\text{glucose} - 5.6}{5.6}\), where \(\kappa\) is 1.6 when
+derived from the publication by Katz and 2.4 when derived from more recent
+work by Hillier (default).
+
+
+
adjust_sodium_for_glucose ( sodium , glucose , method = "Hillier" )
+
+
Arguments
+
+
+
+ sodium
+ Measured sodium level (mmol/l).
+
+
+ glucose
+ Measured glucose level (mmol/l).
+
+
+ method
+ Formula. Options are: "Hillier" (default), "Katz".
+
+
+
+
Value
+
+
Glucose-adjusted sodium level (mmol/l).
+
Caveats
+
+
It's just a prediction, and the choice between both
+formulae may result in quite different predictions.
+
References
+
+
Katz, M. A. Hyperglycemia-Induced Hyponatremia — Calculation of Expected Serum Sodium Depression. N Engl J Med 289, 843–844 (1973). and Hillier, T. A., Abbott, R. D. & Barrett, E. J. Hyponatremia: evaluating the correction factor for hyperglycemia. The American Journal of Medicine 106, 399–403 (1999). .
+
See also
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/docs/reference/index.html b/docs/reference/index.html
index 874db1f..e024e83 100644
--- a/docs/reference/index.html
+++ b/docs/reference/index.html
@@ -141,6 +141,12 @@
+
+ Adjust sodium for hyperglycaemia
+
+
calculate_bmi()
diff --git a/docs/sitemap.xml b/docs/sitemap.xml
index 7b4479b..0ba126b 100644
--- a/docs/sitemap.xml
+++ b/docs/sitemap.xml
@@ -6,6 +6,9 @@
https://ls31.github.io/clinicalr//reference/adjust_calcium_for_albumin.html
+
+ https://ls31.github.io/clinicalr//reference/adjust_sodium_for_glucose.html
+
https://ls31.github.io/clinicalr//reference/calculate_bmi.html
diff --git a/man/adjust_sodium_for_glucose.Rd b/man/adjust_sodium_for_glucose.Rd
new file mode 100644
index 0000000..bd37a87
--- /dev/null
+++ b/man/adjust_sodium_for_glucose.Rd
@@ -0,0 +1,37 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/electrolytes.R
+\name{adjust_sodium_for_glucose}
+\alias{adjust_sodium_for_glucose}
+\title{Adjust sodium for hyperglycaemia}
+\usage{
+adjust_sodium_for_glucose(sodium, glucose, method = "Hillier")
+}
+\arguments{
+\item{sodium}{Measured sodium level (mmol/l).}
+
+\item{glucose}{Measured glucose level (mmol/l).}
+
+\item{method}{Formula. Options are: "Hillier" (default), "Katz".}
+}
+\value{
+Glucose-adjusted sodium level (mmol/l).
+}
+\description{
+Hyperglycaemia can lead to hypertonic hyponatriemia. Using observational
+data, the following formula predicts sodium levels after correction of
+glucose: \eqn{\text{Na}_{predicted} = \text{Na}_\text{measured} + \kappa
+\times \frac{\text{glucose} - 5.6}{5.6}}, where \eqn{\kappa} is 1.6 when
+derived from the publication by Katz and 2.4 when derived from more recent
+work by Hillier (default).
+}
+\section{Caveats}{
+ It's just a prediction, and the choice between both
+formulae may result in quite different predictions.
+}
+
+\references{
+\href{https://doi.org/10.1056/NEJM197310182891607}{Katz, M. A. Hyperglycemia-Induced Hyponatremia — Calculation of Expected Serum Sodium Depression. N Engl J Med 289, 843–844 (1973).} and \href{https://doi.org/10.1016/s0002-9343(99)00055-8}{Hillier, T. A., Abbott, R. D. & Barrett, E. J. Hyponatremia: evaluating the correction factor for hyperglycemia. The American Journal of Medicine 106, 399–403 (1999).}.
+}
+\seealso{
+\code{\link[units:set_units]{units::set_units()}}, \code{\link[units:drop_units]{units::drop_units()}}
+}
diff --git a/tests/testthat/test-blood_pressure.R b/tests/testthat/test-blood_pressure.R
index 6bd25ac..6ac6732 100644
--- a/tests/testthat/test-blood_pressure.R
+++ b/tests/testthat/test-blood_pressure.R
@@ -1,5 +1,13 @@
+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_)
})
+
+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_)
+})
diff --git a/tests/testthat/test-bmi.R b/tests/testthat/test-bmi.R
index d979804..2ca883a 100644
--- a/tests/testthat/test-bmi.R
+++ b/tests/testthat/test-bmi.R
@@ -1,7 +1,18 @@
-context("BMI")
+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_)
})
+
+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))
+})
diff --git a/tests/testthat/test-electrolytes.R b/tests/testthat/test-electrolytes.R
index 95c0fa1..4ce3831 100644
--- a/tests/testthat/test-electrolytes.R
+++ b/tests/testthat/test-electrolytes.R
@@ -9,3 +9,16 @@ test_that("Albumin-corrected calcium crash on illegal arguments", {
expect_error(adjust_calcium_for_albumin("A", 23))
expect_error(adjust_calcium_for_albumin(3, "A"))
})
+
+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_)
+})
+
+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))
+})
+