Skip to content

Commit

Permalink
Bugfixing when no strata are accepted while preparing for across trt …
Browse files Browse the repository at this point in the history
…across strata stats
  • Loading branch information
nsjohnsen committed Mar 1, 2024
1 parent 7a2a22b commit ba4ff5b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
25 changes: 16 additions & 9 deletions R/prepare_for_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/_snaps/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -956,3 +956,4 @@
10: 1-0001-0002-afd3cffa8b1c850f902a32f4d7ac19fe-0010 <data.table[1x4]>
11: 1-0001-0002-150a10ab5600d4260be332983e69a451-0011 <data.table[1x4]>
12: 1-0001-0002-afd3cffa8b1c850f902a32f4d7ac19fe-0012 <data.table[1x4]>

0 comments on commit ba4ff5b

Please sign in to comment.