I was redirected here from stackoverflow - I'm struggling to figure out where to even put this question!

I'm doing a project where I have students listen to 7 stimuli (all students listen to the same 7), and then say for each one whether that stimuli sounds more like PALM or TRAP. There are two groups of students, groupA and groupB (GroupA is younger than GroupB). I want to measure if the difference between their groups in their selection of choice PALM or TRAP across the stimuli is significant or not (i.e. do older students show a difference in their choice of PALM or TRAP depending on the stimuli and is this significant).

I've been told to do a mixed effects logistic regression type model in R, but I've not used glmer much and I find it hard to use (not even sure if it's the right one to use). The only way I could get it to work was to put in the SUBJECT as a slope (?), but that doesn't seem right to me and the result is odd. I would really appreciate any suggestions because I don't really understand the statistics behind it and I don't know how to formulate the code because of this.

My current code in R is this, where CHOICE = outcome resulting in choice of either PALM or TRAP, STIMULUS = stimuli 1 through to 7, GROUP = GroupA or GroupB, and SUBJECT is the participant ID (although I wondered if I should even keep this in) :

table <- read.delim("rawdata2.txt", stringsAsFactors = TRUE)


library (lmerTest)
library (lme4)

table$GROUP <- as.factor(table$GROUP)
table$STIMULUS <- as.numeric(table$STIMULUS)
table$CHOICE <- as.factor(table$CHOICE)
table$SUBJECT <- as.numeric(table$SUBJECT)

contrast <- cbind(c(-0.5, +0.5))
colnames(contrast) <- c("-PALM+TRAP")
contrasts (table$CHOICE) <- contrast

contrast <- cbind(c(-0.5, +0.5))
colnames(contrast) <- c("-A+B")
contrasts (table$GROUP) <- contrast

#Fixed effects = STIMULUS, GROUP
#Random effects = CHOICE
# Within-participants = STIMULUS, CHOICE
# Between-Paticipants = GROUP, SUBJECT
# Running into errors if I leave out SUBJECT, but should it even be there?

model <- glmer(CHOICE ~ STIMULUS * GROUP + (STIMULUS | SUBJECT) + (STIMULUS * GROUP | SUBJECT), data = table, family = binomial)

Response in the console:

Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: binomial  ( logit )
   Data: table
      AIC       BIC    logLik  deviance  df.resid 
 438.0945  507.8760 -202.0473  404.0945       431 
Random effects:
 Groups    Name               Std.Dev. Corr             
 SUBJECT   (Intercept)        2.9190                    
           STIMULUS           0.5851   -1.00            
 SUBJECT.1 (Intercept)        2.7501                    
           STIMULUS           0.5386   -1.00            
           GROUP-A+B          4.2917    0.57 -0.56      
           STIMULUS:GROUP-A+B 0.8327   -0.56  0.55 -0.99
Number of obs: 448, groups:  SUBJECT, 16
Fixed Effects:
       (Intercept)            STIMULUS           GROUP-A+B  STIMULUS:GROUP-A+B  
           -4.2495              0.9265              1.2539             -0.2695  
optimizer (Nelder_Mead) convergence code: 0 (OK) ; 0 optimizer warnings; 2 lme4 warnings 03 

1 Answer 1


My inclination would be to recommend that you include GROUP, STIMULUS, and their interaction as fixed effects and have a random intercept for SUBJECT. This way, you allow the effect of each stimulus to vary by group, and you allow each individual to have a randomly varying intercept (i.e., marginal probability of guess PALM or TRAP) around their group intercept. I would code this as follows:

fit <- glmer(CHOICE ~ GROUP * STIMULUS + (1|SUBJECT), ...)

To test whether there is an interaction between GROUP and STIMULUS, you can look at the Wald ANOVA tests using anova(), i.e.,

anova(fit, type = 3)

The p-value on the interaction indicates whether there is evidence for an interaction. Then you can examine in more depth the coefficient estimates using summary().

If you wanted to save a few degrees of freedom, you can also include STIMULUS as a random effect, even though it only has 7 levels. You would do this as follows:

fit <- glmer(CHOICE ~ GROUP + (1 + GROUP|STIMULUS) + (1|SUBJECT), ...)

This would include a random intercept for SUBJECT, a random intercept for STIMULUS, and a random slope on GROUP that varies by STIMULUS. The test for whether the effect of stimulus depends on group is the test of whether the variance of the random slope for GROUP differs from zero.

  • $\begingroup$ thank you so much, that's an exceptionally helpful answer and it has sorted out a lot of the confusion around this topic for me as I wasn't sure what each element of the GLMER function in R stood for. I'm now getting a response that makes sense and I understand how to report it. Thank you so much! $\endgroup$
    – Michaela
    Commented Jul 8, 2021 at 5:55

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.