Include only certain terms in posterior predictions or `conditional_effects`

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

I’m not sure understand what you’re asking. Do you want the same plot as above, but with mpg ~ 0 when wt=3 (that is, shift the y-axis scale upward by ~ 20 so that mpg=0 occurs where mpg=20 in your example)? Or do you want the x-axis scale shifted to the right by 3 so that wt=0 occurs where wt=3 in your example? Or are you looking for something else entirely?

My statistical vocabulary is a bit limited. I think what I want are partial predictions. Essentially, I want the effect of only the I(wt - 3):am_factor term (the slope), not the effect of both the intercept and the slope (am_factor + I(wt - 3):am_factor).

conditional_smooth does this:

fit2 <- brm(mpg ~ 0 + am_factor + s(wt, by = am_factor),
            data = mtcars2)

conditional_smooths(fit2)

image

Additionally (or alternatively) I hope to be able to generate posterior predictions using only a subset of the model terms.

mgcv::predict.gam() has does this using a terms or exclude parameter to decide which terms are included in the prediction. The rest are set to zero.