diff --git a/NEWS.md b/NEWS.md index 18b6f894..86073f28 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1505,6 +1505,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) + * `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 diff --git a/R/secondary-parameters.R b/R/secondary-parameters.R index ebfe13f4..29d0800b 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 bc0965e2..7cf2d6c6 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", {