# Differences between by-factor smooths in brms

**URL:** <https://discourse.mc-stan.org/t/differences-between-by-factor-smooths-in-brms/20400>\
**Category:** Modeling\
**Tags:** specification, brms\
**Created:** [January 23, 2021, 9:56pm UTC](https://discourse.mc-stan.org/t/differences-between-by-factor-smooths-in-brms/20400 "2021-01-23T21:56:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Garret](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Garret](https://discourse.mc-stan.org/u/Garret)\
**Post date:** [January 23, 2021, 9:56pm UTC](https://discourse.mc-stan.org/t/differences-between-by-factor-smooths-in-brms/20400/1 "2021-01-23T21:56:26Z")

</div>

Hello,

I am fitting splines in brms, and I am wondering how I can plot the smooth of the difference between two individual smooths in a binary by-factor smooth (the difference smooth). This is very easy to do using mgcv, but I’m not sure how to get the same results (and plots) with brms. I’m also wondering how to interpret the brms output with a by-factor smooth when I use an ordered binary factor.

Here’s an example of what I am trying (using the mtcars dataset)

mtcars$vs ← as.ordered(mtcars$vs)  
brms\_mod ← brm(mpg ~ s(hp) + vs + s(hp, by=vs), data = mtcars)  
gam\_mod ← gam(mpg ~ s(hp) + vs + s(hp, by=vs), data = mtcars)

As I understand it in mgcv, if I use an ordered factor in a by-factor smooth, the model will produce the difference in the smooth between two categories of the factor (if it’s binary). I assume this would be the same in brms, but the output looks a bit different, so I’m not 100% sure.

Here is the output from the brms model above (brms\_mod):

 ![Screen Shot 2021-01-23 at 4.33.09 PM](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/1/1491ea94453cd048b401b210c9eaf5a220a032cd.png)

And here is the screenshot from the mgcv model (gam\_mod):

 ![Screen Shot 2021-01-23 at 4.36.21 PM](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/8/872917f84137047f13abaa6ee7fc413fa3360d6b.png)

From what I understand for splines in brms models, the sds() parameters for the smooth terms are the estimates of the “wiggliness”. With an ordered factor in brms, would the term “sds(shpvs1\_1)” represent the wiggliness of the difference smooth? In the mgcv model, I would interpret the term s(hp):vs1 as the estimate of the difference smooth. This term in the mgcv model has an EDF of 1, so there’s no wiggliness in the difference smooth.

I would also like to be able to plot the difference between two smooths. With an ordered binary factor, mgcv produces this automatically. For the mgcv model above, I can obtain this plot from the mgcv model above with plot(gam\_mod):

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/d/d64439ab84b15455369a58e31b44add199779ae3.png)

When I use conditional\_effects(brms\_mod, “hp:vs”) to obtain the smooth plots from the brms model , I obtain each smooth within each factor level on the same plot. Here is what I get:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/6/6e3dee4fc14067e70d26cd6e720b00f9cfee9b49.png)

This plot is very helpful for visualizing the results, but I would also like to see the difference smooth (with intervals) from brms. Is there a way to obtain that with conditional\_effects()?

Thank you!

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [February 5, 2021, 10:32am UTC](https://discourse.mc-stan.org/t/differences-between-by-factor-smooths-in-brms/20400/2 "2021-02-05T10:32:01Z")

</div>

Hi,  
sorry for not getting to your question earlier. It is relevant and well written.

I don’t think what you aim for is directly supported, but note that `conditional_effects` really just computes posterior predictions of the model (by default with `posterior_predict`) while changing only the smooth predictor (here `hp`). So what you are getting is AFAIK not the difference of the smooth themselves, but the difference of the whole predictions.

With that in mind, you can use `posterior_predict` (or `posterior_epred` if you want to work without the measurement noise) to predict the values for two sets of conditions, differring only in the `vs` value, then, for each sample, compute the difference and summarise this in a plot. (note that this will also include the fixed `vs` effect). I don’t think there is an

A similar question has been discussed at [Extracting posterior samples of smooth effects in brms](https://discourse.mc-stan.org/t/extracting-posterior-samples-of-smooth-effects-in-brms/10098), so linking for completeness.

I think there was some work in `tidybayes` to let you extract the smooth terms separately ([Add support for mgcv · Issue #130 · mjskay/tidybayes · GitHub](https://github.com/mjskay/tidybayes/issues/130)) but it doesn’t seem to have been finalized (tagging @mjskay for confirmation)

Does that make sense?

Best of luck with your model!

P.S.: Note that you can use triple backticks (```) to format text as code / output.

---

<div class="post-metadata">

**Author:** ![Garret](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Garret](https://discourse.mc-stan.org/u/Garret)\
**Post date:** [February 9, 2021, 2:16pm UTC](https://discourse.mc-stan.org/t/differences-between-by-factor-smooths-in-brms/20400/3 "2021-02-09T14:16:05Z")

</div>

Hi,

Thank you for the helpful reply! I will look into these techniques the prior posts regarding smooths.

I did, however, appear to have found what I was looking for in terms of obtaining the difference smooth in brms that I was getting in mgcv, and it’s more straightforward than I thought. If I take the same brms model I displayed in my first post but use conditional\_smooths(brms\_mod) instead of conditional\_effects(brms\_mod), I obtain the smooth of the base category (vs = 0) and the difference smooth between vs = 0 and vs = 1. I’m now wondering what the exactly the difference between conditional\_smooths and conditional\_effects is. I’ve tried to do some reading on this, but I don’t think I’m quite understanding the difference besides conditional\_smooths plots the smooth term (although, conditional\_effects appears to do that as well). The plot that mgcv produces appears to be nearly identical to what I obtain with conditional\_smooths, so it seems that conditional\_effects wasn’t producing what I was looking for in the first place. Any further guidance/input is greatly appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [February 11, 2021, 3:19pm UTC](https://discourse.mc-stan.org/t/differences-between-by-factor-smooths-in-brms/20400/4 "2021-02-11T15:19:35Z")

</div>

Honestly, I am not sure - the documentation is not exactly clear and I couldn’t understand the code completely. What I am sure, is that `conditional_effects` will take into account all other predictors (assuming they are at they mean / reference values if you don’t specify anything). It is thus possible that `conditional_smooths` either doesn’t do that or does that differently… But really, not completely sure.
