Skip to content

Commit

Permalink
change parallel simulation set up
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleBeannie committed Jan 24, 2025
1 parent eb802f7 commit 40f3848
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 26 deletions.
4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ Suggests:
survMisc,
survRM2,
testthat,
tidyr,
doRNG,
tictoc
tidyr
LinkingTo:
Rcpp
Roxygen: list(markdown = TRUE)
Expand Down
12 changes: 3 additions & 9 deletions vignettes/sim_fixed_design_custom.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ library(dplyr)
library(tibble)
library(gt)
library(doFuture)
library(doRNG)
library(tictoc)
set.seed(2025)
```
Expand Down Expand Up @@ -279,17 +277,14 @@ one_sim <- function(sim_id = 1,

After that, we will execute `one_sim()` multiple times using parallel computation. The following lines of code takes 4 works to run 100 simulations.
```{r}
tic()
registerDoFuture()
registerDoRNG()
plan("multisession", workers = 4)
set.seed(2025)
ans <- foreach(
sim_id = seq_len(n_sim),
.combine = "rbind",
.errorhandling = "stop"
) %dorng% {
.errorhandling = "stop",
.options.future = list(seed = TRUE)
) %dofuture% {
ans_new <- one_sim(
sim_id = sim_id,
# arguments from Step 1: design characteristic
Expand All @@ -315,7 +310,6 @@ ans <- foreach(
}
plan("sequential")
toc()
```

The output from the parallel computation resembles the output of `sim_fix_n()` described in the vignette [Simulate Fixed Designs with Ease via sim_fixed_n](https://merck.github.io/simtrial/articles/sim_fixed_design_simple.html). Each row in the output corresponds to the simulation results for each testing method per each repeation.
Expand Down
18 changes: 4 additions & 14 deletions vignettes/sim_gs_design_custom.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ library(dplyr)
library(tibble)
library(gt)
library(doFuture)
library(doRNG)
library(tictoc)
set.seed(2025)
```
Expand All @@ -35,7 +33,7 @@ If users are interested in more complex scenarios such as the ones listed below,
The parameters setup to scratch a group sequential simulation is very similar to Step 1 of the vignette [Simulate Group Sequential Designs with Ease via sim_gs_n](https://merck.github.io/simtrial/articles/sim_gs_design_simple.html). To shorten the length of this vignette, we will use the same design characteristics and cutting method.

```{r}
n_sim <- 1e4
n_sim <- 1e2
stratum <- data.frame(stratum = "All", p = 1)
block <- rep(c("experimental", "control"), 2)
enroll_rate <- data.frame(stratum = "All", rate = 1, duration = 12)
Expand Down Expand Up @@ -128,17 +126,12 @@ one_sim <- function(sim_id = 1,
Then we run `one_sim()` `r n_sim` times via parallel computation.

```{r}
tic()
registerDoFuture()
registerDoRNG()
plan("multisession", workers = 4)
set.seed(2025)
ans <- foreach(
sim_id = seq_len(n_sim),
.combine = "rbind",
.errorhandling = "stop"
) %dorng% {
.errorhandling = "stop",
.options.future = list(seed = TRUE)
) %dofuture% {
ans_new <- one_sim(
sim_id = sim_id,
# arguments from Step 1: design characteristic
Expand All @@ -159,9 +152,6 @@ ans <- foreach(
ans_new
}
plan("sequential")
toc()
```

The output of `sim_gs_n` is a data frame with one row per simulation per analysis per testing method.
Expand Down

0 comments on commit 40f3848

Please sign in to comment.