iterate {dvmisc} | R Documentation |
Same idea as purrr::pmap, but with some different
functionality. It can runs all combinations of vector-valued arguments in
...
or the 1st set, 2nd set, and so forth, and multiple trials can be
run for each scenario, which can be useful for simulations.
iterate(f, ..., all_combinations = TRUE, fix = NULL, trials = 1,
varnames = NULL)
f |
A function. |
... |
Arguments to |
all_combinations |
Logical value for whether to iterate over all
combinations of arguments in |
fix |
List of arguments to |
trials |
Numeric value. |
varnames |
Character vector of names for values that |
Data frame.
# Define function to generate data from N(mu, sigsq) and perform t-test.
f <- function(n = 100, mu = 0, sigsq = 1, alpha = 0.05) {
x <- rnorm(n = n, mean = mu, sd = sqrt(sigsq))
fit <- t.test(x = x, alpha = alpha)
return(list(t = fit$statistic, p = fit$p.value))
}
# Call f once for various sample sizes and means
f %>% iterate(n = c(100, 500), mu = c(0.1, 0.25))
# Run 100 trials for each scenario and calculate empirical power
f %>% iterate(n = c(100, 500), mu = c(0.1, 0.25), trials = 100) %>%
group_by(n, mu) %>%
summarise(mean(p < 0.05))