I am trying to fit a linear mixed effects model with several fixed effects and a random intercept that varies per subject.

My problem is that I know that one of the fixed variables, let's call it 'A', depends on another one, 'B', which is categorical. I want to search for associations of 'A' with the response variable, taking into account that 'B' can affect 'A'.

How should I model this? Should I introduce an interaction term, 'A*B' in my model? Or should I introduce a random effect like : (-1 + A | B)? In either of the cases, which should be the association of 'A' and the response variable, how should I identify it?

Thank you very much for your time and effort,


You seem to mistake random effects with nested variables.

If the available values of an predictor variable A depend on the value of another predictor categorical variable (i.e. 'factor') B, then the variable A is 'nested' into B.

For example, if B can have the values B1, B2 and B3, while A is also categorical, but can have the values A1, A2, A3 for B=B1, A2, A3 for B=B2, and A3, A4, A5 for B=B2, then A is nested into B and their relationship can be modelled (e.g. in R's lmer() syntax) as:

B + A:B

Note that this signifies a full A * B interaction without the independent A term.

If, on the other hand, the effect of A on the outcome is allowed to vary on each level of B, but the available values for A are not necessarily different for each level of B, then the effect of A is random (varying by B), while A can also have a fixed effect. This effect can be modelled as:

A + (0 + A|B)

The first part is the fixed effect and the second the random one.

From what you describe, I infer that you wish to model a predictor A nested in another predictor B in a mixed model with a random intercept that varies by the subject S. So, in that case, the correct syntax would be:

outcome ~ B + A:B + (1|S)

I hope this was helpful.

  • $\begingroup$ I thought A*B will always mean A+B+A:B $\endgroup$
    – rnso
    Aug 1 '20 at 19:31
  • $\begingroup$ A*B always means A+B+A:B. On the other hand B+A:B means A*B-A, i.e. that A is nested in B $\endgroup$ Aug 1 '20 at 19:35

I finally added a random effect that allowed 'A' to vary by 'B': (-1 + A | B) and that seemed to work.

The association can be verified directly by looking at the p value of the 'A' variable.

  • $\begingroup$ What does -1 stand here for? Why not 1 as is done commonly? $\endgroup$
    – rnso
    Aug 1 '20 at 19:29
  • $\begingroup$ -1 means without intercept $\endgroup$ Aug 1 '20 at 19:37
  • 1
    $\begingroup$ Why do you want without intercept? $\endgroup$
    – rnso
    Aug 2 '20 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.