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)
                           group    effect variance     sd sd_2.5 sd_97.5 var_prop
Subject.sd((Intercept))  Subject Intercept  612.080 24.740 15.518  39.444    0.470
Subject.sd(Days)         Subject      Days   35.071  5.922  3.904   8.983    0.027
1                       Residual            654.942 25.592 22.797  28.729    0.503


extract_vc(tmb_model)
     group    effect variance     sd sd_2.5 sd_97.5 var_prop
1  Subject Intercept  565.516 23.781 15.017  37.658    0.451
2  Subject      Days   32.682  5.717  3.805   8.588    0.026
3 Residual            654.942 25.592     NA      NA    0.523


extract_vc(brms_model)
                     group    effect variance     sd sd_2.5 sd_97.5 var_prop
Subject.Intercept  Subject Intercept  695.259 26.368 16.608  44.489    0.487
Subject.Days       Subject      Days   39.950  6.321  4.104   9.264    0.028
residual__        Residual            692.387 26.313 23.257  29.863    0.485


extract_vc(mgcv_model)
                   group    effect variance     sd sd_2.5 sd_97.5 var_prop
s(Subject)       Subject Intercept  627.571 25.051 16.085  39.015    0.477
s(Days,Subject)  Subject      Days   35.858  5.988  4.025   8.908    0.027
scale           Residual            653.582 25.565 22.792  28.676    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
)
     group    effect variance   sd var_5 var_95 var_prop
1        d Intercept     0.26 0.51  0.22   0.31     0.15
2        s Intercept     0.11 0.33  0.08   0.14     0.06
3 Residual               1.36 1.17    NA     NA     0.79