Marginal predictions at certain levels of grouping terms

Is there a way to get marginal mean predictions for certain levels of grouping terms with brms and emmeans? Using conditional_effects() I understand this is possible as following:

library(dplyr)
library(brms)
data("rent99", package = "gamlss.data")

m <- brm(rentsqm ~ area + yearc + (1|district), data = rent99)

# Get predictions for specific levels
conditions <- data.frame(district = unique(rent99$district))
preds <- conditional_effects(m, effects = "yearc",
                             conditions = conditions, re_formula = NULL) %>% 
  .[[1]] %>% as.data.frame

preds %>% 
  filter(district %in% c("1235", "611"),
         yearc > 1956, yearc < 1957) %>% 
  select(yearc, district, estimate__)
#>      yearc district estimate__
#> 1 1956.303      611   7.058089
#> 2 1956.303     1235   7.592747

Nonetheless, in emmeans the grouping variable is not part of the reference grid. So, as far as I know, it is not possible:

library(emmeans)
emmeans(m, ~ yearc)
#>  yearc emmean lower.HPD upper.HPD
#>   1956   7.17      7.05      7.28
#> 
#> Point estimate displayed: median 
#> HPD interval probability: 0.95

emmeans(m, ~ yearc, at = list(district = c("1235", "611")))
#>  yearc emmean lower.HPD upper.HPD
#>   1956   7.17      7.05      7.28
#> 
#> Point estimate displayed: median 
#> HPD interval probability: 0.95
1 Like

Yes, this looks correct. Also note that conditional_effects is a wrapper around posterior_linpred / posterior_predict - if you need more control, you can compute the predictions you need and summarise and plot as necessary.

I’ve never used emmeans, so I am tagging @rvlenth if they have time to answer (no pressure :-) )

Best of luck with your model!

1 Like

Traveling so can’t look at it right now. But bend has its own emmeans support so look at brms documentation of what options are offered.

R