### Examples using init_grid and add_simulation, for primitive workflow
### Use init_reftable and add_reftable for the up-to-date workflow
# example of building a list of simulations from scratch:
myrnorm <- function(mu,s2,sample.size) {
s <- rnorm(n=sample.size,mean=mu,sd=sqrt(s2))
return(c(mean=mean(s),var=var(s)))
}
set.seed(123)
onedistrib <- t(replicate(100,myrnorm(1,1,10))) # toy example of simulated distribution
attr(onedistrib,"par") <- c(mu=1,sigma=1,sample.size=10) ## important!
simuls <- add_simulation(NULL, Simulate="myrnorm", nRealizations=500,
newsimuls=list("example"=onedistrib))
# standard use: smulation over a grid of parameter values
parsp <- init_grid(lower=c(mu=2.8,s2=0.2,sample.size=40),
upper=c(mu=5.2,s2=3,sample.size=40))
simuls <- add_simulation(NULL, Simulate="myrnorm", nRealizations=500,
par.grid = parsp[1:7,])
if (FALSE) # example continued: parallel versions of the same
# Slow computations, notably because cluster setup is slow.
# ... parallel over replicates, serial over par.grid rows
# => cl_seed has no effect and can be ignored
simuls <- add_simulation(NULL, Simulate="myrnorm", nRealizations=500,
par.grid = parsp[1:7,], nb_cores=7)
#
# ... parallel over 'par.grid' rows => cl_seed is effective
simuls <- add_simulation(NULL, Simulate="myrnorm", nRealizations=500,
cl_seed=123, # for repeatable results
par.grid = parsp[1:7,], nb_cores=c(foo=7))
####### Example where a single 'Simulate' returns all replicates:
myrnorm_tab <- function(mu,s2,sample.size, nsim) {
## By default, Infusion.getOption('nRealizations') would fail on nodes!
replicate(nsim,
myrnorm(mu=mu,s2=s2,sample.size=sample.size))
}
parsp <- init_grid(lower=c(mu=2.8,s2=0.2,sample.size=40),
upper=c(mu=5.2,s2=3,sample.size=40))
# 'as_one' syntax for 'Simulate' function returning a simulation table:
simuls <- add_simulation(NULL, Simulate="myrnorm_tab",
nRealizations=c(as_one=500),
nsim=500, # myrnorm_tab() argument, part of the 'dots'
par.grid=parsp)
if (FALSE) # example continued: parallel versions of the same.
# Slow cluster setup again
simuls <- add_simulation(NULL,Simulate="myrnorm_tab",par.grid=parsp,
nb_cores=7L,
nRealizations=c(as_one=500),
nsim=500, # myrnorm_tab() argument again
cl_seed=123, # for repeatable results
# need to export other variables used by *myrnorm_tab* to the nodes:
env=list2env(list(myrnorm=myrnorm)))
## see main documentation page for the package for other typical usage
Run the code above in your browser using DataLab