Generalised Additive Modelling (GAMs) in Stan

Hi, and welcome!

brms uses Stan on the back end, so any model that brms fits is most certainly expressible in Stan code. You can see the Stan code that brms is using with the function brms::stancode. The resulting code is consistently very elegant and generally pretty efficient. However, this code can also often a bit dense and opaque, particularly because it uses variable names for parameters and data that are constructed internally by brms and aren’t obviously related to the formula terms and data column names that you pass as input or receive as output. In addition to renaming variables, by default brms also rescales some variables internally and then unscales the output at the very end, which can add a layer of opacity (but to be clear, is a very smart thing for brms to be doing). Despite these challenges, examining brms code is one of the very best ways to understand how to code up a startlingly wide array of models in Stan, and it’s the approach I’d recommend.

When dealing with splines, the other challenge is to understand conceptually how the GAM is parameterized and fit. brms treats splines as random effects, and the best accessible treatment of this that I’m aware of is here:

You might also get something out of this discourse post: Better priors (non-flat) for GAMs (brms) - #4 by ucfagls

I find this stuff pretty challenging conceptually, and I’d be happy to do my best to understand specific snippets of brms Stan code with you as followup.

2 Likes