Skip to content
Open
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
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down
37 changes: 37 additions & 0 deletions R/auc_integrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 21 additions & 5 deletions R/pk.calc.simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
27 changes: 27 additions & 0 deletions man/aucaboveintegrate.Rd

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

9 changes: 7 additions & 2 deletions man/pk.calc.aucabove.Rd

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

86 changes: 85 additions & 1 deletion tests/testthat/test-pk.calc.simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down