Last active
February 10, 2023 20:29
-
-
Save jebyrnes/d1bdea4ad4c8736c4b9e7ac290e8c940 to your computer and use it in GitHub Desktop.
Posthoc contrasts with emmeans, tidybayes, and brms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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
This is great!
Here's some quick brainstorming inspired by your the comment on your call to
emmip
...(reminds me I need to implement the vertical geom_halfeye sometime... or just a generalization of all of the interval+density variants)
Too many intervals here probably, it's a little mind-bending...
Or you could go the other way and approximate a gradient plot (mind you this brings all the perceptual issues with gradients...)
Heh, the way stat_lineribbon interleaves layers makes that last one kind of painterly. Neat, but I wouldn't use it in practice :)