vignettes/random_effects.Rmd
random_effects.Rmd
This creates a table of random effects, including the uncertainty in the estimate where applicable.
library(dplyr)
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)
38;5;246m# A tibble: 36 × 7
[39m
[.5 upper_97.5
group_var effect group value se lower_238;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
[3m
[38;5;250m 1
[39m Subject Intercept 308 2.26 12.1 -
[31m21
[39m
[31m.
[39m
[31m4
[39m 25.9
[38;5;250m 2
[39m Subject Intercept 309 -
[31m40
[39m
[31m.
[39m
[31m4
[39m 12.1 -
[31m64
[39m
[31m.
[39m
[31m1
[39m -
[31m16
[39m
[31m.
[39m
[31m7
[39m
[38;5;250m 3
[39m Subject Intercept 310 -
[31m39
[39m
[31m.
[39m
[31m0
[39m 12.1 -
[31m62
[39m
[31m.
[39m
[31m6
[39m -
[31m15
[39m
[31m.
[39m
[31m3
[39m
[38;5;250m 4
[39m Subject Intercept 330 23.7 12.1 0.032 47.3
[38;5;250m 5
[39m Subject Intercept 331 22.3 12.1 -
[31m1
[39m
[31m.
[39m
[31m40
[39m 45.9
[38;5;250m 6
[39m Subject Intercept 332 9.04 12.1 -
[31m14
[39m
[31m.
[39m
[31m6
[39m 32.7
[38;5;250m 7
[39m Subject Intercept 333 16.8 12.1 -
[31m6
[39m
[31m.
[39m
[31m82
[39m 40.5
[38;5;250m 8
[39m Subject Intercept 334 -
[31m7
[39m
[31m.
[39m
[31m23
[39m 12.1 -
[31m30
[39m
[31m.
[39m
[31m9
[39m 16.4
[38;5;250m 9
[39m Subject Intercept 335 -
[31m0
[39m
[31m.
[39m
[31m334
[39m 12.1 -
[31m24
[39m
[31m.
[39m
[31m0
[39m 23.3
[38;5;250m10
[39m Subject Intercept 337 34.9 12.1 11.2 58.5
[38;5;246m# … with 26 more rows
[39m
[
extract_random_effects(lme_model)
38;5;246m# A tibble: 36 × 4
[39m
[
group_var effect group value38;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;250m 1
[39m Subject Days 308 9.20
[38;5;250m 2
[39m Subject Days 309 -
[31m8
[39m
[31m.
[39m
[31m62
[39m
[38;5;250m 3
[39m Subject Days 310 -
[31m5
[39m
[31m.
[39m
[31m45
[39m
[38;5;250m 4
[39m Subject Days 330 -
[31m4
[39m
[31m.
[39m
[31m81
[39m
[38;5;250m 5
[39m Subject Days 331 -
[31m3
[39m
[31m.
[39m
[31m0
[39m
[31m7
[39m
[38;5;250m 6
[39m Subject Days 332 -
[31m0
[39m
[31m.
[39m
[31m272
[39m
[38;5;250m 7
[39m Subject Days 333 -
[31m0
[39m
[31m.
[39m
[31m224
[39m
[38;5;250m 8
[39m Subject Days 334 1.08
[38;5;250m 9
[39m Subject Days 335 -
[31m10
[39m
[31m.
[39m
[31m8
[39m
[38;5;250m10
[39m Subject Days 337 8.63
[38;5;246m# … with 26 more rows
[39m
[
extract_random_effects(tmb_model)
38;5;246m# A tibble: 36 × 7
[39m
[.5 upper_97.5
group_var effect group value se lower_238;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
[3m
[38;5;250m 1
[39m Subject Intercept 308 2.82 13.7 -
[31m23
[39m
[31m.
[39m
[31m9
[39m 29.6
[38;5;250m 2
[39m Subject Intercept 309 -
[31m40
[39m
[31m.
[39m
[31m0
[39m 13.8 -
[31m67
[39m
[31m.
[39m
[31m2
[39m -
[31m12
[39m
[31m.
[39m
[31m9
[39m
[38;5;250m 3
[39m Subject Intercept 310 -
[31m38
[39m
[31m.
[39m
[31m4
[39m 13.7 -
[31m65
[39m
[31m.
[39m
[31m4
[39m -
[31m11
[39m
[31m.
[39m
[31m5
[39m
[38;5;250m 4
[39m Subject Intercept 330 22.8 13.9 -
[31m4
[39m
[31m.
[39m
[31m51
[39m 50.2
[38;5;250m 5
[39m Subject Intercept 331 21.6 13.6 -
[31m5
[39m
[31m.
[39m
[31m11
[39m 48.2
[38;5;250m 6
[39m Subject Intercept 332 8.82 12.9 -
[31m16
[39m
[31m.
[39m
[31m5
[39m 34.1
[38;5;250m 7
[39m Subject Intercept 333 16.4 13.1 -
[31m9
[39m
[31m.
[39m
[31m23
[39m 42.1
[38;5;250m 8
[39m Subject Intercept 334 -
[31m7
[39m
[31m.
[39m
[31m00
[39m 12.9 -
[31m32
[39m
[31m.
[39m
[31m3
[39m 18.3
[38;5;250m 9
[39m Subject Intercept 335 -
[31m1
[39m
[31m.
[39m
[31m0
[39m
[31m4
[39m 14.0 -
[31m28
[39m
[31m.
[39m
[31m5
[39m 26.4
[38;5;250m10
[39m Subject Intercept 337 34.7 13.6 7.94 61.4
[38;5;246m# … with 26 more rows
[39m
[
extract_random_effects(brms_model)
38;5;246m# A tibble: 36 × 7
[39m
[.5 upper_97.5
group_var effect group value se lower_238;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
[3m
[38;5;250m 1
[39m Subject Intercept 308 2.82 12.9 -
[31m23
[39m
[31m.
[39m
[31m5
[39m 28.6
[38;5;250m 2
[39m Subject Intercept 309 -
[31m39
[39m
[31m.
[39m
[31m0
[39m 13.2 -
[31m64
[39m
[31m.
[39m
[31m4
[39m -
[31m16
[39m
[31m.
[39m
[31m7
[39m
[38;5;250m 3
[39m Subject Intercept 310 -
[31m38
[39m
[31m.
[39m
[31m6
[39m 13.5 -
[31m68
[39m
[31m.
[39m
[31m2
[39m -
[31m12
[39m
[31m.
[39m
[31m1
[39m
[38;5;250m 4
[39m Subject Intercept 330 23.4 14.6 -
[31m2
[39m
[31m.
[39m
[31m46
[39m 55.4
[38;5;250m 5
[39m Subject Intercept 331 23.6 16.3 -
[31m7
[39m
[31m.
[39m
[31m90
[39m 54.6
[38;5;250m 6
[39m Subject Intercept 332 10.1 14.0 -
[31m17
[39m
[31m.
[39m
[31m8
[39m 34.8
[38;5;250m 7
[39m Subject Intercept 333 18.2 15.4 -
[31m10
[39m
[31m.
[39m
[31m4
[39m 48.1
[38;5;250m 8
[39m Subject Intercept 334 -
[31m5
[39m
[31m.
[39m
[31m50
[39m 13.6 -
[31m34
[39m
[31m.
[39m
[31m4
[39m 18.5
[38;5;250m 9
[39m Subject Intercept 335 -
[31m0
[39m
[31m.
[39m
[31m964
[39m 16.5 -
[31m34
[39m
[31m.
[39m
[31m7
[39m 27.9
[38;5;250m10
[39m Subject Intercept 337 37.0 16.1 11.1 71.7
[38;5;246m# … with 26 more rows
[39m
[
extract_random_effects(rstanarm_model)
38;5;246m# A tibble: 36 × 7
[39m
[.5 upper_97.5
group_var effect group value se lower_238;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
[3m
[38;5;250m 1
[39m Subject Intercept 308 3.93 12.3 -
[31m20
[39m
[31m.
[39m
[31m3
[39m 28.1
[38;5;250m 2
[39m Subject Intercept 309 -
[31m38
[39m
[31m.
[39m
[31m4
[39m 12.3 -
[31m62
[39m
[31m.
[39m
[31m6
[39m -
[31m14
[39m
[31m.
[39m
[31m2
[39m
[38;5;250m 3
[39m Subject Intercept 310 -
[31m37
[39m
[31m.
[39m
[31m3
[39m 12.3 -
[31m61
[39m
[31m.
[39m
[31m5
[39m -
[31m13
[39m
[31m.
[39m
[31m1
[39m
[38;5;250m 4
[39m Subject Intercept 330 22.1 12.3 -
[31m2
[39m
[31m.
[39m
[31m10
[39m 46.3
[38;5;250m 5
[39m Subject Intercept 331 18.1 12.3 -
[31m6
[39m
[31m.
[39m
[31m11
[39m 42.3
[38;5;250m 6
[39m Subject Intercept 332 7.91 12.3 -
[31m16
[39m
[31m.
[39m
[31m3
[39m 32.1
[38;5;250m 7
[39m Subject Intercept 333 12.5 12.3 -
[31m11
[39m
[31m.
[39m
[31m7
[39m 36.7
[38;5;250m 8
[39m Subject Intercept 334 -
[31m8
[39m
[31m.
[39m
[31m20
[39m 12.3 -
[31m32
[39m
[31m.
[39m
[31m4
[39m 16.0
[38;5;250m 9
[39m Subject Intercept 335 -
[31m4
[39m
[31m.
[39m
[31m0
[39m
[31m3
[39m 12.3 -
[31m28
[39m
[31m.
[39m
[31m2
[39m 20.2
[38;5;250m10
[39m Subject Intercept 337 31.5 12.3 7.27 55.7
[38;5;246m# … with 26 more rows
[39m
[
extract_random_effects(mgcv_model)
38;5;246m# A tibble: 36 × 7
[39m
[.5 upper_97.5
group_var effect group value se lower_238;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
[3m
[38;5;250m 1
[39m Subject Intercept 308 1.51 13.3 -
[31m24
[39m
[31m.
[39m
[31m5
[39m 27.5
[38;5;250m 2
[39m Subject Intercept 309 -
[31m40
[39m
[31m.
[39m
[31m4
[39m 13.3 -
[31m66
[39m
[31m.
[39m
[31m4
[39m -
[31m14
[39m
[31m.
[39m
[31m3
[39m
[38;5;250m 3
[39m Subject Intercept 310 -
[31m39
[39m
[31m.
[39m
[31m2
[39m 13.3 -
[31m65
[39m
[31m.
[39m
[31m2
[39m -
[31m13
[39m
[31m.
[39m
[31m2
[39m
[38;5;250m 4
[39m Subject Intercept 330 24.5 13.3 -
[31m1
[39m
[31m.
[39m
[31m51
[39m 50.5
[38;5;250m 5
[39m Subject Intercept 331 22.9 13.3 -
[31m3
[39m
[31m.
[39m
[31m11
[39m 48.9
[38;5;250m 6
[39m Subject Intercept 332 9.22 13.3 -
[31m16
[39m
[31m.
[39m
[31m8
[39m 35.2
[38;5;250m 7
[39m Subject Intercept 333 17.2 13.3 -
[31m8
[39m
[31m.
[39m
[31m87
[39m 43.2
[38;5;250m 8
[39m Subject Intercept 334 -
[31m7
[39m
[31m.
[39m
[31m45
[39m 13.3 -
[31m33
[39m
[31m.
[39m
[31m5
[39m 18.6
[38;5;250m 9
[39m Subject Intercept 335 0.579 13.3 -
[31m25
[39m
[31m.
[39m
[31m4
[39m 26.6
[38;5;250m10
[39m Subject Intercept 337 34.8 13.3 8.74 60.8
[38;5;246m# … with 26 more rows
[39m
[
<-
lmer_model lmer(y ~ service + (1 | d) + (1 | s), data = InstEval[1:5000,])
extract_random_effects(
lmer_model,ci_level = .9,
digits = 2
%>%
) head()
38;5;246m# A tibble: 6 × 7
[39m
[
group_var effect group value se lower_5 upper_9538;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
[3m
[38;5;250m1
[39m d Intercept 1 -
[31m0
[39m
[31m.
[39m
[31m0
[39m
[31m2
[39m 0.39 -
[31m0
[39m
[31m.
[39m
[31m66
[39m 0.62
[38;5;250m2
[39m d Intercept 6 0.09 0.34 -
[31m0
[39m
[31m.
[39m
[31m46
[39m 0.65
[38;5;250m3
[39m d Intercept 7 0.3 0.41 -
[31m0
[39m
[31m.
[39m
[31m38
[39m 0.98
[38;5;250m4
[39m d Intercept 8 -
[31m0
[39m
[31m.
[39m
[31m18
[39m 0.39 -
[31m0
[39m
[31m.
[39m
[31m82
[39m 0.46
[38;5;250m5
[39m d Intercept 12 0.21 0.31 -
[31m0
[39m
[31m.
[39m
[31m3
[39m 0.73
[38;5;250m6
[39m d Intercept 13 0.58 0.31 0.07 1.1
[
<- glmmTMB(
tmb_zip ~ spp + mined + (1 | site),
count zi = ~ spp + mined + (1 | site),
family = truncated_poisson,
data = Salamanders
)
extract_random_effects(
tmb_zip,component = 'zi'
%>%
) head()
38;5;246m# A tibble: 6 × 7
[39m
[.5 upper_97.5
group_var effect group value se lower_238;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
[3m
[38;5;250m1
[39m site Intercept R-1 -
[31m0
[39m
[31m.
[39m
[31m0
[39m
[31m2
[39m 0.126 -
[31m0
[39m
[31m.
[39m
[31m267
[39m 0.227
[38;5;250m2
[39m site Intercept R-2 0.037 0.125 -
[31m0
[39m
[31m.
[39m
[31m209
[39m 0.283
[38;5;250m3
[39m site Intercept R-3 0.051 0.135 -
[31m0
[39m
[31m.
[39m
[31m214
[39m 0.316
[38;5;250m4
[39m site Intercept R-4 0.434 0.123 0.193 0.675
[38;5;250m5
[39m site Intercept R-5 0.069 0.125 -
[31m0
[39m
[31m.
[39m
[31m177
[39m 0.314
[38;5;250m6
[39m site Intercept R-6 0.096 0.12 -
[31m0
[39m
[31m.
[39m
[31m139
[39m 0.331
[