# How to set up a Linear mixed effects model with multiple covariates and interactions

I'm a bit unsure about a current project I'm concerned with. I have an outcome of interest, say $$y$$, and a main predictor, say $$x$$, which both are continuous. I have multiple measurements per subject of $$x$$ and $$y$$, at different timepoints. Apart from that, I have some other covariates, which should be included in the model.

I now have the impression that I ignore the time information when I just specify a random effect on intercept + slope (the main predictor). However, if I specify random intercept + slope (time), I feel that I'm not addressing the research question, how the main predictor affects the outcome variable.

Basically there are two questions:

• How does the main predictor affect the outcome?
• How does time affect the outcome?

I'm not sure how to handle this/set up the random effects. I never had the situation of including two random effects and I don't think that's the right way here. I would probably include all covariates as fixed effects, plus an interaction term Time * Main Predictor, but what about the random effects?

How does the main predictor affect the outcome?

The predictor will affect the outcome in the same way it does in a fixed effects linear regression. The only difference is how the point estimates look after accounting for the random effects.

How does time affect the outcome?

Time affects the outcome in two ways:

• If the response changes over time (positive/negative trend), then it could mean that your outcome is not stable longitudinally and may be affected by temporal effects (such as developmental age , which will initially show large increases in letter reading ability but will quickly plateau as children fully learn their letters).
• Because this measure is repeated (e.g. a subject is rated on depression at Year 1 and Year 2), the errors tend to be correlated, which means that they will downward bias the standard errors and produce inaccurate measurements of the fixed effects in a regression. As such, it is useful to model this directly by simply adding in time as a fixed effects predictor.

I'm not sure how to handle this/set up the random effects. I never had the situation of including two random effects and I don't think that's the right way here. I would probably include all covariates as fixed effects, plus an interaction term Time * Main Predictor, but what about the random effects?

Yes, you can include the main effects of time and the main predictor here as well as their interaction if you believe the main predictor changes relationship with the outcome over time. If one assumes there is no change across time, you probably wouldn't need to include this as an interaction at all.

In terms of what that means for estimating a mixed model, you could fit the model with the following syntax:

model <- lmer(y ~ x * time + (1 + x | subject), data = data)


This would model the main effects and interaction of $$x$$ and time, while estimating how the slope between $$x$$ and $$y$$ varies by person (subject is a cluster which has repeated measures across time, thus this produces an individual intercept and slope for $$x$$ and $$y$$ for each subject). You could include time and $$x$$ as random slopes like below (with our without the * operator for an interaction):

model <- lmer(y ~ x * time + (1 + x + time | subject), data = data)


But I doubt it would be that useful and would be more prone to non-convergence or singularity (as complexity of random effects increases, the likelihood of the model converging decreases). In fact, it may be the case that even a simple random slope is too complex, in which case you may be better off simply estimating random intercepts like below, modeling only how intercepts vary by subject:

model <- lmer(y ~ x * time + (1 | subject), data = data)


How you decide on that will depend largely on what your data actually looks like and whether it can fit such models.

• Thanks a lot. Maybe I didn't express my question so well. I know the theoretical background of mixed models and how they are interpreted. The thing I'm not sure about is whether I also have to add time as a random slope (so your second piece of code) or only x (first one). But I was also wondering whether it might become too complex with two random slopes. Due to the data structure, random intercept would not be sufficient. But I think your answer also makes me tending to the second option. Dec 21, 2023 at 9:22
• There's nothing getting in the way of you doing that. Theoretically any fixed effect can operate as a random slope if the data allows it (and it is meaningful to do so). Really just depends on if your model can converge with complex random effects or not. Dec 21, 2023 at 10:07
• Thanks. Unfortunately, it doesn't converge when I add a slope for time. So I'll stick to the 2nd option or try some other specifications to improve convergence. Dec 21, 2023 at 10:19
• If you search through the mixed model posts I have made at CV, you will often see me warning against fitting random slopes by default because it is often the case they simply cannot fit the data in many cases (along with the issues they present with lower statistical power). Usually you want to start simple and build up in complexity, using visualization as a tool for determining whether or not a model that converges even has meaningful random variance in the first place. Dec 21, 2023 at 10:30