Specifying varying effect structures with hierarchical generalised additive models (gams)

Some comments:

Don’t ever do this in brms as it will be slow as a very slow thing. If you want random intercepts/slopes use the native syntax: (time + pid).

Note also that s(time, pid, bs=“re”) is only adding random slopes; you likely need s(pid, bs=“re”) + s(time, pid, bs=“re”) to allow random intercepts and slopes in a GAM model if you are doing this via mgcv.

You don’t need the m = 1 on the random smooth terms (bs = "fs") as these smooths are fully penalized. In my experience, what you might gain from m = 1 is often offset by piecewise linear behaviour of the estimated effects because of the change in derivative).

You don’t want a parametric effect and and random effect for condition - use one or the other. As condition seems an effect of interest, taking only several levels, I would use the parametric effect. Also, in this model you don’t actually want the random intercepts per pid as these are already included in the fs smooth and including both could easily cause identifiability issues that would affect the sampling.

My suggestion, if you want to estimate smooths for each condition while accounting for individual time trends, I would fit these models

pupil ~ 1 + condition +
  s(time, by = condition, bs = “bs”, k = 10) +
  s(time, pid, bs = “fs”, xt = list(bs = "bs"))

and

pupil ~ 1 + condition +
  s(time, by = condition, bs = “bs”, k = 10) +
  s(time, pid, by = condition, bs = “fs”, xt = list(bs = "bs"))

(Noting my use of the xt argument to use a B-spline basis for the fs smooths, to match your specification from the other smooth`.)

These two models account for the smooth time-treatment effects through the factor-by smooth of time by condition, with treatment means modelled through the condition parametric effect. The models differ in how they treat (penalize) the subject-specific smooths:

  1. the first form fits a smooth of time for each subject (including random intercepts and linear slopes), where the penalties will shrink the subject specific curves towards their respective group means (the parametric condition effects), while
  2. the second form extends the first form in two ways:
    i. the subjects in each treatment group have a common wiggliness, but the wiggliness can vary between treatment groups , and
    ii. the penalties will shrink the curve towards their respective treatment-specific smooth

The second form will also be quite a lot more complex to fit however.

11 Likes