Intro

This creates a table of random coefficients, i.e. fixed effects plus random effects. Uncertainty estimates are included where possible, but likely only accurate for Bayesian approaches.

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)

head(extract_random_coefs(lmer_model))

[38;5;246m# A tibble: 6 × 7
[39m
  group_var effect    group value    se lower_2.5 upper_97.5
  
[3m
[38;5;246m<chr>
[39m
[23m     
[3m
[38;5;246m<chr>
[39m
[23m     
[3m
[38;5;246m<fct>
[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 308    254.  13.9      226.       281.

[38;5;250m2
[39m Subject   Intercept 309    211.  13.9      184.       238.

[38;5;250m3
[39m Subject   Intercept 310    212.  13.9      185.       240.

[38;5;250m4
[39m Subject   Intercept 330    275.  13.9      248.       302.

[38;5;250m5
[39m Subject   Intercept 331    274.  13.9      246.       301.

[38;5;250m6
[39m Subject   Intercept 332    260.  13.9      233.       288.


head(extract_random_coefs(lme_model)) # different order

[38;5;246m# A tibble: 6 × 6
[39m
  group_var effect group value lower_2.5 upper_97.5
  
[3m
[38;5;246m<chr>
[39m
[23m     
[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

[38;5;250m1
[39m Subject   Days   308   19.7       7.42       13.5

[38;5;250m2
[39m Subject   Days   309    1.85      7.42       13.5

[38;5;250m3
[39m Subject   Days   310    5.02      7.42       13.5

[38;5;250m4
[39m Subject   Days   330    5.65      7.42       13.5

[38;5;250m5
[39m Subject   Days   331    7.40      7.42       13.5

[38;5;250m6
[39m Subject   Days   332   10.2       7.42       13.5


head(extract_random_coefs(tmb_model))

[38;5;246m# A tibble: 6 × 7
[39m
  group_var effect    group value    se lower_2.5 upper_97.5
  
[3m
[38;5;246m<chr>
[39m
[23m     
[3m
[38;5;246m<chr>
[39m
[23m     
[3m
[38;5;246m<fct>
[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 308    254.  15.2      224.       284.

[38;5;250m2
[39m Subject   Intercept 309    211.  15.3      181.       241.

[38;5;250m3
[39m Subject   Intercept 310    213.  15.3      183.       243.

[38;5;250m4
[39m Subject   Intercept 330    274.  15.4      244.       305.

[38;5;250m5
[39m Subject   Intercept 331    273.  15.1      243.       303.

[38;5;250m6
[39m Subject   Intercept 332    260.  14.5      232.       289.


head(extract_random_coefs(brms_model))

[38;5;246m# A tibble: 6 × 7
[39m
  group_var effect    group value    se lower_2.5 upper_97.5
  
[3m
[38;5;246m<chr>
[39m
[23m     
[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

[38;5;250m1
[39m Subject   Intercept 308    253.  13.5      228.       279.

[38;5;250m2
[39m Subject   Intercept 309    211.  13.2      185.       236.

[38;5;250m3
[39m Subject   Intercept 310    212.  12.7      189.       235.

[38;5;250m4
[39m Subject   Intercept 330    274.  12.9      247.       298.

[38;5;250m5
[39m Subject   Intercept 331    274.  13.3      251.       296.

[38;5;250m6
[39m Subject   Intercept 332    261.  13.4      235.       282.


head(extract_random_coefs(mgcv_model))

[38;5;246m# A tibble: 6 × 7
[39m
  group_var effect    group value    se lower_2.5 upper_97.5
  
[3m
[38;5;246m<chr>
[39m
[23m     
[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

[38;5;250m1
[39m Subject   Intercept 308    253.  15.0      224.       282.

[38;5;250m2
[39m Subject   Intercept 309    211.  15.0      182.       240.

[38;5;250m3
[39m Subject   Intercept 310    212.  15.0      183.       242.

[38;5;250m4
[39m Subject   Intercept 330    276.  15.0      247.       305.

[38;5;250m5
[39m Subject   Intercept 331    274.  15.0      245.       304.

[38;5;250m6
[39m Subject   Intercept 332    261.  15.0      231.       290.

Options

extract_random_coefs(
  lmer_model,
  ci_level = .9,
  digits   = 2
)

[38;5;246m# A tibble: 36 × 7
[39m
   group_var effect    group value    se lower_5 upper_95
   
[3m
[38;5;246m<chr>
[39m
[23m     
[3m
[38;5;246m<chr>
[39m
[23m     
[3m
[38;5;246m<fct>
[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;250m 1
[39m Subject   Intercept 308    254.  13.9    231.     276.

[38;5;250m 2
[39m Subject   Intercept 309    211.  13.9    188.     234.

[38;5;250m 3
[39m Subject   Intercept 310    212.  13.9    190.     235.

[38;5;250m 4
[39m Subject   Intercept 330    275.  13.9    252.     298.

[38;5;250m 5
[39m Subject   Intercept 331    274.  13.9    251.     296.

[38;5;250m 6
[39m Subject   Intercept 332    260.  13.9    238.     283.

[38;5;250m 7
[39m Subject   Intercept 333    268.  13.9    245.     291.

[38;5;250m 8
[39m Subject   Intercept 334    244.  13.9    221.     267.

[38;5;250m 9
[39m Subject   Intercept 335    251.  13.9    228.     274.

[38;5;250m10
[39m Subject   Intercept 337    286.  13.9    263.     309.

[38;5;246m# … with 26 more rows
[39m