Learn R Programming

⚠️There's a newer version (0.1.2) of this package.Take me there.

mcreplicate

Purpose

Multi-core replication to make Monte Carlo simulation faster more easily. Based on the mcreplicate function from the rethinking package. The rethinking package requires installing rstan, which can be a hurdle, while also not adding capabilities to this function.

Note: multi-core support is not available on Windows.

Installation

Install the package from GitHub:

xfun::pkg_attach2("remotes")

install_github("christophergandrud/mcreplicate")

Use

mc_replicate() works just like replicate(), but distributes the replications.

library(mcreplicate)

# Function to replicate
one_sim <- function(n, control_prob, rel_effect) {
  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)
}

diff_means <- mc_replicate(10, one_sim(n = 100, control_prob = 0.1, 
                                      rel_effect = 0.01))
diff_means
##  [1] -0.03 -0.01 -0.09  0.07 -0.06 -0.07  0.00  0.00  0.05 -0.02

Copy Link

Version

Install

install.packages('mcreplicate')

Monthly Downloads

284

Version

0.1.1

License

AGPL (>= 3)

Maintainer

Christopher Gandrud

Last Published

March 23rd, 2021

Functions in mcreplicate (0.1.1)

mc_replicate

Multi-core replicate.