I have some data ($x$ and $y$) collected over multiple days for multiple people. I want to test whether the contemporaneous associations between $x$ and $y$ (measured daily) is stronger depending on variable $z$ measured at baseline. I am not sure how I should add the x*z interaction to my model using lme4 in R. Here is the basic model without the interaction term:

lmer(y ~ x + ( x | id))

Which of the following versions would be correct?

lmer(y ~ x*z + ( x | id))
lmer(y ~ x + ( x*z | id))
lmer(y ~ x*z + ( x*z | id))

I am also wondering how to add covariates measured at baseline? Would it be like this:

lmer(y ~ x + covariate + ( x | id))
  • 2
    $\begingroup$ For those voting to close, I believe this question asks more than coding, as it asks whether or not baseline variables should be entered into the model or not. $\endgroup$ Commented Feb 10 at 5:07
  • 2
    $\begingroup$ Sorry @ShawnHemelstrand, I was the voter. A hasty one, I confess. I am retracting it. $\endgroup$ Commented Feb 10 at 5:16
  • 2
    $\begingroup$ No worries! Just figured I would interject heh $\endgroup$ Commented Feb 10 at 5:20

1 Answer 1


Fixed/Random Variable Inputs

There's nothing stopping you from adding any number of variables that were measured at baseline. If this measure was also repeated, then you would need to code that into your regression in some way that incorporates the repeated measurements across time, but for the purpose of this question that doesn't seem to be an issue. So adding in z or covariate can be done straightforwardly and will partial out the unique variance of the response attributed to that effect after controlling for your other predictors. Whether those should be modeled as an interaction depends on whether or not you believe that is theoretically justifiable. Do you believe the two combined influences of these variables changes how the response looks? If so, you can use an interaction (the * operator), otherwise it doesn't need to be added by default.

As to the random effects portion of your question, it depends on if you believe the main effects or interaction will vary by person. However, random slopes are already difficult to fit if the data doesn't support it (e.g. the by-subject slopes don't vary at all) and are even more difficult to fit with interactions. I would start off fitting a random intercepts-only model first, see if that fits without error messages, then build up to more complicated random effects if present or justifiable (such as uncorrelated slopes, then correlated slopes). The three references below discuss how you code random effects and what the rationale should be for fitting random effects, which includes commentary on fitting simpler models before building up from there.


Coding Random Effects:

  • Bates, D., Mächler, M., Bolker, B., & Walker, S. (2015). Fitting linear mixed-effects models using lme4. Journal of Statistical Software, 67(1). https://doi.org/10.18637/jss.v067.i01

On Simple vs Maximal Models:

  • Matuschek, H., Kliegl, R., Vasishth, S., Baayen, H., & Bates, D. (2017). Balancing Type I error and power in linear mixed models. Journal of Memory and Language, 94, 305–315. https://doi.org/10.1016/j.jml.2017.01.001

Best Practices for Fitting:

  • Meteyard, L., & Davies, R. A. I. (2020). Best practice guidance for linear mixed-effects models in psychological science. Journal of Memory and Language, 112, 104092. https://doi.org/10.1016/j.jml.2020.104092
  • $\begingroup$ Nice, answer (+1) but I'm curious about what "partial out" means ? Can you clarify please ? $\endgroup$
    – Joe King
    Commented Feb 10 at 15:15
  • $\begingroup$ Perhaps poor wording on my part, but what I mean is that it generates a prediction of the change in the outcome variable after setting all other variables to a constant (such as zero or the mean). In other words, the slope for a given covariate determines the predicted change in the dependent variable after controlling for all other predictors in the model. $\endgroup$ Commented Feb 10 at 15:19

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.