please imagine a simple 3x2 design. E.g. intervention (no treatment, normal dose, double dose) where we expect the effect of it to differ between sexes. If I fit a simple model (e.g. in brms or rstan), then I can calculate for each subgroup (no treament & female, no treatment &male, normal dose & male etc…) the expected value \mu from the posterior distributions. Then, I can use those to calculate differences between groups as I wish.
My question is: is there already a function that gives you the expected value mu for each subgroup? I know how to calculate them manually (see a reproducible example below for what I mean using mtcars dataset).
library(brms) library(dplyr) # import data d <- mtcars d$cyl <- as.factor(d$cyl) d$vs <- as.factor(d$vs) # specify model f <- bf(mpg ~ cyl * vs) m <- brm( data = d, formula = f ) # calculate mu for the different subgroups posterior_means = as_draws_df(m) %>% mutate( cyl4vs0 = b_Intercept, cyl4vs1 = b_Intercept + b_vs1 , cyl6vs0 = b_Intercept + b_cyl6 , cyl6vs1 = b_Intercept + b_cyl6 + `b_cyl6:vs1`, cyl8vs0 = b_Intercept + b_cyl8 , cyl8vs1 = b_Intercept + b_cyl8 + `b_cyl8:vs1`) %>% select( cyl4vs0, cyl4vs1, cyl6vs0, cyl6vs1, cyl8vs0, cyl8vs1 ) # now we can calcuate what the difference in mu is between the subgroups if we # wanted to know, e.g. posterior difference of cyl4vs0 and cyl6vs1 test <- posterior_means$cyl4vs0 - posterior_means$cyl6vs1