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)

extract_random_coefs(lmer_model)
# A tibble: 36 x 7
   group_var effect    group value    se lower_2.5 upper_97.5
   <chr>     <chr>     <fct> <dbl> <dbl>     <dbl>      <dbl>
 1 Subject   Intercept 308    254.  13.9      226.       281.
 2 Subject   Intercept 309    211.  13.9      184.       238.
 3 Subject   Intercept 310    212.  13.9      185.       240.
 4 Subject   Intercept 330    275.  13.9      248.       302.
 5 Subject   Intercept 331    274.  13.9      246.       301.
 6 Subject   Intercept 332    260.  13.9      233.       288.
 7 Subject   Intercept 333    268.  13.9      241.       295.
 8 Subject   Intercept 334    244.  13.9      217.       271.
 9 Subject   Intercept 335    251.  13.9      224.       278.
10 Subject   Intercept 337    286.  13.9      259.       313.
# … with 26 more rows


extract_random_coefs(lme_model)
# A tibble: 36 x 4
   group_var effect group  value
   <chr>     <chr>  <chr>  <dbl>
 1 Subject   Days   308   19.7  
 2 Subject   Days   309    1.85 
 3 Subject   Days   310    5.02 
 4 Subject   Days   330    5.65 
 5 Subject   Days   331    7.40 
 6 Subject   Days   332   10.2  
 7 Subject   Days   333   10.2  
 8 Subject   Days   334   11.5  
 9 Subject   Days   335   -0.285
10 Subject   Days   337   19.1  
# … with 26 more rows


extract_random_coefs(tmb_model)
# A tibble: 36 x 7
   group_var effect    group value    se lower_2.5 upper_97.5
   <chr>     <chr>     <fct> <dbl> <dbl>     <dbl>      <dbl>
 1 Subject   Intercept 308    254.  15.2      224.       284.
 2 Subject   Intercept 309    211.  15.3      181.       241.
 3 Subject   Intercept 310    213.  15.3      183.       243.
 4 Subject   Intercept 330    274.  15.4      244.       305.
 5 Subject   Intercept 331    273.  15.1      243.       303.
 6 Subject   Intercept 332    260.  14.5      232.       289.
 7 Subject   Intercept 333    268.  14.7      239.       297.
 8 Subject   Intercept 334    244.  14.5      216.       273.
 9 Subject   Intercept 335    250.  15.5      220.       281.
10 Subject   Intercept 337    286.  15.2      256.       316.
# … with 26 more rows


extract_random_coefs(brms_model)
# A tibble: 36 x 7
   group_var effect    group value    se lower_2.5 upper_97.5
   <chr>     <chr>     <chr> <dbl> <dbl>     <dbl>      <dbl>
 1 Subject   Intercept 308    255.  12.7      231.       279.
 2 Subject   Intercept 309    210.  11.9      183.       229.
 3 Subject   Intercept 310    214.  13.4      188.       243.
 4 Subject   Intercept 330    273.  12.0      251.       299.
 5 Subject   Intercept 331    273.  12.0      249.       294.
 6 Subject   Intercept 332    257.  11.4      234.       278.
 7 Subject   Intercept 333    265.  12.4      237.       287.
 8 Subject   Intercept 334    245.  11.5      223.       267.
 9 Subject   Intercept 335    249.  12.0      225.       272.
10 Subject   Intercept 337    287.  14.2      259.       314.
# … with 26 more rows


extract_random_coefs(mgcv_model)
# A tibble: 36 x 7
   group_var effect    group value    se lower_2.5 upper_97.5
   <chr>     <chr>     <chr> <dbl> <dbl>     <dbl>      <dbl>
 1 Subject   Intercept 308    253.  15.0      224.       282.
 2 Subject   Intercept 309    211.  15.0      182.       240.
 3 Subject   Intercept 310    212.  15.0      183.       242.
 4 Subject   Intercept 330    276.  15.0      247.       305.
 5 Subject   Intercept 331    274.  15.0      245.       304.
 6 Subject   Intercept 332    261.  15.0      231.       290.
 7 Subject   Intercept 333    269.  15.0      239.       298.
 8 Subject   Intercept 334    244.  15.0      215.       273.
 9 Subject   Intercept 335    252.  15.0      223.       281.
10 Subject   Intercept 337    286.  15.0      257.       315.
# … with 26 more rows

Options

extract_random_coefs(
  lmer_model,
  ci_level = .9,
  digits = 2
)
# A tibble: 36 x 7
   group_var effect    group value    se lower_5 upper_95
   <chr>     <chr>     <fct> <dbl> <dbl>   <dbl>    <dbl>
 1 Subject   Intercept 308    254.  13.9    231.     276.
 2 Subject   Intercept 309    211.  13.9    188.     234.
 3 Subject   Intercept 310    212.  13.9    190.     235.
 4 Subject   Intercept 330    275.  13.9    252.     298.
 5 Subject   Intercept 331    274.  13.9    251.     296.
 6 Subject   Intercept 332    260.  13.9    238.     283.
 7 Subject   Intercept 333    268.  13.9    245.     291.
 8 Subject   Intercept 334    244.  13.9    221.     267.
 9 Subject   Intercept 335    251.  13.9    228.     274.
10 Subject   Intercept 337    286.  13.9    263.     309.
# … with 26 more rows