# lmer with random effects that is also associated with fixed effect

I have data of compassion ratings in response to stimuli (pictures). The stimuli can be described by two factors, valence (categorical: positive, negative) and arousal (continuous). All n=145 subjects rated all 44 pictures. The ratings are continuous from 0 to 100 and are normally distributed for both positive and negative pictures. Here is what the data looks like for the first 3 subjects and 2 stimuli:

> df
subject stimulus arousal valence
1   sub-1    pic-1    0.25     pos
2   sub-2    pic-1    0.25     pos
3   sub-3    pic-1    0.25     pos
4   sub-1    pic-2    0.48     neg
5   sub-2    pic-2    0.48     neg
6   sub-3    pic-2    0.48     neg


I want to analyse this data with a linear mixed model, however, I am a bit unsure about the random effects. In my experiments before, I always modelled both stimulus and subject as random effects, so my first instinct was to do somthing like this:

formula = rating ~ valence * arousal + (1|subject) + (1|stimulus)


However, since I have also an effect of interest continuously describing the stimuli, I am unsure if this is correct. Since I have an effect of interest describing each oft the stimuli with exactly one value, is the stimulus then still random? Or would it be better to only use subjects as a random effect, e.g., like this:

formula = rating ~ valence * arousal + (1|subject)


Does anyone know which model is better suited to analyse my data? Or generally has suggestions which model to use to analyse this data?

• If you have Likert ratings you can't use a linear model, you should look into an ordinal model. I would also consider adding random slopes for your fixed effects by participant
– sjp
Commented Jul 9, 2022 at 16:35
• It's not a Likert rating and the ratings are normally distributed. I will add that information to the post.
– Max
Commented Jul 9, 2022 at 20:35

rating ~ valence * arousal + (1|subject) + (1|stimulus)


describes rating with a linear effect of arousal, one such effect per value of valence (the fixed effect interaction term) and offsets, one per subject and one per stimulus. That is totally fine. Those two last terms are called "random effects terms" only because, intuitively, the coefficients are fitted in a "more random way" than the coefficients in the fixed effect terms. The net result of random effects is that, in deciding for each subject dependent offset, the model "looks" at all the subject dependent offsets together and tries to keep them close to each other ("shrinkage"). I.e., the values of one subject will have an influence on the offset of another subject.

Removing the last term, as in your second formula, says that you don't expect some relevant change in offset for different stimulus values. Which of those models is more appropriate depends on your scenario, your expert knowledge, what your actual task is, and, of course, also on which fits best your data.

What is interesting, is that while considering subject/stimulus dependent offsets, you did not provide for subject/stimulus dependent linear effects of arousal (which might be totally appropriate).

As a final remark, with those models that consider all kinds of (random/fixed) interactions of the features, it happens easily that the models get too complex and might overfit your data. In general, you want to keep your model as simple as possible.

As Frank's answer (+1) already mentioned, you can even statistically test which of the two models fits best to your data. Use:

anova(model1, model2)


to obtain a chisquare value (1 degree of freedom for the random stimulus effect) which can be tested for significance. Such random stimulus effect is often applied in experiments like you describe, so probably there are good reasons to do so.

The combination with the continous arousal variable is no problem at all. "Arousal" is just a "stimulus" property, but stimuli may have all kinds of unknown or unmeasurable influences on "ratings" which are not all captured by "arousal", hence the random stimulus effect still makes sense! Compare with research on pupils' grades in schools, where schoolsize (=school feature, continuous) may influence the grades of pupils, and a random school effect is used to model other school influences.