I have a dataset where abundance is being changed by group (2 categories, fixed effect) and I want to account for random effect subject (15 subjects). Basically, each of the 15 subjects went through both groups such that the total number of samples per group and subject is 1

 sample     group     subject     abundance
 s1         A         id1         1.5
 s2         B         id1         10.1
 s3         A         id2         -2.3
 s4         B         id2         -5

I want to model the effect of group on abundance, and I also know that subject has an effect on abundance, i.e. abundance ~ 1 + group + (1 + group | subject).

In total I have 22 samples, and do I get it right that by specifying the random effects above it is not possible to model the slope / intercept as indicated? But in this case, even increasing number of subjects would not help as all subjects added would still be present in both groups? I thought I could model the slope of subject separately from that of group. What am I missing here?

  • $\begingroup$ How do you define a slope for variable group which is a categorical variable? $\endgroup$ Nov 28, 2023 at 13:34
  • $\begingroup$ Sorry but I dont understand the issue. I've seen plenty of examples with 2 categories as fixed variables (example: ed.ac.uk/biomedical-sciences/…) $\endgroup$
    – Sos
    Nov 28, 2023 at 13:39
  • $\begingroup$ Let me clarify, how do you define a random slope for group? $\endgroup$ Nov 28, 2023 at 13:43
  • $\begingroup$ Sorry, this might all be going over my head, but isn't that the case every time you want to evaluate the extent onto which a covariate may have, together with a predictor, on an outcome? $\endgroup$
    – Sos
    Nov 28, 2023 at 16:05
  • 1
    $\begingroup$ There's nothing wrong with your code as such. You can very well include a random slope of a categorical predictor, though it may feel strange to call it a slope because what you get is a contrast estimate for each subject. I'm not sure if the fact that you only have 2 observations per subject causes some issues though (in the example you link, they had several observations for each modality per subject). $\endgroup$
    – Sointu
    Nov 29, 2023 at 16:04

2 Answers 2


The proposed model:

abundance ~ 1 + group + (1 + group | subject)

seems reasonable to me. You are accounting for correlations within subjects by fitting random intercepts. You are also allowing each subject to have their own response to the group variable by fitting random slopes. One concern about your study is whether you have sufficient statistical power to detect the effect you are looking for.


Also you may have another problem. With this model you would estimate 2 random effect variances (of intercept and slope of group), one correlation between these two, and one residual variance. So 4 terms in total. However, in your data you only have 3: the variance for group A, the variance for group B, the correlation between the two. Hence, lmer may not converge. You could e.g. use glmmTMB:

glmmTMB(abundance ~ group + (1+group), dispformula=~0)

With dispformula=~0 the residual variance is put to zero (actually some very small number close to zero).

On the other hand, why would you not use a paired-samples t-test?

Regards, Ben.


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.