From b9adb98bb0949097d271cf0ff5174e67200d22ab Mon Sep 17 00:00:00 2001 From: John Blischak Date: Wed, 6 Mar 2024 20:52:03 -0500 Subject: [PATCH 1/4] Add create_cutting_test() --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/sim_gs_n.R | 39 +++++++++++++++++++++++++++++++++++ _pkgdown.yml | 1 + man/create_cutting_test.Rd | 42 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 man/create_cutting_test.Rd diff --git a/DESCRIPTION b/DESCRIPTION index c90ea708..6de4bea1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: simtrial Type: Package Title: Clinical Trial Simulation -Version: 0.3.2.7 +Version: 0.3.2.8 Authors@R: c( person("Keaven", "Anderson", email = "keaven_anderson@merck.com", role = c("aut")), person("Yilong", "Zhang", email = "elong0527@gmail.com", role = c("aut")), diff --git a/NAMESPACE b/NAMESPACE index c42f556d..939c318f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(counting_process) export(create_cutting) +export(create_cutting_test) export(cut_data_by_date) export(cut_data_by_event) export(early_zero) diff --git a/R/sim_gs_n.R b/R/sim_gs_n.R index e0da8808..fe7d9387 100644 --- a/R/sim_gs_n.R +++ b/R/sim_gs_n.R @@ -324,3 +324,42 @@ create_cutting <- function(...) { get_analysis_date(data, ...) } } + +#' Create a cutting test function +#' +#' Create a cutting test function for use with [sim_gs_n()] +#' +#' @param test A test function such as [wlr()], [maxcombo()], or [rmst()] +#' @param ... Arguments passed to the cutting test function +#' +#' @return A function that accepts a data frame of simulated trial data and +#' returns a test result +#' +#' @export +#' +#' @seealso [sim_gs_n()], [create_cutting()] +#' +#' @examples +#' # Simulate trial data +#' trial_data <- sim_pw_surv() +#' +#' # Cut after 150 events +#' trial_data_cut <- cut_data_by_event(trial_data, 150) +#' +#' # Create a cutting test function that can be used by sim_gs_n() +#' regular_logrank_test <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0)) +#' +#' # Test the cutting +#' regular_logrank_test(trial_data_cut) +#' +#' # The results are the same as directly calling the function +#' stopifnot(all.equal( +#' regular_logrank_test(trial_data_cut), +#' wlr(trial_data_cut, weight = fh(rho = 0, gamma = 0)) +#' )) +create_cutting_test <- function(test, ...) { + stopifnot(is.function(test)) + function(data) { + test(data, ...) + } +} diff --git a/_pkgdown.yml b/_pkgdown.yml index ac94271d..ff6bc46e 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -44,6 +44,7 @@ reference: - get_cut_date_by_event - get_analysis_date - create_cutting + - create_cutting_test - title: "Compute p-values/test statistics" contents: diff --git a/man/create_cutting_test.Rd b/man/create_cutting_test.Rd new file mode 100644 index 00000000..62eb66bf --- /dev/null +++ b/man/create_cutting_test.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sim_gs_n.R +\name{create_cutting_test} +\alias{create_cutting_test} +\title{Create a cutting test function} +\usage{ +create_cutting_test(test, ...) +} +\arguments{ +\item{test}{A test function such as \code{\link[=wlr]{wlr()}}, \code{\link[=maxcombo]{maxcombo()}}, or \code{\link[=rmst]{rmst()}}} + +\item{...}{Arguments passed to the cutting test function} +} +\value{ +A function that accepts a data frame of simulated trial data and +returns a test result +} +\description{ +Create a cutting test function for use with \code{\link[=sim_gs_n]{sim_gs_n()}} +} +\examples{ +# Simulate trial data +trial_data <- sim_pw_surv() + +# Cut after 150 events +trial_data_cut <- cut_data_by_event(trial_data, 150) + +# Create a cutting test function that can be used by sim_gs_n() +regular_logrank_test <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0)) + +# Test the cutting +regular_logrank_test(trial_data_cut) + +# The results are the same as directly calling the function +stopifnot(all.equal( + regular_logrank_test(trial_data_cut), + wlr(trial_data_cut, weight = fh(rho = 0, gamma = 0)) +)) +} +\seealso{ +\code{\link[=sim_gs_n]{sim_gs_n()}}, \code{\link[=create_cutting]{create_cutting()}} +} From 7ac25c854d730ded7d8907abee9ba1772f20f360 Mon Sep 17 00:00:00 2001 From: John Blischak Date: Thu, 7 Mar 2024 11:44:04 -0500 Subject: [PATCH 2/4] Add multitest() --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/sim_gs_n.R | 42 +++++++++++++++++++++ _pkgdown.yml | 3 +- man/multitest.Rd | 42 +++++++++++++++++++++ tests/testthat/test-unvalidated-multitest.R | 22 +++++++++++ 6 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 man/multitest.Rd create mode 100644 tests/testthat/test-unvalidated-multitest.R diff --git a/DESCRIPTION b/DESCRIPTION index 6de4bea1..88d599d4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: simtrial Type: Package Title: Clinical Trial Simulation -Version: 0.3.2.8 +Version: 0.3.2.9 Authors@R: c( person("Keaven", "Anderson", email = "keaven_anderson@merck.com", role = c("aut")), person("Yilong", "Zhang", email = "elong0527@gmail.com", role = c("aut")), diff --git a/NAMESPACE b/NAMESPACE index 939c318f..4f6b6de3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export(maxcombo) export(mb) export(mb_weight) export(milestone) +export(multitest) export(pvalue_maxcombo) export(randomize_by_fixed_block) export(rmst) diff --git a/R/sim_gs_n.R b/R/sim_gs_n.R index fe7d9387..7fac95c1 100644 --- a/R/sim_gs_n.R +++ b/R/sim_gs_n.R @@ -363,3 +363,45 @@ create_cutting_test <- function(test, ...) { test(data, ...) } } + +#' Perform multiple tests on trial data cutting +#' +#' WARNING: This experimental function is a work-in-progress. The function +#' arguments and/or returned output format may change as we add additional +#' features. +#' +#' @param data Trial data cut by [cut_data_by_event()] or [cut_data_by_date()] +#' @param ... One or more test functions. Use [create_cutting_test()] to change +#' the default arguments of each test function. +#' +#' @return A list of test results, one per test. If the test functions are named +#' in the call to `multitest()`, the returned list uses the same names. +#' +#' @export +#' +#' @seealso [create_cutting_test()] +#' +#' @examples +#' trial_data <- sim_pw_surv(n = 200) +#' trial_data_cut <- cut_data_by_event(trial_data, 150) +#' +#' # create cutting test functions +#' wlr_partial <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0)) +#' rmst_partial <- create_cutting_test(rmst, tau = 20) +#' maxcombo_partial <- create_cutting_test(maxcombo, rho = c(0, 0), gamma = c(0, 0.5)) +#' +#' multitest( +#' data = trial_data_cut, +#' wlr = wlr_partial, +#' rmst = rmst_partial, +#' maxcombo = maxcombo_partial +#' ) +multitest <- function(data, ...) { + tests <- list(...) + output <- vector(mode = "list", length = length(tests)) + names(output) <- names(tests) + for (i in seq_along(tests)) { + output[[i]] <- tests[[i]](data) + } + return(output) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index ff6bc46e..bc86c907 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -44,7 +44,6 @@ reference: - get_cut_date_by_event - get_analysis_date - create_cutting - - create_cutting_test - title: "Compute p-values/test statistics" contents: @@ -54,6 +53,8 @@ reference: - milestone - wlr - maxcombo + - create_cutting_test + - multitest - title: "Randomization algorithms" contents: diff --git a/man/multitest.Rd b/man/multitest.Rd new file mode 100644 index 00000000..835a0d2b --- /dev/null +++ b/man/multitest.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sim_gs_n.R +\name{multitest} +\alias{multitest} +\title{Perform multiple tests on trial data cutting} +\usage{ +multitest(data, ...) +} +\arguments{ +\item{data}{Trial data cut by \code{\link[=cut_data_by_event]{cut_data_by_event()}} or \code{\link[=cut_data_by_date]{cut_data_by_date()}}} + +\item{...}{One or more test functions. Use \code{\link[=create_cutting_test]{create_cutting_test()}} to change +the default arguments of each test function.} +} +\value{ +A list of test results, one per test. If the test functions are named +in the call to \code{multitest()}, the returned list uses the same names. +} +\description{ +WARNING: This experimental function is a work-in-progress. The function +arguments and/or returned output format may change as we add additional +features. +} +\examples{ +trial_data <- sim_pw_surv(n = 200) +trial_data_cut <- cut_data_by_event(trial_data, 150) + +# create cutting test functions +wlr_partial <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0)) +rmst_partial <- create_cutting_test(rmst, tau = 20) +maxcombo_partial <- create_cutting_test(maxcombo, rho = c(0, 0), gamma = c(0, 0.5)) + +multitest( + data = trial_data_cut, + wlr = wlr_partial, + rmst = rmst_partial, + maxcombo = maxcombo_partial +) +} +\seealso{ +\code{\link[=create_cutting_test]{create_cutting_test()}} +} diff --git a/tests/testthat/test-unvalidated-multitest.R b/tests/testthat/test-unvalidated-multitest.R new file mode 100644 index 00000000..d5bd7841 --- /dev/null +++ b/tests/testthat/test-unvalidated-multitest.R @@ -0,0 +1,22 @@ +test_that("multitest() is equivalent to running tests individually", { + trial_data <- sim_pw_surv(n = 200) + trial_data_cut <- cut_data_by_event(trial_data, 150) + + # create cutting test functions + wlr_partial <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0)) + rmst_partial <- create_cutting_test(rmst, tau = 20) + maxcombo_partial <- create_cutting_test(maxcombo, rho = c(0, 0), gamma = c(0, 0.5)) + + observed <- multitest( + data = trial_data_cut, + wlr = wlr_partial, + rmst = rmst_partial, + maxcombo = maxcombo_partial + ) + expected <- list( + wlr = wlr(trial_data_cut, weight = fh(rho = 0, gamma = 0)), + rmst = rmst(trial_data_cut, tau = 20), + maxcombo = maxcombo(trial_data_cut, rho = c(0, 0), gamma = c(0, 0.5)) + ) + expect_equal(observed, expected) +}) From 3aa988e34bb894b0b511f11bdcfaf10872ca5392 Mon Sep 17 00:00:00 2001 From: John Blischak Date: Thu, 7 Mar 2024 11:52:48 -0500 Subject: [PATCH 3/4] Use markdown syntax for roxygen2 docs --- R/input_checking.R | 2 +- R/maxcombo.R | 10 +++++----- R/rmst.R | 4 ++-- R/sim_gs_n.R | 6 +++--- R/wlr.R | 4 ++-- R/wlr_weight.R | 4 ++-- man/create_cutting.Rd | 6 +++--- man/fh.Rd | 4 ++-- man/maxcombo.Rd | 6 +++--- man/wlr.Rd | 4 ++-- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/R/input_checking.R b/R/input_checking.R index 0aa90e69..1e090f56 100644 --- a/R/input_checking.R +++ b/R/input_checking.R @@ -87,7 +87,7 @@ input_check_vector <- function(x = NA, require_whole_number = FALSE) { #' @param tol tolerance #' #' @return TRUE, FALSE, or NA -#' @seealso \code{\link[base]{is.integer}} +#' @seealso [base::is.integer()] #' @noRd #' @examples #' x <- c(1.1, -1.1, 0, 2, NA) diff --git a/R/maxcombo.R b/R/maxcombo.R index b9a7f503..18e3eb08 100644 --- a/R/maxcombo.R +++ b/R/maxcombo.R @@ -22,15 +22,15 @@ #' arguments will change as we add additional features. #' #' @param data a tte dataset -#' @param rho Numeric vector passed to \code{\link{fh_weight}}. Must be greater -#' than or equal to zero. Must be the same length as \code{gamma}. -#' @param gamma Numeric vector passed to \code{\link{fh_weight}}. Must be -#' greater than or equal to zero. Must be the same length as \code{rho}. +#' @param rho Numeric vector passed to [fh_weight()]. Must be greater +#' than or equal to zero. Must be the same length as `gamma`. +#' @param gamma Numeric vector passed to [fh_weight()]. Must be +#' greater than or equal to zero. Must be the same length as `rho`. #' #' @return pvalues #' @export #' -#' @seealso \code{\link{fh_weight}} +#' @seealso [fh_weight()] #' #' @examples #' sim_pw_surv(n = 200) |> diff --git a/R/rmst.R b/R/rmst.R index 818c7d3c..8e4cfb86 100644 --- a/R/rmst.R +++ b/R/rmst.R @@ -197,8 +197,8 @@ diff_rmst <- function(x, op_single, reference, trunc_time, alpha = alpha) { #' #' @return #' A data frame of -#' - Cutoff time: same as \code{tau}; -#' - Group label: same as \code{group_label}; +#' - Cutoff time: same as `tau`; +#' - Group label: same as `group_label`; #' - Estimated RMST; #' - Variance, standard error, and CIs of the estimated RMST; #' - Number of events. diff --git a/R/sim_gs_n.R b/R/sim_gs_n.R index 7fac95c1..33cbb770 100644 --- a/R/sim_gs_n.R +++ b/R/sim_gs_n.R @@ -294,16 +294,16 @@ sim_gs_n <- function( #' Create a cutting function #' -#' Create a cutting function for use with \code{\link{sim_gs_n}} +#' Create a cutting function for use with [sim_gs_n()] #' -#' @param ... Arguments passed to \code{\link{get_analysis_date}} +#' @param ... Arguments passed to [get_analysis_date()] #' #' @return A function that accepts a data frame of simulated trial data and #' returns a cut date #' #' @export #' -#' @seealso \code{\link{get_analysis_date}}, \code{\link{sim_gs_n}} +#' @seealso [get_analysis_date()], [sim_gs_n()] #' #' @examples #' # Simulate trial data diff --git a/R/wlr.R b/R/wlr.R index fdd65265..672c8262 100644 --- a/R/wlr.R +++ b/R/wlr.R @@ -19,8 +19,8 @@ #' Weighted logrank test #' #' @param data cutted dataset generated by sim_pw_surv -#' @param weight weighting functions, such as \code{\link{fh_weight}}, -#' \code{\link{mb_weight}}, and \code{\link{early_zero_weight}}. +#' @param weight weighting functions, such as [fh_weight()], [mb_weight()], and +#' [early_zero_weight()]. #' #' @return test results #' diff --git a/R/wlr_weight.R b/R/wlr_weight.R index f4106407..b4ae8d63 100644 --- a/R/wlr_weight.R +++ b/R/wlr_weight.R @@ -18,8 +18,8 @@ #' Fleming-Harrington weighting function #' -#' @param rho Non-negative number. \code{rho = 0, gamma = 0} is equivalent to regular logrank test. -#' @param gamma Non-negative number. \code{rho = 0, gamma = 0} is equivalent to regular logrank test. +#' @param rho Non-negative number. `rho = 0, gamma = 0` is equivalent to regular logrank test. +#' @param gamma Non-negative number. `rho = 0, gamma = 0` is equivalent to regular logrank test. #' #' @export #' @return A list of parameters of the Fleming-Harrington weighting function diff --git a/man/create_cutting.Rd b/man/create_cutting.Rd index 9e7b70eb..de090b54 100644 --- a/man/create_cutting.Rd +++ b/man/create_cutting.Rd @@ -7,14 +7,14 @@ create_cutting(...) } \arguments{ -\item{...}{Arguments passed to \code{\link{get_analysis_date}}} +\item{...}{Arguments passed to \code{\link[=get_analysis_date]{get_analysis_date()}}} } \value{ A function that accepts a data frame of simulated trial data and returns a cut date } \description{ -Create a cutting function for use with \code{\link{sim_gs_n}} +Create a cutting function for use with \code{\link[=sim_gs_n]{sim_gs_n()}} } \examples{ # Simulate trial data @@ -32,5 +32,5 @@ cutting <- create_cutting( cutting(trial_data) } \seealso{ -\code{\link{get_analysis_date}}, \code{\link{sim_gs_n}} +\code{\link[=get_analysis_date]{get_analysis_date()}}, \code{\link[=sim_gs_n]{sim_gs_n()}} } diff --git a/man/fh.Rd b/man/fh.Rd index 9f191609..a9a5e19b 100644 --- a/man/fh.Rd +++ b/man/fh.Rd @@ -7,9 +7,9 @@ fh(rho = 0, gamma = 0) } \arguments{ -\item{rho}{Non-negative number. \code{rho = 0, gamma = 0} is equivalent to regular logrank test.} +\item{rho}{Non-negative number. \verb{rho = 0, gamma = 0} is equivalent to regular logrank test.} -\item{gamma}{Non-negative number. \code{rho = 0, gamma = 0} is equivalent to regular logrank test.} +\item{gamma}{Non-negative number. \verb{rho = 0, gamma = 0} is equivalent to regular logrank test.} } \value{ A list of parameters of the Fleming-Harrington weighting function diff --git a/man/maxcombo.Rd b/man/maxcombo.Rd index 4159f060..8b75bb1f 100644 --- a/man/maxcombo.Rd +++ b/man/maxcombo.Rd @@ -9,10 +9,10 @@ maxcombo(data, rho, gamma) \arguments{ \item{data}{a tte dataset} -\item{rho}{Numeric vector passed to \code{\link{fh_weight}}. Must be greater +\item{rho}{Numeric vector passed to \code{\link[=fh_weight]{fh_weight()}}. Must be greater than or equal to zero. Must be the same length as \code{gamma}.} -\item{gamma}{Numeric vector passed to \code{\link{fh_weight}}. Must be +\item{gamma}{Numeric vector passed to \code{\link[=fh_weight]{fh_weight()}}. Must be greater than or equal to zero. Must be the same length as \code{rho}.} } \value{ @@ -28,5 +28,5 @@ sim_pw_surv(n = 200) |> maxcombo(rho = c(0, 0), gamma = c(0, 0.5)) } \seealso{ -\code{\link{fh_weight}} +\code{\link[=fh_weight]{fh_weight()}} } diff --git a/man/wlr.Rd b/man/wlr.Rd index 8a017f8a..ff98653d 100644 --- a/man/wlr.Rd +++ b/man/wlr.Rd @@ -9,8 +9,8 @@ wlr(data, weight) \arguments{ \item{data}{cutted dataset generated by sim_pw_surv} -\item{weight}{weighting functions, such as \code{\link{fh_weight}}, -\code{\link{mb_weight}}, and \code{\link{early_zero_weight}}.} +\item{weight}{weighting functions, such as \code{\link[=fh_weight]{fh_weight()}}, \code{\link[=mb_weight]{mb_weight()}}, and +\code{\link[=early_zero_weight]{early_zero_weight()}}.} } \value{ test results From f96a370513b62ea069824203a2ac138a0e2b5f31 Mon Sep 17 00:00:00 2001 From: John Blischak Date: Thu, 7 Mar 2024 12:46:56 -0500 Subject: [PATCH 4/4] Allow passing one test per cutting to sim_gs_n() --- DESCRIPTION | 2 +- R/sim_gs_n.R | 23 ++++++++-- man/sim_gs_n.Rd | 10 +++-- tests/testthat/test-unvalidated-sim_gs_n.R | 49 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 88d599d4..ca21590c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: simtrial Type: Package Title: Clinical Trial Simulation -Version: 0.3.2.9 +Version: 0.3.2.10 Authors@R: c( person("Keaven", "Anderson", email = "keaven_anderson@merck.com", role = c("aut")), person("Yilong", "Zhang", email = "elong0527@gmail.com", role = c("aut")), diff --git a/R/sim_gs_n.R b/R/sim_gs_n.R index 33cbb770..34395994 100644 --- a/R/sim_gs_n.R +++ b/R/sim_gs_n.R @@ -22,9 +22,13 @@ #' arguments will change as we add additional features. #' #' @inheritParams sim_fixed_n -#' @param test A test function such as [wlr()], -#' [maxcombo()], or [rmst()]. The simulated data set is -#' passed as the first positional argument to the test function provided. +#' @param test A test function such as [wlr()], [maxcombo()], or [rmst()]. The +#' simulated data set is passed as the first positional argument to the test +#' function provided. Alternatively a list of functions created by +#' [create_cutting_test()]. The list form is experimental and currently +#' limited. It only accepts one test per cutting (in the future multiple tests +#' may be accepted), and all the tests must consistently return the same exact +#' results (again this may be more flexible in the future). #' @param cutting A list of cutting functions created by [create_cutting()], #' see examples. #' @param seed Random seed. @@ -268,6 +272,17 @@ sim_gs_n <- function( cut_date <- rep(-100, n_analysis) ans_1sim <- NULL + # Organize tests for each cutting + if (is.function(test)) { + test_single <- test + test <- vector(mode = "list", length = n_analysis) + test[] <- list(test_single) + } + if (length(test) != length(cutting)) { + stop("If you want to run different tests at each cutting, the list of + tests must be the same length as the list of cuttings") + } + for (i_analysis in seq_len(n_analysis)) { # Get cut date cut_date[i_analysis] <- cutting[[i_analysis]](data = simu_data) @@ -276,7 +291,7 @@ sim_gs_n <- function( simu_data_cut <- simu_data |> cut_data_by_date(cut_date[i_analysis]) # Test - ans_1sim_new <- test(simu_data_cut, ...) + ans_1sim_new <- test[[i_analysis]](simu_data_cut, ...) ans_1sim_new$analysis <- i_analysis ans_1sim_new$cut_date <- cut_date[i_analysis] ans_1sim_new$sim_id <- sim_id diff --git a/man/sim_gs_n.Rd b/man/sim_gs_n.Rd index 959f42db..38538351 100644 --- a/man/sim_gs_n.Rd +++ b/man/sim_gs_n.Rd @@ -36,9 +36,13 @@ for experimental vs. control, and dropout rates by stratum and time period.} \item{block}{As in \code{\link[=sim_pw_surv]{sim_pw_surv()}}. Vector of treatments to be included in each block.} -\item{test}{A test function such as \code{\link[=wlr]{wlr()}}, -\code{\link[=maxcombo]{maxcombo()}}, or \code{\link[=rmst]{rmst()}}. The simulated data set is -passed as the first positional argument to the test function provided.} +\item{test}{A test function such as \code{\link[=wlr]{wlr()}}, \code{\link[=maxcombo]{maxcombo()}}, or \code{\link[=rmst]{rmst()}}. The +simulated data set is passed as the first positional argument to the test +function provided. Alternatively a list of functions created by +\code{\link[=create_cutting_test]{create_cutting_test()}}. The list form is experimental and currently +limited. It only accepts one test per cutting (in the future multiple tests +may be accepted), and all the tests must consistently return the same exact +results (again this may be more flexible in the future).} \item{cutting}{A list of cutting functions created by \code{\link[=create_cutting]{create_cutting()}}, see examples.} diff --git a/tests/testthat/test-unvalidated-sim_gs_n.R b/tests/testthat/test-unvalidated-sim_gs_n.R index 5a5b119d..b6a3df23 100644 --- a/tests/testthat/test-unvalidated-sim_gs_n.R +++ b/tests/testthat/test-unvalidated-sim_gs_n.R @@ -231,3 +231,52 @@ test_that("Test 7: MaxCombo (WLR-FH(0,0) + WLR-FH(0, 0.5))", { ) expect_equal(observed, expected) }) + +test_that("sim_gs_n() accepts different tests per cutting", { + wlr_cut1 <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0)) + wlr_cut2 <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0.5)) + wlr_cut3 <- create_cutting_test(wlr, weight = fh(rho = 0.5, gamma = 0)) + + observed <- sim_gs_n( + n_sim = 3, + sample_size = 400, + enroll_rate = test_enroll_rate(), + fail_rate = test_fail_rate(), + test = list(wlr_cut1, wlr_cut2, wlr_cut3), + cutting = test_cutting(), + seed = 2024 + ) + expected <- data.frame( + rho = rep(c(0, 0, 0.5), 3), + gamma = rep(c(0, 0.5, 0), 3), + z = c( + -3.7486049782713247, -4.778107819550277, -4.189693884801371, + -3.4771440155825752, -3.945081123231263, -3.438138809871842, + -3.075862925191481, -3.640458610667732, -3.9489173860678495 + ), + analysis = rep(1:3, 3), + cut_date = c(24, 32, 45, 24, 32, 46.219327415802894, 24, 32, 50.86585486314699), + sim_id = rep(1:3, each = 3L), + n = rep(400L, 9L), + event = c(229, 295, 355, 241, 290, 350, 226, 282, 350) + ) + expect_equal(observed, expected) +}) + +test_that("sim_gs_n() requires a test for each cutting", { + wlr_cut1 <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0)) + wlr_cut2 <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0.5)) + + expect_error( + sim_gs_n( + n_sim = 3, + sample_size = 400, + enroll_rate = test_enroll_rate(), + fail_rate = test_fail_rate(), + test = list(wlr_cut1, wlr_cut2), + cutting = test_cutting(), + seed = 2024 + ), + "If you want to run different tests at each cutting" + ) +})