Created
January 12, 2020 10:56
-
-
Save christophergandrud/37837e7baed1281d6c9d35c2226f3158 to your computer and use it in GitHub Desktop.
Simulate data from discrete power law distribution in parallel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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