simulate: Simulate data and statistical models.

Description Usage Arguments Value See Also Examples

View source: R/simulate.R

Description

simulate simulates data by generating samples according to a user- defined function. These data can then be used to examine whatever statistic you wish. This function is intentionally kept general and flexible, to allow for a wide variety of applications.

Usage

1
2
3
4
simulate(func, params = NULL, n.sims = 5000, output = c("list",
  "dataframe", "vector"), boot = FALSE, bootParams = NULL,
  parallel = c("no", "multicore", "snow"), ncpus = 1, cl = NULL,
  beep = NULL, ...)

Arguments

func

A user-defined function that generates data and performs a statistical test on that data.

params

A list of parameters to be passed to func. Each of these parameters can be a vector of multiple values, and simulate will test each combination of these values.

n.sims

Number of simulations (per combination of params).

output

Specifies how the function provides the ultimate results of the simulations: can return a "list", a "dataframe", or a "vector". Note that the output from the supplied function must be able to be coerced into this output type.

boot

Whether or not to use bootstrapped data to pass along to your simulation.

bootParams

If boot=TRUE, then use bootParams to pass along a named list of arguments to the boot function. The statistic and R parameters will be filled automatically, but at minimum you will need to pass along data. Information about parallel processing will also be passed along automatically.

parallel

The type of parallel operation to be used (if any).

ncpus

Integer: the number of processes to be used in parallel operation.

cl

An optional parallel or snow cluster for use if parallel = 'snow'. If not supplied, a cluster on the local machine is created for the duration of the simulations.

beep

Include a numeric value or character vector indicating the sound you wish to play once the simulation is done running. Requires the "beepr" package, and information about supported values is available in the documentation for that package.

...

Additional arguments to be passed to func. If you do not need to vary certain parameters in your model, you can pass them to func here.

Value

Returns a list (by default) with one element per simulation. If output is specified as "dataframe", then func must return a (named) vector with the results you wish to capture; if output is specified as "vector", then func must return a one-element vector.

See Also

boot

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
lm_test <- function(N, b0, b1) {
x <- rnorm(N, 0, 1)
y <- rnorm(N, b0 + b1*x, sqrt(1 - b1^2))
data <- data.frame(y, x)
model <- lm(y ~ x, data)

# capture output from model summary
est <- coef(summary(model))['x', 'Estimate']
se <- coef(summary(model))['x', 'Std. Error']
p <- coef(summary(model))['x', 'Pr(>|t|)']

return(c(xm=mean(x), xsd=sd(x), ym=mean(y), ysd=sd(y), est=est, se=se, p=p,
    sig=est > 0 & p <= .05))
}

# test power for two sample sizes: N=200 and N=300, with 5000 sims for each
power_sim <- simulate(lm_test, params=list(N=c(200, 300)), n.sims=5000, b0=0, b1=.15)

jeff-hughes/simulateR documentation built on May 19, 2019, 1:45 a.m.