From f46ba6de4a96ac890642b0b4adf65352b5bf6dfb Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Thu, 7 Sep 2023 20:00:07 -0400 Subject: [PATCH] The calc_derived family of functions works with vector inputs (Fix #29) --- NEWS.md | 4 ++ R/calc_derived.R | 144 ++++++++++++++++++------------------- tests/testthat/test-calc.R | 40 +++++++++++ 3 files changed, 114 insertions(+), 74 deletions(-) diff --git a/NEWS.md b/NEWS.md index 5e9fe43..4c905b9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# pmxTools (development version) + +* The calc_derived family of functions works with vector inputs (#29) + # pmxTools 1.3 * Added NCA parameter estimation to `calc_derived_1cpt()`, `calc_derived_2cpt()` and `calc_derived_3cpt()`, if dose and other required information (e.g. `tinf`, `dur`, `tau`) is provided. diff --git a/R/calc_derived.R b/R/calc_derived.R index 6ce2707..00bc173 100644 --- a/R/calc_derived.R +++ b/R/calc_derived.R @@ -25,12 +25,12 @@ #' @param verbose For \code{calc_derived()}, provide a message indicating the #' type of model detected. #' @param ... Passed to the other \code{calc_derived_*()} functions. -#' +#' #' @return Return a list of derived PK parameters for a 1-, 2-, or 3-compartment #' linear model given provided clearances and volumes based on the #' \code{type}. If a dose is provided, estimated non-compartmental analysis (NCA) parameters will #' be provided as well, based on simulation of single-dose and (if `tau` is specified) steady-state time courses. -#' \itemize{ +#' \itemize{ #' \item \code{Vss}: Volume of distribution at steady state, \eqn{V_{ss}} (volume units, e.g. L); 1-, 2-, and 3-compartment #' \item \code{k10}: First-order elimination rate, \eqn{k_{10}} (inverse time units, e.g. 1/h); 1-, 2-, and 3-compartment #' \item \code{k12}: First-order rate of transfer from central to first peripheral compartment, \eqn{k_{12}} (inverse time units, e.g. 1/h); 2- and 3-compartment @@ -53,7 +53,7 @@ #' \item \code{AUCtau}: Area under the concentration-time curve over the dosing interval at steady state #' \item \code{Cmax}: Maximum concentration after a single dose #' \item \code{Cmaxss}: Maximum concentration over the dosing interval at steady state -#' \item \code{Tmax}: Time after dose of maximum concentration +#' \item \code{Tmax}: Time after dose of maximum concentration #' \item \code{AUCinf_dose_normalized}: Dose-normalized area under the concentration-time curve to infinity (single dose) #' \item \code{AUCtau_dose_normalized}: Dose-normalized area under the concentration-time curve over the dosing interval at steady state #' \item \code{Cmax_dose_normalized}: Dose-normalized maximum concentration after a single dose @@ -97,6 +97,23 @@ calc_derived <- function(..., verbose=FALSE) { } } +update_tend <- function(tend, guess_tmax) { + mask_tend_tmax <- tend < guess_tmax + if (any(mask_tend_tmax)) { + tend[mask_tend_tmax] <- + tend * 2^ceiling(log2(guess_tmax[mask_tend_tmax]/tend[mask_tend_tmax])) + } + tend +} + +maybe_warn_flipflop <- function(ka, k10) { + if(!is.null(ka)) { + if(any(ka < k10)) { + warning("Flip-flop kinetics detected. ka is lower than k10, and half-life may therefore not be similar to the half-life calculated by typical NCA.\n") + } + } +} + #' @rdname calc_derived #' @examples #' params <- calc_derived_1cpt(CL=16, V=25) @@ -113,7 +130,7 @@ calc_derived_1cpt <- function(CL, V=NULL, V1=NULL, ka=NULL, dur=NULL, tlag=NULL, trueA <- 1/V1 thalf <- log(2)/k10 alpha <- k10 - + AUCinf <- NULL AUCtau <- NULL Cmax <- NULL @@ -123,23 +140,20 @@ calc_derived_1cpt <- function(CL, V=NULL, V1=NULL, ka=NULL, dur=NULL, tlag=NULL, AUCtau_dose_normalized <- NULL Cmax_dose_normalized <- NULL Cmaxss_dose_normalized <- NULL - + guess_tmax <- 0 if(!is.null(ka)) guess_tmax <- 1/ka if(!is.null(dur)) guess_tmax <- dur if(!is.null(tinf)) guess_tmax <- tinf - - tend <- 24 - while(tend