# NOT RUN {
library(lme4)
fit0 <- lmer(Reaction ~ 1 + (1 | Subject), sleepstudy)
icc(fit0)
# note: ICC for random-slope-intercept model usually not
# meaningful - see 'Note'.
fit1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
icc(fit1)
sleepstudy$mygrp <- sample(1:45, size = 180, replace = TRUE)
fit2 <- lmer(Reaction ~ Days + (1 | mygrp) + (1 | Subject), sleepstudy)
icc(fit2)
# return icc for all models at once
icc(fit0, fit1, fit2)
icc1 <- icc(fit1)
icc2 <- icc(fit2)
print(icc1, comp = "var")
print(icc2, comp = "var")
# }
# NOT RUN {
# compute ICC for Bayesian mixed model, with an ICC for each
# sample of the posterior. The print()-method then shows
# the median ICC as well as 89% HDI for the ICC.
# Change interval with print-method:
# print(icc(m, posterior = TRUE), prob = .5)
if (requireNamespace("brms", quietly = TRUE)) {
  library(dplyr)
  sleepstudy$mygrp <- sample(1:5, size = 180, replace = TRUE)
  sleepstudy <- sleepstudy %>%
    group_by(mygrp) %>%
    mutate(mysubgrp = sample(1:30, size = n(), replace = TRUE))
  m <- brms::brm(
    Reaction ~ Days + (1 | mygrp / mysubgrp) + (1 | Subject),
    data = sleepstudy
  )
  # by default, 89% interval
  icc(m, posterior = TRUE)
  # show 50% interval
  print(icc(m, posterior = TRUE), prob = .5, digits = 3)
}
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab