# Mixed effects model with cross random effects and nested random effects

I'm a beginner with Mixed effects model, and I'm having a hard time trying to fit the model to my data.

My data has the following properties :

1. I have two disjointed groups of participants
2. All participants perform the same trials
3. There are three trial difficulties
4. During each trial, we measure each participant's response every 20 ms. Total trial time is 5000ms

I'm interested in comparing the performance of each group in trials of the same difficulty.

From what I've read about mixed effects models, my fixed effects should be Time, Group and Trial type. I also think I need to use the interaction of Group and Trial, so that I get coefficients for each combination

Regarding the random effects, I'm not so sure but I think that

1. Participants are a nested random effect of Group
2. Trial type is a cross random effect of Group
3. Time is a cross random effect of Participant

Is that correct? In addition I'm not sure how to represent this structure in the formula. I've tried several formulas but all oh them returned NLL of - 220k

Interesting problem - I'll chime in with some thoughts though I am curious to hear what others on this forum have to say.

First, I think it will help if you think of your modelling in slightly different terms.

What are the random grouping factors in your modelling? Presuming that your subjects are representative of a larger set of subjects you are really interested in and your trials are representative of a larger set of trials you are really interested in, then you have two random grouping factors: Subject and Trial. Since all trials are the same for each subject, these two random grouping factors are indeed crossed.

You can imagine the subjects included in your study forming one pile and the trials included in your study forming another pile. Ideally, the subjects in your study were selected at random from the larger set of subjects and the trials in your study were selected at random from the larger set of trials; this would be a way to ensure their representativeness.

Now, imagine that each subject in your suject pile is paired (or crossed) with each trial in your trial pile. For each such pairing, you collect the value of your response variable every 20ms until 5000ms elapse.

Each of your two types of piles has certain properties you can measure; these will result in pile-specific predictors.

The subject pile has a property called Group, which divides the subjects in that pile into Group 1 and Group 2. In this sense, Group is a subject-level predictor.

The trial pile has a property called Trial_Type, which divides the trials in that pile into Trial Type 1, Trial Type 2 and Trial Type 3. In this sense, Trial_Type is a trial-level predictor.

There is also a Time predictor, measured at the lowest level of your data hierarchy - indeed, each time you measure your response variable for a (subject,trial) pairing, you also record the value of the Time predictor.

You didn't indicate the nature of your response variable; let's assume you can treat this response variable as a continuous variable. If you only measured the response variable once for each (subject,trial) pairing, your model could perhaps be specified like this in R under appropriate assumptions for the conditional distribution of the response variable :

lmer(response ~ Group*Trial_Type + (1|Subject) + (1|Trial)


This model includes (crossed) random effects for Subject and Trial and fixed effects for Group and Trial_Type.

If you only measured your response variable at a relatively small number of times (say, four times for simplicity), then you would have to include Time in your model. You could include it like so, for example:

lmer(response ~ Time*Group*Trial_Type +  (1 + Time|Subject) +
(1 + Time|Trial)


This last model includes both a fixed effect of time (which can depend on Group and Trial_Type) and a random effect of time across Subjects and a random effect of time across Trials. The reason you can allow for these random effects is because the value of Time was recorded repeatedly within each Subject and also within each Trial.

Note that only predictors measured at the lowest level of your data hierarchy can have random effects in your model, as is the case for the Time predictor.

The difficulty in your setting is that you have quite a large number of values for Time within each (subject,trial) pairing. So you will likely need to use a function like gam or bam to fit your model (see the mgcv package in R). This would allow you to consider a fixed effect of Time that is smooth and possibly nonlinear in addition to considering random effects of Subject and Trial as well as random effects of Time (smooth, nonlinear) across Subject and across Trial. You will likely need to look into factor-smooth interactions (https://fromthebottomoftheheap.net/2017/10/10/difference-splines-i/) and hierarchical generalized additive models (https://peerj.com/articles/6876/) to solve your problem.