iterate: Iterate Function Over All Combinations of User-Specified...

Description Usage Arguments Value Examples

View source: R/iterate.R

Description

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.

Usage

1
2
iterate(f, ..., all_combinations = TRUE, fix = NULL, trials = 1,
  varnames = NULL)

Arguments

f

A function.

...

Arguments to f, any of which can be vector-valued.

all_combinations

Logical value for whether to iterate over all combinations of arguments in ..., or just use the first set of elements, then the second, and so on.

fix

List of arguments to f to hold fixed rather than loop over.

trials

Numeric value.

varnames

Character vector of names for values that f returns, to avoid generic labels (V1, V2, ...).

Value

Data frame.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 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))

dvmisc documentation built on Dec. 18, 2019, 1:35 a.m.