## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(intraclass)

## ----tree, echo = FALSE, out.width = "100%", fig.alt = "A four-step decision flow: agreement vs. consistency (type), single vs. average (unit), random vs. fixed raters (raters), and complete vs. incomplete designs, each mapping to an icc() argument."----
knitr::include_graphics("choosing-icc-tree.svg")

## ----data---------------------------------------------------------------------
str(ratings)

## ----oneway-------------------------------------------------------------------
oneway <- icc(ratings, score, subject, rater, model = "oneway", seed = 2024)
oneway

## ----type---------------------------------------------------------------------
agreement <- icc(ratings, score, subject, rater, type = "agreement", seed = 2024)
agreement

consistency <- icc(ratings, score, subject, rater, type = "consistency", seed = 2024)
consistency

## ----type-values, echo = FALSE------------------------------------------------
a1 <- with(tidy(agreement), estimate[term == "ICC(A,1)"])
c1 <- with(tidy(consistency), estimate[term == "ICC(C,1)"])

## ----unit-values, echo = FALSE------------------------------------------------
ak <- with(tidy(agreement), estimate[term == "ICC(A,k)"])

## ----raters-------------------------------------------------------------------
fixed <- icc(ratings, score, subject, rater, raters = "fixed", seed = 2024)

## ----incomplete---------------------------------------------------------------
inc <- icc(ratings_incomplete, score, subject, rater, seed = 2024)
inc

## ----incomplete-fixed---------------------------------------------------------
random_inc <- tidy(icc(ratings_incomplete, score, subject, rater,
  raters = "random", seed = 2024))
fixed_inc <- suppressWarnings(tidy(icc(ratings_incomplete, score, subject, rater,
  raters = "fixed", seed = 2024)))

## ----incomplete-fixed-table, echo = FALSE, eval = requireNamespace("gt", quietly = TRUE)----
cols <- c("term", "estimate", "conf.low", "conf.high")
rbind(
  data.frame(raters = "Random raters", random_inc[, cols]),
  data.frame(raters = "Fixed raters", fixed_inc[, cols])
) |>
  gt::gt(groupname_col = "raters") |>
  gt::tab_header(title = "Random and fixed raters on ratings_incomplete") |>
  gt::cols_label(
    term = "Coefficient",
    estimate = "Estimate",
    conf.low = "95% interval, lower",
    conf.high = "95% interval, upper"
  ) |>
  gt::fmt_number(columns = c(estimate, conf.low, conf.high), decimals = 3)

## ----disconnected, error = TRUE-----------------------------------------------
try({
disconnected <- data.frame(
  subject = factor(c(1, 1, 2, 2, 3, 3, 4, 4)),
  rater = factor(c(1, 2, 1, 2, 3, 4, 3, 4)),
  score = c(5, 6, 4, 5, 7, 8, 6, 7)
)
icc(disconnected, score, subject, rater)
})

## ----choose-------------------------------------------------------------------
choose_icc(type = "agreement", unit = "single", raters = "random")

