MyNixOS website logo
Description

Multi-Core Replicate.

Multi-core replication function to make it easier to do fast Monte Carlo simulation. Based on the mcreplicate() function from the 'rethinking' package. The 'rethinking' package requires installing 'rstan', which is onerous to install, while also not adding capabilities to this function.

mcreplicate: Multi-Core Replications

R-CMD-check Codecov testcoverage Lifecycle:stable

mcreplicate adds multi-core functionality to R’s replicate function. It allows easy parallelization on all platforms, including on Windows.

Installation

Install the package from GitHub:

if (!require(remotes)) install.packages("remotes")
remotes::install_github("christophergandrud/mcreplicate")

Use

mc_replicate() works just like replicate(), but distributes the replications on multiple cores

library(mcreplicate)

# Function to replicate
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())
##  [1] -0.04  0.10 -0.02 -0.02  0.05 -0.02 -0.01 -0.07 -0.05  0.00

Windows users

On Windows, mcreplicate relies on a parallel socket cluster backend. This requires the user to explicitly specify which packages and variables should be used to populate the workers’ environments. By default, mcreplicate attaches all currently loaded packages and all variables from the current environment which do not start with a “.”. This can be changed using the packages, varlist and envir optional arguments. You can learn more on the function’s help file.

Example

k = 2

# The following works as intended since the variable "k" is exported by 
# default to each worker.
mc_replicate(10, rnorm(k))

# For a reduced overhead, you can specify to *only* export the variable "k" 
# from the current environment and to not load any particular package.
mc_replicate(10, rnorm(k), packages = NULL, varlist = c("k"), 
             envir = environment())

References

This is inspired by the mcreplicate function from the rethinking package. We added Windows support and we provide a lightweight package.

Metadata

Version

0.1.2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows