Thanks for the additional resource, @JimBob.
Here’s a version of the code applied to a generic brms
example. I have no idea if the second fit makes sense—I just wanted to create a second model to show how to piece everything together. It’s a 100% R plot. No post-processing.
library(tidyverse)
library(brms)
library(bayesplot)
library(tidybayes)
library(patchwork)
library(ggtext)
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
data = epilepsy, family = poisson())
fit2 <- brm(count ~ zAge + zBase * Trt + (1|patient),
data = epilepsy, family = zero_inflated_poisson())
fit1.p <- as_tibble(as.matrix(fit1)) %>%
rename(`Treatment` = `b_Trt1`) %>%
select(`Treatment`)
fit2.p <- as_tibble(as.matrix(fit2)) %>%
rename(`Treatment` = `b_Trt1`) %>%
select(`Treatment`)
fit1H <- as.numeric((hypothesis(fit1,
"Trt1 > 0.1")$hypothesis["Post.Prob"])*100)
fit2H <- as.numeric((hypothesis(fit2,
"Trt1 > 0.1")$hypothesis["Post.Prob"])*100)
fit1.median <- median_hdi(fit1.p) %>%
pull(`Treatment`)
fit2.median <- median_hdi(fit2.p) %>%
pull(`Treatment`)
color_scheme_set(c("#FDE725", "#440154", "#440154",
"#440154", "#440154", "#440154"))
bayesplot_theme_set(theme_bw())
p1 <- fit1.p %>%
mcmc_areas(prob = 0.89, point_est = "median") +
xlab("Title") +
theme(#plot.title.position = "plot",
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.title = element_markdown(face="bold", size=16)) +
xlim(-1,1) +
geom_vline(xintercept = 0, linetype="solid") +
geom_vline(xintercept = -0.1, linetype="dashed") +
geom_vline(xintercept = 0.1, linetype="dashed") +
labs(title="Model 1: Some title",
subtitle = "Something here") +
theme(axis.text = element_text(size=14),
axis.title = element_text(size=15),
plot.subtitle = element_text(size=14)) +
annotate("text", x = 0.3, y = 1.8, hjust = 0,
label = "Some label",
size = 4, color="#818283") +
geom_curve(aes(x = 0, y = 1.8, xend = 0.25, yend = 1.8),
color = "#818283",
arrow = arrow(type = "open",
length = unit(0.1, "inches")),
curvature = 0, angle = 100, ncp =15) +
annotate("text", x = -Inf, y = 1.7, hjust = -0.08,
label = paste0("P(effect > 0.1): ",
round(fit1H, 1), "%"),
size = 4.3, color="black") +
annotate("text", x = -0.7, y = 1.1, hjust = 0,
label = paste0("Median: ",
round(fit1.median, 2)),
size = 4.3, color="#440154")
p2 <- fit2.p %>%
mcmc_areas(prob = 0.89, point_est = "median") +
xlab("Title") +
theme(#plot.title.position = "plot",
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.title = element_markdown(face="bold", size=16)) +
xlim(-1,1) +
geom_vline(xintercept = 0, linetype="solid") +
geom_vline(xintercept = -0.1, linetype="dashed") +
geom_vline(xintercept = 0.1, linetype="dashed") +
labs(title="Model 2: Some title",
subtitle = "Something here") +
theme(axis.text = element_text(size=14),
axis.title = element_text(size=15),
plot.subtitle = element_text(size=14)) +
annotate("text", x = 0.3, y = 1.7, hjust = 0,
label = "Some label",
size = 4, color="#818283") +
geom_curve(aes(x = 0, y = 1.8, xend = 0.25, yend = 1.8),
color = "#818283",
arrow = arrow(type = "open",
length = unit(0.1, "inches")),
curvature = 0, angle = 100, ncp =15) +
annotate("text", x = -Inf, y = 1.8, hjust = -0.08,
label = paste0("P(effect > 0.1): ",
round(fit2H, 1), "%"),
size = 4.3, color="black") +
annotate("text", x = -0.7, y = 1.1, hjust = 0,
label = paste0("Median: ",
round(fit2.median, 2)),
size = 4.3, color="#440154")
((p1 / p2)) + plot_annotation(
title = 'Overall title here',
subtitle = 'Overall subtitle here',
caption = paste0("Data from ", nrow(distinct(epilepsy)), " participants, etc."),
theme = theme(plot.title = element_text(face="bold", size=16))
) & theme(plot.caption = element_markdown(size=12, color="#686869"))