Description Usage Arguments Value See Also Examples
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.
1 2 3 4 |
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 |
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 |
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 |
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 |
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.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.