| Title: | Parallel Simulator |
| Version: | 0.3.1 |
| Description: | Perform flexible simulation studies using one or multiple computer cores. The package is set up to be usable on high-performance clusters in addition to being run locally (i.e., see the package vignettes for more information). |
| License: | GPL-2 |
| URL: | https://github.com/SachaEpskamp/parSim |
| BugReports: | https://github.com/SachaEpskamp/parSim/issues |
| Imports: | data.table, dplyr, parabar, utils |
| Encoding: | UTF-8 |
| Suggests: | knitr, rmarkdown, ggplot2, tidyr |
| VignetteBuilder: | knitr |
| NeedsCompilation: | no |
| Packaged: | 2026-02-26 12:27:11 UTC; sachaepskamp |
| Author: | Sacha Epskamp |
| Maintainer: | Sacha Epskamp <mail@sachaepskamp.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-02-26 12:40:02 UTC |
Parallel Simulation Studies
Description
Perform flexible simulation studies using one or multiple computer cores. The package is set up to be usable on high-performance clusters in addition to being run locally (i.e., see the package vignettes for more information).
Author(s)
Maintainer: Sacha Epskamp mail@sachaepskamp.com (ORCID) (https://sachaepskamp.com)
Authors:
Mihai Constantin mihai@mihaiconstantin.com (ORCID) (https://mihaiconstantin.com)
Other contributors:
Xinkai Du xinkai.du.xd@gmail.com [contributor]
See Also
Useful links:
Configure The Progress Bar
Description
This function can be used to conveniently configure the progress bar. It is
exported for convenience from the
parabar::parabar package. Please
consult its documentation at
parabar::configure_bar for more
information on how to use it.
Usage
configure_bar(type = "modern", ...)
Arguments
type |
A character string specifying the type of progress bar to be used with
compatible |
... |
A list of named arguments used to configure the progress bar. See the Details section for more information. |
Parallel Simulator
Description
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.
Usage
parSim(
...,
expression,
replications = 1,
reps,
exclude = NULL,
export = NULL,
packages = NULL,
write = FALSE,
name,
nCores = 1,
progress = TRUE,
env = parent.frame()
)
Arguments
... |
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
|
Details
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.
Value
The parSim function returns a
base::data.frame with the results of every
iteration as a row.
See Also
bootnet::bootnet,
parabar::par_lapply, and
parabar::configure_bar.
Examples
# 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)
Parallel Simulator data.table version
Description
The function uses the more efficient data.table in its internal code instead of dplyr, thereby speeding up the function itself and also allowing researchers to use data.table to speed up the code they want to simulate with parSim.
Usage
parSim_dt(
...,
expression,
reps = 1,
write = FALSE,
name,
nCores = 1,
export,
exclude,
debug = FALSE,
progressbar = TRUE,
env = parent.frame()
)
Arguments
... |
Any number of |
expression |
An |
reps |
Integer. The number of replications for each condition. Defaults to |
write |
Logical, should the results be written to a file instead of returned as
a data frame? If |
name |
Name of the file if |
nCores |
An integer value indicating the number of cores to use for parallel
execution. Defaults to |
export |
A character vector of global objects to export to the cluster. |
exclude |
A character vector of logical expressions to exclude cases. These are
combined with |
debug |
Logical. If |
progressbar |
Logical. If |
env |
The environment from which to export variables specified in the
|
Details
The function uses data.table in its internal code instead of dplyr, thereby speeding up the simulation and allowing researchers to use data.table in the code they want to simulate with parSim.
Value
A data.table with the results of each
iteration in each row. Returns NULL if write = TRUE.
Author(s)
Xinkai Du <xinkai.du.xd@gmail.com>