tests/testthat/test-clmm.R

skip_if_not_installed("ordinal")

# Removes the em-space indents and figure-space padding, too
strip <- function(x) trimws(x, whitespace = "[\\h\\v]")
wine_data <- function() {
  e <- new.env()
  utils::data("wine", package = "ordinal", envir = e)
  e$wine
}
wine <- wine_data()
fit <- ordinal::clmm(rating ~ temp + contact + (1 | judge), data = wine)
fit_temp <- ordinal::clmm(rating ~ temp + (1 | judge), data = wine)

test_that("apa_parameters() shows predictors, thresholds, and random effects", {
  tbl <- apa_parameters(fit)
  expect_s3_class(tbl, "apa7_tbl")
  expect_equal(colnames(tbl), c("Variable", "*B*", "*SE*", "*OR*", "*z*", "*p*"))
  labels <- strip(tbl$Variable)
  expect_equal(labels[1], "Fixed effects")
  expect_true(all(c("1|2", "2|3", "3|4", "4|5") %in% labels))
  expect_true(any(startsWith(labels, "Random effects")))
  expect_true("Judge" %in% labels)
  # Cumulative odds ratios for the predictors, not the thresholds
  or_temp <- exp(fit$beta[["tempwarm"]])
  expect_equal(strip(tbl$`*OR*`[2]), sprintf("%.2f", or_temp))
  threshold_rows <- labels %in% c("1|2", "2|3", "3|4", "4|5")
  expect_true(all(strip(tbl$`*OR*`[threshold_rows]) == ""))
  expect_true(all(strip(tbl$`*z*`[threshold_rows]) == ""))
  # The judge SD
  sd_judge <- attr(ordinal::VarCorr(fit)$judge, "stddev")[[1]]
  expect_equal(strip(tbl$`*B*`[nrow(tbl)]), sprintf("%.2f", sd_judge))
})

test_that("thresholds, random effects, and headings can be left out", {
  expect_false(any(strip(apa_parameters(fit, thresholds = FALSE)$Variable) == "1|2"))
  no_random <- apa_parameters(fit, random = FALSE)
  expect_false(any(grepl("Random effects", no_random$Variable, fixed = TRUE)))
  flat <- apa_parameters(fit, effect_headings = FALSE)
  expect_true(any(grepl("Judge: *SD*", flat$Variable, fixed = TRUE)))
})

test_that("the note covers the thresholds and the random effects", {
  note <- apa_note(apa_parameters(fit))
  expect_match(note, "Thresholds (cutpoints) are on the logit scale and are not tested.", fixed = TRUE)
  expect_match(note, "*OR*\u00A0=\u00A0cumulative odds ratio", fixed = TRUE)
  expect_match(note, "*SD*\u00A0=\u00A0standard deviation", fixed = TRUE)
})

test_that("apa_performance() and comparisons work for ordinal mixed models", {
  perf <- apa_performance(fit)
  expect_equal(colnames(perf), c("*R*^2^~marginal~", "*R*^2^~conditional~", "*AIC*"))
  comparison <- apa_performance_comparison(fit_temp, fit)
  chisq <- 2 * (as.numeric(stats::logLik(fit)) - as.numeric(stats::logLik(fit_temp)))
  expect_equal(strip(comparison$`*&chi;*^2^`[2]), sprintf("%.2f", chisq))
  expect_true("*R*^2^~conditional~" %in% colnames(comparison))
})

test_that("lists of ordinal mixed models get both R2, a likelihood ratio test, and AIC", {
  tbl <- apa_parameters(list(fit_temp, fit))
  labels <- strip(tbl$Variable)
  expect_equal(sum(labels == "*R*^2^~marginal~"), 2)
  expect_equal(sum(labels == "*R*^2^~conditional~"), 2)
  expect_equal(sum(labels == "*AIC*"), 2)
  expect_equal(sum(labels == "*&chi;*^2^(1)"), 2)
  # Model 2 against model 1
  chisq <- 2 * (as.numeric(stats::logLik(fit)) - as.numeric(stats::logLik(fit_temp)))
  values <- strip(tbl[["*B*"]][labels == "*&chi;*^2^(1)"])
  expect_equal(sub("\\^.*$", "", values[2]), sprintf("%.2f", chisq))
})

Try the apa7 package in your browser

Any scripts or data that you put into this service are public.

apa7 documentation built on Dec. 1, 2026, 5:06 p.m.