Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring 1.35.1 and 1.35.2 changes to release-1.35-stable #21

Merged
merged 10 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: clinPK
Type: Package
Title: Clinical Pharmacokinetics Toolkit
Version: 0.10.4
Date: 2021-05-19
Version: 0.10.5
Date: 2021-06-11
Authors@R: c(person("Ron", "Keizer", email = "ron@insight-rx.com", role
= "aut"), person("Jasmine", "Hughes", email =
"jasmine@insight-rx.com", role = "aut"), person("Dominic",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(auc2dose)
export(calc_abw)
export(calc_aki_stage)
export(calc_amts_for_conc)
export(calc_baseline_scr)
export(calc_bmi)
export(calc_bsa)
export(calc_creat)
Expand Down
32 changes: 18 additions & 14 deletions R/calc_aki_stage.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#' numeric (0.2)
#' @param times creatinine sample times in hours
#' @param method classification method, one of `KDIGO`, `RIFLE`, `pRIFLE` (case insensitive)
#' @param baseline_scr baseline serum creatinine, required for `RIFLE` classifation. Will use value if numeric. If `character`, can be either `median` or `expected`. The latter will use the expected value based on sex and age.
#' @param baseline_scr baseline serum creatinine, required for `RIFLE` classifation. Will use value if numeric. If `character`, can be either `median`, `median_before_treatment`, `lowest`, or `first`.
#' @param baseline_egfr baseline eGFR, required for `RIFLE` classifations. Will take median of `egfr` values if `NULL`.
#' @param first_dose_time time in hours of first dose relative to sCr value, used for calculate baseline serum creatinine in `median_before_treatment` approach.
#' @param override_prifle_baseline by default, `pRIFLE` compares eGFR to 120 ml/min. Override by setting to TRUE.
#' @param age age in years, needed when eGFR is used in the classification method
#' @param egfr eGFR in ml/min/1.73m^2. Optional, can also be calcualted if `age`, `weight`, `height`, `sex`, `egfr_method` are specified as arguments.
Expand Down Expand Up @@ -39,6 +40,7 @@ calc_aki_stage <- function (
method = "kdigo",
baseline_scr = "median",
baseline_egfr = NULL,
first_dose_time = NULL,
age = NULL,
egfr = NULL,
egfr_method = NULL,
Expand All @@ -65,11 +67,18 @@ calc_aki_stage <- function (
egfr_method <- "cockroft_gault"
}
}

## Make sure values are ordered by time
if(!is.null(times)) {
idx <- order(times)
times <- times[idx]
scr <- scr[idx]
egfr <- egfr[idx]
}

if(is.null(scr) && method !='prifle') stop("No serum creatinine values provided.")
if(is.null(times) && method !='prifle') stop("No sample times (days) provided.")
if(class(times) == "Date") {
times <- times[order(times)]
times <- as.numeric(difftime(times, min(times), units = "days"))
}
if((!class(times[1]) %in% c("numeric","integer")) && method !='prifle') {
Expand All @@ -96,17 +105,12 @@ calc_aki_stage <- function (
}
# pRIFLE does not require SCr
if(class(baseline_scr) == "character" && method !='prifle') {
if(baseline_scr == "median") {
baseline_scr <- stats::median(scr)
if(verbose) message("No baseline SCr value specified, using *median* of supplied values.")
}
if(baseline_scr == "first" && method !='prifle') {
baseline_scr <- scr[1]
if(verbose) message("No baseline SCr value specified, using *first* of supplied values.")
}
if(baseline_scr == "expected" && method !='prifle') {
## need to implement
}
baseline_scr <- calc_baseline_scr(baseline_scr,
scr,
times,
method,
first_dose_time,
verbose)
}
if(method %in% ('prifle') && !(override_prifle_baseline)) baseline_egfr <- 120

Expand All @@ -129,7 +133,7 @@ calc_aki_stage <- function (

## Differences in SCr - don't need scr for pRIFLE
if (method %in% c("kdigo", "rifle")) {
dat <- data.frame(scr = scr, t = times, deltat = c(0, diff(times)))
dat <- data.frame(scr = scr, t = times, deltat = c(0, diff(times)), baseline_scr = baseline_scr)
dat$baseline_scr_diff <- (dat$scr - baseline_scr)
dat$baseline_scr_reldiff <- dat$baseline_scr_diff / baseline_scr
dat$stage <- rep(NA, length(scr))
Expand Down
56 changes: 56 additions & 0 deletions R/calc_baseline_scr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Calculate baseline sCr
#'
#' @inheritParams calc_aki_stage
#' @param baseline_scr baseline serum creatinine method (character). See calc_aki_stage() for availabloptions.
#'
#' @export
calc_baseline_scr <- function(
baseline_scr,
scr,
times,
method,
first_dose_time = NULL,
verbose) {
if(baseline_scr == "median_before_treatment") {
if(is.null(first_dose_time)) {
baseline_scr <- stats::median(scr, na.rm = TRUE)
if(verbose) message("To calculate median baseline before treatment start need time of first dose. Using median over whole treatment period.")
} else {
if((!class(first_dose_time) %in% c("numeric","integer"))) {
stop("`first_dose_times` argument should be supplied as numeric (hours).")
}
if(any(times <= first_dose_time)) {
baseline_scr <- stats::median(scr[times <= first_dose_time], na.rm = TRUE)
if(verbose) message("No baseline SCr value specified, using *median* of supplied values before first dose.")
} else {
if(any(times < 48)) {
baseline_scr <- stats::median(scr[times < 48], na.rm = TRUE)
if(verbose) message("No baseline before first dose, using *median* of supplied values in first 48 hours.")
} else {
baseline_scr <- stats::median(scr, na.rm = TRUE)
if(verbose) message("No baseline in first 48 hours, using *median* of supplied values.")
}
}
}
}
if(baseline_scr == "median") {
baseline_scr <- stats::median(scr)
if(verbose) message("No baseline SCr value specified, using *median* of supplied values.")
}
if(baseline_scr == "first" && method !='prifle') {
baseline_scr <- scr[1]
if(verbose) message("No baseline SCr value specified, using *first* of supplied values.")
}
if(baseline_scr == "lowest") {
baseline_scr <- min(scr)
if(verbose) message("No baseline SCr value specified, using *lowest* of supplied values.")
}
if(baseline_scr == "expected" && method !='prifle') {
## TODO: need to implement
}
if(! class(baseline_scr) %in% c("numeric", "integer")) {
stop("Requested baseline calculation method not recognized.")
}

baseline_scr
}
7 changes: 5 additions & 2 deletions R/weight2kg.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Convert any weight unit to kg
#'
#' @param value weight in any allowed unit
#' @param unit unit of weight, one of "lbs", "pound", "pounds", "oz", "ounce", "ounces"
#' @param unit unit of weight, one of "lbs", "pound", "pounds", "oz", "ounce", "ounces", "g", "gram", "grams"
#' @examples
#' weight2kg(250, unit = "oz")
#' weight2kg(250, unit = "pounds")
Expand All @@ -12,13 +12,16 @@ weight2kg <- function(value = NULL, unit = NULL) {
stop("No weight value specified.")
}
if(!is.null(unit) && !is.na(unit)) {
if(tolower(unit) %in% c("kg", "lbs", "pound", "pounds", "oz", "ounce", "ounces")) {
if(tolower(unit) %in% c("kg", "lbs", "pound", "pounds", "oz", "ounce", "ounces", "g", "gram", "grams")) {
if(tolower(unit) %in% c("lbs", "pound", "pounds")) {
value <- value / 2.20462
}
if(tolower(unit) %in% c("oz", "ounce", "ounces")) {
value <- value / 35.274
}
if (tolower(unit) %in% c("g", "gram", "grams")) {
value <- value / 1000
}
} else {
warning(paste0("Unit (", unit,") not recognized, returning original weight."))
}
Expand Down
5 changes: 4 additions & 1 deletion man/calc_aki_stage.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions man/calc_baseline_scr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/weight2kg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions tests/testthat/test_calc_aki_stage.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,46 @@ test_that("calc_aki_stage errors if can't coerce scr to numeric", {
)
)
})

test_that("calc_aki_stage correctly calculates baseline_scr", {
test11 <- calc_aki_stage(
method = "kDIGO",
scr = c(0.5, 1.5),
t = c(0, 24),
egfr = c(50, 30),
baseline_scr = "median",
first_dose_time = 50,
verbose = FALSE
)
expect_equal(test11$data$baseline_scr, c(1,1))
test12 <- calc_aki_stage(
method = "kDIGO",
scr = c(0.5, 1.0, 1.5),
t = c(0, 10, 24),
egfr = c(50, 40, 30),
baseline_scr = "median_before_treatment",
first_dose_time = 12,
verbose = FALSE
)
expect_equal(test12$data$baseline_scr, c(0.75, 0.75, 0.75))
expect_equal(
calc_aki_stage(
method = "kDIGO",
scr = c(0.5, 1.0, 1.5),
t = c(0, 10, 24),
egfr = c(50, 40, 30),
baseline_scr = "lowest",
first_dose_time = 12,
verbose = FALSE
)$data$baseline_scr, rep(0.5, 3))
expect_error(
calc_aki_stage(
method = "kDIGO",
scr = c(0.5, 1.0, 1.5),
t = c(0, 10, 24),
egfr = c(50, 40, 30),
baseline_scr = "typo",
first_dose_time = 12,
verbose = FALSE
)$data$baseline_scr)
})
4 changes: 4 additions & 0 deletions tests/testthat/test_weight2kg.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ test_that("vectorized input works", {
c(4.0823362, 4.5359291)
)
})

test_that("weight2kg supports grams as input unit", {
expect_equal(weight2kg(1000, "g"), 1)
})