R/summary.aibias.R

Defines functions summary.aibias print.aibias

Documented in print.aibias summary.aibias

#' Print an aibias object
#'
#' @param x An `aibias` object.
#' @param ... Ignored.
#' @return Invisibly returns \code{x}, called for its side effect of printing a concise summary of the audit object to the console.
#' @export
print.aibias <- function(x, ...) {
  cat("\n")
  cat("== AIBias Audit Object ==\n")
  cat("  Units       :", x$meta$n_units, "\n")
  cat("  Time points :", x$meta$n_times,
      "(", paste(x$meta$raw_times, collapse = ", "), ")\n")
  cat("  Groups      :", paste(x$meta$groups, collapse = ", "), "\n")
  cat("  Decision    :", x$meta$decision, "\n")
  if (!is.null(x$meta$outcome))
    cat("  Outcome     :", x$meta$outcome, "\n")
  cat("\n  Components populated:\n")
  components <- c("bias", "transitions", "amplification", "decomposition",
                  "adjusted", "bootstrap")
  for (comp in components) {
    status <- if (!is.null(x[[comp]])) "\u2713" else "\u2715"
    cat("   ", status, comp, "\n")
  }
  cat("\n")
  invisible(x)
}

#' Summarize an aibias object
#'
#' @description
#' Produces a comprehensive audit summary including trajectory statistics,
#' transition gaps, amplification indices, and narrative interpretation.
#'
#' @param object An `aibias` object.
#' @param ... Ignored.
#' @return Invisibly returns \code{object}, called for its side effect of printing a comprehensive audit summary to the console.
#' @export
summary.aibias <- function(object, ...) {
  cat("\n")
  cat("== AIBias Audit Summary ==\n")

  cat(sprintf("  Panel: %d units x %d waves | %d groups\n",
              object$meta$n_units, object$meta$n_times,
              length(object$meta$groups)))
  cat(sprintf("  Decision variable : '%s'\n", object$meta$decision))
  cat("\n")

  # ---- Decision rates by group ------------------------------------------
  cli_h2("Decision Rates by Group and Time")
  rates_wide <- object$rates |>
    select("group", "time", "rate") |>
    pivot_wider(names_from = "time", values_from = "rate",
                names_prefix = "t=")
  rates_wide[, -1] <- round(rates_wide[, -1], 3)
  print(as.data.frame(rates_wide), row.names = FALSE)
  cat("\n")

  # ---- Bias trajectories ------------------------------------------------
  if (!is.null(object$bias)) {
    cli_h2("Bias Trajectory Summary")
    ref <- object$bias$ref_group
    cat(sprintf("  Reference group: %s\n\n", ref))

    cb <- object$bias$cumulative |>
      select("group", "CB_normalized", "peak_disparity", "time_to_peak")
    cb[, -1] <- round(cb[, -1], 3)
    print(as.data.frame(cb), row.names = FALSE)
    cat("\n")
  }

  # ---- Transitions -------------------------------------------------------
  if (!is.null(object$transitions)) {
    cli_h2("Transition Gaps (pooled, vs reference)")
    rg  <- object$transitions$recovery_gap
    rtn <- object$transitions$retention_gap

    if (nrow(rg) > 0 && nrow(rtn) > 0) {
      gaps <- left_join(
        rg  |> select("group", "p_recovery_g",  "recovery_gap"),
        rtn |> select("group", "p_retention_g", "retention_gap"),
        by = "group"
      )
      gaps[, -1] <- round(gaps[, -1], 3)
      print(as.data.frame(gaps), row.names = FALSE)
    } else {
      cat("  (Insufficient data for transition gaps)\n")
    }
    cat("\n")
  }

  # ---- Amplification -----------------------------------------------------
  if (!is.null(object$amplification)) {
    cli_h2("Amplification Index (recovery gap minus retention gap)")
    amp_tbl <- object$amplification$cumulative
    amp_tbl[, -1] <- round(amp_tbl[, -1], 3)
    print(as.data.frame(amp_tbl), row.names = FALSE)
    cat("\n")

    cli_h2("Narrative Interpretation")
    for (i in seq_len(nrow(object$amplification$narratives))) {
      row <- object$amplification$narratives[i, ]
      g   <- row$group
      msg <- row$narrative
      if (nchar(trimws(msg)) > 0) {
        cat(sprintf("  [%s] %s\n", g, msg))
      } else {
        cat(sprintf("  [%s] No large transition gaps or index values.\n", g))
      }
    }
    cat("\n")
  }

  # ---- Decomposition -----------------------------------------------------
  if (!is.null(object$decomposition)) {
    dc <- object$decomposition
    cli_h2("Gap Decomposition by Step (ref: {dc$ref_group})")
    cat("  change = decay + recovery + retention + sample\n\n")
    st <- dc$steps
    tbl <- data.frame(
      group     = st$group,
      step      = paste0(st$time_from, "->", st$time_to),
      gap_prev  = st$gap_prev,
      gap_curr  = st$gap_curr,
      change    = st$change,
      decay     = st$decay,
      recovery  = st$recovery,
      retention = st$retention,
      sample    = st$sample_change,
      n_pair    = paste0(st$n_pair_g, "/", st$n_pair_ref),
      flag      = ifelse(!st$supported, "empty cell",
                         ifelse(st$sparse, "sparse", "")),
      stringsAsFactors = FALSE
    )
    num <- vapply(tbl, is.numeric, logical(1))
    tbl[num] <- lapply(tbl[num], round, 3)
    print(tbl, row.names = FALSE)
    cat("\n")

    cli_h2("Gap Decomposition, All Steps")
    tot <- as.data.frame(dc$totals)
    tot <- tot[, c("group", "gap_first", "gap_last", "change", "decay",
                   "recovery", "retention", "sample_change")]
    num <- vapply(tot, is.numeric, logical(1))
    tot[num] <- lapply(tot[num], round, 3)
    print(tot, row.names = FALSE)
    if (any(!st$supported))
      cat("  Steps with an empty prior-state cell have NA components.\n")
    if (any(st$sparse))
      cat(sprintf("  'sparse': a prior-state cell has fewer than %d paired units.\n",
                  dc$min_cell))
    cat("\n")
  }

  # ---- Bootstrap ---------------------------------------------------------
  if (!is.null(object$bootstrap$intervals)) {
    bi <- object$bootstrap$intervals
    show <- bi[bi$component %in% c("cumulative", "transitions",
                                   "decomposition_total") |
                 (bi$component == "amplification" & is.na(bi$time)), ]
    if (nrow(show) > 0) {
      cli_h2("Bootstrap Intervals ({object$bootstrap$conf * 100}%, B = {object$bootstrap$B})")
      show <- as.data.frame(show[, c("component", "group", "quantity",
                                     "estimate", "lo", "hi", "n_valid")])
      num <- vapply(show, is.numeric, logical(1)) & names(show) != "n_valid"
      show[num] <- lapply(show[num], round, 3)
      print(show, row.names = FALSE)
      cat("\n")
    }
  }

  invisible(object)
}

Try the AIBias package in your browser

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

AIBias documentation built on Dec. 1, 2026, 1:06 a.m.