Forest plot with chains not merged

Hi all,

I have a fairly simple question. I have posterior draws from fitting a model using cmdstanr. I’m wondering how I can make a forest plot showing medians and credible intervals for each chain (as opposed to merging all chains), like here. I’ve been using bayesplot’s mcmc_intervals function to make forest plots but afaik there’s no option to keep the chains separated. Any recommendations?

Thanks!

tidybayes is a good place to start:

library(tidybayes)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

res <- cmdstanr::cmdstanr_example("logistic")
plot_tbl <- res |> 
  gather_draws(alpha, beta[i]) |> 
  mutate(i = tidyr::replace_na(as.character(i), "")) # may/may not be necessary

ggplot(
  data = plot_tbl,
  mapping = aes(
    y = interaction(.variable, .chain, i), 
    x = .value, 
    colour = as.factor(.chain)
  )
) +
  stat_pointinterval()

Created on 2023-10-24 with reprex v2.0.2

1 Like

Great, thanks a lot!!