From ba4ff5b45770bfc16a88ccbcd9254a37a2404e55 Mon Sep 17 00:00:00 2001 From: nsjohnsen Date: Fri, 1 Mar 2024 13:04:40 +0100 Subject: [PATCH] Bugfixing when no strata are accepted while preparing for across trt across strata stats --- R/prepare_for_stats.R | 25 ++++++++++++++++--------- tests/testthat/_snaps/targets.md | 1 + 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/R/prepare_for_stats.R b/R/prepare_for_stats.R index 44e171f..d7fa356 100644 --- a/R/prepare_for_stats.R +++ b/R/prepare_for_stats.R @@ -32,6 +32,7 @@ prepare_for_stats <- function(ep, id_col = "strata_id") { type <- match.arg(type) + # Map stat function type to associated criterion variable crit_var <- switch( type, "stat_by_strata_by_trt" = "crit_accept_by_strata_by_trt", @@ -40,13 +41,6 @@ prepare_for_stats <- function(ep, stop("Unknown stat function type") ) - - # Return early if no endpoint rows are accepted by criterion or if no stat functions are suppied - if(nrow(ep[get(crit_var)]) == 0 | - nrow(fn_map[fn_type == type]) == 0){ - return(data.table::data.table(SKIP_=TRUE)) - } - # Set of columns used for slicing the population depending on the type of stat function grouping_cols <- switch( type, @@ -56,12 +50,25 @@ prepare_for_stats <- function(ep, stop("Unknown stat function type") ) + ep_accepted <- ep[get(crit_var) == TRUE] + + # Return early if: + # 1) no endpoint rows are accepted by criterion + # 2) no stat functions are supplied + # 3) no stratum is accepted when preparing for stat_across_strata_across_trt + if(nrow(ep_accepted) == 0 | + nrow(fn_map[fn_type == type]) == 0 | + (type == "stat_across_strata_across_trt" & !any(ep_accepted[[grouping_cols[[1]]]] != "TOTAL_")) + ){ + return(data.table::data.table(SKIP_=TRUE)) + } + if (type %in% c("stat_by_strata_by_trt", "stat_by_strata_across_trt")) { # Expand endpoints by treatment and/or strata ep_expanded <- expand_ep_for_stats( - ep = ep[get(crit_var) == TRUE], + ep = ep_accepted, grouping_cols = grouping_cols, analysis_data_container = analysis_data_container, data_col = data_col, @@ -87,7 +94,7 @@ prepare_for_stats <- function(ep, } else{ ep_fn <- - merge(ep[get(crit_var) == TRUE], + merge(ep_accepted, fn_map[fn_type == type], by = "endpoint_spec_id", allow.cartesian = TRUE) diff --git a/tests/testthat/_snaps/targets.md b/tests/testthat/_snaps/targets.md index 13a9175..d810706 100644 --- a/tests/testthat/_snaps/targets.md +++ b/tests/testthat/_snaps/targets.md @@ -956,3 +956,4 @@ 10: 1-0001-0002-afd3cffa8b1c850f902a32f4d7ac19fe-0010 11: 1-0001-0002-150a10ab5600d4260be332983e69a451-0011 12: 1-0001-0002-afd3cffa8b1c850f902a32f4d7ac19fe-0012 +