Skip to content

Instantly share code, notes, and snippets.

@jebyrnes
Last active February 10, 2023 20:29
Show Gist options
  • Save jebyrnes/d1bdea4ad4c8736c4b9e7ac290e8c940 to your computer and use it in GitHub Desktop.
Save jebyrnes/d1bdea4ad4c8736c4b9e7ac290e8c940 to your computer and use it in GitHub Desktop.
Posthoc contrasts with emmeans, tidybayes, and brms
library(tidyverse)
library(emmeans)
library(brms)
library(tidybayes)
warp.brms <- brm(breaks ~ wool * tension, data = warpbreaks)
#get the adjusted means
warp_em <- emmeans (warp.brms, ~ tension | wool)
warp_em
#get all possible contrasts
cont <- contrast(warm_em, "tukey")
cont
#get the posterior draws from the contrasts
cont_posterior <- gather_emmeans_draws(cont)
#plot
ggplot(cont_posterior,
aes(y = contrast, x = .value)) +
geom_halfeyeh() +
facet_wrap(~wool) +
geom_vline(xintercept = 0, color = "red", lty = 2)
ggplot(cont_posterior,
aes(y = contrast, x = .value, fill = wool, group = wool)) +
geom_halfeyeh(alpha = 0.5)+
geom_vline(xintercept = 0, color = "red", lty = 2)
#need to figure out how to make this look all posterior-y
emmip(warp.brms, ~ tension | wool, CIs = TRUE, cov.reduce = range)
@JAQuent
Copy link

JAQuent commented Apr 24, 2022

Hey thank you so much for this code! This definitely helped me to get going myself. I am wondering though is there any reason why you used contrast(..., "tukey") instead of pairwise? I am getting the same results for both for my model but I want to be sure that I am not missing anything about using tukey for Bayesian mdoels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment