Consider the following lme4 model: Y ~ X*Condition + (1+X|Trial).

Here Trial is nested in a binary Condition. This model calculates the variance of random intercept, variance of random slopes and the correlation of random intercepts and random slopes. I would like to calculate all these values but separately for Condition A and Condition B. How can I do that?

Additional experimental details

The experiment we conducted is a two-choice task where people have to choose the correct answer. There are two conditions in the task (condition A and B), one of which makes the task harder. All participants go through sets of trials for both condition A and condition B. All trials are coded in a single variable named Trial - A trials are named A1, A2, A3, and B trials B1, B2, B3... We want to predict Y (binomial variable) from X (continuous variable) while controlling for the random effects in different trials.


1 Answer 1


Thanks to @amoeba and using @BenBolker's brief remark here

Extensions such as allowing different residual variances or different variance-covariance matrices of random effects per (fixed-effect) group can be achieved, somewhat clunkily, by using the dummy() helper function to construct an indicator variable to multiply by individual levels of interest.

we got to the bottom of the problem.

The solution is following:

Y ~ X*Condition + 
    (X*Condition | Subject) + 
    (0 + dummy(Condition, "A") + X:dummy(Condition, "A") | Trial) + 
    (0 + dummy(Condition, "B") + X:dummy(Condition, "B") | Trial) 

The summary(model) in random effects yields:

Groups  Name                        Variance Std.Dev. Corr             
 subject (Intercept)                 0.89343  0.9452                    
         X                           0.11695  0.3420   -0.85            
         ConditionB                  0.66731  0.8169   -0.33  0.06      
         X:ConditionB                0.07391  0.2719    0.34 -0.05 -0.47
 Trial   dummy(Condition, "A")       0.63854  0.7991                    
         dummy(Condition, "A"):X     0.09372  0.3061   -0.76            
 Trial.1 dummy(Condition, "B")       0.88833  0.9425                    
         dummy(Condition, "B"):X     0.12175  0.3489   -0.60    

which now makes perfect sense, because only correlations that can be calculated are between intercepts and slopes for a certain condition (because they are estimated for the same trials). Correlations between Conditions are senseless.

Furthermore, coef(model)$Trial now shows logical values:

      dummy(Condition, "A")   dummy(Condition, "A"):X    dummy(Condition, "B")   dummy(Condition, "B"):X      (Intercept)          X           ConditionB    X:ConditionB
A1                0.9198822                 0.0209849                        0                         0       2.703544   -0.9929765          -0.07102448        0.2415836
A2               -1.3029020                 0.3894812                        0                         0       2.703544   -0.9929765          -0.07102448        0.2415836
A3                1.1294702                -0.2475288                        0                         0       2.703544   -0.9929765          -0.07102448        0.2415836
B1              0.000000000              0.0000000000               1.21725268              -0.305314643       2.703544   -0.9929765          -0.07102448        0.2415836
B2              0.000000000              0.0000000000               0.88317976              -0.209529267       2.703544   -0.9929765          -0.07102448        0.2415836
B3              0.000000000              0.0000000000               0.27859781              -0.065708851       2.703544   -0.9929765          -0.07102448        0.2415836
  1. Fixed effects are the same for all trials
  2. dummy(Condition, "A") intercepts and dummy(Condition, "A"):X slopes are calculated only for Condition A, in Condition B trials they are estimated to be 0, and vice versa.

N.B. When specifying random effects for this purpose, it is important to:

  • specify random effects for different groups of trials independently, not under the same |Trial. If you don't do that, lme4 will estimate random effects for all trials, not just for the given condition.
  • include 0, so as to prevent lme4 from including a general random intercept across all trials.

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.