I have a tibble df in which each row contains a list (beta) that is a posterior distribution (4000 samples). I would like to compute Bayesfactor using bayestestR::bayesfactor_parameters, but the way I did using rowwise() is pretty slow (taking 20 minutes for around 3000 rows). Do you know any faster ways to apply this function to each row of the tibble? Thanks a lot.
df <- tibble(idx = seq(1, 3000), beta = list(rnorm(4000, 0.5, 3)))
df <- df %>%
slice(1:10) %>%
rowwise() %>%
mutate(ioi = bayestestR::
bayesfactor_parameters(posterior = unlist(beta), prior = rnorm(1e4, 0, 10),
direction = "two-sided",
null = c(-1, 1))$log_BF) %>%
ungroup()
Yes! Apply in parallel using multidplyr
cluster <- new_cluster(parallel::detectCores() - 2)
cluster_library(cluster, c('tidyverse', 'furrr'))
cluster_copy([...])
df %>%
rowwise() %>%
partition(cluster) %>%
mutate([...]) %>%
collect()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With