Drawing CIs with ggplot2

Hi everyone,

I just used rstan to fit a nonlinear growth model to my age and length data, which worked just fine. Now I would like to include the credible intervals in my ggplot but I can’t find a good solution to draw them from my posteriors. Does anybody have a good tutorial on how to do it?

Cheers

In general, you can just put your draws of the parameters of interest into a data.frame or tibble, then summarize the things you’re interested in with the quantiles you want your CI to be on: e.g.,

post  <- as.data.frame(fit)

post_summary <- post %>%
  #... selecting/grouping/pivoting
  summarise(
    ci_lo = quantile(variable, probs = 0.05),
    ci_hi = quantile(variable, probs = 0.95)
)

ggplot(post_summary) + geom_ribbon(aes(x, ymin = ci_lo, ymax = ci_hi))
# or whatever geometry you want to draw

Or you can also use bayesplot.

5 Likes