Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
708b909
fix: a negative dose amount is an error
billdenney Nov 14, 2026
f3fec2e
fix: say "numeric" dose, and pin the five-row boundary and a characte…
billdenney Nov 15, 2026
aba3c97
fix: PKNCAdose() with a rate column works with more than one dose row
billdenney Nov 16, 2026
e763a8e
fix: pk.calc.tlag() skips missing concentrations, as its siblings do
billdenney Nov 16, 2026
5d34478
fix: the excretion rates are NA when a collection with a volume has n…
billdenney Nov 16, 2026
144474f
docs: the setDuration() dose argument and the previous errors, with a…
billdenney Nov 16, 2026
37ad50f
docs: a missing sample before the first increase can move the lag time
billdenney Nov 16, 2026
d9dce1c
refactor: one helper for a collection whose amount is unknown
billdenney Nov 16, 2026
fc0d560
fix: pk.calc.swing() gives NA for a missing cmin
billdenney Nov 16, 2026
bd391ba
Merge remote-tracking branch 'origin/fix/impute-start-na-conc' into f…
billdenney Nov 16, 2026
f66a3e6
Merge branch 'main' into fix/tlag-missing-conc
billdenney Nov 19, 2026
24149d4
Merge branch 'fix/swing-missing-cmin' into fix/tlag-missing-conc
billdenney Nov 19, 2026
408f075
docs: regenerate pk.calc.mrt.Rd to match the functions' arguments
billdenney Nov 19, 2026
09cacde
fix: pk.calc.swing() gives NA for a missing cmax too, with the issue'…
billdenney Nov 19, 2026
16e74d4
Merge branch 'fix/impute-start-na-conc' into fix/urine-rates-missing-na
billdenney Nov 19, 2026
cf77608
docs: NEWS links the urine-rate entry to its pull request
billdenney Nov 19, 2026
564e193
docs: NEWS links the dose-rate entry to its pull request
billdenney Nov 19, 2026
efa14ca
fix: a reason given by more than one input of a parameter is listed o…
billdenney Nov 19, 2026
25b527c
feat: a simple parameter that is NA because of missing or unusable da…
billdenney Nov 19, 2026
b18960c
docs: NEWS lists the reasons now given with NA simple parameters
billdenney Nov 19, 2026
dcb5705
fix: reasons only on single values, 0/0 dose normalisation explained,…
billdenney Nov 19, 2026
21a36d4
test: every registered parameter gives a reason with each NA from pk.…
billdenney Nov 19, 2026
eccaa53
docs: NEWS lists the reason attribute as a breaking change and the re…
billdenney Nov 19, 2026
6068940
fix: pk.calc.swing() gives one value for each pair of cmax and cmin
billdenney Nov 20, 2026
bf64f5b
Merge origin/fix/urine-rates-missing-na into fix/tlag-missing-conc
billdenney Nov 21, 2026
ccfca46
Merge origin/fix/dose-rate-column into fix/tlag-missing-conc
billdenney Nov 21, 2026
a8cc14e
test: a missing start gives aucabove.predose.all cstart's reason first
billdenney Nov 21, 2026
4927743
Merge remote-tracking branch 'origin/main' into HEAD
billdenney Nov 25, 2026
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
105 changes: 105 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

* 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()`
(@billdenney). Code that passed a negative dose before now stops. A zero
or missing dose amount is allowed as before, a dose row excluded with
`exclude` is not checked, and a dose column that is not numeric is not
checked here.

* The excretion rates `ertlst`, `ermax`, `ertmax`, and `erlst` are now `NA`
when a collection with a volume has no concentration, as `ae`, `fe`, and
`erint` already were (@billdenney, #729). They were calculated
Expand All @@ -18,6 +26,27 @@ the dosing including dose amount and route.
missing both its concentration and its volume, or a known concentration
with a missing volume, leaves the rates as they were.

* A calculation function that returns `NA` because of missing or unusable data
now returns it with an `exclude` attribute that gives the reason, as the AUC
functions already did, so that `pk.nca()` reports why (@billdenney, #727).
The value is unchanged. The functions are `pk.calc.cmax()`, `pk.calc.cmin()`,
`pk.calc.tmax()`, `pk.calc.tmin()`, `pk.calc.tlast()`, `pk.calc.tfirst()`,
`pk.calc.clast.obs()`, `pk.calc.aucpext()`, `pk.calc.cl()`, `pk.calc.f()`,
`pk.calc.mrt()`, `pk.calc.mrt.iv()`, `pk.calc.mrt.md()`,
`pk.calc.mrt.md.iv()`, `pk.calc.cav()`, `pk.calc.ctrough()`,
`pk.calc.cstart()`, `pk.calc.ptr()`, `pk.calc.tlag()`, `pk.calc.deg.fluc()`,
`pk.calc.ceoi()`, `pk.calc.totdose()`, and `pk.calc.dn()`; the vectorised
ones give the attribute only for a result of length one. For a result `x`
such as `pk.calc.cmax(c(NA, NA))`:
* these now fail: `identical(x, NA)`, `isTRUE(all.equal(x, NA))`,
`expect_equal(x, NA)`, `expect_identical(x, NA)`, and
`expect_equal(x, NA_real_)`;
* the attribute is carried by `x * 2` and by `data.frame(a = x)$a`;
* `is.na()`, `ifelse()`, `c()`, `sapply()`, `sum()`, `max()`, and `paste()`
are unaffected;
* `as.numeric(x)` drops the attribute (giving `NA_real_`, where `cmax`,
`cmin`, `tmax`, `tmin`, `tlast`, and `tfirst` gave a logical `NA`).

* Parameter names and descriptions follow one style, so some column headers of
`summary()` and some descriptions change (@billdenney):
* Pretty names write lambda.z as math (`$\lambda_z$`), including "Number of
Expand Down Expand Up @@ -1222,6 +1251,82 @@ the generated intervals:

## Bug fixes

* `PKNCAdose()` with a `rate` column now works with more than one dose row
(@billdenney, #728). It stopped with "'length = 2' in coercion
to 'logical(1)'", because the dose amounts were passed where the name of the
dose column was expected; one row worked by accident. Each dose's duration
is its amount divided by its rate. A `rate` with no dose amount (a formula
without a left side) is now the error `pknca_error_rate_without_dose`; it
was `pknca_error_dose_invalid_duration` with one row and an error without a
class with more. A direct `setDuration(object, rate = 5, dose = NA)`
returned a duration of 0 and is now that error too.

* `pk.calc.tlag()` skips missing concentrations, as `pk.calc.cmax()` and the
other simple parameters do, and gains a `check` argument like theirs
(reported by @kawasakin, #723; @billdenney, #727). Two results change: a
missing value before or between the measured ones gave `NA` (for example
`pk.calc.tlag(c(NA, 40, 80, 10), c(0, 2, 4, 8))`) and now gives the lag time
of the measured values (2); and a missing value with no increase after it,
or every value missing, stopped the calculation (in `pk.nca()`, "Please
report a bug ... missing value where TRUE/FALSE needed") and now gives `NA`.

* `pk.calc.swing()` gives `NA` for a missing `cmin` or `cmax` instead of
stopping with "missing value where TRUE/FALSE needed" (reported by
@kawasakin, #723; @billdenney, #727). In `pk.nca()`, an interval whose
concentrations are all missing stopped with "Please report a bug" when
`swing` was requested, which stopped the calculation for every subject, and
now gives `NA` for that interval. `pk.calc.swing()` gives one value for
each pair of `cmax` and `cmin`; a vector of `cmax` with one `cmin` gives the
values it gave before, except that with a `cmin` of zero or below the result
is one `Inf` for each `cmax` (before, a single `Inf`).

* A simple parameter that is `NA` because of missing or unusable data now says
why in the `exclude` column of `pk.nca()` results, where the column was
empty (@billdenney, #727). No value changes; only the `exclude` text of
results that were already `NA`. The reasons, which the calculation functions
return as an `exclude` attribute (see "Breaking changes"):
- "All concentrations are missing": `cmax`, `cmin`, `tmax`, `tmin`, `tlast`,
`tfirst`, `clast.obs`, `tlag`.
- "No concentration is above zero": `tmax`, `tlast`, `tfirst`.
- "Concentrations never increase": `tlag`.
- "No non-missing concentration at the start of the interval": `cstart`;
"No non-missing concentration at the end of the interval" and "The
interval has no finite end": `ctrough`; "No non-missing concentration at
the end of the infusion" and "Dose duration is missing (or there is no
dose in the interval)": `ceoi`.
- "AUC is zero or negative": `cl.*`, `mrt.*` (all forms), and the reference
AUC of `f.*`; "AUCinf is zero": `aucpext.*`; "Cav is zero": `deg.fluc`;
"Ctrough is zero": `ptr`.
- "Dose is missing": `cl.*`, `f.*`, `totdose`, and the dose-normalized
parameters (`*.dn`); "Dose is zero": `*.dn` when the parameter is also
zero (the value stays `NaN`); "Dose is zero or negative": `f.*`; "No dose
in the interval": `cl.*`, `totdose`, `mrt.iv.*`, and `mrt.ivmd.*`.
- "A dose time or duration is missing", or "A dose amount is missing, or the
doses sum to zero" (when several doses are weighted): `mrt.iv.*` and
`mrt.ivmd.*`; "The dosing interval (tau) is not known": `mrt.md.*` and
`mrt.ivmd.*`.
- "The interval start or end is missing", "The interval has zero duration":
`cav.*`.

Everything calculated from one of these carries its reason, as for any
other reason (for example `swing`, `vss.*`, `vz.*`, `kel.*`, `thalf.eff.*`).
What changes for code reading the results: `summary()` gives the same
numbers (it ignores an `NA` either way); `as.data.frame(filter_excluded =
TRUE)` now leaves out these `NA` rows, as it leaves out any row with a
reason; and `exclude()` adds its reason after the one already there.

* The `exclude` text of a result that already had a reason now starts with the
root cause and lists each reason once (@billdenney, #727). For example,
the half-life results of an interval with every concentration missing read
"All concentrations are missing; Too few points for half-life calculation
(min.hl.points=3 with only 0 points)" instead of starting at "Too few
points"; `aucabove.trough.all` with no concentration at the end of the
interval reads "No non-missing concentration at the end of the interval;
Missing concentration to be above"; and a reason that reached a result from
two inputs ("X; X") is given once ("X"). Code that matches a reason by its
exact text must be updated. The values, and which results have a reason,
are unchanged.

* `mrt.iv.*`, `mrt.ivint.*`, `mrt.ivmd.*`, and everything calculated from them
(`vss.iv.*`, `vss.ivint.*`, `vss.ivmd.*`, `kel.iv.*`, `thalf.eff.iv.*`) are
now correct, and one value per interval, when an interval has more than one
Expand Down
69 changes: 62 additions & 7 deletions R/class-PKNCAdose.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
#' column from the `data`, and if given as a number, it is the value for all
#' doses. Only one may be given, and if neither is given, then the dose is
#' assumed to be a bolus (`duration=0`). If `rate` is given, then the dose
#' amount must be given (the left hand side of the `formula`). A numeric
#' duration is in the time unit of the analysis; a difftime duration is
#' converted to that unit in [PKNCAdata()].
#' amount must be given (the left hand side of the `formula`;
#' `pknca_error_rate_without_dose` otherwise), and the duration of each dose
#' is its amount divided by its rate. A missing dose amount or rate, or a
#' rate of zero, gives no valid duration (`pknca_error_dose_invalid_duration`).
#' A numeric duration is in the time unit of the analysis; a difftime
#' duration is converted to that unit in [PKNCAdata()].
#' @param time.nominal (optional) The name of the nominal time column (if the
#' main time variable is actual time. The `time.nominal` is not used during
#' calculations; it is available to assist with data summary and checking.
Expand All @@ -43,6 +46,12 @@
#' same dose is given in every interval. When given as a two-sided
#' formula, both the dose amount and time are used directly from the
#' data.
#'
#' A negative numeric dose amount is an error (`pknca_error_dose_negative`,
#' naming the rows), here and again in [pk.nca()] for a dose made negative
#' later (for example by `mutate()`). A zero or missing amount is allowed,
#' and a row excluded with `exclude` is not checked. A dose column that is
#' not numeric is not checked here.
#' @family PKNCA objects
#' @export
PKNCAdose <- function(data, ...)
Expand Down Expand Up @@ -152,6 +161,7 @@ PKNCAdose.data.frame <- function(data, formula, route, rate, duration,
class = "pknca_error_partial_missing_indepvar"
)
}
assert_dose_not_negative(ret)
if (length(parsed_form$time) == 1 && inherits(data[[parsed_form$time]], "Date")) {
pknca_warn_date_time(time_col = parsed_form$time, data_type = "dose")
}
Expand All @@ -160,8 +170,8 @@ PKNCAdose.data.frame <- function(data, formula, route, rate, duration,
} else {
ret <- setRoute(ret, route)
}
ret <- setDuration.PKNCAdose(ret, duration=duration,
rate=rate, dose=getDepVar.PKNCAdose(ret))
# The dose is the column name, which setDuration.PKNCAdose() uses by default
ret <- setDuration.PKNCAdose(ret, duration=duration, rate=rate)
if (!missing(time.nominal)) {
ret <-
setAttributeColumn(object=ret,
Expand Down Expand Up @@ -224,7 +234,9 @@ setRoute.PKNCAdose <- function(object, route, ...) {
#' @param duration The value to set for the duration or the name of the column
#' in the data to use for the duration.
#' @param rate (for PKNCAdose objects only) The rate of infusion
#' @param dose (for PKNCAdose objects only) The dose amount
#' @param dose (for PKNCAdose objects only) The name of the dose column (the
#' default, from the formula), or the dose amounts (a value, or one per
#' row); used with `rate`
#' @param ... Arguments passed to another setDuration function
#' @returns The object with duration set
#' @export
Expand All @@ -245,7 +257,14 @@ setDuration.PKNCAdose <- function(object, duration, rate, dose, ...) {
# requiring near-equal checks for floating point error.
} else if (!missing(duration)) {
object <- setAttributeColumn(object=object, attr_name="duration", col_or_value=duration)
} else if (!missing(rate) && !missing(dose) && !is.na(dose)) {
} else if (!missing(rate)) {
# `dose` is a column name (the default) or the dose amounts
if (length(dose) == 0 || all(is.na(dose))) {
rlang::abort(
"`rate` needs the dose amount (the left side of the formula) to calculate the duration; without a dose amount, give `duration` instead",
class = "pknca_error_rate_without_dose"
)
}
tmprate <- getColumnValueOrNot(object$data, rate, "rate")
tmpdose <- getColumnValueOrNot(object$data, dose, "dose")
duration <- tmpdose$data[[tmpdose$name]]/tmprate$data[[tmprate$name]]
Expand Down Expand Up @@ -293,6 +312,42 @@ getDepVar.PKNCAdose <- function(x, ...) {
}
}

#' Require that no dose amount is negative
#'
#' A negative dose amount is invalid input. A zero amount and a missing
#' amount are allowed, and an excluded row is not checked, as for the dose
#' times. A dose column that is not numeric (character, factor, difftime) is
#' not checked: it fails later, where it is used as a number.
#'
#' @param object A PKNCAdose object
#' @returns `object`, invisibly, or the error `pknca_error_dose_negative`
#' naming the first rows of the dose data with a negative amount and their
#' count
#' @keywords Internal
#' @noRd
assert_dose_not_negative <- function(object) {
dose <- getDepVar.PKNCAdose(object)
if (!is.numeric(dose)) {
return(invisible(object))
}
# The plain numbers, so that a units column needs no method
values <- as.numeric(dose)
rows <- which(!is.na(values) & values < 0 & is.na(normalize_exclude(object)))
if (length(rows) > 0) {
rlang::abort(
sprintf(
"Dose amounts (column '%s') must not be negative; %d row(s) of the dose data are: %s%s",
object$columns$dose,
length(rows),
paste(utils::head(rows, 5), collapse = ", "),
if (length(rows) > 5) ", ..." else ""
),
class = "pknca_error_dose_negative"
)
}
invisible(object)
}

#' @export
getIndepVar.PKNCAdose <- function(x, ...) {
time_col <- x$columns$time
Expand Down
14 changes: 11 additions & 3 deletions R/pk.calc.all.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
#' @export
pk.nca <- function(data, verbose=FALSE) {
assert_PKNCAdata(data)
# PKNCAdose() refused a negative dose amount; a mutate() or join on the
# dose data afterwards may have made one
if (inherits(data$dose, "PKNCAdose")) {
assert_dose_not_negative(data$dose)
}
# Date-time times and intervals and difftime durations become numbers here,
# so that intervals can change after PKNCAdata(). The results keep the
# converted data, which is the data the calculations used.
Expand Down Expand Up @@ -975,10 +980,13 @@ bind_interval_result <- function(interval_cols, calculated) {

# Combine exclusion reasons from a calculation's inputs with the exclusion the
# calculation itself set (the "exclude" attribute). "DO NOT EXCLUDE" on the
# result wins and clears everything. Documented in
# vignettes/v80-writing-parameter-functions.Rmd.
# result wins and clears everything. Each input's exclusion may already be
# several reasons joined with "; ", so they are split before a reason given by
# more than one input (cmax and cmin of an all-missing profile) is listed once.
# Documented in vignettes/v80-writing-parameter-functions.Rmd.
combine_exclude_reasons <- function(from_inputs, from_result) {
reasons <- stats::na.omit(c(from_inputs, from_result))
reasons <- stats::na.omit(as.character(c(from_inputs, from_result)))
reasons <- unique(unlist(strsplit(reasons, split = "; ", fixed = TRUE)))
if (identical(from_result, "DO NOT EXCLUDE")) {
NA_character_
} else if (length(reasons) > 0) {
Expand Down
Loading
Loading