R/clmm.R

Defines functions apa_performance.clmm apa_parameters.clmm

Documented in apa_parameters.clmm apa_performance.clmm

# Ordinal mixed model tables ----

#' @details
#' For ordinal mixed models (cumulative link mixed models fit with
#' `ordinal::clmm()`), the predictors and thresholds are displayed as for
#' `ordinal::clm()` models: Wald *z* tests, cumulative odds ratios (*OR*) for
#' logit links, and thresholds (cutpoints) below the predictors, without
#' tests. The random effects are displayed as for `lme4` models, under a
#' "Random effects" heading (see `random`, `effect_headings`, and
#' `random_scale`). Models fit with the older `ordinal::clmm2()` are not
#' supported.
#' @export
#' @rdname apa_parameters
#' @examples
#' \donttest{
#' if (requireNamespace("ordinal", quietly = TRUE)) {
#'   # Bitterness ratings of wine, with judges as a random factor
#'   data("wine", package = "ordinal")
#'   ordinal::clmm(rating ~ temp + contact + (1 | judge), data = wine) |>
#'     apa_parameters()
#' }
#' }
apa_parameters.clmm <- function(
  fit,
  predictor_parameters = c(
    "Parameter",
    "Coefficient",
    "SE",
    "Exp_Coefficient",
    "z",
    "p"
  ),
  starred = NA,
  bolded = NA,
  column_formats = NULL,
  t_with_df = TRUE,
  exponentiate = NULL,
  thresholds = TRUE,
  random = TRUE,
  effect_headings = TRUE,
  random_scale = c("sd", "variance"),
  ...
) {
  if (is.null(column_formats)) {
    column_formats <- the$column_formats
  }
  names(predictor_parameters) <- NULL
  random_scale <- match.arg(random_scale)
  link <- as.character(fit$link)
  if (is.null(exponentiate)) {
    exponentiate <- link == "logit"
  }

  d_b <- parameters::model_parameters(fit, effects = "fixed", ...) |>
    tibble::as_tibble()
  is_threshold <- d_b$Parameter %in% names(fit$alpha)

  # Format variable names here, because random-effect labels are built below
  name_formatter <- column_formats$Parameter@formatter
  labels <- pretty_parameter_names(fit, d_b$Parameter)
  labels[!is_threshold] <- name_formatter(labels[!is_threshold])
  d_b$Parameter <- labels

  if (exponentiate) {
    d_b <- add_exp_columns(d_b, predictor_parameters)
    exp_columns <- intersect(c("Exp_Coefficient", "Exp_CI_low", "Exp_CI_high"), colnames(d_b))
    d_b[is_threshold, exp_columns] <- NA
    if (link == "logit") {
      column_formats <- label_exp_columns(
        column_formats,
        c(header = "*OR*", latex = "$OR$", abbreviation = "cumulative odds ratio")
      )
    }
  }

  # Testing whether a threshold equals 0 is not meaningful
  d_b$z[is_threshold] <- NA
  d_b$p[is_threshold] <- NA

  # z tests have infinite df, which should not be displayed
  if ("df_error" %in% colnames(d_b) && all(is.infinite(d_b$df_error))) {
    d_b$df_error <- NULL
  }

  # Predictors first, then thresholds
  d_b <- d_b[order(is_threshold), ]
  if (!thresholds) {
    d_b <- d_b[!sort(is_threshold), ]
  }

  random_abbreviations <- NULL
  if (random) {
    d_r <- parameters::model_parameters(fit, effects = "random", ci = NULL) |>
      tibble::as_tibble()
    random_rows <- random_effect_rows(
      d_r,
      name_formatter,
      column_formats@accuracy,
      scale = random_scale,
      symbols_in_labels = !effect_headings
    )
    kinds <- attr(random_rows, "kinds")
    random_abbreviations <- random_effect_abbreviations(random_scale, kinds)
    d_b$Coefficient <- signs::signs(d_b$Coefficient, accuracy = column_formats@accuracy)
    d_b <- mixed_effect_sections(
      d_b,
      random_rows,
      headings = effect_headings,
      random_heading = random_effects_heading(random_scale, kinds)
    )
  }

  # Names were formatted above
  column_formats$Parameter@formatter <- \(x, ...) x

  format_apa_parameters(
    d_b,
    predictor_parameters = predictor_parameters,
    starred = starred,
    bolded = bolded,
    column_formats = column_formats,
    t_with_df = t_with_df,
    note = list(
      n = model_n_sentence(fit),
      vcov = describe_vcov(list(...)$vcov),
      thresholds = if (thresholds && any(is_threshold)) link,
      random_abbreviations = random_abbreviations
    )
  ) |>
    labels_formatted()
}

#' @details
#' For ordinal mixed models (`ordinal::clmm`), the default metrics are the
#' marginal and conditional *R*^2^ (`R2_marginal` and `R2_conditional`) and
#' `AIC`. Nested models are compared with likelihood ratio tests in
#' [apa_performance_comparison()].
#' @export
#' @rdname apa_performance
apa_performance.clmm <- function(
  fit,
  metrics = c("R2_marginal", "R2_conditional", "AIC"),
  column_formats = NULL
) {
  if (is.null(column_formats)) {
    column_formats <- the$column_formats
  }
  names(metrics) <- NULL
  if (any(metrics == "all")) {
    metrics <- c("R2_marginal", "R2_conditional", "AIC", "BIC")
  }
  # performance says that it cannot compute scoring rules for ordinal models
  d <- suppressMessages(muffle_backtick_warning(
    performance::model_performance(fit, metrics = c("AIC", "BIC", "R2"))
  )) |>
    tibble::as_tibble()
  format_performance(d, metrics, column_formats)
}

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.