I want to show the effects of only a subset of population-level terms. This has been discussed before (Predictions including only certain population-level terms and partial fitted values (like mgcv::predict.gam(., exclude=...) ) ? · Issue #515 · paul-buerkner/brms · GitHub), but I have not found a solution.
Example:
With the mtcars
dataset, I want to find the effect of wt
on mpg
for each transmission type, am
.
library(brms)
mtcars2 <- dplyr::mutate(mtcars,
am_factor = ifelse(am==0, "auto", "manual") |> factor()
)
fit <- brm(mpg ~ 0 + am_factor + I(wt - 3):am_factor,
data = mtcars2)
#> Compiling Stan program...
conditional_effects(fit, effects = "wt:am_factor")
conditional_effects
includes the direct effect of am
. I want two lines that are 0 at wt=3.
I can of cause find the slopes directly as the terms b_am_factorauto:IwtM3
and b_am_factormanual:IwtM3
, but that can be quite difficult to keep track of with more complex models.
brms::posterior_summary(fit)
#> Estimate Est.Error Q2.5 Q97.5
#> b_am_factorauto 20.067360 0.87704812 18.321888 21.758327
#> b_am_factormanual 19.042237 1.04415031 16.910261 21.085518
#> b_am_factorauto:IwtM3 -3.793470 0.82533897 -5.399330 -2.185053
#> b_am_factormanual:IwtM3 -9.076530 1.24107242 -11.547117 -6.704079
#> sigma 2.704675 0.38855295 2.065940 3.574265
#> lprior -2.157401 0.04627711 -2.266762 -2.089414
#> lp__ -77.665693 1.72041473 -82.078406 -75.420340
Created on 2022-05-18 by the reprex package (v2.0.0)
- Operating System: Ubuntu 21.10
- brms Version: 2.17.0