Skip to content

Commit

Permalink
Fail early for mismatched treatment names
Browse files Browse the repository at this point in the history
  • Loading branch information
jdblischak committed Nov 3, 2023
1 parent 3ad6948 commit 16da13a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions R/sim_pw_surv.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ sim_pw_surv <- function(
duration = rep(100, 2),
rate = rep(.001, 2)
)) {
# Enforce consistent treatment names
treatments <- unique(c(block, fail_rate$treatment, dropout_rate$treatment))
stopifnot(
treatments %in% block,
treatments %in% fail_rate$treatment,
treatments %in% dropout_rate$treatment
)
# Start table by generating stratum and enrollment times
x <- data.table(stratum = sample(
x = stratum$stratum,
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-double_programming_simPWSurv.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,30 @@ zevent <- dplyr::bind_rows(rate00, rate01, rate10, rate11)
testthat::test_that("The actual number of events changes by changing total sample size", {
expect_false(unique(xevent$event == zevent$event))
})

testthat::test_that("sim_pw_surv() fails early with mismatched treatment names", {
block <- c(rep("x", 2), rep("y", 2))
fail_rate <- data.frame(
stratum = rep("All", 4),
period = rep(1:2, 2),
treatment = c(rep("x", 2), rep("y", 2)),
duration = rep(c(3, 1), 2),
rate = log(2) / c(9, 9, 9, 18)
)
dropout_rate <- data.frame(
stratum = rep("All", 2),
period = rep(1, 2),
treatment = c("x", "y"),
duration = rep(100, 2),
rate = rep(0.001, 2)
)

expect_error(sim_pw_surv(block = block))
expect_error(sim_pw_surv(fail_rate = fail_rate))
expect_error(sim_pw_surv(dropout_rate = dropout_rate))
# works as long as treatment names are consistent
expect_silent(
xy <- sim_pw_surv(block = block, fail_rate = fail_rate, dropout_rate = dropout_rate)
)
expect_identical(sort(unique(xy$treatment)), c("x", "y"))
})

0 comments on commit 16da13a

Please sign in to comment.