run: Run a simulation function

Description Usage Arguments Details Value Examples

Description

Run a simulation function

Usage

1
run(data, fun, fixed_parameters, add_data = FALSE, ...)

Arguments

data

a data.frame (or tibble, in future)

fun

a function

fixed_parameters

an environment or list

...

additional parameters passed to 'fun'

Details

'fun' must have signature (a1, a2, <...>, aN, fixed_params, ...) where

1) N is the number of columns in data (the names of these arguments don't matter). Note <...> elides intervening arguments and is NOT R's ... parameter!

2) the ... parameter is optional

3) The function must return a data.frame.

Value

a data.frame equivalent (tbl_df) including the columns of data and the return from 'fun'

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
growth <- function(n, r, K, b) {
  # Ricker-like growth curve in n = log N
  # this is an obviously-inefficient way to do this ;)
  n  + r - exp(n) / K - b - rnorm(1, 0, 0.1)
}
data <- expand.grid(
                    b = seq(0.01, 0.5, length.out=10),
                    K = exp(seq(0.1, 5, length.out=10)),
                    r = seq(0.5, 3.5, length.out=10)
                    )
initial_data = list(N0=0.9, T=5, reps=10)

growth_runner <- function(r, K, b, ic, ...) {
  n0 = ic$N0
  T = ic$T
  reps = ic$reps
  data.frame(n_final = replicate(reps, {for(t in 1:T) {
                    n0 <- growth(n0, r, K, b)
                    };
                  n0}))
}

output <- run(data, growth_runner, initial_data)
head(cbind(data, output))

ashander/runr documentation built on May 10, 2019, 1:52 p.m.