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 toNULL
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 ofTRUE
. Ignored if nof_summarize
function is specified. Set toNULL
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 ofTRUE
. IfFALSE
, then the function will return replications in a list and sof_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.7868038
#> 2 0.4180741
#> 3 0.5673490
#> 4 0.3951253
# 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.06315991 0.377662
# 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.4771196
#>
#> [[2]]
#> y_bar
#> 1 0.3933322
#>
#> [[3]]
#> y_bar
#> 1 1.444174
#>
#> [[4]]
#> y_bar
#> 1 0.1824337
#>