Skip to content

Commit db0d586

Browse files
authored
Merge pull request #215 from jdblischak/create-cutting-test
Add `create_cutting_test()` and `multitest()`
2 parents 9ac40f3 + f96a370 commit db0d586

18 files changed

+292
-33
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: simtrial
22
Type: Package
33
Title: Clinical Trial Simulation
4-
Version: 0.3.2.7
4+
Version: 0.3.2.10
55
Authors@R: c(
66
person("Keaven", "Anderson", email = "[email protected]", role = c("aut")),
77
person("Yilong", "Zhang", email = "[email protected]", role = c("aut")),

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export(counting_process)
44
export(create_cutting)
5+
export(create_cutting_test)
56
export(cut_data_by_date)
67
export(cut_data_by_event)
78
export(early_zero)
@@ -15,6 +16,7 @@ export(maxcombo)
1516
export(mb)
1617
export(mb_weight)
1718
export(milestone)
19+
export(multitest)
1820
export(pvalue_maxcombo)
1921
export(randomize_by_fixed_block)
2022
export(rmst)

R/input_checking.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ input_check_vector <- function(x = NA, require_whole_number = FALSE) {
8787
#' @param tol tolerance
8888
#'
8989
#' @return TRUE, FALSE, or NA
90-
#' @seealso \code{\link[base]{is.integer}}
90+
#' @seealso [base::is.integer()]
9191
#' @noRd
9292
#' @examples
9393
#' x <- c(1.1, -1.1, 0, 2, NA)

R/maxcombo.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
#' arguments will change as we add additional features.
2323
#'
2424
#' @param data a tte dataset
25-
#' @param rho Numeric vector passed to \code{\link{fh_weight}}. Must be greater
26-
#' than or equal to zero. Must be the same length as \code{gamma}.
27-
#' @param gamma Numeric vector passed to \code{\link{fh_weight}}. Must be
28-
#' greater than or equal to zero. Must be the same length as \code{rho}.
25+
#' @param rho Numeric vector passed to [fh_weight()]. Must be greater
26+
#' than or equal to zero. Must be the same length as `gamma`.
27+
#' @param gamma Numeric vector passed to [fh_weight()]. Must be
28+
#' greater than or equal to zero. Must be the same length as `rho`.
2929
#'
3030
#' @return pvalues
3131
#' @export
3232
#'
33-
#' @seealso \code{\link{fh_weight}}
33+
#' @seealso [fh_weight()]
3434
#'
3535
#' @examples
3636
#' sim_pw_surv(n = 200) |>

R/rmst.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ diff_rmst <- function(x, op_single, reference, trunc_time, alpha = alpha) {
197197
#'
198198
#' @return
199199
#' A data frame of
200-
#' - Cutoff time: same as \code{tau};
201-
#' - Group label: same as \code{group_label};
200+
#' - Cutoff time: same as `tau`;
201+
#' - Group label: same as `group_label`;
202202
#' - Estimated RMST;
203203
#' - Variance, standard error, and CIs of the estimated RMST;
204204
#' - Number of events.

R/sim_gs_n.R

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
#' arguments will change as we add additional features.
2323
#'
2424
#' @inheritParams sim_fixed_n
25-
#' @param test A test function such as [wlr()],
26-
#' [maxcombo()], or [rmst()]. The simulated data set is
27-
#' passed as the first positional argument to the test function provided.
25+
#' @param test A test function such as [wlr()], [maxcombo()], or [rmst()]. The
26+
#' simulated data set is passed as the first positional argument to the test
27+
#' function provided. Alternatively a list of functions created by
28+
#' [create_cutting_test()]. The list form is experimental and currently
29+
#' limited. It only accepts one test per cutting (in the future multiple tests
30+
#' may be accepted), and all the tests must consistently return the same exact
31+
#' results (again this may be more flexible in the future).
2832
#' @param cutting A list of cutting functions created by [create_cutting()],
2933
#' see examples.
3034
#' @param seed Random seed.
@@ -268,6 +272,17 @@ sim_gs_n <- function(
268272
cut_date <- rep(-100, n_analysis)
269273
ans_1sim <- NULL
270274

275+
# Organize tests for each cutting
276+
if (is.function(test)) {
277+
test_single <- test
278+
test <- vector(mode = "list", length = n_analysis)
279+
test[] <- list(test_single)
280+
}
281+
if (length(test) != length(cutting)) {
282+
stop("If you want to run different tests at each cutting, the list of
283+
tests must be the same length as the list of cuttings")
284+
}
285+
271286
for (i_analysis in seq_len(n_analysis)) {
272287
# Get cut date
273288
cut_date[i_analysis] <- cutting[[i_analysis]](data = simu_data)
@@ -276,7 +291,7 @@ sim_gs_n <- function(
276291
simu_data_cut <- simu_data |> cut_data_by_date(cut_date[i_analysis])
277292

278293
# Test
279-
ans_1sim_new <- test(simu_data_cut, ...)
294+
ans_1sim_new <- test[[i_analysis]](simu_data_cut, ...)
280295
ans_1sim_new$analysis <- i_analysis
281296
ans_1sim_new$cut_date <- cut_date[i_analysis]
282297
ans_1sim_new$sim_id <- sim_id
@@ -294,16 +309,16 @@ sim_gs_n <- function(
294309

295310
#' Create a cutting function
296311
#'
297-
#' Create a cutting function for use with \code{\link{sim_gs_n}}
312+
#' Create a cutting function for use with [sim_gs_n()]
298313
#'
299-
#' @param ... Arguments passed to \code{\link{get_analysis_date}}
314+
#' @param ... Arguments passed to [get_analysis_date()]
300315
#'
301316
#' @return A function that accepts a data frame of simulated trial data and
302317
#' returns a cut date
303318
#'
304319
#' @export
305320
#'
306-
#' @seealso \code{\link{get_analysis_date}}, \code{\link{sim_gs_n}}
321+
#' @seealso [get_analysis_date()], [sim_gs_n()]
307322
#'
308323
#' @examples
309324
#' # Simulate trial data
@@ -324,3 +339,84 @@ create_cutting <- function(...) {
324339
get_analysis_date(data, ...)
325340
}
326341
}
342+
343+
#' Create a cutting test function
344+
#'
345+
#' Create a cutting test function for use with [sim_gs_n()]
346+
#'
347+
#' @param test A test function such as [wlr()], [maxcombo()], or [rmst()]
348+
#' @param ... Arguments passed to the cutting test function
349+
#'
350+
#' @return A function that accepts a data frame of simulated trial data and
351+
#' returns a test result
352+
#'
353+
#' @export
354+
#'
355+
#' @seealso [sim_gs_n()], [create_cutting()]
356+
#'
357+
#' @examples
358+
#' # Simulate trial data
359+
#' trial_data <- sim_pw_surv()
360+
#'
361+
#' # Cut after 150 events
362+
#' trial_data_cut <- cut_data_by_event(trial_data, 150)
363+
#'
364+
#' # Create a cutting test function that can be used by sim_gs_n()
365+
#' regular_logrank_test <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0))
366+
#'
367+
#' # Test the cutting
368+
#' regular_logrank_test(trial_data_cut)
369+
#'
370+
#' # The results are the same as directly calling the function
371+
#' stopifnot(all.equal(
372+
#' regular_logrank_test(trial_data_cut),
373+
#' wlr(trial_data_cut, weight = fh(rho = 0, gamma = 0))
374+
#' ))
375+
create_cutting_test <- function(test, ...) {
376+
stopifnot(is.function(test))
377+
function(data) {
378+
test(data, ...)
379+
}
380+
}
381+
382+
#' Perform multiple tests on trial data cutting
383+
#'
384+
#' WARNING: This experimental function is a work-in-progress. The function
385+
#' arguments and/or returned output format may change as we add additional
386+
#' features.
387+
#'
388+
#' @param data Trial data cut by [cut_data_by_event()] or [cut_data_by_date()]
389+
#' @param ... One or more test functions. Use [create_cutting_test()] to change
390+
#' the default arguments of each test function.
391+
#'
392+
#' @return A list of test results, one per test. If the test functions are named
393+
#' in the call to `multitest()`, the returned list uses the same names.
394+
#'
395+
#' @export
396+
#'
397+
#' @seealso [create_cutting_test()]
398+
#'
399+
#' @examples
400+
#' trial_data <- sim_pw_surv(n = 200)
401+
#' trial_data_cut <- cut_data_by_event(trial_data, 150)
402+
#'
403+
#' # create cutting test functions
404+
#' wlr_partial <- create_cutting_test(wlr, weight = fh(rho = 0, gamma = 0))
405+
#' rmst_partial <- create_cutting_test(rmst, tau = 20)
406+
#' maxcombo_partial <- create_cutting_test(maxcombo, rho = c(0, 0), gamma = c(0, 0.5))
407+
#'
408+
#' multitest(
409+
#' data = trial_data_cut,
410+
#' wlr = wlr_partial,
411+
#' rmst = rmst_partial,
412+
#' maxcombo = maxcombo_partial
413+
#' )
414+
multitest <- function(data, ...) {
415+
tests <- list(...)
416+
output <- vector(mode = "list", length = length(tests))
417+
names(output) <- names(tests)
418+
for (i in seq_along(tests)) {
419+
output[[i]] <- tests[[i]](data)
420+
}
421+
return(output)
422+
}

R/wlr.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#' Weighted logrank test
2020
#'
2121
#' @param data cutted dataset generated by sim_pw_surv
22-
#' @param weight weighting functions, such as \code{\link{fh_weight}},
23-
#' \code{\link{mb_weight}}, and \code{\link{early_zero_weight}}.
22+
#' @param weight weighting functions, such as [fh_weight()], [mb_weight()], and
23+
#' [early_zero_weight()].
2424
#'
2525
#' @return test results
2626
#'

R/wlr_weight.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
#' Fleming-Harrington weighting function
2020
#'
21-
#' @param rho Non-negative number. \code{rho = 0, gamma = 0} is equivalent to regular logrank test.
22-
#' @param gamma Non-negative number. \code{rho = 0, gamma = 0} is equivalent to regular logrank test.
21+
#' @param rho Non-negative number. `rho = 0, gamma = 0` is equivalent to regular logrank test.
22+
#' @param gamma Non-negative number. `rho = 0, gamma = 0` is equivalent to regular logrank test.
2323
#'
2424
#' @export
2525
#' @return A list of parameters of the Fleming-Harrington weighting function

_pkgdown.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ reference:
5353
- milestone
5454
- wlr
5555
- maxcombo
56+
- create_cutting_test
57+
- multitest
5658

5759
- title: "Randomization algorithms"
5860
contents:

man/create_cutting.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_cutting_test.Rd

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fh.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/maxcombo.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/multitest.Rd

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)