Skip to contents

Bundle a data-generation function, a data-analysis function, and (optionally) a performance summary function into a simulation driver.

Usage

bundle_sim(
  f_generate,
  f_analyze,
  f_summarize = NULL,
  reps_name = "reps",
  seed_name = "seed",
  summarize_opt_name = "summarize",
  row_bind_reps = TRUE
)

Arguments

f_generate

function for data-generation

f_analyze

function for data-analysis. The first argument must be the data, in the format generated by f_analyze().

f_summarize

function for calculating performance summaries across replications. The first argument must be the replicated data analysis results. Default is NULL, so that no summary function is used.

reps_name

character string to set the name of the argument for the number of replications, with a default value of "reps".

seed_name

character string to set the name of the argument for the seed option, with a default value of "seed". Set to NULL to remove the argument from the simulation driver.

summarize_opt_name

character string to set the name of the argument for where to apply f_summarize to the simulation results, with a default value of TRUE. Ignored if no f_summarize function is specified. Set to NULL to remove the argument from the simulation driver.

row_bind_reps

logical indicating whether to combine the simulation results into a data frame using rbind(), with a default value of TRUE. If FALSE, then the function will return replications in a list and so f_summarize must be able to take a list as its first argument.

Value

A function to repeatedly run the `f_generate` and `f_analyze` functions and (optionally) apply `f_summarize` to the resulting replications.

Examples

f_G <- rnorm
f_A <- function(x, trim = 0) data.frame(y_bar = mean(x, trim = trim))
f_S <- function(x, calc_sd = FALSE) {
  if (calc_sd) {
    res_SD <- apply(x, 2, sd)
    res <- data.frame(M = colMeans(x), SD = res_SD)
  } else {
    res <- data.frame(M = colMeans(x))
  }
  res
}

# bundle data-generation and data-analysis functions
sim1 <- bundle_sim(f_generate = f_G, f_analyze = f_A)
args(sim1)
#> function (reps, n, mean = 0, sd = 1, trim = 0, seed = NA_integer_) 
#> NULL
res1 <- sim1(4, n = 70, mean = 0.5, sd = 1, trim = 0.2)
res1
#>       y_bar
#> 1 0.6452644
#> 2 0.6874491
#> 3 0.2052848
#> 4 0.5775816

# bundle data-generation, data-analysis, and performance summary functions
sim2 <- bundle_sim(f_generate = f_G, f_analyze = f_A, f_summarize = f_S)
args(sim2)
#> function (reps, n, mean = 0, sd = 1, trim = 0, calc_sd = FALSE, 
#>     seed = NA_integer_, summarize = TRUE) 
#> NULL
res2 <- sim2(24, n = 7, mean = 0, sd = 1, trim = 0.2, calc_sd = TRUE)
res2
#>                 M       SD
#> y_bar -0.06870148 0.427031

# bundle data-generation and data-analysis functions, returning results as a list
sim3 <- bundle_sim(f_generate = f_G, f_analyze = f_A, row_bind_reps = FALSE)
args(sim3)
#> function (reps, n, mean = 0, sd = 1, trim = 0, seed = NA_integer_) 
#> NULL
res3 <- sim3(4, n = 70, mean = 0.5, sd = 3, trim = 0.2)
res3
#> [[1]]
#>      y_bar
#> 1 0.826796
#> 
#> [[2]]
#>      y_bar
#> 1 1.024805
#> 
#> [[3]]
#>       y_bar
#> 1 0.1259141
#> 
#> [[4]]
#>       y_bar
#> 1 0.4401875
#>