if (FALSE) {
setwd('my_working_directory')
## run simulations to save the .rds files (or move them to the working directory)
# seeds1 <- genSeeds(design)
# seeds2 <- genSeeds(design, old.seeds=seeds1)
# ret1 <- runSimulation(design, ..., seed=seeds1, filename='file1')
# ret2 <- runSimulation(design, ..., seed=seeds2, filename='file2')
# saves to the hard-drive and stores in workspace
final <- SimCollect(files = c('file1.rds', 'file2.rds'))
final
# If filename not included, can be extracted from results
# files <- c(SimExtract(ret1, 'filename'), SimExtract(ret2, 'filename'))
# final <- SimCollect(files = files)
#################################################
# Example where each row condition is repeated, evaluated independently,
# and later collapsed into a single analysis object
# Each condition repeated four times (hence, replications
# should be set to desired.reps/4)
Design <- createDesign(mu = c(0,5),
N = c(30, 60))
Design
# assume the N=60 takes longer, and should be spread out across more arrays
Design_long <- expandDesign(Design, c(2,2,4,4))
Design_long
replications <- c(rep(50, 4), rep(25,8))
data.frame(Design_long, replications)
#-------------------------------------------------------------------
Generate <- function(condition, fixed_objects) {
dat <- with(condition, rnorm(N, mean=mu))
dat
}
Analyse <- function(condition, dat, fixed_objects) {
ret <- c(mean=mean(dat), SD=sd(dat))
ret
}
Summarise <- function(condition, results, fixed_objects) {
ret <- colMeans(results)
ret
}
#-------------------------------------------------------------------
# create directory to store all final simulation files
dir.create('sim_files/')
iseed <- genSeeds()
# distribute jobs independently
sapply(1:nrow(Design_long), \(i) {
runArraySimulation(design=Design_long, replications=replications,
generate=Generate, analyse=Analyse, summarise=Summarise,
arrayID=i, dirname='sim_files/', filename='job', iseed=iseed)
}) |> invisible()
# check that all replications satisfy target
SimCollect('sim_files/', check.only = TRUE)
# this would have been returned were the target.rep supposed to be 1000
SimCollect('sim_files/', check.only = TRUE, target.reps=1000)
# aggregate into single object
sim <- SimCollect('sim_files/')
sim
SimClean(dir='sim_files/')
}
Run the code above in your browser using DataLab