library(glmmTMB)
library(insight)
library(brms)
library(lme4)
fish <- read.csv("https://stats.idre.ucla.edu/stat/data/fish.csv")
fish$nofish <- as.factor(fish$nofish)
fish$livebait <- as.factor(fish$livebait)
fish$camper <- as.factor(fish$camper)
fish$ID <- sample(1:4, nrow(fish), replace = TRUE)
data(sleepstudy)
sleepstudy$mygrp <- sample(1:5, size = 180, replace = TRUE)
sleepstudy$mysubgrp <- NA
for (i in 1:5) {
filter_group <- sleepstudy$mygrp == i
sleepstudy$mysubgrp[filter_group] <- sample(1:30, size = sum(filter_group), replace = TRUE)
}
m1 <- glmmTMB(
count ~ child + camper + (1 | persons),
ziformula = ~ child + livebait + (1 | ID),
dispformula = ~xb,
data = fish,
family = truncated_poisson()
)
m2 <- lme4::lmer(
Reaction ~ Days + (1 | mygrp / mysubgrp) + (1 | Subject),
data = sleepstudy
)
m3 <- brm(
bf(count ~ child + camper + (1 | persons),
zi ~ child + camper + (1 | ID)),
data = fish,
family = zero_inflated_poisson(),
chains = 1,
iter = 500
)
##
## SAMPLING FOR MODEL '758d6d0acc248f6bfaa3250ba988e1a1' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 0 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 500 [ 0%] (Warmup)
## Chain 1: Iteration: 50 / 500 [ 10%] (Warmup)
## Chain 1: Iteration: 100 / 500 [ 20%] (Warmup)
## Chain 1: Iteration: 150 / 500 [ 30%] (Warmup)
## Chain 1: Iteration: 200 / 500 [ 40%] (Warmup)
## Chain 1: Iteration: 250 / 500 [ 50%] (Warmup)
## Chain 1: Iteration: 251 / 500 [ 50%] (Sampling)
## Chain 1: Iteration: 300 / 500 [ 60%] (Sampling)
## Chain 1: Iteration: 350 / 500 [ 70%] (Sampling)
## Chain 1: Iteration: 400 / 500 [ 80%] (Sampling)
## Chain 1: Iteration: 450 / 500 [ 90%] (Sampling)
## Chain 1: Iteration: 500 / 500 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 18.118 seconds (Warm-up)
## Chain 1: 11.439 seconds (Sampling)
## Chain 1: 29.557 seconds (Total)
## Chain 1:
find_parameters(m1)
## $conditional
## [1] "(Intercept)" "child" "camper1"
##
## $random
## $random$persons
## [1] "(Intercept)"
##
##
## $zero_inflated
## [1] "(Intercept)" "child" "livebait1"
##
## $zero_inflated_random
## $zero_inflated_random$ID
## [1] "(Intercept)"
find_parameters(m2)
## $conditional
## [1] "(Intercept)" "Days"
##
## $random
## $random$`mysubgrp:mygrp`
## [1] "(Intercept)"
##
## $random$Subject
## [1] "(Intercept)"
##
## $random$mygrp
## [1] "(Intercept)"
find_parameters(m3)
## $conditional
## [1] "b_Intercept" "b_child" "b_camper1"
##
## $random
## [1] "r_persons.1.Intercept." "r_persons.2.Intercept."
## [3] "r_persons.3.Intercept." "r_persons.4.Intercept."
##
## $zero_inflated
## [1] "b_zi_Intercept" "b_zi_child" "b_zi_camper1"
##
## $zero_inflated_random
## [1] "r_ID__zi.1.Intercept." "r_ID__zi.2.Intercept." "r_ID__zi.3.Intercept."
## [4] "r_ID__zi.4.Intercept."
get_parameters(m1)
## parameter estimate group
## 1 (Intercept) 1.2628046 conditional
## 2 child -1.1416519 conditional
## 3 camper1 0.7335392 conditional
## 4 (Intercept) 0.3048439 zero_inflated
## 5 child 1.1336880 zero_inflated
## 6 livebait1 -0.8033914 zero_inflated
get_parameters(m2)
## parameter estimate
## 1 (Intercept) 251.40327
## 2 Days 10.46705
head(get_parameters(m3))
## b_Intercept b_child b_camper1 b_zi_Intercept b_zi_child b_zi_camper1
## 1 2.0542495 -1.186066 0.7818462 -1.3010459 1.8567137 -0.6752599
## 2 1.6344319 -1.082427 0.9168629 -0.6183553 1.6241038 -0.9338347
## 3 2.1012163 -1.180806 0.5581082 -0.6157602 1.4933227 -0.6237581
## 4 1.8699144 -1.144831 0.8705879 -0.8082500 1.7635088 -1.2690562
## 5 1.4934644 -1.126598 0.8343244 -0.8640618 1.8734654 -0.9610354
## 6 0.5022466 -1.232757 0.6634711 -0.3689103 0.9980293 -0.7922050
get_parameters(m1, "random")
## [[1]]
## [[1]]$persons
## (Intercept)
## 1 -1.2400212
## 2 -0.3456358
## 3 0.3617482
## 4 1.2553087
##
##
## [[2]]
## [[2]]$ID
## (Intercept)
## 1 -3.923348e-09
## 2 -5.087596e-09
## 3 -1.131362e-08
## 4 2.032456e-08
get_parameters(m2, "random")
## $`mysubgrp:mygrp`
## (Intercept)
## 1:1 -0.0243918513
## 1:2 -0.0096004890
## 1:3 -0.0161689643
## 1:4 0.0788391059
## 1:5 -0.0421463191
## 2:1 -0.2636600063
## 2:3 0.0361718127
## 2:4 0.0295976786
## 2:5 0.0594435825
## 3:2 0.0730665433
## 3:3 0.3111273394
## 4:1 -0.0382678870
## 4:4 -0.0165278498
## 4:5 0.1081311195
## 5:1 -0.0462981879
## 5:2 -0.2171665249
## 5:3 0.1612928708
## 5:4 0.0043834928
## 5:5 -0.0302983359
## 6:4 0.0442715549
## 7:1 0.0732291517
## 7:2 -0.1093998076
## 7:3 0.1786036050
## 7:4 0.0220110041
## 7:5 0.1538625614
## 8:1 0.0518094542
## 8:2 -0.0736068996
## 8:5 -0.0333509244
## 9:1 -0.0978986764
## 9:2 -0.0395036820
## 9:4 -0.1646140848
## 9:5 0.0160659912
## 10:2 0.0218039248
## 10:3 -0.0444330063
## 10:5 -0.0352285736
## 11:1 0.0173926217
## 11:2 0.1024468797
## 11:3 0.0424334180
## 11:5 0.0574065883
## 12:1 -0.0758397790
## 12:2 0.1140929550
## 12:3 -0.2170997899
## 12:5 -0.0283196317
## 13:2 -0.0141220709
## 13:3 -0.1061076148
## 13:4 0.1031258942
## 14:2 0.1556475326
## 14:3 0.0596443163
## 14:4 0.1273234361
## 15:1 0.1990991745
## 15:3 0.0162870366
## 15:5 0.0679019399
## 16:1 0.2801962921
## 16:2 0.0007468811
## 16:3 -0.0229157225
## 16:5 -0.0397026519
## 17:3 -0.0501222674
## 17:4 -0.0024828347
## 17:5 0.0659627679
## 18:2 -0.0268146844
## 18:3 -0.0917504399
## 19:2 0.0820410959
## 19:3 -0.2090442558
## 19:4 -0.0263167026
## 20:1 0.0910663783
## 20:2 0.0127141711
## 20:3 -0.0665403232
## 20:4 -0.0019222817
## 21:1 -0.0955613159
## 21:3 -0.0269597744
## 22:1 0.0393746273
## 22:2 -0.0046843950
## 22:3 -0.0401584309
## 22:4 -0.1116093259
## 22:5 0.0102270911
## 23:2 -0.0492271816
## 23:3 -0.0269418068
## 23:4 -0.0303152857
## 23:5 0.0176808269
## 24:1 0.0223234580
## 24:2 0.0682152517
## 24:3 -0.1103046336
## 24:5 0.0775515421
## 25:1 -0.0789541114
## 25:3 -0.0015762324
## 26:1 0.0345676104
## 26:3 -0.0754166162
## 26:4 0.0541663725
## 26:5 -0.2489723897
## 27:1 0.0882611917
## 27:4 -0.0191976130
## 27:5 -0.0578099993
## 28:2 -0.0283957544
## 28:5 -0.0752639276
## 29:2 -0.0281454034
## 29:4 0.0601198410
## 29:5 -0.0574914493
## 30:2 -0.1639860057
## 30:5 0.1209067879
##
## $Subject
## (Intercept)
## 308 40.776159
## 309 -77.844409
## 310 -63.148390
## 330 4.417411
## 331 10.198964
## 332 8.260505
## 333 16.530575
## 334 -3.008461
## 335 -45.246161
## 337 72.160940
## 349 -21.235261
## 350 14.097913
## 351 -7.862729
## 352 36.393100
## 369 7.021882
## 370 -6.330726
## 371 -3.279458
## 372 18.098146
##
## $mygrp
## (Intercept)
## 1 3.628855e-07
## 2 -2.753345e-07
## 3 -6.169417e-07
## 4 3.102450e-07
## 5 2.191457e-07
##
## with conditional variances for "mysubgrp:mygrp" "Subject" "mygrp"
head(get_parameters(m3, "random"))
## r_persons.1.Intercept. r_persons.2.Intercept. r_persons.3.Intercept.
## 1 -2.3659755 -1.1350469 -0.53683067
## 2 -2.1650053 -0.8681478 -0.24351399
## 3 -2.5186970 -1.2889036 -0.37518423
## 4 -2.5381681 -1.1626873 -0.47128980
## 5 -1.8354803 -0.5778968 0.01688385
## 6 -0.9977171 0.3094210 1.14464184
## r_persons.4.Intercept. r_ID__zi.1.Intercept. r_ID__zi.2.Intercept.
## 1 0.4638486 -0.18962150 0.02038113
## 2 0.6619376 -0.27174074 -0.23236978
## 3 0.6042148 -0.00189451 0.22572416
## 4 0.5595441 -0.08737092 0.31639670
## 5 0.9022011 -0.04697185 0.40943500
## 6 2.0961958 0.05975272 0.08305179
## r_ID__zi.3.Intercept. r_ID__zi.4.Intercept.
## 1 -0.21563981 0.26858139
## 2 -0.57868298 1.30668552
## 3 -0.14615958 -0.02553554
## 4 0.05176094 1.06274005
## 5 0.11439675 0.37850859
## 6 0.02486363 0.10662918
head(get_parameters(m3, effects = "fixed", component = "zi"))
## b_zi_Intercept b_zi_child b_zi_camper1
## 1 -1.3010459 1.8567137 -0.6752599
## 2 -0.6183553 1.6241038 -0.9338347
## 3 -0.6157602 1.4933227 -0.6237581
## 4 -0.8082500 1.7635088 -1.2690562
## 5 -0.8640618 1.8734654 -0.9610354
## 6 -0.3689103 0.9980293 -0.7922050