Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/christophergandrud/mcreplicate
Multi-core replicate for RStats
https://github.com/christophergandrud/mcreplicate
parallel-computing rstats rstats-package simulation
Last synced: 2 months ago
JSON representation
Multi-core replicate for RStats
- Host: GitHub
- URL: https://github.com/christophergandrud/mcreplicate
- Owner: christophergandrud
- License: agpl-3.0
- Created: 2021-03-19T06:19:26.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-30T18:59:46.000Z (over 3 years ago)
- Last Synced: 2024-06-05T02:32:18.510Z (8 months ago)
- Topics: parallel-computing, rstats, rstats-package, simulation
- Language: R
- Homepage:
- Size: 53.7 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.md
Awesome Lists containing this project
- jimsghstars - christophergandrud/mcreplicate - Multi-core replicate for RStats (R)
README
---
output:
github_document:
html_preview: false
---# mcreplicate: Multi-Core Replications
[![R-CMD-check](https://github.com/christophergandrud/mcreplicate/workflows/R-CMD-check/badge.svg)](https://github.com/christophergandrud/mcreplicate/actions)
[![Codecov test coverage](https://codecov.io/gh/christophergandrud/mcreplicate/branch/main/graph/badge.svg)](https://codecov.io/gh/christophergandrud/mcreplicate?branch=main)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#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:
```r
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
```{r}
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())
```### 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```{r, eval=FALSE}
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())
```
## ReferencesThis is inspired by the `mcreplicate` function from the [rethinking](https://github.com/rmcelreath/rethinking) package. We added Windows support and we provide a lightweight package.