Intro

This creates a standard table of variance components. Includes the estimated variance and standard deviation of the random effects, a confidence interval for either, and the relative proportion of variance for each component.

Examples

library(lme4)
library(glmmTMB)
library(nlme)
library(brms)
library(mgcv)


lmer_model <-
  lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)

lme_model  <-
  lme(Reaction ~ Days, random = ~ 1 + Days | Subject, data = sleepstudy)

tmb_model  <-
  glmmTMB(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)

# brms_model <-
#   brm(Reaction ~ Days + (1 + Days | Subject), 
#       data = sleepstudy,
#       cores = 4,
#       refresh = -1,
#       verbose = FALSE
#   )

# this is akin to (1 | Subject) + (0 + Days | Subject) in lme4
mgcv_model <-
  gam(
    Reaction ~  Days +
      s(Subject, bs = 're') +
      s(Days, Subject, bs = 're'),
    data   = lme4::sleepstudy,
    method = 'REML'
  )
library(mixedup)

extract_vc(lmer_model)
                          group    effect variance     sd sd_2.5 sd_97.5 var_prop
sd_(Intercept)|Subject  Subject Intercept  612.100 24.741 14.381  37.716    0.470
sd_Days|Subject         Subject      Days   35.072  5.922  3.801   8.753    0.027
sigma                  Residual            654.940 25.592 22.898  28.858    0.503


extract_vc(lme_model)

[38;5;246m# A tibble: 3 × 7
[39m
  group    effect    variance    sd sd_2.5 sd_97.5 var_prop
  
[3m
[38;5;246m<chr>
[39m
[23m    
[3m
[38;5;246m<chr>
[39m
[23m        
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m  
[3m
[38;5;246m<dbl>
[39m
[23m   
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m Subject  Intercept    612.  24.7   15.5    39.4     0.47 

[38;5;250m2
[39m Subject  Days          35.1  5.92   3.90    8.98    0.027

[38;5;250m3
[39m Residual 
[31mNA
[39m           655.  25.6   22.8    28.7     0.503


extract_vc(tmb_model)

[38;5;246m# A tibble: 3 × 7
[39m
  group    effect    variance    sd sd_2.5 sd_97.5 var_prop
  
[3m
[38;5;246m<chr>
[39m
[23m    
[3m
[38;5;246m<chr>
[39m
[23m        
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m  
[3m
[38;5;246m<dbl>
[39m
[23m   
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m Subject  Intercept    566.  23.8   15.0    37.7     0.451

[38;5;250m2
[39m Subject  Days          32.7  5.72   3.80    8.59    0.026

[38;5;250m3
[39m Residual 
[31mNA
[39m           655.  25.6   
[31mNA
[39m      
[31mNA
[39m       0.523


extract_vc(brms_model)

[38;5;246m# A tibble: 3 × 7
[39m
  group    effect    variance    sd sd_2.5 sd_97.5 var_prop
  
[3m
[38;5;246m<chr>
[39m
[23m    
[3m
[38;5;246m<chr>
[39m
[23m        
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m  
[3m
[38;5;246m<dbl>
[39m
[23m   
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m Subject  Intercept    793.  28.2   15.8    46.3     0.527

[38;5;250m2
[39m Subject  Days          42.2  6.50   4.32    9.28    0.028

[38;5;250m3
[39m Residual 
[31mNA
[39m           669.  25.9   22.5    29.6     0.445


extract_vc(mgcv_model)

[38;5;246m# A tibble: 3 × 7
[39m
  group    effect    variance    sd sd_2.5 sd_97.5 var_prop
  
[3m
[38;5;246m<chr>
[39m
[23m    
[3m
[38;5;246m<chr>
[39m
[23m        
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m  
[3m
[38;5;246m<dbl>
[39m
[23m   
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m Subject  Intercept    628.  25.1   16.1    39.0     0.477

[38;5;250m2
[39m Subject  Days          35.9  5.99   4.03    8.91    0.027

[38;5;250m3
[39m Residual 
[31mNA
[39m           654.  25.6   22.8    28.7     0.496

Options

tmb_model <-
  glmmTMB(y ~ service + (1 | d) + (1 | s), data = InstEval[1:5000,])

extract_vc(
  tmb_model,
  ci_level = .9,
  ci_scale = 'var',
  digits   = 2
)

[38;5;246m# A tibble: 3 × 7
[39m
  group    effect    variance    sd var_5 var_95 var_prop
  
[3m
[38;5;246m<chr>
[39m
[23m    
[3m
[38;5;246m<chr>
[39m
[23m        
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m 
[3m
[38;5;246m<dbl>
[39m
[23m  
[3m
[38;5;246m<dbl>
[39m
[23m    
[3m
[38;5;246m<dbl>
[39m
[23m

[38;5;250m1
[39m d        Intercept     0.26  0.51  0.22   0.31     0.15

[38;5;250m2
[39m s        Intercept     0.11  0.33  0.08   0.14     0.06

[38;5;250m3
[39m Residual 
[31mNA
[39m            1.36  1.17 
[31mNA
[39m     
[31mNA
[39m        0.79