run: Run the simulation

View source: R/run.R

runR Documentation

Run the simulation


This is the workhorse function of SimEngine that actually runs the simulation. This should be called after all functions that set up the simulation (set_config, set_script, etc.) have been called.


run(sim, sim_uids = NA)



A simulation object of class sim_obj, usually created by new_sim


Advanced; a vector of sim_uid values, each of which uniquely identifies a simulation replicate. This will normally be omitted. If this is specified, only the simulation replicates with a matching sim_uid will be run.


The original simulation object but with the results attached (along with any errors and warnings). Results are stored in sim$results, errors are stored in sim$errors, and warnings are stored in sim$warnings.


# The following is a toy example of a simulation, illustrating the use of
# the run function.
sim <- new_sim()
create_data <- function(n) { rpois(n, lambda=5) }
est_mean <- function(dat, type) {
  if (type=="M") { return(mean(dat)) }
  if (type=="V") { return(var(dat)) }
sim %<>% set_levels(n=c(10,100,1000), est=c("M","V"))
sim %<>% set_config(num_sim=1)
sim %<>% set_script(function() {
  dat <- create_data(L$n)
  lambda_hat <- est_mean(dat=dat, type=L$est)
  return (list("lambda_hat"=lambda_hat))
sim %<>% run()
sim$results %>% print()

Avi-Kenny/SimEngine documentation built on June 23, 2022, 11:09 a.m.