bundle_sim {simhelpers} | R Documentation |
Bundle functions into a simulation driver function
Description
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_summarize |
function for calculating performance summaries across
replications. The first argument must be the replicated data analysis
results. Default is |
reps_name |
character string to set the name of the argument for the
number of replications, with a default value of |
seed_name |
character string to set the name of the argument for the
seed option, with a default value of |
summarize_opt_name |
character string to set the name of the argument
for where to apply |
row_bind_reps |
logical indicating whether to combine the simulation
results into a data frame using |
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)
res1 <- sim1(4, n = 70, mean = 0.5, sd = 1, trim = 0.2)
res1
# 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)
res2 <- sim2(24, n = 7, mean = 0, sd = 1, trim = 0.2, calc_sd = TRUE)
res2
# 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)
res3 <- sim3(4, n = 70, mean = 0.5, sd = 3, trim = 0.2)
res3