Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any faster way than rowwise() to apply a function row-wise to one column of a tibble?

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()
like image 756
user1299648 Avatar asked Dec 05 '25 16:12

user1299648


1 Answers

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()
like image 137
Dmitry Zotikov Avatar answered Dec 08 '25 09:12

Dmitry Zotikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!