From e5b7b2a3aa2f723f6c6ec265996124de7b5d772f Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Mon, 9 Nov 2026 18:31:51 +0000 Subject: [PATCH 1/9] fix: interval_add_secondary() links per-subject intervals to their own subject Automatically generated intervals have one row per subject, so a reference such as data.frame(start = 0, end = 12) matched one row per subject. The reference was chosen by time alone, so every later interval matched several references and the helper stopped with pknca_error_secondary_ref_ambiguous_spec. The candidate references are now first narrowed to those sharing the test row's values in the columns that `reference` and `ref_vary` do not name. Reads of a possibly absent interval_id column use [[ ]], so tibble intervals no longer warn about an unknown column. Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 9 +++ R/secondary-parameters.R | 64 +++++++++++++---- tests/testthat/test-secondary-parameters.R | 80 ++++++++++++++++++++++ 3 files changed, 138 insertions(+), 15 deletions(-) diff --git a/NEWS.md b/NEWS.md index a58102da..6a48bd71 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1327,6 +1327,15 @@ the generated intervals: function name has a dot in it, so loading the package failed with `could not find function "getS3method"`. `getS3method` is now imported. +* `interval_add_secondary()` works on automatically generated intervals, which + have one row per subject. A `reference` such as + `data.frame(start = 0, end = 12)` matched one row per subject, and the helper + stopped with `pknca_error_secondary_ref_ambiguous_spec` because it chose + among those rows by their times alone; each interval now takes the reference + row of its own group (its own subject), ignoring the group columns named in + `reference` or `ref_vary`. The helper also no longer warns about the + unknown `interval_id` column of tibble intervals. (@billdenney) + ## Other changes * The sparse AUC vignette and the help of `var_sparse_auc()` and diff --git a/R/secondary-parameters.R b/R/secondary-parameters.R index 2ab1f082..1c851f77 100644 --- a/R/secondary-parameters.R +++ b/R/secondary-parameters.R @@ -1444,7 +1444,7 @@ interval_next_ref_names <- function(used, n, prefix = "ref") { # for a factor. An absent or unfilled (all-NA logical) column takes character # identifiers, and is created here so the caller can assign into it. interval_new_ids <- function(intervals, n, prefix = "ref") { - existing <- intervals$interval_id + existing <- intervals[["interval_id"]] if (is.null(existing) || (is.logical(existing) && all(is.na(existing)))) { intervals$interval_id <- NA_character_ existing <- character(0) @@ -1484,7 +1484,7 @@ interval_assign_ref_ids <- function(intervals, ref_groups, ref_id, prefix = "ref need_new <- rep(FALSE, length(ref_groups)) for (i in seq_along(ref_groups)) { current <- - if (is.null(intervals$interval_id)) { + if (is.null(intervals[["interval_id"]])) { NULL } else { stats::na.omit(intervals$interval_id[ref_groups[[i]]]) @@ -1511,8 +1511,8 @@ interval_assign_ref_ids <- function(intervals, ref_groups, ref_id, prefix = "ref } } if (!is.null(ref_id) && - (is.null(intervals$interval_id) || - (is.logical(intervals$interval_id) && all(is.na(intervals$interval_id))))) { + (is.null(intervals[["interval_id"]]) || + (is.logical(intervals[["interval_id"]]) && all(is.na(intervals[["interval_id"]]))))) { intervals$interval_id <- ref_id[rep(NA_integer_, nrow(intervals))] } if (any(need_new)) { @@ -1543,34 +1543,54 @@ interval_reference_groups <- function(intervals, ref_rows) { } # Which of the reference intervals a test row uses. One reference serves every -# test row; several are told apart by matching the test row's own times, or by -# a single reference covering them (a created reference can be wider than its -# test interval when it spans whole collections). -interval_pick_reference <- function(intervals, row, ref_groups, param) { +# test row. Several are first narrowed to those sharing the test row's values +# in `match_cols` (the group columns that `reference` does not name and that may +# not vary), so per-subject intervals each take their own subject's reference; +# the engine reads the group values of the reference row it is pointed at. +# When none shares them, all are candidates. The candidates are then told +# apart by matching the test row's own times, or by a single reference covering +# them (a created reference can be wider than its test interval when it spans +# whole collections). +interval_pick_reference <- function(intervals, row, ref_groups, param, + match_cols = character(0)) { if (length(ref_groups) == 1) { return(1L) } + candidates <- seq_along(ref_groups) + if (length(match_cols) > 0) { + first_rows <- vapply(X = ref_groups, FUN = `[`, 1L, FUN.VALUE = 1L) + same_group <- + which(interval_match_groups( + intervals[first_rows, match_cols, drop = FALSE], + intervals[row, match_cols, drop = FALSE] + )) + if (length(same_group) == 1) { + return(same_group) + } else if (length(same_group) > 1) { + candidates <- same_group + } + } same_time <- - which(vapply( - X = ref_groups, + candidates[vapply( + X = ref_groups[candidates], FUN = function(rows) { (intervals$start[rows[1]] %in% intervals$start[row]) && (intervals$end[rows[1]] %in% intervals$end[row]) }, FUN.VALUE = TRUE - )) + )] if (length(same_time) == 1) { return(same_time) } covering <- - which(vapply( - X = ref_groups, + candidates[vapply( + X = ref_groups[candidates], FUN = function(rows) { (intervals$start[rows[1]] <= intervals$start[row]) && (intervals$end[rows[1]] >= intervals$end[row]) }, FUN.VALUE = TRUE - )) + )] if (length(covering) == 1) { return(covering) } @@ -1866,7 +1886,21 @@ interval_edit_secondary <- function(intervals, param, reference, target_groups, ref_col <- paste0(param, "_ref") used_groups <- integer(0) for (row in test_rows) { - current_group <- interval_pick_reference(intervals, row, ref_groups, param) + row_ref_vary <- + if (is.null(ref_vary)) { + interval_ref_vary(intervals, row, param, default = ref_vary_default) + } else { + ref_vary + } + current_group <- + interval_pick_reference( + intervals, row, ref_groups, param, + match_cols = + setdiff( + interval_describe_cols(intervals), + c("start", "end", names(reference), row_ref_vary) + ) + ) current_id <- assigned$ids[[current_group]] current_pointer <- intervals[[ref_col]][row] if (!is.na(current_pointer) && diff --git a/tests/testthat/test-secondary-parameters.R b/tests/testthat/test-secondary-parameters.R index f7e7d598..e73cb08e 100644 --- a/tests/testthat/test-secondary-parameters.R +++ b/tests/testthat/test-secondary-parameters.R @@ -1126,6 +1126,86 @@ test_that("interval_add_secondary() creates one reference per test interval", { expect_equal(intervals_linked$clr.last_ref, c("ref1", "ref2", NA, NA)) }) +# Automatically generated intervals have one row per subject and dosing +# interval, so `reference = data.frame(start = 0, end = 12)` matches one row per +# subject; each subject's later interval must take its own subject's reference, +# since the calculation reads the group values of the row it points at. +# linear auclast, subject 1: 0-12 = (0+10)/2*2 + (10+2)/2*10 = 70 +# 12-24 = (2+12)/2*2 + (12+4)/2*10 = 94 +# subject 2: 0-12 = (0+20)/2*2 + (20+2)/2*10 = 130 +test_that("interval_add_secondary() links generated per-subject intervals to their own subject", { + d_conc_md <- + data.frame( + subject = rep(1:2, each = 5), + time = rep(c(0, 2, 12, 14, 24), 2), + conc = c(0, 10, 2, 12, 4, 0, 20, 2, 12, 4) + ) + d_dose_md <- data.frame(subject = rep(1:2, each = 2), time = rep(c(0, 12), 2), dose = 1) + o_data_md <- + PKNCAdata( + PKNCAconc(d_conc_md, conc~time|subject), PKNCAdose(d_dose_md, dose~time|subject), + options = list(auc.method = "linear") + ) + # The generated intervals are a tibble without an `interval_id` column + expect_s3_class(o_data_md$intervals, "tbl_df") + expect_false("interval_id" %in% names(o_data_md$intervals)) + expect_no_warning( + o_data_linked <- + interval_add_secondary( + o_data_md, param = "ratio.auclast", reference = data.frame(start = 0, end = 12) + ) + ) + expect_equal(o_data_linked$intervals$subject, c(1L, 1L, 2L, 2L)) + expect_equal(o_data_linked$intervals$start, c(0, 12, 0, 12)) + expect_equal(o_data_linked$intervals$interval_id, c("ref1", NA, "ref2", NA)) + expect_equal(o_data_linked$intervals$ratio.auclast_ref, c(NA, "ref1", NA, "ref2")) + expect_equal(o_data_linked$intervals$ratio.auclast, c(FALSE, TRUE, FALSE, TRUE)) + d_res <- suppressWarnings(as.data.frame(pk.nca(o_data_linked))) + d_ratio <- d_res[d_res$PPTESTCD %in% "ratio.auclast", ] + expect_equal(d_ratio$subject, 1:2) + expect_equal(d_ratio$PPORRES, c(94/70, 94/130)) +}) + +# A group column named in `ref_vary` may differ between the interval and its +# reference, so it does not take part in choosing among the reference rows: in +# this crossover subject 1 received the reference in period 1 and subject 2 in +# period 2, and each test profile takes its own subject's reference. +test_that("interval_add_secondary() picks the subject's own reference across varying groups", { + d_conc_xo <- + data.frame( + subject = rep(1:2, each = 6), + period = rep(rep(1:2, each = 3), 2), + treatment = c(rep(c("ref", "test"), each = 3), rep(c("test", "ref"), each = 3)), + time = 0:2, + conc = c(0, 10, 5, 0, 6, 3, 0, 9, 4, 0, 20, 8) + ) + d_dose_xo <- unique(d_conc_xo[, c("subject", "period", "treatment")]) + d_dose_xo$time <- 0 + d_dose_xo$dose <- 1 + o_data_xo <- + PKNCAdata( + PKNCAconc(d_conc_xo, conc~time|treatment+period+subject), + PKNCAdose(d_dose_xo, dose~time|treatment+period+subject), + intervals = + data.frame( + subject = c(1, 1, 2, 2), period = c(1, 2, 1, 2), + treatment = c("ref", "test", "test", "ref"), + start = 0, end = Inf, cmax = TRUE + ) + ) + o_data_linked <- + interval_add_secondary( + o_data_xo, param = "ratio.cmax", reference = data.frame(treatment = "ref"), + ref_vary = "period" + ) + expect_equal(o_data_linked$intervals$interval_id, c("ref1", NA, NA, "ref2")) + expect_equal(o_data_linked$intervals$ratio.cmax_ref, c(NA, "ref1", "ref2", NA)) + d_res <- as.data.frame(pk.nca(o_data_linked)) + d_ratio <- d_res[d_res$PPTESTCD %in% "ratio.cmax", ] + expect_equal(d_ratio$subject, c(1, 2)) + expect_equal(d_ratio$PPORRES, c(6/10, 9/20)) +}) + # A created reference row is a new interval: it carries the columns of the # intervals it was built from but none of their bookkeeping test_that("a created reference interval takes no identifier or imputation of its own", { From a97dcdaf7ffbd96e64302c7224125851bababc0d Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Mon, 9 Nov 2026 18:42:12 +0000 Subject: [PATCH 2/9] fix: match results to a group-stratified units table by value When units differ between groups (a subject without a dose, or unit columns), the units table keeps the group columns, holding their values as character. pknca_unit_conversion() joined the results to it with dplyr::left_join(), which stops on an integer, numeric, or date group column ("Can't join ... due to incompatible types") and turns a factor group column into character. left_join_by_value() matches the keys as character and restores the key columns of the results. Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 8 ++++ R/unit-support.R | 7 +-- R/update.PKNCAresults.R | 30 ++++++++++++ tests/testthat/test-unit-support.R | 75 ++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 5 deletions(-) diff --git a/NEWS.md b/NEWS.md index c3598838..09fbdd69 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1117,6 +1117,14 @@ the generated intervals: ## Bug fixes +* `pk.nca()` with units that differ between groups (for example, a subject + without a dose, or a unit column) no longer stops with "Can't join ... due + to incompatible types" when a group column is integer, numeric, or a date: + the units table holds group values as character, and the results are now + matched to it by value. This includes the `_ref` columns of + secondary parameters. A factor group column also stays a factor in the + results where it used to become character (@billdenney). + * Group columns that are ordered factors with different levels in the concentration data, the dose data, the intervals, or a normalization table now join by value. Before, `PKNCAdata()` with units, `pk.nca()`, diff --git a/R/unit-support.R b/R/unit-support.R index 0f865b46..cc9c94cf 100644 --- a/R/unit-support.R +++ b/R/unit-support.R @@ -854,11 +854,8 @@ pknca_unit_conversion <- function(result, units, allow_partial_missing_units = F for (col in setdiff(grep("_ref$", names(units), value = TRUE), names(ret))) { units <- units[is.na(units[[col]]), setdiff(names(units), col), drop = FALSE] } - ret <- - dplyr::left_join( - ret, units, - by=intersect(names(ret), names(units)) - ) + # The units table that PKNCAdata() builds holds group values as character + ret <- left_join_by_value(ret, units, by = intersect(names(ret), names(units))) mask_missing_units <- is.na(ret$PPORRESU) if (any(mask_missing_units)) { msg_missing <- diff --git a/R/update.PKNCAresults.R b/R/update.PKNCAresults.R index b3550a70..5fb95c96 100644 --- a/R/update.PKNCAresults.R +++ b/R/update.PKNCAresults.R @@ -170,6 +170,36 @@ semi_join_by_value <- function(x, y, by) { join_rows_by_value(x, y, by = by, join_fun = dplyr::semi_join) } +#' `dplyr::left_join()` matching key columns by their values as text +#' +#' The units table that `PKNCAdata()` builds holds group values as character, +#' while the results keep the types of the source data. dplyr will not join an +#' integer, numeric, or date column to a character one, and joining a factor to +#' a character column makes the joined column character. The keys are matched +#' as character, and the returned rows keep the key columns of `x` as they +#' were. +#' +#' @inheritParams join_rows_by_value +#' @returns The rows of `dplyr::left_join(x, y, by = by)`, with the key columns +#' of `x` unchanged +#' @noRd +left_join_by_value <- function(x, y, by) { + rowid_col <- paste0(max(c(names(x), names(y))), "X") + tracking <- x + y_text <- y + for (nm in by) { + tracking[[nm]] <- as.character(tracking[[nm]]) + y_text[[nm]] <- as.character(y_text[[nm]]) + } + tracking[[rowid_col]] <- seq_len(nrow(x)) + ret <- dplyr::left_join(tracking, y_text, by = by) + for (nm in by) { + ret[[nm]] <- x[[nm]][ret[[rowid_col]]] + } + ret[[rowid_col]] <- NULL + ret +} + # remove the original data.frames from the source data to enable comparison for # updates strip_source_data <- function(data) { diff --git a/tests/testthat/test-unit-support.R b/tests/testthat/test-unit-support.R index 84f560bc..1a2fcc44 100644 --- a/tests/testthat/test-unit-support.R +++ b/tests/testthat/test-unit-support.R @@ -571,3 +571,78 @@ test_that("pknca_units_table for PKNCAdata joins ordered factor groups whose lev c("a", "b") ) }) + +test_that("unit conversion joins results to the units table by group value, keeping the group column types", { + # Subject 2 has no dose, so the units differ by subject and the units table + # keeps the subject column (as character, while the data have integers) + d_conc <- + data.frame( + subject = rep(1:2, each = 3), time = rep(0:2, 2), + conc = c(0, 2, 1, 0, 3, 1) + ) + o_data <- + suppressWarnings(PKNCAdata( + PKNCAconc(d_conc, conc~time|subject, concu = "ng/mL", timeu = "hr", amountu = "mg"), + PKNCAdose(data.frame(subject = 1L, time = 0, dose = 10), dose~time|subject, doseu = "mg"), + intervals = data.frame(start = 0, end = 2, cmax = TRUE) + )) + expect_type(o_data$units$subject, "character") + expect_equal( + o_data$units$PPORRESU[o_data$units$PPTESTCD == "cmax.dn"], + c("(ng/mL)/mg", NA) + ) + df_nca <- as.data.frame(pk.nca(o_data)) + expect_identical(df_nca$subject, 1:2) + expect_equal(df_nca$PPORRES, c(2, 3)) + expect_equal(df_nca$PPORRESU, c("ng/mL", "ng/mL")) + + # A factor group column stays a factor with its levels + d_conc_fct <- d_conc + d_conc_fct$subject <- factor(d_conc_fct$subject, levels = 2:1) + d_conc_fct$concu <- rep(c("ng/mL", "ug/mL"), each = 3) + o_data_fct <- + PKNCAdata( + PKNCAconc(d_conc_fct, conc~time|subject, concu = "concu", timeu = "hr"), + intervals = data.frame(start = 0, end = 2, cmax = TRUE) + ) + df_nca_fct <- as.data.frame(pk.nca(o_data_fct)) + # Results are in the order of the factor levels + expect_identical(df_nca_fct$subject, factor(c(2, 1), levels = 2:1)) + expect_equal(df_nca_fct$PPORRES, c(3, 2)) + expect_equal(df_nca_fct$PPORRESU, c("ug/mL", "ng/mL")) +}) + +test_that("unit conversion matches integer reference group columns of secondary parameters", { + d_conc <- + data.frame( + subject = 1L, spec = rep(1:2, times = c(3, 2)), + time = c(0, 12, 24, 0, 12), conc = c(10, 6, 2, 2, 1), vol = c(NA, NA, NA, 100, 150), + cu = rep(c("mg/L", "mg/mL"), times = c(3, 2)) + ) + o_conc <- PKNCAconc(d_conc, conc~time|spec + subject, volume = "vol", concu = "cu", timeu = "hr", amountu = "mg") + o_dose <- PKNCAdose(data.frame(subject = 1L, time = 0, dose = 50), dose~time|subject, doseu = "mg") + intervals <- + data.frame( + spec = 1:2, start = 0, end = 24, interval_id = c("p", NA), + auclast = c(TRUE, FALSE), clr.last = c(FALSE, TRUE), clr.last_ref = c(NA, "p") + ) + df_nca <- as.data.frame(pk.nca(PKNCAdata(o_conc, o_dose, intervals = intervals))) + expect_equal(df_nca$PPTESTCD, c("auclast", "ae", "clr.last")) + expect_identical(df_nca$spec_ref, c(NA, NA, 1L)) + expect_equal(df_nca$PPORRESU, c("hr*mg/L", "mg", "mg/(hr*mg/L)")) +}) + +test_that("left_join_by_value matches keys of different types by value", { + x <- data.frame(g = c(2L, 1L, 2L, 3L), f = factor(c("b", "a", "b", "c"), levels = c("c", "b", "a")), v = 1:4) + y <- data.frame(g = c("1", "2"), f = c("a", "b"), w = c("one", "two")) + expect_identical( + left_join_by_value(x, y, by = c("g", "f")), + data.frame(x, w = c("two", "one", "two", NA)) + ) + # A row of x with several matches in y is repeated + y_dup <- data.frame(g = c("2", "2"), w = c("a", "b")) + expect_identical( + left_join_by_value(x[1, c("g", "v")], y_dup, by = "g"), + data.frame(g = c(2L, 2L), v = c(1L, 1L), w = c("a", "b")) + ) +}) From c71413d3f38cf2c0f9a3a578b58fa8c08471e54f Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Mon, 9 Nov 2026 18:44:53 +0000 Subject: [PATCH 3/9] fix: preferred units when some unit sets need no conversion With unit columns and a preferred unit, a unit set already in the preferred units got a table without PPSTRESU and conversion_factor, while the sets that convert had them. Composing the secondary parameter units read the missing columns as zero-length and stopped with "arguments imply differing number of rows: 1, 0". Every unit set now has the standardization columns when any has them, with the set's own units and a factor of 1 where it does not convert, as pknca_units_table() does within one set. The comment that preferred units only apply to scalar units was wrong: pknca_set_units() keeps them for unit columns too. Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 6 +++++ R/unit-support.R | 22 +++++++++++------ tests/testthat/test-unit-support.R | 39 ++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 09fbdd69..86e2ea5c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1117,6 +1117,12 @@ the generated intervals: ## Bug fixes +* Preferred units (`concu_pref`, `doseu_pref`, and the others) with unit + columns whose units differ between groups no longer stop `PKNCAdata()` with + "arguments imply differing number of rows: 1, 0" when some groups are + already in the preferred units. Those groups now report their own units with + a conversion factor of 1, and the others are converted (@billdenney). + * `pk.nca()` with units that differ between groups (for example, a subject without a dose, or a unit column) no longer stops with "Can't join ... due to incompatible types" when a group column is integer, numeric, or a date: diff --git a/R/unit-support.R b/R/unit-support.R index cc9c94cf..64eb7d75 100644 --- a/R/unit-support.R +++ b/R/unit-support.R @@ -295,10 +295,8 @@ pknca_units_table.PKNCAdata <- function(concu, ..., conversions = data.frame()) concu = groups_units_tbl[[concu_col]][i], amountu = groups_units_tbl[[amountu_col]][i], timeu = groups_units_tbl[[timeu_col]][i], - # Note: $units$concu_pref (and amountu_pref, timeu_pref) are only non-NULL - # when the original units were specified as scalar values rather than as a - # column name. When units are column-based, preferred unit conversion is - # not supported through the _pref scalar mechanism and these will be NULL. + # Preferred units are always one value (pknca_set_units()), so every unit + # set, including one from unit columns, converts to the same units. concu_pref = o_conc$units$concu_pref[1], amountu_pref = o_conc$units$amountu_pref[1], timeu_pref = o_conc$units$timeu_pref[1], @@ -306,7 +304,6 @@ pknca_units_table.PKNCAdata <- function(concu, ..., conversions = data.frame()) ) if (has_dose) { pknca_units_tbl_args$doseu <- groups_units_tbl[[doseu_col]][i] - # Same limitation applies to doseu_pref; see concu_pref note above. pknca_units_tbl_args$doseu_pref <- o_dose$units$doseu_pref[1] } pknca_units_tbl_i <- do.call(pknca_units_table, pknca_units_tbl_args) @@ -315,12 +312,23 @@ pknca_units_table.PKNCAdata <- function(concu, ..., conversions = data.frame()) pknca_units_tbl_i <- pknca_units_tbl_i[!is.na(pknca_units_tbl_i$PPORRESU), ] } unit_tables[[i]] <- pknca_units_tbl_i + } + # A unit set whose units are already the preferred ones has no conversions, + # so its table lacks the standardization columns that another set's table + # has. As in pknca_units_table(), its units are the ones to report, with a + # factor of 1. + has_std <- vapply(X = unit_tables, FUN = function(x) "PPSTRESU" %in% names(x), FUN.VALUE = TRUE) + for (i in which(!has_std & any(has_std))) { + unit_tables[[i]]$PPSTRESU <- unit_tables[[i]]$PPORRESU + unit_tables[[i]]$conversion_factor <- rep(1, nrow(unit_tables[[i]])) + } + for (i in seq_along(unit_tables)) { if (length(groups_cols) > 0) { groups_values <- groups_units_tbl[i, groups_cols, drop = FALSE] row.names(groups_values) <- NULL - ret[[i]] <- cbind(groups_values, pknca_units_tbl_i) + ret[[i]] <- cbind(groups_values, unit_tables[[i]]) } else { - ret[[i]] <- pknca_units_tbl_i + ret[[i]] <- unit_tables[[i]] } } diff --git a/tests/testthat/test-unit-support.R b/tests/testthat/test-unit-support.R index 1a2fcc44..ae9a0383 100644 --- a/tests/testthat/test-unit-support.R +++ b/tests/testthat/test-unit-support.R @@ -646,3 +646,42 @@ test_that("left_join_by_value matches keys of different types by value", { data.frame(g = c(2L, 2L), v = c(1L, 1L), w = c("a", "b")) ) }) + +test_that("Preferred units with unit columns convert the groups that need it and keep the others", { + skip_if_not_installed("units") + d_conc <- + data.frame( + analyte = rep(c("parent", "metabolite"), each = 3), subject = 1, + time = rep(c(0, 12, 24), 2), conc = c(4, 10, 2, 8000, 20000, 4000), + cu = rep(c("mg/L", "ng/mL"), each = 3), tu = "hr" + ) + o_conc <- PKNCAconc(d_conc, conc~time|analyte + subject, concu = "cu", timeu = "tu", concu_pref = "mg/L") + o_data <- PKNCAdata(o_conc, intervals = data.frame(start = 0, end = 24, cmax = TRUE)) + units_cmax <- o_data$units[o_data$units$PPTESTCD == "cmax", ] + expect_equal(units_cmax$analyte, c("parent", "metabolite")) + expect_equal(units_cmax$PPORRESU, c("mg/L", "ng/mL")) + # The parent is already in the preferred units + expect_equal(units_cmax$PPSTRESU, c("mg/L", "mg/L")) + expect_equal(units_cmax$conversion_factor, c(1, 0.001)) + df_nca <- as.data.frame(pk.nca(o_data)) + expect_equal(df_nca$analyte, c("metabolite", "parent")) + expect_equal(df_nca$PPORRES, c(20000, 10)) + expect_equal(df_nca$PPSTRES, c(20, 10)) + expect_equal(df_nca$PPSTRESU, c("mg/L", "mg/L")) + + # The same with a dose unit column and preferred dose units + d_conc_dose <- data.frame(treatment = rep(c("A", "B"), each = 3), time = rep(c(0, 12, 24), 2), conc = c(4, 10, 2, 4, 10, 2)) + d_dose <- data.frame(treatment = c("A", "B"), time = 0, dose = c(1, 1000), du = c("mg", "ug")) + o_data_dose <- + PKNCAdata( + PKNCAconc(d_conc_dose, conc~time|treatment, concu = "mg/L", timeu = "hr"), + PKNCAdose(d_dose, dose~time|treatment, doseu = "du", doseu_pref = "mg"), + intervals = data.frame(start = 0, end = 24, cmax.dn = TRUE) + ) + units_cmax_dn <- o_data_dose$units[o_data_dose$units$PPTESTCD == "cmax.dn" & is.na(o_data_dose$units$treatment_ref), ] + expect_equal(units_cmax_dn$PPORRESU, c("(mg/L)/mg", "(mg/L)/ug")) + expect_equal(units_cmax_dn$PPSTRESU, c("(mg/L)/mg", "(mg/L)/mg")) + expect_equal(units_cmax_dn$conversion_factor, c(1, 1000)) + df_nca_dose <- as.data.frame(pk.nca(o_data_dose)) + expect_equal(df_nca_dose$PPSTRES[df_nca_dose$PPTESTCD == "cmax.dn"], c(10, 10)) +}) From ec83512ad5587b41ecc41216385e10f2346ff848 Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Mon, 9 Nov 2026 18:58:46 +0000 Subject: [PATCH 4/9] fix: calculate dose-normalized renal clearance clr.last.dn, clr.obs.dn, and clr.pred.dn were registered like the other dose-normalized parameters, as pk.calc.dn(parameter, dose). They are secondary (they depend on a renal clearance), and secondary parameters are calculated from NCA parameter values only, so the dose source argument stopped every request with "has arguments not covered by formalsmap (dose)". The dose-normalized form of a secondary parameter is now a secondary parameter of its own: the base parameter's inputs and reference, with the dose from totdose (as bioavailability takes it), calculated by pk.calc.clr.dn(). Explicit links, the parameter's own _ref column, and automatic references work, and its units are composed from the urine and plasma groups like renal clearance. The classification of the three parameters is unchanged. New tests calculate every registered secondary parameter through a linked reference interval and check every dose-normalized parameter's registration against its base parameter. Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 9 ++ R/parameter-classification.R | 2 +- R/unit-support.R | 1 + R/zzz-pk.calc.dn.R | 47 +++++- .../testthat/test-parameter-classification.R | 14 +- tests/testthat/test-secondary-parameters.R | 135 ++++++++++++++++++ 6 files changed, 201 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 86e2ea5c..b2112fc1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1117,6 +1117,15 @@ the generated intervals: ## Bug fixes +* The dose-normalized renal clearances (`clr.last.dn`, `clr.obs.dn`, and + `clr.pred.dn`) can now be calculated. Before, every request stopped with + "The secondary parameter 'clr.last.dn' has arguments not covered by + formalsmap (dose)". Like renal clearance, they are secondary parameters: + they take the amount excreted from the urine interval, the AUC from a + reference plasma interval (given with `clr.last.dn_ref` or found + automatically), and the dose from the urine interval's `totdose`, and their + units are composed from both groups' units (@billdenney). + * Preferred units (`concu_pref`, `doseu_pref`, and the others) with unit columns whose units differ between groups no longer stop `PKNCAdata()` with "arguments imply differing number of rows: 1, 0" when some groups are diff --git a/R/parameter-classification.R b/R/parameter-classification.R index 5b4d2440..97ce00cc 100644 --- a/R/parameter-classification.R +++ b/R/parameter-classification.R @@ -238,7 +238,7 @@ parameter_classification <- function() { dose_normalized = vapply( all_intervals, - function(x) identical(x$FUN, "pk.calc.dn"), + function(x) isTRUE(x$FUN %in% c("pk.calc.dn", pknca_secondary_dn_fun)), TRUE ) ) diff --git a/R/unit-support.R b/R/unit-support.R index 64eb7d75..908c832d 100644 --- a/R/unit-support.R +++ b/R/unit-support.R @@ -355,6 +355,7 @@ secondary_unit_sides <- function(param) { switch( get.interval.cols()[[param]]$FUN, pk.calc.clr = list(own = "ae", ref = "auc"), + pk.calc.clr.dn = list(own = c("ae", "dose"), ref = "auc"), pk.calc.ratio = list(own = "test", ref = "reference"), pk.calc.f = list(own = c("auc2", "dose2"), ref = c("auc1", "dose1")), NULL diff --git a/R/zzz-pk.calc.dn.R b/R/zzz-pk.calc.dn.R index 921b255f..4ab0220f 100644 --- a/R/zzz-pk.calc.dn.R +++ b/R/zzz-pk.calc.dn.R @@ -10,6 +10,29 @@ pk.calc.dn <- function(parameter, dose) { parameter/dose } +#' Dose-normalized renal clearance +#' +#' Renal clearance is a secondary parameter: the amount excreted comes from +#' the urine interval and the AUC from a plasma interval. Secondary +#' parameters are calculated from NCA parameter values only, so the +#' dose-normalized form takes everything renal clearance does plus the +#' interval's total dose (`totdose`), and is linked to its plasma interval +#' the same way. +#' +#' @inheritParams pk.calc.clr +#' @inheritParams pk.calc.dn +#' @returns The renal clearance divided by the dose +#' @keywords Internal +#' @noRd +pk.calc.clr.dn <- function(ae, auc, dose) { + pk.calc.dn(parameter = pk.calc.clr(ae = ae, auc = auc), dose = dose) +} +pknca_concept(pk.calc.clr.dn) <- "renal_clearance" + +# The calculation function for the dose-normalized form of each secondary +# parameter that has one, by the calculation function of the parameter +pknca_secondary_dn_fun <- c(pk.calc.clr = "pk.calc.clr.dn") + local({ for (n in c("auclast", "aucall", "aucinf.obs", "aucinf.pred", "aumclast", "aumcall", "aumcinf.obs", "aumcinf.pred", @@ -53,19 +76,35 @@ local({ } dn_formula <- paste0("$", lhs_dn, " = \\frac{", lhs, "}{Dose}$") } + current_spec <- get.interval.cols()[[n]] + if (isTRUE(current_spec$selection$secondary)) { + # A secondary parameter is calculated from NCA parameter values only, so + # its dose-normalized form is a secondary parameter of its own with the + # same inputs and reference, and with the dose from `totdose`. + current_fun <- pknca_secondary_dn_fun[[current_spec$FUN]] + current_formalsmap <- c(current_spec$formalsmap, list(dose="totdose")) + current_depends <- c(current_spec$depends, "totdose") + current_selection <- current_spec$selection + } else { + current_fun <- "pk.calc.dn" + current_formalsmap <- list(parameter=n) + current_depends <- n + current_selection <- NULL + } # Add the column to the interval specification add.interval.col( name=paste(n, "dn", sep="."), - FUN="pk.calc.dn", + FUN=current_fun, values=c(FALSE, TRUE), unit_type=paste0(current_unit_type, "_dose"), pretty_name=paste(current_pretty_name, "(dose-normalized)"), desc=paste("Dose-normalized", n), - formalsmap=list(parameter=n), - depends=c(n), + formalsmap=current_formalsmap, + depends=current_depends, formula=dn_formula, pptestcd_cdisc=dn_pptestcd, - pptest_cdisc=dn_pptest + pptest_cdisc=dn_pptest, + selection=current_selection ) PKNCA.set.summary( name=paste(n, "dn", sep="."), diff --git a/tests/testthat/test-parameter-classification.R b/tests/testthat/test-parameter-classification.R index 547fd990..f288bbd3 100644 --- a/tests/testthat/test-parameter-classification.R +++ b/tests/testthat/test-parameter-classification.R @@ -167,14 +167,24 @@ test_that("the multiple-dose family is the closure of the declared seeds", { ) }) -test_that("dose_normalized marks exactly the pk.calc.dn parameters", { +test_that("dose_normalized marks exactly the dose-normalized parameters", { tbl <- pknca_parameter_table() all_intervals <- get.interval.cols() from_dn <- names(all_intervals)[ vapply(all_intervals, function(x) identical(x$FUN, "pk.calc.dn"), TRUE) ] - expect_equal(sort(tbl$parameter[tbl$dose_normalized]), sort(from_dn)) + # Dose-normalized renal clearance is a secondary parameter with a calculation + # function of its own + expect_equal( + sort(tbl$parameter[tbl$dose_normalized]), + sort(c(from_dn, "clr.last.dn", "clr.obs.dn", "clr.pred.dn")) + ) + # ... and every parameter named as dose-normalized is marked + expect_equal( + sort(tbl$parameter[tbl$dose_normalized]), + sort(grep("\\.dn$", names(all_intervals), value = TRUE)) + ) }) test_that("a dose-normalized parameter takes the concept of its base parameter", { diff --git a/tests/testthat/test-secondary-parameters.R b/tests/testthat/test-secondary-parameters.R index f7e7d598..037a1f5e 100644 --- a/tests/testthat/test-secondary-parameters.R +++ b/tests/testthat/test-secondary-parameters.R @@ -3322,3 +3322,138 @@ test_that("an empty data-level ref_vary is the same as none", { printed <- utils::capture.output(print(o_data)) expect_false(any(grepl(pattern = "ref_vary", x = printed, fixed = TRUE))) }) + +# A subject with two plasma profiles (treatments A and B, with different doses) +# and a urine collection after treatment B, so that every secondary parameter +# has a reference to link to: renal clearance takes the urine collection's +# amount and a plasma AUC, and bioavailability and the ratios compare the two +# plasma profiles. +d_conc_all_sec <- + data.frame( + subject = 1, + treatment = c(rep("A", 7), rep(c("B", "B"), times = c(7, 3))), + PCSPEC = c(rep("plasma", 14), rep("urine", 3)), + time = c(rep(c(0, 1, 2, 4, 8, 12, 24), 2), 0, 12, 24), + conc = c(0, 8, 10, 7, 4, 2.5, 0.8, 0, 12, 15, 10, 6, 3.5, 1.2, 0, 2, 1), + vol = c(rep(NA, 14), 0, 100, 150) + ) +d_dose_all_sec <- data.frame(subject = 1, treatment = c("A", "B"), time = 0, dose = c(50, 100)) +o_conc_all_sec <- PKNCAconc(d_conc_all_sec, conc~time|treatment+PCSPEC+subject, volume = "vol") +o_dose_all_sec <- PKNCAdose(d_dose_all_sec, dose~time|treatment+subject) + +test_that("every secondary parameter is calculated when linked to a reference interval", { + for (current_param in secondary_parameter_names()) { + info <- secondary_param_info(current_param) + own_spec <- + if (pknca_parameter_table(current_param)$sample_type %in% "interval") "urine" else "plasma" + intervals <- data.frame(treatment = c("A", "B"), PCSPEC = c("plasma", own_spec), start = 0, end = 24) + intervals$interval_id <- c("ref", NA) + intervals[[current_param]] <- c(FALSE, TRUE) + intervals[[paste0(current_param, "_ref")]] <- c(NA, "ref") + # The warnings are for the groups this parameter does not use and for the + # reference row, which requests nothing until it is linked + d_res <- + suppressWarnings(as.data.frame(pk.nca(PKNCAdata(o_conc_all_sec, o_dose_all_sec, intervals = intervals)))) + value <- d_res$PPORRES[d_res$PPTESTCD == current_param] + expect_length(value, 1) + expect_true(is.finite(value), label = current_param) + # The value is the calculation function applied to the parameter values of + # the requesting and the reference intervals + own_values <- d_res[d_res$treatment == "B" & d_res$PCSPEC == own_spec, ] + ref_values <- d_res[d_res$treatment == "A" & d_res$PCSPEC == "plasma", ] + args <- + c( + lapply(info$own_args, function(x) own_values$PPORRES[own_values$PPTESTCD == x]), + lapply(info$ref_args, function(x) ref_values$PPORRES[ref_values$PPTESTCD == x]) + ) + expect_equal(value, do.call(info$fun, args), label = current_param) + } +}) + +test_that("dose-normalized renal clearance is renal clearance divided by the dose", { + for (current_base in c("clr.last", "clr.obs", "clr.pred")) { + current_dn <- paste0(current_base, ".dn") + intervals <- + data.frame( + treatment = "B", PCSPEC = c("plasma", "urine"), start = 0, end = 24, + interval_id = c("p", NA) + ) + intervals[[current_base]] <- c(FALSE, TRUE) + intervals[[paste0(current_base, "_ref")]] <- c(NA, "p") + intervals[[current_dn]] <- c(FALSE, TRUE) + # The dose-normalized parameter names its own reference + intervals[[paste0(current_dn, "_ref")]] <- c(NA, "p") + # The warnings are for the treatment A profile, which is not used, and for + # the reference row, which requests nothing until it is linked + d_res <- + suppressWarnings(as.data.frame(pk.nca(PKNCAdata(o_conc_all_sec, o_dose_all_sec, intervals = intervals)))) + value_base <- d_res$PPORRES[d_res$PPTESTCD == current_base] + expect_true(is.finite(value_base)) + expect_equal(d_res$PPORRES[d_res$PPTESTCD == current_dn], value_base / 100) + expect_equal(d_res$PCSPEC_ref[d_res$PPTESTCD == current_dn], "plasma") + } + # Requested alone, the reference is found automatically + o_data_auto <- + PKNCAdata( + o_conc_all_sec, o_dose_all_sec, + intervals = data.frame(treatment = "B", PCSPEC = "urine", start = 0, end = 24, clr.last.dn = TRUE) + ) + expect_message( + d_res <- suppressWarnings(as.data.frame(pk.nca(o_data_auto))), + class = "pknca_message_secondary_ref_created" + ) + # ae = 2*100 + 1*150 = 350, over the auclast of the plasma profile of + # treatment B, over its dose of 100 + o_data_auclast <- + PKNCAdata( + o_conc_all_sec, o_dose_all_sec, + intervals = data.frame(treatment = "B", PCSPEC = "plasma", start = 0, end = 24, auclast = TRUE) + ) + auclast_b <- suppressWarnings(as.data.frame(pk.nca(o_data_auclast)))$PPORRES + expect_equal(d_res$PPORRES[d_res$PPTESTCD == "clr.last.dn"], 350 / auclast_b / 100) +}) + +test_that("every dose-normalized parameter is registered consistently with its base", { + all_intervals <- get.interval.cols() + tbl <- pknca_parameter_table() + dn_params <- tbl$parameter[tbl$dose_normalized] + expect_true(length(dn_params) > 0) + for (current_dn in dn_params) { + base <- sub("\\.dn$", "", current_dn) + expect_true(base %in% names(all_intervals), label = current_dn) + spec <- all_intervals[[current_dn]] + base_secondary <- isTRUE(all_intervals[[base]]$selection$secondary) + if (base_secondary) { + # The same inputs and reference as the base, and the dose from totdose + expect_equal(spec$formalsmap, c(all_intervals[[base]]$formalsmap, list(dose = "totdose")), label = current_dn) + expect_true(isTRUE(spec$selection$secondary), label = current_dn) + } else { + expect_equal(spec$FUN, "pk.calc.dn", label = current_dn) + expect_equal(spec$formalsmap, list(parameter = base), label = current_dn) + } + } + # Every secondary parameter takes only NCA parameter values as inputs + for (current_param in secondary_parameter_names()) { + expect_no_error(secondary_param_info(current_param)) + } +}) + +test_that("dose-normalized renal clearance takes the AUC units of its reference group", { + d_conc_u <- d_conc_sec + d_conc_u$conc_units <- ifelse(d_conc_u$PCSPEC %in% "plasma", "ng/mL", "mg/L") + o_conc_u <- + PKNCAconc( + d_conc_u, conc~time|PCSPEC+subject, volume = "vol", + concu = "conc_units", timeu = "hr", amountu = "mg" + ) + o_dose_u <- PKNCAdose(data.frame(subject = 1, time = 0, dose = 50), dose~time|subject, doseu = "mg") + intervals <- intervals_sec + intervals$clr.last.dn <- c(FALSE, TRUE) + intervals$clr.last.dn_ref <- c(NA, "plasma024") + d_res <- + as.data.frame(pk.nca( + PKNCAdata(o_conc_u, o_dose_u, intervals = intervals, options = list(auc.method = "linear")) + )) + expect_equal(d_res$PPORRES[d_res$PPTESTCD %in% "clr.last.dn"], 350/144/50) + expect_equal(d_res$PPORRESU[d_res$PPTESTCD %in% "clr.last.dn"], "(mg/mg)/(hr*ng/mL)") +}) From 6abd600788b44f44d9128c9d572755ec796481f7 Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Mon, 9 Nov 2026 19:03:13 +0000 Subject: [PATCH 5/9] docs: separate the news bullets Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 1a6fc0a1..8660f7e1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1403,6 +1403,7 @@ the generated intervals: row of its own group (its own subject), ignoring the group columns named in `reference` or `ref_vary`. The helper also no longer warns about the unknown `interval_id` column of tibble intervals. (@billdenney) + * `PKNCAdata(impute = NA)` and `PKNCAdata(impute = NULL)` now mean the same as the default: the `"impute"` column of the intervals when there is one, else no imputation. Before, `impute = NA` stored no imputation setting and From df40d66464395f300e2704cd80ad94993d9f86ac Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Mon, 9 Nov 2026 19:42:47 +0000 Subject: [PATCH 6/9] docs: code-format the parameter name in the news Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index ec4b015a..e3f1249c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1118,9 +1118,9 @@ the generated intervals: ## Bug fixes * The dose-normalized renal clearances (`clr.last.dn`, `clr.obs.dn`, and - `clr.pred.dn`) can now be calculated. Before, every request stopped with - "The secondary parameter 'clr.last.dn' has arguments not covered by - formalsmap (dose)". Like renal clearance, they are secondary parameters: + `clr.pred.dn`) can now be calculated. Before, every request stopped with an + error that the secondary parameter (such as `clr.last.dn`) "has arguments + not covered by formalsmap (dose)". Like renal clearance, they are secondary parameters: they take the amount excreted from the urine interval, the AUC from a reference plasma interval (given with `clr.last.dn_ref` or found automatically), and the dose from the urine interval's `totdose`, and their From 6dd5dde82f140309321b4f70b4958887aa2ac78e Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Tue, 10 Nov 2026 02:32:08 +0000 Subject: [PATCH 7/9] test: include the renamed and deprecated renal clearance dose-normalized names The dose-normalized renal clearance is now clr.inf.obs.dn and clr.inf.pred.dn, with clr.obs.dn and clr.pred.dn as their deprecated names; a deprecated dose-normalized name is checked against the base of its replacement. Co-Authored-By: Claude Opus 5.5 --- tests/testthat/test-parameter-classification.R | 6 +++++- tests/testthat/test-secondary-parameters.R | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-parameter-classification.R b/tests/testthat/test-parameter-classification.R index 656f18fa..b25f6a26 100644 --- a/tests/testthat/test-parameter-classification.R +++ b/tests/testthat/test-parameter-classification.R @@ -178,7 +178,11 @@ test_that("dose_normalized marks exactly the dose-normalized parameters", { # function of its own expect_equal( sort(tbl$parameter[tbl$dose_normalized]), - sort(c(from_dn, "clr.last.dn", "clr.obs.dn", "clr.pred.dn")) + sort(c( + from_dn, "clr.last.dn", "clr.inf.obs.dn", "clr.inf.pred.dn", + # The deprecated names of the last two + "clr.obs.dn", "clr.pred.dn" + )) ) # ... and every parameter named as dose-normalized is marked expect_equal( diff --git a/tests/testthat/test-secondary-parameters.R b/tests/testthat/test-secondary-parameters.R index 520fcc4e..cd0c8d4d 100644 --- a/tests/testthat/test-secondary-parameters.R +++ b/tests/testthat/test-secondary-parameters.R @@ -3432,7 +3432,9 @@ test_that("every dose-normalized parameter is registered consistently with its b dn_params <- tbl$parameter[tbl$dose_normalized] expect_true(length(dn_params) > 0) for (current_dn in dn_params) { - base <- sub("\\.dn$", "", current_dn) + # A deprecated name is registered as its replacement is + replacement <- unname(deprecated_renamed_parameters[current_dn]) + base <- sub("\\.dn$", "", if (is.na(replacement)) current_dn else replacement) expect_true(base %in% names(all_intervals), label = current_dn) spec <- all_intervals[[current_dn]] base_secondary <- isTRUE(all_intervals[[base]]$selection$secondary) From 949576c0b113022357cf854d7c1ff3e6e7eb9a50 Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Tue, 10 Nov 2026 02:34:16 +0000 Subject: [PATCH 8/9] docs: use the renamed renal clearance names in the news and the test Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 4 ++-- tests/testthat/test-secondary-parameters.R | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index fe2f8958..f996ebb6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1214,8 +1214,8 @@ the generated intervals: ## Bug fixes -* The dose-normalized renal clearances (`clr.last.dn`, `clr.obs.dn`, and - `clr.pred.dn`) can now be calculated. Before, every request stopped with an +* The dose-normalized renal clearances (`clr.last.dn`, `clr.inf.obs.dn`, and + `clr.inf.pred.dn`) can now be calculated. Before, every request stopped with an error that the secondary parameter (such as `clr.last.dn`) "has arguments not covered by formalsmap (dose)". Like renal clearance, they are secondary parameters: they take the amount excreted from the urine interval, the AUC from a diff --git a/tests/testthat/test-secondary-parameters.R b/tests/testthat/test-secondary-parameters.R index cd0c8d4d..4cf45b8a 100644 --- a/tests/testthat/test-secondary-parameters.R +++ b/tests/testthat/test-secondary-parameters.R @@ -3384,7 +3384,7 @@ test_that("every secondary parameter is calculated when linked to a reference in }) test_that("dose-normalized renal clearance is renal clearance divided by the dose", { - for (current_base in c("clr.last", "clr.obs", "clr.pred")) { + for (current_base in c("clr.last", "clr.inf.obs", "clr.inf.pred")) { current_dn <- paste0(current_base, ".dn") intervals <- data.frame( From 2ac4b8a9acfe58d2288ef1393a415d278e51f05c Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Tue, 10 Nov 2026 02:56:22 +0000 Subject: [PATCH 9/9] revert: drop the calculation of dose-normalized renal clearance Renal clearance divided by the dose is not a parameter used in the pharmacokinetic literature (CDISC has a code for it, RENALCLD, but no publication reports it), so this stack does not make clr.*.dn calculable. This reverts the changes of fix/clr-dn-formalsmap, which leaves the stack. Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 9 -- R/parameter-classification.R | 2 +- R/unit-support.R | 1 - R/zzz-pk.calc.dn.R | 47 +----- .../testthat/test-parameter-classification.R | 18 +-- tests/testthat/test-secondary-parameters.R | 137 ------------------ 6 files changed, 7 insertions(+), 207 deletions(-) diff --git a/NEWS.md b/NEWS.md index e0b3057a..2f8f7a74 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1214,15 +1214,6 @@ the generated intervals: ## Bug fixes -* The dose-normalized renal clearances (`clr.last.dn`, `clr.inf.obs.dn`, and - `clr.inf.pred.dn`) can now be calculated. Before, every request stopped with an - error that the secondary parameter (such as `clr.last.dn`) "has arguments - not covered by formalsmap (dose)". Like renal clearance, they are secondary parameters: - they take the amount excreted from the urine interval, the AUC from a - reference plasma interval (given with `clr.last.dn_ref` or found - automatically), and the dose from the urine interval's `totdose`, and their - units are composed from both groups' units (@billdenney). - * Preferred units (`concu_pref`, `doseu_pref`, and the others) with unit columns whose units differ between groups no longer stop `PKNCAdata()` with "arguments imply differing number of rows: 1, 0" when some groups are diff --git a/R/parameter-classification.R b/R/parameter-classification.R index e8d84c5c..9aa8afa3 100644 --- a/R/parameter-classification.R +++ b/R/parameter-classification.R @@ -248,7 +248,7 @@ parameter_classification <- function() { dose_normalized = vapply( all_intervals, - function(x) isTRUE(x$FUN %in% c("pk.calc.dn", pknca_secondary_dn_fun)), + function(x) identical(x$FUN, "pk.calc.dn"), TRUE ) ) diff --git a/R/unit-support.R b/R/unit-support.R index 908c832d..64eb7d75 100644 --- a/R/unit-support.R +++ b/R/unit-support.R @@ -355,7 +355,6 @@ secondary_unit_sides <- function(param) { switch( get.interval.cols()[[param]]$FUN, pk.calc.clr = list(own = "ae", ref = "auc"), - pk.calc.clr.dn = list(own = c("ae", "dose"), ref = "auc"), pk.calc.ratio = list(own = "test", ref = "reference"), pk.calc.f = list(own = c("auc2", "dose2"), ref = c("auc1", "dose1")), NULL diff --git a/R/zzz-pk.calc.dn.R b/R/zzz-pk.calc.dn.R index 3e96a420..e4d7ba54 100644 --- a/R/zzz-pk.calc.dn.R +++ b/R/zzz-pk.calc.dn.R @@ -10,29 +10,6 @@ pk.calc.dn <- function(parameter, dose) { parameter/dose } -#' Dose-normalized renal clearance -#' -#' Renal clearance is a secondary parameter: the amount excreted comes from -#' the urine interval and the AUC from a plasma interval. Secondary -#' parameters are calculated from NCA parameter values only, so the -#' dose-normalized form takes everything renal clearance does plus the -#' interval's total dose (`totdose`), and is linked to its plasma interval -#' the same way. -#' -#' @inheritParams pk.calc.clr -#' @inheritParams pk.calc.dn -#' @returns The renal clearance divided by the dose -#' @keywords Internal -#' @noRd -pk.calc.clr.dn <- function(ae, auc, dose) { - pk.calc.dn(parameter = pk.calc.clr(ae = ae, auc = auc), dose = dose) -} -pknca_concept(pk.calc.clr.dn) <- "renal_clearance" - -# The calculation function for the dose-normalized form of each secondary -# parameter that has one, by the calculation function of the parameter -pknca_secondary_dn_fun <- c(pk.calc.clr = "pk.calc.clr.dn") - local({ for (n in c("auclast", "aucall", "aucinf.obs", "aucinf.pred", "aumclast", "aumcall", "aumcinf.obs", "aumcinf.pred", @@ -76,35 +53,19 @@ local({ } dn_formula <- paste0("$", lhs_dn, " = \\frac{", lhs, "}{Dose}$") } - current_spec <- get.interval.cols()[[n]] - if (isTRUE(current_spec$selection$secondary)) { - # A secondary parameter is calculated from NCA parameter values only, so - # its dose-normalized form is a secondary parameter of its own with the - # same inputs and reference, and with the dose from `totdose`. - current_fun <- pknca_secondary_dn_fun[[current_spec$FUN]] - current_formalsmap <- c(current_spec$formalsmap, list(dose="totdose")) - current_depends <- c(current_spec$depends, "totdose") - current_selection <- current_spec$selection - } else { - current_fun <- "pk.calc.dn" - current_formalsmap <- list(parameter=n) - current_depends <- n - current_selection <- NULL - } # Add the column to the interval specification add.interval.col( name=paste(n, "dn", sep="."), - FUN=current_fun, + FUN="pk.calc.dn", values=c(FALSE, TRUE), unit_type=paste0(current_unit_type, "_dose"), pretty_name=paste(current_pretty_name, "(dose-normalized)"), desc=paste("Dose-normalized", n), - formalsmap=current_formalsmap, - depends=current_depends, + formalsmap=list(parameter=n), + depends=c(n), formula=dn_formula, pptestcd_cdisc=dn_pptestcd, - pptest_cdisc=dn_pptest, - selection=current_selection + pptest_cdisc=dn_pptest ) PKNCA.set.summary( name=paste(n, "dn", sep="."), diff --git a/tests/testthat/test-parameter-classification.R b/tests/testthat/test-parameter-classification.R index b25f6a26..37eb9052 100644 --- a/tests/testthat/test-parameter-classification.R +++ b/tests/testthat/test-parameter-classification.R @@ -167,28 +167,14 @@ test_that("the multiple-dose family is the closure of the declared seeds", { ) }) -test_that("dose_normalized marks exactly the dose-normalized parameters", { +test_that("dose_normalized marks exactly the pk.calc.dn parameters", { tbl <- pknca_parameter_table() all_intervals <- get.interval.cols() from_dn <- names(all_intervals)[ vapply(all_intervals, function(x) identical(x$FUN, "pk.calc.dn"), TRUE) ] - # Dose-normalized renal clearance is a secondary parameter with a calculation - # function of its own - expect_equal( - sort(tbl$parameter[tbl$dose_normalized]), - sort(c( - from_dn, "clr.last.dn", "clr.inf.obs.dn", "clr.inf.pred.dn", - # The deprecated names of the last two - "clr.obs.dn", "clr.pred.dn" - )) - ) - # ... and every parameter named as dose-normalized is marked - expect_equal( - sort(tbl$parameter[tbl$dose_normalized]), - sort(grep("\\.dn$", names(all_intervals), value = TRUE)) - ) + expect_equal(sort(tbl$parameter[tbl$dose_normalized]), sort(from_dn)) }) test_that("a dose-normalized parameter takes the concept of its base parameter", { diff --git a/tests/testthat/test-secondary-parameters.R b/tests/testthat/test-secondary-parameters.R index c6721ce6..86e51b26 100644 --- a/tests/testthat/test-secondary-parameters.R +++ b/tests/testthat/test-secondary-parameters.R @@ -3415,140 +3415,3 @@ test_that("an empty data-level ref_vary is the same as none", { printed <- utils::capture.output(print(o_data)) expect_false(any(grepl(pattern = "ref_vary", x = printed, fixed = TRUE))) }) - -# A subject with two plasma profiles (treatments A and B, with different doses) -# and a urine collection after treatment B, so that every secondary parameter -# has a reference to link to: renal clearance takes the urine collection's -# amount and a plasma AUC, and bioavailability and the ratios compare the two -# plasma profiles. -d_conc_all_sec <- - data.frame( - subject = 1, - treatment = c(rep("A", 7), rep(c("B", "B"), times = c(7, 3))), - PCSPEC = c(rep("plasma", 14), rep("urine", 3)), - time = c(rep(c(0, 1, 2, 4, 8, 12, 24), 2), 0, 12, 24), - conc = c(0, 8, 10, 7, 4, 2.5, 0.8, 0, 12, 15, 10, 6, 3.5, 1.2, 0, 2, 1), - vol = c(rep(NA, 14), 0, 100, 150) - ) -d_dose_all_sec <- data.frame(subject = 1, treatment = c("A", "B"), time = 0, dose = c(50, 100)) -o_conc_all_sec <- PKNCAconc(d_conc_all_sec, conc~time|treatment+PCSPEC+subject, volume = "vol") -o_dose_all_sec <- PKNCAdose(d_dose_all_sec, dose~time|treatment+subject) - -test_that("every secondary parameter is calculated when linked to a reference interval", { - for (current_param in secondary_parameter_names()) { - info <- secondary_param_info(current_param) - own_spec <- - if (pknca_parameter_table(current_param)$sample_type %in% "interval") "urine" else "plasma" - intervals <- data.frame(treatment = c("A", "B"), PCSPEC = c("plasma", own_spec), start = 0, end = 24) - intervals$interval_id <- c("ref", NA) - intervals[[current_param]] <- c(FALSE, TRUE) - intervals[[paste0(current_param, "_ref")]] <- c(NA, "ref") - # The warnings are for the groups this parameter does not use and for the - # reference row, which requests nothing until it is linked - d_res <- - suppressWarnings(as.data.frame(pk.nca(PKNCAdata(o_conc_all_sec, o_dose_all_sec, intervals = intervals)))) - value <- d_res$PPORRES[d_res$PPTESTCD == current_param] - expect_length(value, 1) - expect_true(is.finite(value), label = current_param) - # The value is the calculation function applied to the parameter values of - # the requesting and the reference intervals - own_values <- d_res[d_res$treatment == "B" & d_res$PCSPEC == own_spec, ] - ref_values <- d_res[d_res$treatment == "A" & d_res$PCSPEC == "plasma", ] - args <- - c( - lapply(info$own_args, function(x) own_values$PPORRES[own_values$PPTESTCD == x]), - lapply(info$ref_args, function(x) ref_values$PPORRES[ref_values$PPTESTCD == x]) - ) - expect_equal(value, do.call(info$fun, args), label = current_param) - } -}) - -test_that("dose-normalized renal clearance is renal clearance divided by the dose", { - for (current_base in c("clr.last", "clr.inf.obs", "clr.inf.pred")) { - current_dn <- paste0(current_base, ".dn") - intervals <- - data.frame( - treatment = "B", PCSPEC = c("plasma", "urine"), start = 0, end = 24, - interval_id = c("p", NA) - ) - intervals[[current_base]] <- c(FALSE, TRUE) - intervals[[paste0(current_base, "_ref")]] <- c(NA, "p") - intervals[[current_dn]] <- c(FALSE, TRUE) - # The dose-normalized parameter names its own reference - intervals[[paste0(current_dn, "_ref")]] <- c(NA, "p") - # The warnings are for the treatment A profile, which is not used, and for - # the reference row, which requests nothing until it is linked - d_res <- - suppressWarnings(as.data.frame(pk.nca(PKNCAdata(o_conc_all_sec, o_dose_all_sec, intervals = intervals)))) - value_base <- d_res$PPORRES[d_res$PPTESTCD == current_base] - expect_true(is.finite(value_base)) - expect_equal(d_res$PPORRES[d_res$PPTESTCD == current_dn], value_base / 100) - expect_equal(d_res$PCSPEC_ref[d_res$PPTESTCD == current_dn], "plasma") - } - # Requested alone, the reference is found automatically - o_data_auto <- - PKNCAdata( - o_conc_all_sec, o_dose_all_sec, - intervals = data.frame(treatment = "B", PCSPEC = "urine", start = 0, end = 24, clr.last.dn = TRUE) - ) - expect_message( - d_res <- suppressWarnings(as.data.frame(pk.nca(o_data_auto))), - class = "pknca_message_secondary_ref_created" - ) - # ae = 2*100 + 1*150 = 350, over the auclast of the plasma profile of - # treatment B, over its dose of 100 - o_data_auclast <- - PKNCAdata( - o_conc_all_sec, o_dose_all_sec, - intervals = data.frame(treatment = "B", PCSPEC = "plasma", start = 0, end = 24, auclast = TRUE) - ) - auclast_b <- suppressWarnings(as.data.frame(pk.nca(o_data_auclast)))$PPORRES - expect_equal(d_res$PPORRES[d_res$PPTESTCD == "clr.last.dn"], 350 / auclast_b / 100) -}) - -test_that("every dose-normalized parameter is registered consistently with its base", { - all_intervals <- get.interval.cols() - tbl <- pknca_parameter_table() - dn_params <- tbl$parameter[tbl$dose_normalized] - expect_true(length(dn_params) > 0) - for (current_dn in dn_params) { - # A deprecated name is registered as its replacement is - replacement <- unname(deprecated_renamed_parameters[current_dn]) - base <- sub("\\.dn$", "", if (is.na(replacement)) current_dn else replacement) - expect_true(base %in% names(all_intervals), label = current_dn) - spec <- all_intervals[[current_dn]] - base_secondary <- isTRUE(all_intervals[[base]]$selection$secondary) - if (base_secondary) { - # The same inputs and reference as the base, and the dose from totdose - expect_equal(spec$formalsmap, c(all_intervals[[base]]$formalsmap, list(dose = "totdose")), label = current_dn) - expect_true(isTRUE(spec$selection$secondary), label = current_dn) - } else { - expect_equal(spec$FUN, "pk.calc.dn", label = current_dn) - expect_equal(spec$formalsmap, list(parameter = base), label = current_dn) - } - } - # Every secondary parameter takes only NCA parameter values as inputs - for (current_param in secondary_parameter_names()) { - expect_no_error(secondary_param_info(current_param)) - } -}) - -test_that("dose-normalized renal clearance takes the AUC units of its reference group", { - d_conc_u <- d_conc_sec - d_conc_u$conc_units <- ifelse(d_conc_u$PCSPEC %in% "plasma", "ng/mL", "mg/L") - o_conc_u <- - PKNCAconc( - d_conc_u, conc~time|PCSPEC+subject, volume = "vol", - concu = "conc_units", timeu = "hr", amountu = "mg" - ) - o_dose_u <- PKNCAdose(data.frame(subject = 1, time = 0, dose = 50), dose~time|subject, doseu = "mg") - intervals <- intervals_sec - intervals$clr.last.dn <- c(FALSE, TRUE) - intervals$clr.last.dn_ref <- c(NA, "plasma024") - d_res <- - as.data.frame(pk.nca( - PKNCAdata(o_conc_u, o_dose_u, intervals = intervals, options = list(auc.method = "linear")) - )) - expect_equal(d_res$PPORRES[d_res$PPTESTCD %in% "clr.last.dn"], 350/144/50) - expect_equal(d_res$PPORRESU[d_res$PPTESTCD %in% "clr.last.dn"], "(mg/mg)/(hr*ng/mL)") -})