Smooth spline modeling with brm()

I’m trying smooth spline modeling with brm(). However, it does not seem to allow me to handle smooth splines at the group level as indicated in the following error message:

Error: Cannot handle splines or GPs in group-level terms.

Is there any fundamental issue that does not permit group-level spline modeling? Or is it simply an implementation issue?

In general, please provide the code of the brms models you want to fit.

For your problem I fortunately know what you did so I can comment on your problem without seeing the code.

group-level splines have to be specified within the s() or t2() calls. I suggest you take a look a the doc of mgcv::s and look for “factor-smooth interactions” (argument bs="fs"). You will need to install the latest versions of mgcv and brms in order to run this. Be aware that such models may take very long time to fit.

1 Like

Thanks a lot, Paul!

One more question -

bf(Y ~ s(Age) + s(Age, by=factor1) + s(Age, by=factor2))

works fine. However, is there a way to do something like the following?

bf(Y ~ s(Age) + s(Age, by=factor1) + s(Age, by=factor2) + s(Age, by=factor1:factor2))

Currently it does not like this part: s(Age, by=factor1:factor2)

1 Like

I think you can do s(Age, by = interaction(factor1, factor2))


Thanks, Ben! Yes, that’s the trick.


I am very new to brms and R in general and would love some help on this issue.

I am trying to fit this brms model

fit_preference_nl ← brm(form_preference_nonlinear,
data = dfEUPA_NoNAs_SiteVisit_Single,
cores = cores,
chains = chains,
init_r = init_r,
iter = iter,
thin = thin,
seed = seed,
file = paste0(“results”,run_date),
control = control)

The formula I am using is

f_preference_linear ← cEatenUneatenPartiallyLikely ~ 1 + iAge +
cSex +
iDaysSinceLastFed +

and then added this code

form_preference_linear ← bf(f_preference_linear) + categorical()

When I try to fit the model, I the same error message as Gang:

Error: Cannot handle splines or GPs in group-level terms.

In paul.buerkner’s answer I saw he recommended looking into the factor-smooth interactions with argument bs=“fs”, however I am using bs=‘ts’ for my code, does this change the solution?

Thanks, Sean