### Example: load dplyr and future packages Source: https://merck.github.io/simtrial/reference/sim_fixed_n.html The example section begins by loading the dplyr and future packages, which are used in subsequent simulation examples. ```r library(dplyr) library(future) ``` -------------------------------- ### Example: Setting up trial parameters for get_analysis_date Source: https://merck.github.io/simtrial/reference/get_analysis_date.html Initializes parameters such as alpha, ratio, sample size, and study duration for use with the analysis date function. This example is from the documentation's Examples section. ```r library(gsDesign2) alpha <- 0.025 ratio <- 3 n <- 500 info_frac <- c(0.7, 1) prevalence_ratio <- c(0.4, 0.6) study_duration <- 48 ``` -------------------------------- ### Example: Simulate data and get cut date for 50 events Source: https://merck.github.io/simtrial/reference/get_cut_date_by_event.html Complete example that simulates a time-to-event dataset with `sim_pw_surv()`, filters to the 'Positive' stratum, gets the cut date for 50 events, cuts the data, and displays the event table. The output shows the resulting counts. ```r library(dplyr) #> #> Attaching package: ‘dplyr’ #> The following objects are masked from ‘package:stats’: #> #> filter, lag #> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union # Use default enrollment and calendar cut date # for 50 events in the "Positive" stratum x <- sim_pw_surv( n = 200, stratum = data.frame( stratum = c("Positive", "Negative"), p = c(.5, .5) ), fail_rate = data.frame( stratum = rep(c("Positive", "Negative"), 2), period = rep(1, 4), treatment = c(rep("control", 2), rep("experimental", 2)), duration = rep(1, 4), rate = log(2) / c(6, 9, 9, 12) ), dropout_rate = data.frame( stratum = rep(c("Positive", "Negative"), 2), period = rep(1, 4), treatment = c(rep("control", 2), rep("experimental", 2)), duration = rep(1, 4), rate = rep(.001, 4) ) ) d <- get_cut_date_by_event(x |> filter(stratum == "Positive"), event = 50) y <- cut_data_by_date(x, cut_date = d) table(y$stratum, y$event) #> #> 0 1 #> Negative 49 45 #> Positive 37 50 ``` -------------------------------- ### Install simtrial from CRAN Source: https://merck.github.io/simtrial/index.html Install the stable version of simtrial from CRAN. ```r install.packages("simtrial") ``` -------------------------------- ### Install simtrial development version from GitHub Source: https://merck.github.io/simtrial/index.html Install the development version of simtrial from GitHub to access new features or bug fixes. Requires the remotes package. ```r # install.packages("remotes") remotes::install_github("Merck/simtrial") ``` -------------------------------- ### Example: Summarize group sequential simulations with design and bound Source: https://merck.github.io/simtrial/reference/summary.html Complete example showing how to set up enrollment and failure rates, build a one-sided group sequential design with gs_design_ahr(), run simulations with sim_gs_n(), and summarize results with and without the design. The output shows the summary data frames. ```r library(gsDesign2) # Parameters for enrollment enroll_rampup_duration <- 4 # Duration for enrollment ramp up enroll_duration <- 16 # Total enrollment duration enroll_rate <- define_enroll_rate( duration = c( enroll_rampup_duration, enroll_duration - enroll_rampup_duration), rate = c(10, 30)) # Parameters for treatment effect delay_effect_duration <- 3 # Delay treatment effect in months median_ctrl <- 9 # Survival median of the control arm median_exp <- c(9, 14) # Survival median of the experimental arm dropout_rate <- 0.001 fail_rate <- define_fail_rate( duration = c(delay_effect_duration, 100), fail_rate = log(2) / median_ctrl, hr = median_ctrl / median_exp, dropout_rate = dropout_rate) # Other related parameters alpha <- 0.025 # Type I error beta <- 0.1 # Type II error ratio <- 1 # Randomization ratio (experimental:control) # Build a one-sided group sequential design design <- gs_design_ahr( enroll_rate = enroll_rate, fail_rate = fail_rate, ratio = ratio, alpha = alpha, beta = beta, analysis_time = c(12, 24, 36), upper = gs_spending_bound, upar = list(sf = gsDesign::sfLDOF, total_spend = alpha), lower = gs_b, lpar = rep(-Inf, 3)) # Define cuttings of 2 IAs and 1 FA ia1_cut <- create_cut(target_event_overall = ceiling(design$analysis$event[1])) ia2_cut <- create_cut(target_event_overall = ceiling(design$analysis$event[2])) fa_cut <- create_cut(target_event_overall = ceiling(design$analysis$event[3])) # Run simulations simulation <- sim_gs_n( n_sim = 3, sample_size = ceiling(design$analysis$n[3]), enroll_rate = design$enroll_rate, fail_rate = design$fail_rate, test = wlr, cut = list(ia1 = ia1_cut, ia2 = ia2_cut, fa = fa_cut), weight = fh(rho = 0, gamma = 0.5)) #> Backend uses sequential processing. # Summarize simulations bound <- gsDesign::gsDesign(k = 3, test.type = 1, sfu = gsDesign::sfLDOF)$upper$bound simulation |> summary(bound = bound) #> analysis sim_n sim_event sim_time sim_upper_prob #> 1 1 353.6667 97 12.29699 NA #> 2 2 505.0000 305 23.90850 0.6666667 #> 3 3 505.0000 405 35.79231 1.0000000 # Summarize simulation and compare with the planned design simulation |> summary(design = design) #> analysis asy_upper_prob sim_upper_prob sim_event sim_n sim_time asy_time #> 1 1 0.0001486592 NA 97 353.6667 12.29699 12 #> 2 2 0.5723210881 0.6666667 305 505.0000 23.90850 24 #> 3 3 0.8999997572 1.0000000 405 505.0000 35.79231 36 #> asy_n asy_event #> 1 353.0464 96.77449 #> 2 504.3520 304.00970 #> 3 504.3520 404.14162 ``` -------------------------------- ### Load required R packages for simtrial discrepancy example Source: https://merck.github.io/simtrial/articles/discrepancy-between-simtrial-and-survival.html Loads the packages used in the article: gsDesign, gsDesign2, dplyr, gt, simtrial, tidyr, and survival. Required before running the simulation and comparison examples. ```r library(gsDesign) library(gsDesign2) library(dplyr) library(gt) library(simtrial) library(tidyr) library(survival) ``` -------------------------------- ### Install bshazard package from GitHub Source: https://merck.github.io/simtrial/articles/arbitrary-hazard.html Installs the bshazard package from GitHub if it is not available on CRAN. Run this before using bshazard::bshazard() later in the vignette. ```r remotes::install_github("cran/bshazard") ``` -------------------------------- ### Example 1: counting_process on a small data frame Source: https://merck.github.io/simtrial/reference/counting_process.html Example 1 creates a small data frame with two strata and calls counting_process with arm = 1. The output shows the counting process dataset with columns including stratum, event_total, event_trt, tte, n_risk_total, n_risk_trt, s, o_minus_e, and var_o_minus_e. ```r # Example 1 x <- data.frame( stratum = c(rep(1, 10), rep(2, 6)), treatment = rep(c(1, 1, 0, 0), 4), tte = 1:16, event = rep(c(0, 1), 8) ) counting_process(x, arm = 1) #> stratum event_total event_trt tte n_risk_total n_risk_trt s #> 1 1 1 1 2 9 5 1.0000000 #> 2 1 1 0 4 7 4 0.8888889 #> 3 1 1 1 6 5 3 0.7619048 #> 4 1 1 0 8 3 2 0.6095238 #> 5 2 1 0 12 5 2 1.0000000 #> 6 2 1 1 14 3 1 0.8000000 #> o_minus_e var_o_minus_e #> 1 0.4444444 0.2469136 #> 2 -0.5714286 0.2448980 #> 3 0.4000000 0.2400000 #> 4 -0.6666667 0.2222222 #> 5 -0.4000000 0.2400000 #> 6 0.6666667 0.2222222 ``` -------------------------------- ### Exponential and piecewise exponential failure time simulation with rpwexp() Source: https://merck.github.io/simtrial/reference/rpwexp.html Two example workflows cover a constant failure rate (Example 1) and a three-interval piecewise rate (Example 2), generating 10000 failure times and plotting the empirical survival curve on a log scale. ```R # Example 1 # Exponential failure times x <- rpwexp( n = 10000, fail_rate = data.frame(rate = 5, duration = 1) ) plot(sort(x), (10000:1) / 10001, log = "y", main = "Exponential simulated survival curve", xlab = "Time", ylab = "P{Survival}" ) ``` ```R # Example 2 # Get 10k piecewise exponential failure times. # Failure rates are 1 for time 0 to 0.5, 3 for time 0.5 to 1, and 10 for > 1. # Intervals specifies duration of each failure rate interval # with the final interval running to infinity. x <- rpwexp( n = 1e4, fail_rate = data.frame(rate = c(1, 3, 10), duration = c(.5, .5, 1)) ) plot(sort(x), (1e4:1) / 10001, log = "y", main = "PW Exponential simulated survival curve", xlab = "Time", ylab = "P{Survival}" ) ``` -------------------------------- ### Switch to sequential backend with plan(sequential) Source: https://merck.github.io/simtrial/articles/parallel.html Switches the future backend back to sequential execution. Use this to revert from the multisession backend used in the parallel example. ```r plan(sequential) ``` -------------------------------- ### Example 2: counting_process with simulated data and weighted logrank test Source: https://merck.github.io/simtrial/reference/counting_process.html Example 2 simulates a survival dataset with sim_pw_surv, cuts it at 150 events, and applies counting_process with arm = "experimental". It then computes the weighted logrank test Z-value and one-sided p-value from the o_minus_e and var_o_minus_e columns. ```r # Example 2 x <- sim_pw_surv(n = 400) y <- cut_data_by_event(x, 150) |> counting_process(arm = "experimental") # Weighted logrank test (Z-value and 1-sided p-value) z <- sum(y$o_minus_e) / sqrt(sum(y$var_o_minus_e)) c(z, pnorm(z)) #> [1] -3.808578e+00 6.988404e-05 ``` -------------------------------- ### milestone() example with naive test Source: https://merck.github.io/simtrial/reference/milestone.html Example workflow that simulates survival data, cuts it at 150 events, and applies the milestone() function with test_type = "naive". The output shows the method, parameter, estimate, standard error, and z statistic. ```r cut_data <- sim_pw_surv(n = 200) |> cut_data_by_event(150) cut_data |> milestone(10, test_type = "naive") #> $method #> [1] "milestone" #> #> $parameter #> [1] 10 #> #> $estimate #> [1] 0.1760742 #> #> $se #> [1] 0.06979553 #> #> $z #> [1] 2.522714 #> ``` -------------------------------- ### Load required packages Source: https://merck.github.io/simtrial/articles/modest-wlrt.html Loads the simtrial, dplyr, and survival packages required for the examples on this page. ```r library(simtrial) library(dplyr) library(survival) ``` -------------------------------- ### rmst() with formula interface Source: https://merck.github.io/simtrial/reference/rmst.html Example using the formula interface with Surv(month, evntd) ~ trt. The output is identical to the string-column example, confirming the formula is equivalent. ```r # Formula interface rmst( data = ex1_delayed_effect, formula = Surv(month, evntd) ~ trt, tau = 10, reference = "0" ) #> $method #> [1] "RMST" #> #> $parameter #> [1] 10 #> #> $estimate #> [1] 0.8650493 #> #> $se #> [1] 0.3900344 #> #> $z #> [1] 2.21788 #> ``` -------------------------------- ### milestone() example with log-log test Source: https://merck.github.io/simtrial/reference/milestone.html Example workflow that simulates survival data, cuts it at 150 events, and applies the milestone() function with test_type = "log-log". The output shows the method, parameter, estimate, standard error, and z statistic. ```r cut_data <- sim_pw_surv(n = 200) |> cut_data_by_event(150) cut_data |> milestone(10, test_type = "log-log") #> $method #> [1] "milestone" #> #> $parameter #> [1] 10 #> #> $estimate #> [1] 0.5293995 #> #> $se #> [1] 0.2147341 #> #> $z #> [1] 2.465372 #> ``` -------------------------------- ### fit_pwexp() examples with ex1_delayed_effect and treatment comparison Source: https://merck.github.io/simtrial/reference/fit_pwexp.html Runs fit_pwexp() on the ex1_delayed_effect dataset, first with default arguments and then separately by treatment group. The output shows the estimated rates and -2 log-likelihood per interval, and the code computes a chi-square test for treatment effect and compares with a logrank test via survdiff(). Note that the example uses rate0 and rate1 objects that are not defined in the visible code, so the final pchisq() call may fail if run as-is. ```r # Use default arguments for delayed effect example dataset (ex1_delayed_effect) library(survival) # Example 1 rateall <- fit_pwexp() rateall #> intervals ttot event rate m2ll #> 1 3 937.1785 97 0.10350216 634.0236 #> 2 3 605.3572 71 0.11728612 446.3257 #> 3 3 346.8482 30 0.08649317 206.8614 #> 4 Inf 254.1148 20 0.07870458 141.6822 # Example 2 # Estimate by treatment effect rate1 <- with(subset(ex1_delayed_effect, trt == 1), fit_pwexp(Surv(month, evntd))) rate0 <- with(subset(ex1_delayed_effect, trt == 0), fit_pwexp(Surv(month, evntd))) rate1 #> intervals ttot event rate m2ll #> 1 3 620.4375 64 0.10315302 418.75734 #> 2 3 415.8482 36 0.08657005 248.16970 #> 3 3 256.2053 19 0.07415927 136.85853 #> 4 Inf 205.4186 13 0.06328542 97.76261 rate0 #> intervals ttot event rate m2ll #> 1 3 316.74106 33 0.1041861 215.26408 #> 2 3 189.50899 35 0.1846878 188.23619 #> 3 3 90.64288 11 0.1213554 68.39871 #> 4 Inf 48.69624 7 0.1437483 41.15568 rate1$rate / rate0$rate #> [1] 0.9900847 0.4687372 0.6110917 0.4402517 # Chi-square test for (any) treatment effect (8 - 4 parameters = 4 df) pchisq(sum(rateall$m2ll) - sum(rate1$m2ll + rate0$m2ll), df = 4, lower.tail = FALSE ) #> [1] 0.006424744 # Compare with logrank survdiff(formula = Surv(month, evntd) ~ trt, data = ex1_delayed_effect) #> Call: #> survdiff(formula = Surv(month, evntd) ~ trt, data = ex1_delayed_effect) #> #> N Observed Expected (O-E)^2/E (O-E)^2/V #> trt=0 121 86 67.7 4.97 7.35 #> trt=1 240 132 150.3 2.24 7.35 #> #> Chisq= 7.3 on 1 degrees of freedom, p= 0.007 # Example 3 # Simple model with 3 rates same for each for 3 months, # different for each treatment after months rate1a <- with(subset(ex1_delayed_effect, trt == 1), fit_pwexp(Surv(month, evntd), 3)) rate0a <- with(subset(ex1_delayed_effect, trt == 0), fit_pwexp(Surv(month, evntd), 3)) rate1a$rate / rate0a$rate #> [1] 0.9900847 0.4808339 m2ll0 <- rateall$m2ll[1] + rate1a$m2ll[2] + rate0a$m2ll[2] m2ll1 <- sum(rate0$m2ll) + sum(rate1$m2ll) # As a measure of strength, chi-square examines improvement in likelihood pchisq(m2ll0 - m2ll1, df = 5, lower.tail = FALSE) #> [1] 0.741822 ``` -------------------------------- ### Load required libraries for simtrial MaxCombo vignette Source: https://merck.github.io/simtrial/articles/maxcombo.html Loads the simtrial, knitr, dplyr, and gt packages needed for the examples in this vignette. ```r library(simtrial) library(knitr) library(dplyr) library(gt) ``` -------------------------------- ### maxcombo() example with return_corr = TRUE Source: https://merck.github.io/simtrial/reference/maxcombo.html Example pipeline that simulates a survival dataset, cuts it at 150 events, and applies `maxcombo()` with `rho = c(0, 0)` and `gamma = c(0, 1)`. The `return_corr = TRUE` flag adds the correlation matrix to the output. The displayed output shows the method, parameter, z-scores, correlation matrix, and p-value. ```r sim_pw_surv(n = 200) |> cut_data_by_event(150) |> maxcombo(rho = c(0, 0), gamma = c(0, 1), return_corr = TRUE) #> $method #> [1] "MaxCombo" #> #> $parameter #> [1] "FH(0, 0) + FH(0, 1)" #> #> $z #> [1] -1.464343 -1.819921 #> #> $corr #> v1 v2 #> 1 1.0000000 0.8574337 #> 2 0.8574337 1.0000000 #> #> $p_value #> [1] 0.05015051 #> ``` -------------------------------- ### Create and apply a cutting function with create_cut() Source: https://merck.github.io/simtrial/reference/create_cut.html Example from the documentation showing how to simulate trial data with sim_pw_surv(), create a cutting function with create_cut() using planned_calendar_time = 45 and target_event_overall = 350, then apply it to the trial data. The output is a cut date of 79.83332. ```r # Simulate trial data trial_data <- sim_pw_surv() # Create a cutting function that applies the following 2 conditions: # - At least 45 months have passed since the start of the study # - At least 300 events have occurred cutting <- create_cut( planned_calendar_time = 45, target_event_overall = 350 ) # Cut the trial data cutting(trial_data) #> [1] 79.83332 ``` -------------------------------- ### Run multitest() with WLR, RMST, and MaxCombo tests Source: https://merck.github.io/simtrial/reference/multitest.html Example from the multitest() documentation. It simulates trial data, cuts it by event count, creates three test functions with create_test(), and runs multitest() with named arguments. The displayed output shows the returned list structure with method, parameter, estimate, se, z, info, info0, and p_value fields. ```r trial_data <- sim_pw_surv(n = 200) trial_data_cut <- cut_data_by_event(trial_data, 150) # create cutting test functions wlr_partial <- create_test(wlr, weight = fh(rho = 0, gamma = 0)) rmst_partial <- create_test(rmst, tau = 20) maxcombo_partial <- create_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 ) #> $wlr #> $wlr$method #> [1] "WLR" #> #> $wlr$parameter #> [1] "FH(rho=0, gamma=0)" #> #> $wlr$estimate #> [1] -18.81751 #> #> $wlr$se #> [1] 5.97492 #> #> $wlr$z #> [1] 3.149416 #> #> $wlr$info #> [1] 37.17333 #> #> $wlr$info0 #> [1] 37.5 #> #> #> $rmst #> $rmst$method #> [1] "RMST" #> #> $rmst$parameter #> [1] 20 #> #> $rmst$estimate #> [1] 2.948586 #> #> $rmst$se #> [1] 1.063905 #> #> $rmst$z #> [1] 2.771474 #> #> #> $maxcombo #> $maxcombo$method #> [1] "MaxCombo" #> #> $maxcombo$parameter #> [1] "FH(0, 0) + FH(0, 0.5)" #> #> $maxcombo$z #> [1] -3.149416 -3.268623 #> #> $maxcombo$p_value #> [1] 0.0007944802 #> #> ``` -------------------------------- ### Study parameters for Freidlin-Korn example Source: https://merck.github.io/simtrial/articles/modest-wlrt.html Specifies study parameters for the Freidlin and Korn example: 5-year study duration, 2000 patients, very short enrollment period, and piecewise failure rates with hazard ratios 16 for 0.1 years and 0.76 thereafter. ```r study_duration <- 5 sample_size <- 2000 enroll_duration <- .0001 enroll_rate <- data.frame( duration = enroll_duration, rate = sample_size / enroll_duration ) fail_rate <- data.frame( stratum = "All", fail_rate = 0.25, dropout_rate = 0, hr = c(4 / .25, .19 / .25), duration = c(.1, 4.9) ) ``` -------------------------------- ### Run simulation with nested parallel backend using foreach and %dofuture% Source: https://merck.github.io/simtrial/articles/parallel.html Runs the simulation using the nested parallel backend. Sets a seed, defines enrollment rates, and uses foreach with %dofuture% to execute sim_fixed_n in parallel. The .options.future argument with seed = TRUE ensures reproducibility. ```R set.seed(1) enroll_rates <- list(enroll_rate1, enroll_rate2) nested_result <- foreach::foreach( i = 1:2, .combine = "list", .options.future = list(seed = TRUE) ) %dofuture% { sim_fixed_n( n_sim = n_sim, sample_size = 3000, target_event = 700, enroll_rate = enroll_rates[[i]], timing_type = 2 # Time until targeted event count achieved ) } ``` -------------------------------- ### Summarize simulations and convert to gt table Source: https://merck.github.io/simtrial/reference/as_gt.html Complete example that sets up enrollment and failure rates, builds a group sequential design, runs simulations, summarizes results with a bound, and converts to a gt table. The output shows the resulting table with columns for analysis, time, N, event, and crossing probability. ```r # Parameters for enrollment enroll_rampup_duration <- 4 # Duration for enrollment ramp up enroll_duration <- 16 # Total enrollment duration enroll_rate <- gsDesign2::define_enroll_rate( duration = c( enroll_rampup_duration, enroll_duration - enroll_rampup_duration), rate = c(10, 30)) # Parameters for treatment effect delay_effect_duration <- 3 # Delay treatment effect in months median_ctrl <- 9 # Survival median of the control arm median_exp <- c(9, 14) # Survival median of the experimental arm dropout_rate <- 0.001 fail_rate <- gsDesign2::define_fail_rate( duration = c(delay_effect_duration, 100), fail_rate = log(2) / median_ctrl, hr = median_ctrl / median_exp, dropout_rate = dropout_rate) # Other related parameters alpha <- 0.025 # Type I error beta <- 0.1 # Type II error ratio <- 1 # Randomization ratio (experimental:control) # Build a one-sided group sequential design design <- gsDesign2::gs_design_ahr( enroll_rate = enroll_rate, fail_rate = fail_rate, ratio = ratio, alpha = alpha, beta = beta, analysis_time = c(12, 24, 36), upper = gsDesign2::gs_spending_bound, upar = list(sf = gsDesign::sfLDOF, total_spend = alpha), lower = gsDesign2::gs_b, lpar = rep(-Inf, 3)) # Define cuttings of 2 IAs and 1 FA ia1_cut <- create_cut(target_event_overall = ceiling(design$analysis$event[1])) ia2_cut <- create_cut(target_event_overall = ceiling(design$analysis$event[2])) fa_cut <- create_cut(target_event_overall = ceiling(design$analysis$event[3])) # Run simulations simulation <- sim_gs_n( n_sim = 3, sample_size = ceiling(design$analysis$n[3]), enroll_rate = design$enroll_rate, fail_rate = design$fail_rate, test = wlr, cut = list(ia1 = ia1_cut, ia2 = ia2_cut, fa = fa_cut), weight = fh(rho = 0, gamma = 0.5)) #> Backend uses sequential processing. # Summarize simulations simulation |> summary(bound = gsDesign::gsDesign(k = 3, test.type = 1, sfu = gsDesign::sfLDOF)$upper$bound) |> simtrial::as_gt() ``` -------------------------------- ### rmst_single_arm example with ex1_delayed_effect Source: https://merck.github.io/simtrial/reference/rmst_single_arm.html Example usage of rmst_single_arm with the ex1_delayed_effect dataset, filtering to treatment arm 1. The call computes RMST at tau=10 for the treatment group, with alpha=0.05. The output shows the resulting data frame with cutoff_time=10, group="Treatment 1", rmst=6.495175, variance=0.05711322, std=0.2389837, lcl=6.026776, ucl=6.963575, and event=127. ```r data(ex1_delayed_effect) data_single_arm <- ex1_delayed_effect[ex1_delayed_effect$trt == 1, ] simtrial:::rmst_single_arm( time_var = data_single_arm$month, event_var = data_single_arm$evntd, tau = 10, group_label = "Treatment 1", alpha = 0.05 ) #> cutoff_time group rmst variance std lcl ucl event #> 1 10 Treatment 1 6.495175 0.05711322 0.2389837 6.026776 6.963575 127 ``` -------------------------------- ### randomize_by_fixed_block examples with dplyr Source: https://merck.github.io/simtrial/reference/randomize_by_fixed_block.html Two examples in one block: first, a 2:1 randomization with block size 3 and treatments "A" and "B" applied to a data frame of 10 rows; second, stratified randomization by a stratum column with two groups, using the default block c(0,0,1,1). The output shows the resulting treatment assignments. ```r library(dplyr) # Example 1 # 2:1 randomization with block size 3, treatments "A" and "B" data.frame(x = 1:10) |> mutate(Treatment = randomize_by_fixed_block(block = c("A", "B", "B"))) #> x Treatment #> 1 1 A #> 2 2 B #> 3 3 B #> 4 4 B #> 5 5 A #> 6 6 B #> 7 7 B #> 8 8 B #> 9 9 A #> 10 10 A # Example 2 # Stratified randomization data.frame(stratum = c(rep("A", 10), rep("B", 10))) |> group_by(stratum) |> mutate(Treatment = randomize_by_fixed_block()) #> # A tibble: 20 × 2 #> # Groups: stratum [2] #> stratum Treatment #> #> 1 A 1 #> 2 A 1 #> 3 A 0 #> 4 A 0 #> 5 A 0 #> 6 A 0 #> 7 A 1 #> 8 A 1 #> 9 A 0 #> 10 A 0 #> 11 B 1 #> 12 B 0 #> 13 B 0 #> 14 B 1 #> 15 B 1 #> 16 B 1 #> 17 B 0 #> 18 B 0 #> 19 B 0 #> 20 B 0 ``` -------------------------------- ### Set up multisession backend with 2 workers Source: https://merck.github.io/simtrial/articles/parallel.html Initializes the multisession backend for parallel processing with two workers. By default, all available cores are used, but here we specify two. ```r plan(multisession, workers = 2) ``` -------------------------------- ### WLR test with FH weights and return_variance=TRUE Source: https://merck.github.io/simtrial/reference/wlr.html Performs the same WLR test as Example 1A but with return_variance=TRUE, which returns the variance-covariance matrix in addition to the standard output. ```r x |> wlr(weight = fh(rho = 0, gamma = 0.5), return_variance = TRUE) ``` -------------------------------- ### Example: Cut simulated data at 100 events and tabulate Source: https://merck.github.io/simtrial/reference/cut_data_by_event.html Simulates 200 observations with `sim_pw_surv()` and cuts the data at 100 events using the pipe operator. The resulting table shows the counts of censored (event = 0) and event (event = 1) subjects per treatment, with 50 events in each arm. ```R # Use default enrollment and event rates at cut at 100 events x <- sim_pw_surv(n = 200) |> cut_data_by_event(100) table(x$event, x$treatment) #> #> control experimental #> 0 48 49 #> 1 50 50 ``` -------------------------------- ### Simulate and cut survival data with sim_pw_surv and cut_data_by_event Source: https://merck.github.io/simtrial/reference/wlr.html Generates a simulated dataset with 200 patients and cuts it at the time of the 100th event. This dataset is used in the subsequent WLR test examples. ```r x <- sim_pw_surv(n = 200) |> cut_data_by_event(100) ``` -------------------------------- ### Run sim_fixed_n() with all timing types Source: https://merck.github.io/simtrial/articles/routines.html Simulates 2 trials with a sample size of 500 and target events of 350, using all five data cutoff methods. The output is formatted with gt() and fmt_number(). The backend uses sequential processing. ```R sim_fixed_n( n_sim = 2, # Number of simulations sample_size = 500, # Trial sample size target_event = 350, # Targeted events at analysis stratum = stratum, # Study stratum enroll_rate = enroll_rate, # Enrollment rates fail_rate = fail_rate, # Failure rates total_duration = 30, # Planned trial duration block = block, # Block for treatment timing_type = 1:5, # Use all possible data cutoff methods rho_gamma = rho_gamma # FH test(s) to use; in this case, logrank ) |> gt() |> fmt_number(columns = c("ln_hr", "z", "duration")) #> Backend uses sequential processing. ``` -------------------------------- ### Set up input variables for sim_pw_surv() Source: https://merck.github.io/simtrial/articles/routines.html Defines stratum, block, enroll_rate, fail_rate, and dropout_rate for a trial with 400 patients. The strand division into Negative and Positive, block randomization with control/experimental arms, and piecewise constant enrollment and failure rates are configured here. ```R stratum <- data.frame(stratum = c("Negative", "Positive"), p = c(.5, .5)) block <- c(rep("control", 2), rep("experimental", 2)) enroll_rate <- data.frame(rate = c(3, 6, 9), duration = c(3, 2, 1)) fail_rate <- data.frame( stratum = c(rep("Negative", 4), rep("Positive", 4)), period = rep(1:2, 4), treatment = rep(c(rep("control", 2), rep("experimental", 2)), 2), duration = rep(c(3, 1), 4), rate = log(2) / c(4, 9, 4.5, 10, 4, 9, 8, 18) ) dropout_rate <- data.frame( stratum = c(rep("Negative", 4), rep("Positive", 4)), period = rep(1:2, 4), treatment = rep(c(rep("control", 2), rep("experimental", 2)), 2), duration = rep(c(3, 1), 4), rate = rep(c(.001, .001), 4) ) ``` -------------------------------- ### rmst() with string column names Source: https://merck.github.io/simtrial/reference/rmst.html Example using the ex1_delayed_effect dataset with string column names. The output shows the RMST method, tau parameter, estimate, standard error, and z-statistic. ```r data(ex1_delayed_effect) rmst( data = ex1_delayed_effect, var_label_tte = "month", var_label_event = "evntd", var_label_group = "trt", tau = 10, reference = "0" ) #> $method #> [1] "RMST" #> #> $parameter #> [1] 10 #> #> $estimate #> [1] 0.8650493 #> #> $se #> [1] 0.3900344 #> #> $z #> [1] 2.21788 #> ``` -------------------------------- ### Estimate single-arm RMST with simtrial:::rmst_single_arm Source: https://merck.github.io/simtrial/articles/rmst.html Estimates RMST for a single arm using the delayed effect example dataset, with tau set to 10, and displays the result as a gt table. ```r data(ex1_delayed_effect) data_single_arm <- ex1_delayed_effect[ex1_delayed_effect$trt == 1, ] simtrial:::rmst_single_arm( time_var = data_single_arm$month, event_var = data_single_arm$evntd, tau = 10 ) |> gt() ``` -------------------------------- ### WLR with FH(0,1) using sim_fixed_n Source: https://merck.github.io/simtrial/reference/sim_fixed_n.html Runs a single simulation with timing_type=1 and rho=0, gamma=1. The output shows the WLR FH(rho=0, gamma=1) method with estimate, se, z, event, ln_hr, cut, duration, and sim columns. The power approximation is also shown as 0. ```R # Example 2: WLR with FH(0,1) ---- sim_fixed_n(n_sim = 1, timing_type = 1, rho_gamma = data.frame(rho = 0, gamma = 1)) #> Backend uses sequential processing. #> method parameter estimate se z event ln_hr #> 1 WLR FH(rho=0, gamma=1) -1.608741 1.14817 1.401136 90 -0.2204257 #> cut duration sim #> 1 Planned duration 30 1 # Get power approximation mean(x$z <= qnorm(.025)) #> [1] 0 ``` -------------------------------- ### Set up input parameters for sim_fixed_n() Source: https://merck.github.io/simtrial/articles/routines.html Defines stratum, enrollment rates, failure rates, treatment block, and rho_gamma for a 2-arm trial simulation. These parameters are used as arguments to sim_fixed_n(). ```R stratum <- data.frame(stratum = "All", p = 1) enroll_rate <- data.frame( duration = c(2, 2, 10), rate = c(3, 6, 9) ) fail_rate <- data.frame( stratum = "All", duration = c(3, 100), fail_rate = log(2) / c(9, 18), hr = c(0.9, 0.6), dropout_rate = rep(0.001, 2) ) block <- rep(c("experimental", "control"), 2) rho_gamma <- data.frame(rho = 0, gamma = 0) ``` -------------------------------- ### cut_data_by_date function signature Source: https://merck.github.io/simtrial/reference/cut_data_by_date.html Usage signature for the cut_data_by_date function. It takes a time-to-event dataset x and a cut_date relative to the start of randomization (cte) and returns a data frame ready for survival analysis. ```r cut_data_by_date(x, cut_date) ``` -------------------------------- ### Get analysis date at 24 months with get_analysis_date Source: https://merck.github.io/simtrial/reference/get_analysis_date.html Calls get_analysis_date with planned_calendar_time = 24 and leaves all other arguments at their default NA. The function returns the analysis date as 24, matching the planned calendar time. ```R get_analysis_date( simulated_data, planned_calendar_time = 24 ) #> [1] 24 ``` -------------------------------- ### Piecewise uniform enrollment simulation with rpwexp_enroll() Source: https://merck.github.io/simtrial/reference/rpwexp_enroll.html Simulates piecewise uniform enrollment for 100,000 patients with rates 5, 15, and 30 over durations 100, 200, and 100, then plots the enrollment times. The comment notes this approximates piecewise exponential inter-arrival times. ```r # Example 1 # Piecewise uniform (piecewise exponential inter-arrival times) for 10k patients enrollment # Enrollment rates of 5 for time 0-100, 15 for 100-300, and 30 thereafter x <- rpwexp_enroll( n = 1e5, enroll_rate = data.frame( rate = c(5, 15, 30), duration = c(100, 200, 100) ) ) plot(x, 1:1e5, main = "Piecewise uniform enrollment simulation", xlab = "Time", ylab = "Enrollment" ) ```