Intro

This creates a table of random effects, including the uncertainty in the estimate where applicable.

Examples

library(lme4)
library(glmmTMB)
library(nlme)
library(brms)
library(rstanarm)
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
#   )

# rstanarm_model <-
#   stan_glmer(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_effects(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     2.26   12.1   -21.4         25.9
 2 Subject   Intercept 309   -40.4    12.1   -64.1        -16.7
 3 Subject   Intercept 310   -39.0    12.1   -62.6        -15.3
 4 Subject   Intercept 330    23.7    12.1     0.032       47.3
 5 Subject   Intercept 331    22.3    12.1    -1.40        45.9
 6 Subject   Intercept 332     9.04   12.1   -14.6         32.7
 7 Subject   Intercept 333    16.8    12.1    -6.82        40.5
 8 Subject   Intercept 334    -7.23   12.1   -30.9         16.4
 9 Subject   Intercept 335    -0.334  12.1   -24.0         23.3
10 Subject   Intercept 337    34.9    12.1    11.2         58.5
# … with 26 more rows


extract_random_effects(lme_model)
# A tibble: 36 x 4
   group_var effect group   value
   <chr>     <chr>  <chr>   <dbl>
 1 Subject   Days   308     9.20 
 2 Subject   Days   309    -8.62 
 3 Subject   Days   310    -5.45 
 4 Subject   Days   330    -4.81 
 5 Subject   Days   331    -3.07 
 6 Subject   Days   332    -0.272
 7 Subject   Days   333    -0.224
 8 Subject   Days   334     1.08 
 9 Subject   Days   335   -10.8  
10 Subject   Days   337     8.63 
# … with 26 more rows


extract_random_effects(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     2.82  13.7    -23.9        29.6
 2 Subject   Intercept 309   -40.0   13.8    -67.2       -12.9
 3 Subject   Intercept 310   -38.4   13.7    -65.4       -11.5
 4 Subject   Intercept 330    22.8   13.9     -4.51       50.2
 5 Subject   Intercept 331    21.6   13.6     -5.11       48.2
 6 Subject   Intercept 332     8.82  12.9    -16.5        34.1
 7 Subject   Intercept 333    16.4   13.1     -9.23       42.1
 8 Subject   Intercept 334    -7.00  12.9    -32.3        18.3
 9 Subject   Intercept 335    -1.04  14.0    -28.5        26.4
10 Subject   Intercept 337    34.7   13.6      7.94       61.4
# … with 26 more rows

extract_random_effects(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     5.16   14.2   -22.1        33.6 
 2 Subject   Intercept 309   -40.3    13.1   -69.7       -19.2 
 3 Subject   Intercept 310   -36.3    14.3   -64.5        -8.45
 4 Subject   Intercept 330    23.5    13.4     1.22       52.7 
 5 Subject   Intercept 331    23.0    13.1     0.625      50.0 
 6 Subject   Intercept 332     7.08   13.2   -15.7        28.8 
 7 Subject   Intercept 333    15.1    13.2   -10.0        40.8 
 8 Subject   Intercept 334    -4.68   12.3   -25.6        18.2 
 9 Subject   Intercept 335    -0.334  13.3   -26.3        24.8 
10 Subject   Intercept 337    37.2    15.0     7.58       65.8 
# … with 26 more rows


extract_random_effects(rstanarm_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     1.69   12.3    -22.5        25.9
 2 Subject   Intercept 309   -37.7    12.3    -61.9       -13.5
 3 Subject   Intercept 310   -35.0    12.3    -59.2       -10.8
 4 Subject   Intercept 330    19.3    12.3     -4.92       43.5
 5 Subject   Intercept 331    20.8    12.3     -3.38       45.0
 6 Subject   Intercept 332     8.67   12.3    -15.5        32.9
 7 Subject   Intercept 333    16.1    12.3     -8.06       40.3
 8 Subject   Intercept 334    -7.83   12.3    -32.0        16.4
 9 Subject   Intercept 335     0.385  12.3    -23.8        24.6
10 Subject   Intercept 337    32.5    12.3      8.26       56.7
# … with 26 more rows


extract_random_effects(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     1.51   13.3    -24.5        27.5
 2 Subject   Intercept 309   -40.4    13.3    -66.4       -14.3
 3 Subject   Intercept 310   -39.2    13.3    -65.2       -13.2
 4 Subject   Intercept 330    24.5    13.3     -1.51       50.5
 5 Subject   Intercept 331    22.9    13.3     -3.11       48.9
 6 Subject   Intercept 332     9.22   13.3    -16.8        35.2
 7 Subject   Intercept 333    17.2    13.3     -8.87       43.2
 8 Subject   Intercept 334    -7.45   13.3    -33.5        18.6
 9 Subject   Intercept 335     0.579  13.3    -25.4        26.6
10 Subject   Intercept 337    34.8    13.3      8.74       60.8
# … with 26 more rows

Options

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

extract_random_effects(
  lmer_model,
  ci_level = .9,
  digits = 2
)
# A tibble: 1,012 x 7
   group_var effect    group  value    se lower_5 upper_95
   <chr>     <chr>     <fct>  <dbl> <dbl>   <dbl>    <dbl>
 1 d         Intercept 1     -0.02   0.39   -0.66     0.62
 2 d         Intercept 6      0.09   0.34   -0.46     0.65
 3 d         Intercept 7      0.3    0.41   -0.38     0.98
 4 d         Intercept 8     -0.18   0.39   -0.82     0.46
 5 d         Intercept 12     0.21   0.31   -0.3      0.73
 6 d         Intercept 13     0.580  0.31    0.07     1.1 
 7 d         Intercept 14     0.39   0.3    -0.11     0.89
 8 d         Intercept 15    -0.13   0.47   -0.91     0.65
 9 d         Intercept 17     0.4    0.35   -0.18     0.99
10 d         Intercept 18     0.08   0.47   -0.69     0.86
# … with 1,002 more rows
tmb_zip <- glmmTMB(
  count ~ spp + mined + (1 | site),
  zi =  ~ spp + mined + (1 | site),
  family = truncated_poisson,
  data = Salamanders
)

extract_random_effects(
  tmb_zip,
  cond = 'zi'
)
# A tibble: 46 x 7
   group_var effect    group  value    se lower_2.5 upper_97.5
   <chr>     <chr>     <fct>  <dbl> <dbl>     <dbl>      <dbl>
 1 site      Intercept R-1   -0.02  0.126    -0.267      0.227
 2 site      Intercept R-2    0.037 0.125    -0.209      0.283
 3 site      Intercept R-3    0.051 0.135    -0.214      0.316
 4 site      Intercept R-4    0.434 0.123     0.193      0.675
 5 site      Intercept R-5    0.069 0.125    -0.177      0.314
 6 site      Intercept R-6    0.096 0.12     -0.139      0.331
 7 site      Intercept R-7   -0.202 0.146    -0.488      0.084
 8 site      Intercept R-8   -0.068 0.13     -0.323      0.188
 9 site      Intercept R-9    0.22  0.158    -0.09       0.53 
10 site      Intercept R-10  -0.1   0.135    -0.366      0.165
# … with 36 more rows