Elise
Elise

Reputation: 45

How to extract only the random effects correlation parameters from an lmer model?

I am trying to extract random effect correlation parameters from an lmer output.

This is my model:

m <- lmer(RT ~ Condition + (1 + Condition| Participant), data)

Giving me the following output:

REML criterion at convergence: 6533.6

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.4666 -0.6318 -0.0232  0.5696  4.1010 

Random effects:
 Groups      Name        Variance Std.Dev. Corr             
 Participant (Intercept) 0.045483 0.21327                   
             Condition2  0.001271 0.03565  -0.43            
             Condition3  0.005774 0.07599  -0.04 -0.09      
             Condition4  0.003817 0.06178  -0.57  0.60  0.69
 Residual                0.147445 0.38399                   
Number of obs: 6841, groups:  Participant, 39

Fixed effects:
            Estimate Std. Error t value
(Intercept)  1.57546    0.03537  44.544
Condition2   0.06677    0.01420   4.703
Condition3  -0.09581    0.01798  -5.328
Condition4   0.02710    0.01639   1.653

Correlation of Fixed Effects:
           (Intr) Cndtn2 Cndtn3
Condition2 -0.334              
Condition3 -0.157  0.307       
Condition4 -0.476  0.508  0.571

However, I only want to extract specific correlation parameters of the random effects, say the correlation between Condition3 and Condition2 (-0.04). Does anyone know how to do that?

I tried using the VarCorr() function which only displays the results for the random effects, but still does not let me extract specific values from it. I would really appreciate any help!

Upvotes: 1

Views: 1295

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226192

@mfidino's answer is good. Alternatively

cc <- cov2cor(VarCorr(m1)$Subject)
cc["Days",  "(Intercept)"]

or

cc <- attr(VarCorr(m1)$Subject, "corr")
cc["Days", "(Intercept)"]

The $Subject part is required because lmer models can have multiple random effects terms, so VarCorr is always returned as a list of covariance matrices (named according to the name of the corresponding grouping variable)

Upvotes: 3

mfidino
mfidino

Reputation: 3055

You want to use lme4::VarCorr to extract those values. Here is an example.

library(lme4)

data("sleepstudy")

sl <- sleepstudy

m1 <- lmer(
  Reaction ~ Days + (Days | Subject),
  data = sl
)
summary(m1)
Linear mixed model fit by REML ['lmerMod']
Formula: Reaction ~ Days + (Days | Subject)
   Data: sl

REML criterion at convergence: 1743.6

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.9536 -0.4634  0.0231  0.4634  5.1793 

Random effects:
 Groups   Name        Variance Std.Dev. Corr
 Subject  (Intercept) 612.10   24.741       
          Days         35.07    5.922   0.07
 Residual             654.94   25.592       
Number of obs: 180, groups:  Subject, 18

Fixed effects:
            Estimate Std. Error t value
(Intercept)  251.405      6.825  36.838
Days          10.467      1.546   6.771

Correlation of Fixed Effects:
     (Intr)
Days -0.138

Here, we want to extract that correlation between (Intercept) and Days. We do that like so:

(ranef_vals <- data.frame(VarCorr(m1)))
       grp        var1 var2       vcov       sdcor
1  Subject (Intercept) <NA> 612.100158 24.74065799
2  Subject        Days <NA>  35.071714  5.92213766
3  Subject (Intercept) Days   9.604409  0.06555124
4 Residual        <NA> <NA> 654.940008 25.59179572


The value we'd want here is on the third row in the sdcor column.

ranef_vals$sdcor[3]
[1] 0.06555124

Upvotes: 2

Related Questions