I have a dataset where 50 Subjects were each exposed to 2 Testing Conditions over 5 Time Periods and they reported their Happiness Levels at every time point (i.e., I have 50 * 2 * 5 Happiness data points in total). Based on past work, I know I need to run a "linear mixed-effects model with a fully specified random-effects structure, with subjects as a non-nested random factor, and all intercepts and slopes included".

If I just had 50 Subjects with 5 Time Periods, I know that my lme4 formula would be:

Happiness ~ Time + (Time | Subjects)

But if I have 50 Subjects with 5 Time Periods AND 2 Testing Conditions, would this be:

Happiness ~ Time * TestingCondition + (Time * TestingCondition | Subjects)

I'm still learning more complicated LMM designs at the moment, so if I'm completely off the mark here, I'd appreciate any help or advice I can get!


1 Answer 1


This is almost right, although it probably needs to be adjusted for practical/identifiability purposes. The rubric is that any condition or treatment that varies within groups should be allow to vary across groups; since you have measurements for both testing conditions and every time point for every subject, you can in principle measure the variation of all of these effects (for example: "how does the difference in testing conditions at time point 3 vary across subjects?" [this quantity isn't explicitly estimated by the model]).

There are a few important points to consider, however, in two cases.

Time considered as categorical

In this case you have two problems:

  • since you only have one measurement per time point $\times$ treatment combination per subject, the among-subject variance in the interaction is confounded with the residual variance term. There are a few different options for overriding this, but ...
  • in addition, your model will almost certainly be overparameterized/too complex. With two time points and 5 treatments, you have 10 parameters that vary across subjects, which implies a 10 $\times$ 10 covariance matrix with (10*11)/2 = 55 parameters to estimate. It's extremely unlikely that you will be able to do this with 50 subjects/500 data points ... you could:
    • use a Bayesian framework (e.g. blme, MCMCglmm, or brms) to put regularizing priors on the covariance matrix
    • simplify the model, at least to (Time + Treatment|Subject) (this will "only" require a 6 $\times$ 6 covariance matrix with 21 parameters ...)
    • simplify in some other way, e.g. use glmmTMB to fit a factor-analytic covariance matrix, or afex::mixed to fit a zero-covariance model

Time considered as continuous

This version is much easier, although it requires you to assume a linear trend across time points.

Now (Treatment*Time|Subject) only implies a 4 $\times$ 4 covariance matrix = 10 parameters. Still a little bit ambitious, but it might work.

  • $\begingroup$ This is wonderful, thank you for the detailed answer Ben! So if I'm understanding this correctly: (1) if I consider Time to be categorical, my formula would be "Happiness ~ Time * Testing + (Time + Testing | Subject)" to account for the overcomplexity of my model OR (2) if I consider Time to be continuous, my formula would be "Happiness ~ Time * Testing + (Time * Testing | Subject)"? $\endgroup$
    – EPP
    Jun 17, 2022 at 2:30
  • $\begingroup$ Yes, although you might not be entirely out of the woods - either of these models might still be too complex for the data. $\endgroup$
    – Ben Bolker
    Jun 17, 2022 at 15:58
  • $\begingroup$ Okay good to know, thanks a bunch for the quick responses @BenBolker, and I'll definitely look into whether I'll need to simplify the model further! $\endgroup$
    – EPP
    Jun 17, 2022 at 17:02
  • $\begingroup$ If this solved your problem you are encouraged to click on the check-mark to accept it .. $\endgroup$
    – Ben Bolker
    Jun 17, 2022 at 17:09

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.