set_levels: Set simulation levels

View source: R/set_levels.R

set_levelsR Documentation

Set simulation levels

Description

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

Usage

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

Arguments

sim

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.

.add

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.

.keep

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

Value

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

Examples

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

# 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))
  )
)
sim$levels

# 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))
sim$levels

# 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)
sim$levels

# 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$levels_grid
sim %<>% set_levels(.keep=c(1,2,6))
sim$levels_grid

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