Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophergandrud/37837e7baed1281d6c9d35c2226f3158 to your computer and use it in GitHub Desktop.
Save christophergandrud/37837e7baed1281d6c9d35c2226f3158 to your computer and use it in GitHub Desktop.
Simulate data from discrete power law distribution in parallel
library(xfun)
pkg_attach2("tidyverse", "poweRlaw", "furrr")
plan(multiprocess)
# Simulate from a discrete power law
# @param counter numeric, to enable mapping
# @param n integer, number of draws
# @param xmin numeric, minimum value subject to power law
# @param alpha, scaling parameter
#
# @importFrom poweRlaw rpldis
pwrdis_counter <- function(counter = 1, n = 1e4, xmin = 2, alpha = 3,
drop_box_outliers = FALSE,
drop_99p_outliers = FALSE) {
x <- rpldis(n, xmin = xmin, alpha = alpha)
if (isTRUE(drop_box_outliers))
x <- x[!x %in% boxplot.stats(x)$out]
if (isTRUE(drop_99p_outliers))
x <- x[x < quantile(x = x, probs = 0.99)]
return(x)
}
# Simulate in parallel
# @param n integer, number of simulations
#
# @importFrom furrr future_map future_map_dbl
sim_many <- function(n, ...) {
ndraws %>%
future_map(pwrdis_counter, ...) %>%
future_map_dbl(mean)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment