I would like to be able to test either via a linear mixed effects model or a hierachical GAM (where appropriate) to see whether there is a relationship (either linear or non-linear) between participants stool metabolites and time.

Each subject (n=150) has provided a sample every month across one year beginning in November (Autumn).

I have different time components to consider, either seasons (autumn, winter, spring summer, autumn) or month.

  1. If I want to look at seasonal variation (having this as both continuous or categorical to compare different pairs of seasons), do I need to have time (in months) as a covariate, if so, as continuous or categorical?

  2. If I want to look at just relationship with time (either continuous or comparing months, without that being confounded by seasonal variation), do I have seasons as a covariate in my model? Of course, not in the instances when looking at pairwise comparisons when the months happen to be in the same seasonal period.

The reason I am asking is I'm a little unsure based on some example models I have seen, e.g. where there is information for both seasons and day or month of the year, there is no seasonal covariate in their model when looking at any possible relationship between time and their response, when seasonal changes would have influenced the response over time.

Any thoughts, or redirection to better resources/examples would be appreciated.


1 Answer 1


These questions are perfectly suited to Hierarchical Generalized Additive Models (HGAMs). The canonical reference for these types of models is Generalized Additive Models: An Introduction with R, Second Edition, by Simon Wood (maintainer of the mgcv package in R). However there is also a very useful and detailed 'tutorial' paper that gives examples of how these models can be set up, written by Pedersen et al: Hierarchical generalized additive models in ecology: an introduction with mgcv.

In short, once you have narrowed down on an appropriate observation likelihood (you haven't specified how these metabolite observations are distributed), you can use the mgcv package in R to test different hypotheses you might have about how these observations vary over seasons and across participants. An example model may be:

gam(y ~ s(month) + s(month, participant, bs = 'fs'), family = ..., data = ...).

This sort of setup allows you to use partial pooling to learn nonlinear relationships between your response variable $y$ (metabolite measurements) and $month$ (included as an variable of class integer), where each participant's nonlinear smooth function of $month$ is regularized toward a shared (i.e. global) smooth function of $month$. This model also incorporates hierarchical (random) intercerpts to capture variation in average measurements per participant.

If you fit such a model and carry out the usual residual diagnostics, you may find that there is still unmodelled temporal autocorrelation left (particularly if you look at residuals per participant. This autocorrelation can be accomodated in various ways in the mgcv universe, and you can look on this site in particular to find useful guidance (search for autocorrelation mgcv to get started).

As for your question about what effects to include, it seems that because your participants submitted samples once per month, both $month$ and $time$ are the same thing here (unless I'm misreading). If you had samples from more than one year, then you would need to make this distinction more clearly. But for now I don't think that is necessary.

  • $\begingroup$ Thank you for this – very helpful! I’m aware from the literature that these individuals in this study eat very seasonally, as they’re not able to access supermarkets where food comes from all over the place. And I understand that the different available foods across spring, summer, autumn, winter would affect the metabolites measured. Would it make sense to include season as a categorical (factor) variable like this, to see how the metabolites are changing over time (in months) by season? gam(y ~ seasons + s(month, by=seasons) + s(month, participant, bs=”fs”)) $\endgroup$
    – aim6789
    Commented Mar 25 at 11:44
  • $\begingroup$ I definitely need to do more reading at this stage, as I need to get my head around the difference between using s(month, participant, bs=”fs”) vs using s(participant, bs=”re”) + s(participant, month, by= “re”), and what one would be more appropriate. $\endgroup$
    – aim6789
    Commented Mar 25 at 11:52
  • $\begingroup$ I wouldn't recommend that strategy of including an additional factor for season, as the monthly smooth will be confounded with that variable. Typically we wouldn't expect a seasonal pattern to change abruptly (i.e. a step change) from one season to the next, rather we'd expect that change to be smooth. The strategy I mentioned is one useful way of capturing this, while letting seasonality vary over participants. Have a look at the Pedersen et al paper and I'm sure you'll find some useful tidbits to get you started $\endgroup$ Commented Mar 26 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.