diff --git a/NEWS.md b/NEWS.md index 81a4a470..4ce83793 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,14 @@ the dosing including dose amount and route. ## Breaking changes +* `pk.calc.aucabove()`, and so `aucabove.predose.all` and + `aucabove.trough.all`, now interpolates the time when the concentration + crosses the threshold, and uses log interpolation on the measured + concentrations (@SAUMYARSH, #696). Measured concentrations below the + threshold are no longer treated as BLQ, so a trough between two peaks is no + longer dropped. Results change when a profile crosses the threshold, and + for log intervals when the threshold is not zero. + * A negative numeric dose amount is an error (`pknca_error_dose_negative`, naming the first rows and their count), in `PKNCAdose()` and again in `pk.nca()` for a dose made negative later, for example by `mutate()` diff --git a/R/auc_integrate.R b/R/auc_integrate.R index a780b30e..999458ff 100644 --- a/R/auc_integrate.R +++ b/R/auc_integrate.R @@ -15,6 +15,43 @@ aucintegrate_inf <- function(clast, tlast, lambda.z) { clast/lambda.z } +#' Integrate the area above a concentration within each interval +#' +#' @param conc.1,conc.2 The concentration at `time.1` and `time.2` +#' @param time.1,time.2 The time at the start and end of each interval +#' @param conc_above The concentration to integrate above +#' @returns The area between the interpolated concentrations and `conc_above` +#' while the concentration is above `conc_above` +#' @keywords Internal +#' @name aucaboveintegrate +NULL + +#' @rdname aucaboveintegrate +aucaboveintegrate_linear <- function(conc.1, conc.2, time.1, time.2, conc_above) { + conc_high <- pmax(conc.1, conc.2) + conc_low <- pmin(conc.1, conc.2) + conc_floor <- pmax(conc_low, conc_above) + fraction_above <- + ifelse(conc_low >= conc_above, 1, (conc_high - conc_floor)/(conc_high - conc_low)) + ret <- (time.2 - time.1) * fraction_above * ((conc_high + conc_floor)/2 - conc_above) + ifelse(conc_high > conc_above, ret, 0) +} + +#' @rdname aucaboveintegrate +aucaboveintegrate_log <- function(conc.1, conc.2, time.1, time.2, conc_above) { + ret <- aucaboveintegrate_linear(conc.1, conc.2, time.1, time.2, conc_above) + conc_high <- pmax(conc.1, conc.2) + conc_low <- pmin(conc.1, conc.2) + mask_log <- conc_low > 0 & conc_high > conc_low & conc_high > conc_above + conc_high <- conc_high[mask_log] + conc_low <- conc_low[mask_log] + conc_floor <- pmax(conc_low, conc_above) + time_per_log <- (time.2 - time.1)[mask_log]/log(conc_high/conc_low) + ret[mask_log] <- + time_per_log * (conc_high - conc_floor - conc_above*log(conc_high/conc_floor)) + ret +} + aumcintegrate_linear <- function(conc.1, conc.2, time.1, time.2) { (time.2-time.1) * (conc.2*time.2+conc.1*time.1)/2 } diff --git a/R/pk.calc.simple.R b/R/pk.calc.simple.R index b8a00c81..0fb082b0 100644 --- a/R/pk.calc.simple.R +++ b/R/pk.calc.simple.R @@ -2574,8 +2574,12 @@ add.interval.col("ceoi", #' Calculate the AUC above a given concentration #' -#' Concentrations below the given concentration (`conc_above`) will be set -#' to zero. +#' The area is calculated between the concentration-time curve and `conc_above` +#' while the curve is above `conc_above`. The times when the curve crosses +#' `conc_above` are interpolated using the AUC calculation method. +#' +#' Logarithmic interpolation is only used between two positive concentrations +#' that are not equal. Otherwise, linear interpolation is used. #' @inheritParams pk.calc.time_above #' @param conc_above The concentration threshold to calculate AUC above. #' Must be finite (`Inf`/`-Inf` are not allowed); if `NA`, no AUC is @@ -2587,12 +2591,24 @@ pk.calc.aucabove <- function(conc, time, conc_above = NA_real_, ..., options=lis if (is.na(conc_above)) { ret <- structure(NA_real_, exclude = "Missing concentration to be above") } else { + lowest <- min(conc, conc_above, na.rm = TRUE) + shift <- if (lowest < 0) 1 - lowest else 0 ret <- - pk.calc.auc( - conc=pmax(conc - conc_above, 0), time=time, ..., options=options, + pk.calc.auxc( + conc=conc + shift, time=time, ..., options=options, auc.type="AUCall", - lambda.z=NA + lambda.z=NA, + fun_linear=function(conc.1, conc.2, time.1, time.2) { + aucaboveintegrate_linear(conc.1 - shift, conc.2 - shift, time.1, time.2, conc_above) + }, + fun_log=function(conc.1, conc.2, time.1, time.2) { + aucaboveintegrate_log(conc.1 - shift, conc.2 - shift, time.1, time.2, conc_above) + }, + fun_inf=aucintegrate_inf ) + if (ret %in% 0) { + ret <- structure(0, exclude = "DO NOT EXCLUDE") + } } ret } diff --git a/man/aucaboveintegrate.Rd b/man/aucaboveintegrate.Rd new file mode 100644 index 00000000..a95cbddc --- /dev/null +++ b/man/aucaboveintegrate.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/auc_integrate.R +\name{aucaboveintegrate} +\alias{aucaboveintegrate} +\alias{aucaboveintegrate_linear} +\alias{aucaboveintegrate_log} +\title{Integrate the area above a concentration within each interval} +\usage{ +aucaboveintegrate_linear(conc.1, conc.2, time.1, time.2, conc_above) + +aucaboveintegrate_log(conc.1, conc.2, time.1, time.2, conc_above) +} +\arguments{ +\item{conc.1, conc.2}{The concentration at \code{time.1} and \code{time.2}} + +\item{time.1, time.2}{The time at the start and end of each interval} + +\item{conc_above}{The concentration to integrate above} +} +\value{ +The area between the interpolated concentrations and \code{conc_above} +while the concentration is above \code{conc_above} +} +\description{ +Integrate the area above a concentration within each interval +} +\keyword{Internal} diff --git a/man/pk.calc.aucabove.Rd b/man/pk.calc.aucabove.Rd index 5dfb8eff..898e78a4 100644 --- a/man/pk.calc.aucabove.Rd +++ b/man/pk.calc.aucabove.Rd @@ -25,6 +25,11 @@ is \code{method} as described in the details section.} The AUC of the concentration above the limit } \description{ -Concentrations below the given concentration (\code{conc_above}) will be set -to zero. +The area is calculated between the concentration-time curve and \code{conc_above} +while the curve is above \code{conc_above}. The times when the curve crosses +\code{conc_above} are interpolated using the AUC calculation method. +} +\details{ +Logarithmic interpolation is only used between two positive concentrations +that are not equal. Otherwise, linear interpolation is used. } diff --git a/tests/testthat/test-pk.calc.simple.R b/tests/testthat/test-pk.calc.simple.R index 684c871b..3afb9c1c 100644 --- a/tests/testthat/test-pk.calc.simple.R +++ b/tests/testthat/test-pk.calc.simple.R @@ -802,7 +802,7 @@ test_that("pk.calc.ceoi", { test_that("pk.calc.aucabove", { expect_equal( pk.calc.aucabove(conc = c(0:5, 1), time = 0:6, conc_above = 2), - pk.calc.auc.all(conc = c(0, 0, 0, 1:3, 0), time = 0:6) + structure(4.5 + (3 - 2*log(5/2))/log(5), method = "AUC: lin up/log down") ) expect_equal( @@ -868,6 +868,90 @@ test_that("pk.calc.cstart", { ) }) +test_that("pk.calc.aucabove interpolates when the threshold is crossed (issue 696)", { + aucabove <- function(...) as.numeric(pk.calc.aucabove(...)) + expect_equal(aucabove(conc = c(12, 8), time = 0:1, conc_above = 10, method = "linear"), 0.5) + expect_equal(aucabove(conc = c(8, 12), time = 0:1, conc_above = 10, method = "linear"), 0.5) + expect_equal( + aucabove(conc = c(12, 8), time = 0:1, conc_above = 10, method = "lin up/log down"), + (2 - 10*log(12/10))/log(12/8) + ) + expect_equal( + aucabove(conc = c(20, 15), time = 0:1, conc_above = 10, method = "lin up/log down"), + 5/log(20/15) - 10 + ) +}) + +test_that("pk.calc.aucabove matches numerical integration with several crossings (issue 696)", { + conc <- c(0, 6, 1, 8, 5, 3, 7, 2, 0.5, 0) + time <- c(0, 1, 2, 4, 5, 8, 9, 12, 16, 24) + width <- 0.001 + time_mid <- seq(width/2, 24, by = width) + for (method in c("linear", "lin up/log down", "lin-log")) { + conc_mid <- + interp.extrap.conc( + conc = conc, time = time, time.out = time_mid, method = method, auc.type = "AUCall" + ) + for (conc_above in c(0, 2, 4.5, 7.5)) { + expect_equal( + as.numeric(pk.calc.aucabove(conc = conc, time = time, conc_above = conc_above, method = method)), + sum(pmax(as.numeric(conc_mid) - conc_above, 0))*width, + tolerance = 1e-5, + info = paste(method, conc_above) + ) + } + expect_equal( + pk.calc.aucabove(conc = conc, time = time, conc_above = 0, method = method), + pk.calc.auc.all(conc = conc, time = time, method = method) + ) + } +}) + +test_that("pk.calc.aucabove keeps measured values below the threshold (issue 696)", { + aucabove <- function(...) as.numeric(pk.calc.aucabove(...)) + expect_equal( + aucabove(conc = c(0, 4, 1, 4, 0), time = 0:4, conc_above = 2, method = "linear"), + 1 + 2*2/3 + ) + expect_equal( + aucabove(conc = c(0, 4, NA, 4, 0), time = 0:4, conc_above = 2, method = "linear"), + 5 + ) + expect_equal( + aucabove(conc = c(0, 4, 4, 0), time = 0:3, conc_above = 2, method = "linear", interval = c(0, 2)), + 2.5 + ) + expect_equal( + aucabove(conc = c(0, 10, 10, 5), time = 0:3, conc_above = 2, method = "lin-log"), + 3.2 + 8 + 5/log(2) - 2 + ) +}) + +test_that("pk.calc.aucabove works with negative values (issue 696)", { + aucabove <- function(...) as.numeric(pk.calc.aucabove(...)) + for (method in c("linear", "lin up/log down")) { + expect_equal( + aucabove(conc = c(-5, 5, -5), time = 0:2, conc_above = 0, method = method), 2.5, + info = method + ) + expect_equal( + aucabove(conc = c(-1, -3), time = 0:1, conc_above = -2, method = method), 0.25, + info = method + ) + expect_equal( + aucabove(conc = c(0, 0, 0), time = 0:2, conc_above = -5, method = method), 10, + info = method + ) + } +}) + +test_that("pk.calc.aucabove is zero when nothing is above the threshold", { + zero <- structure(0, exclude = "DO NOT EXCLUDE") + expect_equal(pk.calc.aucabove(conc = c(1, 2, 1), time = 0:2, conc_above = 5), zero) + expect_equal(pk.calc.aucabove(conc = c(2, 2, 2), time = 0:2, conc_above = 2), zero) + expect_equal(pk.calc.aucabove(conc = c(0, 0, 0), time = 0:2, conc_above = 5), zero) +}) + test_that("pk.calc.aucabove rejects non-finite conc_above", { # This is a deliberate tightening from the previous stopifnot()-based check, # which allowed conc_above = Inf (silently yielding AUC = 0 for all