set_levels: Set simulation levels

View source: R/set_levels.R

set_levelsR Documentation

Set simulation levels


Set one or more simulation levels, which are things that vary between simulation replicates.


set_levels(sim, ..., .add = FALSE, .keep = NA)



A simulation object of class sim_obj, usually created by new_sim


One or more key-value pairs representing simulation levels. Each value can either be a vector (for simple levels) or a list of lists (for more complex levels). See examples.


Only relevant if set_levels is called twice or more. On the second call, if add=FALSE (default) the old set of levels will be replaced by the new set, whereas if add=TRUE the new set of levels will be merged with the old set. See examples.


An integer vector specifying which level combinations to keep; see examples.


The original simulation object with the old set of levels replaced with the new set


# Basic usage is as follows:
sim <- new_sim()
sim %<>% set_levels(
  "n" = c(10, 100, 1000),
  "theta" = c(2, 3)

# More complex levels can be set using lists:
sim %<>% set_levels(
  "n" = c(10, 100, 1000),
  "theta" = c(2, 3),
  "method" = list(
    "spline1" = list(knots=c(2,4), slopes=c(0.1,0.4)),
    "spline2" = list(knots=c(1,5), slopes=c(0.2,0.3))

# By default, set_levels will overwrite old levels if it is called twice:
sim %<>% set_levels(alpha=c(1,2), beta=c(5,6))
sim %<>% set_levels(alpha=c(3,4), gamma=c(7,8))

# To merge the old levels with the new levels instead, specify .add=TRUE:
sim %<>% set_levels(alpha=c(1,2), beta=c(5,6))
sim %<>% set_levels(alpha=c(3,4), gamma=c(7,8), .add=TRUE)

# If you don't want to run simulations for all level combinations, use the
# .keep option. First, set the levels normally. Second, view the
# sim$levels_grid dataframe to examine the level combinations and the
# associated level_id values. Third, call set_levels again with the .keep
# option to specify which levels to keep (via a vector of level_id values).
sim %<>% set_levels(alpha=c(1,2,3), beta=c(5,6))
sim %<>% set_levels(.keep=c(1,2,6))

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