Brms multilevel with splines

Hi everyone!
New to the forum, first brms model.
I am triying to fit a multilevel model with random intercepts + slopes, and I am not sure how to add the splines to random efects in brms.

model<- brm(
bf( y ~ +Time + gender+ageonset +t x + Time:gender+ Time:ageonset+
(Time | ID)),
data = dat,
family = “gaussian”,
prior = c(
prior(normal( 0, 1), class = “Intercept”),
prior(normal( 1, 1), class = “b”, coef = “Timestd” ),
prior(normal(0, 1), class = “b”),
prior(exponential(1), class = “sd”),
prior(lkj(1), class = “cor”),
prior(exponential(1), class = “sigma”)),
control = list(adapt_delta = .95),
iter = 2000, warmup = 1000, chains = 4, cores = 4,
seed = 1215)

I read in previous post you can add s(Time, ageonset, bs = “cr”, k = 10) + s(Time, gender, bs = “cr”, k = 10) but do not know how to add that to the random effects (Time | ID).

Thanks for your help.

PS = is there something about strategies for selecting priors for splines?

Howdy. If you are looking for so-called ‘random smooths’ then you can use s(Time, ID, bs="fs")
Note that this can take an impractically long amount of time to fit if ID is of any decent size. You should probably have a look at the documentation for the mgcv package for factor.smooth and smooth.construct.fs.smooth.spec https://cran.r-project.org/web/packages/mgcv/mgcv.pdf so that you know what is happening (smooths in brms are implemented through mgcv). There are multiple ways to have smooths vary by factor, and they all do something a bit different.
For priors, this post might help Better priors (non-flat) for GAMs (brms) - #4 by ucfagls

Hi, thanks a lot for the info, the post “Better priors” really clarifies many things.
I actually tried s(Time, ID, bs=“fs”) and took reaaaally long to run. Anyway I am not sure this allows varyiing intercepts for each ID. Also not sure if adding also (1 | ID) is appropiate (did not try, may run forever ).
I will continue trying to figure it out with mgcv document.
Thanks again

@Sebastian_camerlingo you might also see this post Does brms have issues with (perfect) linear dependencies between (smooth) covariates? - #2 by martinmodrak by @martinmodrak who seems to indicate that you could use the ‘splines’ package and work directly with non-penalized splines. I have never tried this, so I can’t vouch for how it works.

1 Like