mc_replicate: Multi-core replicate.

Description Usage Arguments Value Source Examples

View source: R/mcreplicate.R

Description

Use multiple cores for repeated evaluation of an expression. This also works on Windows using a parallel socket cluster.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
mc_replicate(
  n,
  expr,
  mc.cores = detectCores(),
  cluster,
  varlist,
  envir,
  packages,
  refresh = 0.1
)

Arguments

n

integer; the number of replications.

expr

the expression (a language object, usually a call) to evaluate repeatedly.

mc.cores

number of cores to use.

cluster

logical. If TRUE then clustering, rather than forking, is used to replicate the specified function in parallel. Note: if you are using Windows, only cluster is available.

varlist

Only used on Windows! Character vector of variable names to export on each worker. Default is all variables in the current environment which do not begin with a ".". See clusterExport for more information.

envir

Only used on Windows! Environment from which to export variables. Default is the environment from which this function was called. See clusterExport for more information.

packages

Only used on Windows! Environment from which to export variables. Default is all loaded packages. See clusterExport for more information.

refresh

Not on Windows! status update refresh interval

Value

A vector, matrix, or list of length n.

Source

Modified from: Richard McElreath (2020). rethinking: Statistical Rethinking book package. R package version 2.13. https://github.com/rmcelreath/rethinking

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
one_sim <- function(n = 100, control_prob = 0.1, rel_effect = 0.01) {
  treat_prob <- control_prob + (control_prob * rel_effect)
  cy <- rbinom(n = n, size = 1, prob = control_prob)
  ty <- rbinom(n = n, size = 1, prob = treat_prob)
  mean(ty) - mean(cy)
  }

  mc_replicate(10, one_sim(), mc.cores = 2)

  # On Windows, when no particular packages or additional variables are needed
 # mc_replicate(10, one_sim(), , mc.cores = 2, packages = NULL,
 #              varlist = "one_sim", envir = environment())

mcreplicate documentation built on June 20, 2021, 9:06 a.m.