Skip to content

Commit

Permalink
Merge pull request #121 from jdblischak/MBdelayed
Browse files Browse the repository at this point in the history
Update script to generate MBdelayed.rda
  • Loading branch information
nanxstats authored Nov 3, 2023
2 parents 0ddb0d6 + 16da13a commit e9f2f71
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion R/MBdelayed.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#' under the above scenario.
#'
#' @format
#' A tibble with 200 rows and xx columns:
#' A tibble with 200 rows and 4 columns:
#' - `tte`: Time to event.
#'
#' @references
Expand Down
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
30 changes: 16 additions & 14 deletions data-raw/DATASET.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
## code to prepare `DATASET` dataset goes here
## code to prepare `MBdelayed` dataset goes here
library(simtrial)
library(tibble)
set.seed(6671)
ds <- simPWSurv(
ds <- sim_pw_surv(
n = 200,
enrollRates = tibble(rate = 200 / 12, duration = 12),
failRates = tribble(
~Stratum, ~Period, ~Treatment, ~duration, ~rate,
"All", 1, "Control", 42, log(2) / 15,
"All", 1, "Experimental", 6, log(2) / 15,
"All", 2, "Experimental", 36, log(2) / 15 * 0.6
block = c(rep("control", 2), rep("experimental", 2)),
enroll_rate = tibble(rate = 200 / 12, duration = 12),
fail_rate = tribble(
~stratum, ~period, ~treatment, ~duration, ~rate,
"All", 1, "control", 42, log(2) / 15,
"All", 1, "experimental", 6, log(2) / 15,
"All", 2, "experimental", 36, log(2) / 15 * 0.6
),
dropoutRates = tribble(
~Stratum, ~Period, ~Treatment, ~duration, ~rate,
"All", 1, "Control", 42, 0,
"All", 1, "Experimental", 42, 0
dropout_rate = tribble(
~stratum, ~period, ~treatment, ~duration, ~rate,
"All", 1, "control", 42, 0,
"All", 1, "experimental", 42, 0
)
)
# cut data at 24 months after final enrollment
MBdelayed <- ds %>% cutData(max(ds$enrollTime) + 24)
MBdelayed <- ds %>% cut_data_by_date(max(ds$enroll_time) + 24)

usethis::use_data("MBdelayed")
usethis::use_data(MBdelayed, overwrite = TRUE)
Binary file modified data/MBdelayed.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/MBdelayed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 e9f2f71

Please sign in to comment.