Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed icon indicating copy to clipboard operation
Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed copied to clipboard

4.3.6 tidyverse parallel for the transformed base R

Open ukleiner opened this issue 10 months ago • 0 comments

In section 4.3.6 the base R version t(apply(post[, 1:2], 2, quantile, probs = c(.5, .025, .75))) doesn't have a parallel tidyverse version. One can use a solution like this:

post %>%
  select(b_Intercept, sigma) %>%
  reframe(qs = c(.5, .025, 0.75), across(.cols=c(b_Intercept, sigma), .fns=~quantile(.x, probs=qs)))

The existing tidyverse code uses a superseded function mutate_if the code can be updated to (only the mutate function changes)

post %>%
  pivot_longer(b_Intercept:sigma) %>% 
  group_by(name) %>%
  summarise(mean = mean(value),
            sd   = sd(value),
            `2.5%`  = quantile(value, probs = .025),
            `97.5%` = quantile(value, probs = .975)) %>%
  mutate(across(.cols=is.numeric, .fns=~round(.x, digits = 2)))

Thanks for an excellent book!

ukleiner avatar Apr 04 '24 06:04 ukleiner