# How to specify a linear mixed model (LMM) for a repeated measures design with two categorical predictors and one continuous?

I would appreciate some help figuring whether a linear mixed model (LMM) is a good choice for my data.

My experiment is a 3 (neurostimulation target) X 2 (task block) repeated measures design that takes place over 3 sessions on different days. I have data from 27 subjects. Each session involves a different neurostimulation target: A, B, or C. We are interested in how neurostimulation of these different targets affects response time in a behavioral task. So, a subject comes in, they complete a pre-stimulation block of the task, we administer the neurostimulation to one of the targets (A, B, or C), and then they complete a post-stimulation block of the task. Each block of the behavioral task is 10 minutes long and includes hundreds of trials. We would also like to add task time as a continuous predictor because we expect a change in response time over time within a sitting, and that this change will be modulated by stimulation target and/or block.

This design has several features that might make an LMM a good choice. The experiment is repeated-measures/within-subjects, all subjects experience all levels. The 10 minutes of the task occur within the task blocks, and the task blocks occur within the sessions. Because sessions take place on different days, comparing task blocks between sessions is confounded by individual differences between days, which might make being able to specify random effects helpful. Lastly, we are mixing categorical (stimulation target and task block) and continuous (task time) predictors.

Here are my questions:

1. Is an LMM appropriate here? If so,
2. Should I aggregate the data (i.e., take the mean for each subject, target, and task block)?
3. Where does this design fall with respect to nesting or crossing?
4. How should I specify my random effects structure to account best for individual differences between days?

Previous attempts with lmer in R:

model <- lmer(rt ~ target * block * time + (1 | sub/target/block), data = data, REML = TRUE)


This led to no errors. However with this model:

model <- lmer(rt ~ target * block * time + (1 | sub) + (1 | target) + (1 | block), data = data, REML = TRUE)


This led to (but maybe it is OK?):

optimizer (nloptwrap) convergence code: 0 (OK)
unable to evaluate scaled gradient
Hessian is numerically singular: parameters are not uniquely determined


#### Which Effects to Include?

I just posted about the difference between fixed and random effects for categorical variables. To summarize what I said there, fixed effects are those that you have a theoretically motivated reason to predict an outcome with, whereas random effects are the noise we want to remove from the model due to variance we know affects the outcome variable in some way. Your target and block, to me, represent theoretically driven effects (though it's not clear from your question which per se), and thus should only be included as fixed effects here. Including them as both fixed and random intercepts here to me doesn't make a lot of sense.

With that in mind, I believe you should only include subjects as random intercepts if they have conducted repeated measures, but you can also include trial as a random intercept to make a "crossed effects" design (Baayen et al., 2008), which will improve the power of your analysis by multiplying the subject x item grid of observations (if you have 20 subjects and 10 items, you effectively get 200 observations). Given that you mentioned there were several trials, this would definitely be useful to include, coded as so:

model <- lmer(rt ~ target * block * time + (1 | sub) + (1|trial), data = data, REML = TRUE)


#### Errors in Model

As to your model output, you should not ignore your errors here, particularly the past part which I saw here:

optimizer (nloptwrap) convergence code: 0 (OK) unable to evaluate scaled gradient Hessian is numerically singular: parameters are not uniquely determined


Remember that in linear algebra, we do not want singular matrices or non-unique solutions to a set of linear equations (Lay et al., 2016). If we are unable to find a unique solution, then this means we can find infinitely solutions to the linear system of equations, which is not meaningful for determining a mixed model. An example of how this can arise is if one equation is the multiple of another. This is usually not something you want and should force you to rethink your model, which here it makes sense to given the random effects structure isn't helpful. Singular matrices usually have a determinant of zero or close to zero, and this is a common issue when a random effects structure tries to pull out too much random variance from the model which simply doesn't exist (for example, having too many zero entries because the variance is super low).

In other words, lme4 is trying to tell you your model doesn't match your data, and you should rethink it. Which in the first section I highlight a better solution anyway.

#### References

• Baayen, R. H., Davidson, D. J., & Bates, D. M. (2008). Mixed-effects modeling with crossed random effects for subjects and items. Journal of Memory and Language, 59, 390–412. https://doi.org/10.1016/j.jml.2007.12.005
• Lay, D. C., Lay, S. R., & McDonald, J. (2016). Linear algebra and its applications (Fifth edition). Pearson.
• Thank you! Very helpful. Oct 4, 2023 at 17:04
• No problem. If you feel my answer was useful, feel free to select the checkmark next to the answer to accept it. Oct 4, 2023 at 23:46