| parSim | R Documentation |
The function parSim takes a set of conditions and an R
base::expression and returns a
base::data.frame with simulation results. The
parSim function is based on
dplyr::dplyr functions for handling the
results, and the parabar::parabar
package for handling parallelization and progress tracking.
parSim(
...,
expression,
replications = 1,
reps,
exclude = NULL,
export = NULL,
packages = NULL,
write = FALSE,
name,
nCores = 1,
progress = TRUE,
env = parent.frame()
)
... |
Any number of |
expression |
An |
replications |
An integer representing the number of times each condition will be
replicated. Please ensure that an adequate number of simulations is used
(i.e., the more the better). Defaults to |
reps |
Deprecated alternative to 'replications' |
exclude |
A list of logical calls indicating the simulation conditions to exclude
cases, written as formula. For example, |
export |
A character string containing the names of the objects to be exported to
the parallel backend. This argument is only relevant when using parallel
execution (i.e., |
packages |
A character vector containing the names of the packages to be loaded on
the parallel backend. For example |
write |
Logical, should the results be written to a file? If |
name |
A character string specifying the base name of the file to save the
results to (a |
nCores |
An integer value indicating the number of cores to use for parallel
execution. Setting this argument to |
progress |
A logical value indicating whether to show a progress bar while running
the simulation. Defaults to |
env |
The environment from which to export variables specified in the
|
The R base::expression should use
object names assigned as conditions, and should return a named list with
single values, or a base::data.frame. If you
want to output more than one row of results per condition, you may return a
base::data.frame with multiple rows. When
running the simulation conditions in parallel, note that all packages needed
should be loaded via the packages argument and all external objects
used within the expression should be exported via the export
argument.
The parSim function returns a
base::data.frame with the results of every
iteration as a row.
bootnet::bootnet,
parabar::par_lapply, and
parabar::configure_bar.
# Determine a function to evaluate for each simulation condition.
bias <- function(x, y) {
# Perform some computation.
result <- abs(x - y)
# Return the result.
return(result)
}
# Run the simulation.
results <- parSim(
# The simulation conditions.
sample_size = c(50, 100, 250),
beta = c(0, 0.5, 1),
sigma = c(0.25, 0.5, 1),
# The expression to evaluate for each simulation condition.
expression = {
# Generate the data.
x <- rnorm(sample_size)
y <- beta * x + rnorm(sample_size, sigma)
# Fit the model.
fit <- lm(y ~ x)
# Compute the relevant quantities.
beta_estimate <- coef(fit)[2]
r_squared <- summary(fit)$r.squared
bias <- bias(beta, beta_estimate)
# Return in a compatible format.
list(
beta_estimate = beta_estimate,
r_squared = r_squared,
bias = bias
)
},
# The number of replications.
replications = 10,
# The conditions to exclude.
exclude = sample_size == 50 | beta <= 0.5,
# The variables to export.
export = c("bias"),
# No packages are required for export.
packages = NULL,
# Do not save the results.
write = FALSE,
# Execute the simulation on a single core.
nCores = 1,
# Show the progress bar.
progress = TRUE
)
# Print the head of the results.
head(results)
# Configure the progress bar.
configure_bar(
type = "modern",
format = "[:bar] [:percent] [:elapsed]",
show_after = 0.15
)
# Run the simulation again with more cores and the updated progress bar.
results <- parSim(
# The simulation conditions.
sample_size = c(50, 100, 250),
beta = c(0, 0.5, 1),
sigma = c(0.25, 0.5, 1),
# The expression to evaluate for each simulation condition.
expression = {
# Generate the data.
x <- rnorm(sample_size)
y <- beta * x + rnorm(sample_size, sigma)
# Fit the model.
fit <- lm(y ~ x)
# Compute the relevant quantities.
beta_estimate <- coef(fit)[2]
r_squared <- summary(fit)$r.squared
bias <- bias(beta, beta_estimate)
# Return in a compatible format.
list(
beta_estimate = beta_estimate,
r_squared = r_squared,
bias = bias
)
},
# The number of replications.
replications = 1000,
# The conditions to exclude.
exclude = sample_size == 50 | beta <= 0.5,
# The variables to export.
export = c("bias"),
# No packages are required for export.
packages = NULL,
# Save the results to a temporary file.
write = TRUE,
# Execute the simulation in parallel.
nCores = 2,
# Show the progress bar.
progress = TRUE
)
# Print the tail of the results.
tail(results)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.