R/axes_reliability_oop.R

Defines functions summary.circumplex_axes_reliability axes_cat_fit_note print.circumplex_axes_reliability axes_metric_note axes_fmt is_axes_reliability new_axes_reliability

Documented in print.circumplex_axes_reliability summary.circumplex_axes_reliability

# S3 class and constructor for circumplex axes-reliability results (Strack,
# Jacobs & Grosse Holtforth, 2013). The constructor mirrors new_structure()
# (R/fit_structure_oop.R): a scalar S3 list with named components and a class
# tag. axes_reliability() (R/axes_reliability.R) is the only caller.
# print()/summary() methods are added by T10.

new_axes_reliability <- function(results, components, fit, details, call) {
  stopifnot(is.data.frame(results))
  stopifnot(is.data.frame(components))
  stopifnot(is.list(fit))
  stopifnot(is.list(details))
  new_s3_lst(
    list(
      results = results,
      components = components,
      fit = fit,
      details = details,
      call = call
    ),
    class = "circumplex_axes_reliability"
  )
}

is_axes_reliability <- function(x) {
  is.list(x) && inherits(x, "circumplex_axes_reliability")
}

# ---- shared formatting ------------------------------------------------------

# Round-or-dash: format a numeric vector to `digits`, showing NA as "--".
axes_fmt <- function(x, digits = 3) {
  out <- formatC(round(x, digits), format = "f", digits = digits)
  out[is.na(x)] <- "--"
  out
}

# The metric note shared by print()/summary() and the roxygen: the model is fit
# to the item correlation matrix as if it were a covariance matrix (the paper's
# own practice), so the point estimates are correct but normal-theory maximum
# likelihood misprices everything downstream of the sampling variability
# (cudeck1989, p. 323). The printed text below cites author-year rather than a
# citekey because it is read by users, not by this repo.
#
# This text used to WARN. Both halves of the warning are now gone, in two
# milestones: M66 corrected the component standard errors (RR13 BC1, D-035),
# and M68 scaled the global test statistic (satorra1994 (p. 407), D-036). What is
# left is not a caveat but an orientation note -- it says what was corrected,
# because the reported numbers differ from the ones a reader reproduces in
# LISREL or by calling lavaan directly, and an unexplained difference reads as
# a bug.
#
# Why neither warning could simply have been sharpened instead: the SE ratio
# runs [0.81, 1.97] across the accepted input space and dips below 1 for
# weak-axes/strong-general instruments (RR13 section 2), and the fit distortion
# is likewise a per-fit quantity that the retired "roughly 4%" reported as a
# constant. A caveat that quantifies one population is not a caveat that can be
# stated honestly; that is what overturned "document, don't fix" on both sides.
# Split into three pieces because the two corrections can fail INDEPENDENTLY,
# and a note that asserts a correction which did not happen is worse than no
# note. print() below prints the shared opening beside whichever halves are
# live; a failed half gets its own NA-with-reason note instead.
#
# The opening is a FUNCTION of which halves are live rather than a constant,
# because its own summary clause is an assertion too: "both sides of that
# mismatch are corrected" is false on an object where one correction failed,
# and it was printed unconditionally -- the exact failure the three-way split
# exists to prevent, reintroduced one level up (M68 review, F1). The three
# variants say only what is true of the object in hand; where neither half is
# live, print() emits no opening at all.
axes_metric_note <- function(se_live, fit_live) {
  # Each variant is returned unwrapped: the clause lengths differ, so the three
  # tails stay separate, but print() wraps whichever one is live to the
  # reader's console width.
  tail <- if (se_live && fit_live) {
    paste0("both sides of that mismatch are",
           " corrected, so these numbers differ from LISREL's, and from",
           " lavaan's own, by design.")
  } else if (se_live) {
    paste0("the standard-error side of that",
           " mismatch is corrected, so those numbers differ from LISREL's,",
           " and from lavaan's own, by design.")
  } else {
    paste0("the global-fit side of that",
           " mismatch is corrected, so those numbers differ from LISREL's,",
           " and from lavaan's own, by design.")
  }
  paste0(
    "Note: the model is fit to the item correlation matrix as if it were a",
    " covariance matrix (Cudeck, 1989), and ", tail
  )
}
# Unwrapped, like the note above: a constant built at load time cannot know the
# width its reader will print at, so print() wraps it at the point of use.
axes_se_corrected_note <- paste0(
  "The component standard errors are adjusted to the correlation metric ",
  "and are calibrated; they are typically smaller than the values printed ",
  "by Strack et al. (2013), whose LISREL output carries no correction."
)
# The third surface RR14 required (BC5). It carries the DIRECTION of the
# small-sample error and a pointer, deliberately no rates: the measured numbers
# live in the roxygen and the vignette, where they are tied to the committed
# fixture and move with it, and a rate printed here would drift out of agreement
# with them the first time the fixture is regenerated.
#
# It is printed by summary(), directly UNDER the chi-square/RMSEA/CFI line,
# rather than by print() -- BC5 asks for it beside that line, and print() emits
# its notes before summary() has printed the components table and the fit line,
# which put two blocks between the claim and the numbers it qualifies (M68
# review, F16). print() reports no fit statistic at all, so nothing is orphaned
# by the move, and the sentence appears exactly once in a summary() call. The
# scaling-failure note travels with it for the same reason: it explains four NAs
# that only summary() displays.
axes_fit_scaled_note <- c(
  paste0(
    "The global fit statistics chisq, pvalue, rmsea and cfi are scaled to ",
    "that metric (Satorra & Bentler, 1994), which removes a distortion that ",
    "flatters fit; df and srmr are unchanged. The scaled test can modestly ",
    "over-reject at typical sample sizes: it over-flags misfit rather than ",
    "flattering it; see ?axes_reliability for the measured rates."
  ),
  # Named here and not only in the help page because the cross-check a reader
  # runs is prompted by seeing these four numbers, and both of the obvious
  # comparisons -- lavaan's bare cfi and its cfi.robust -- disagree with them.
  # A second element, so wrap_prose() starts it on a line of its own (M146).
  paste0(
    "They follow lavaan's *.scaled definitions, not its *.robust ones, and ",
    "differ from what fitMeasures() reports for an equivalent ML fit."
  )
)

# The extra sentence the FIML path owes on top of the caveat above. Its SEs
# carry an approximation the correction does NOT remove: they are computed on
# the standardized metric holding the standardization constants fixed, so they
# do not propagate the uncertainty in the FIML means and SDs that produced that
# metric. M66 corrected the correlation-as-covariance metric error on this path
# as on every other -- composed multiplicatively, so the observed-information
# pricing of the missing information survives -- and RR13 section 4 measured
# what is left over afterwards at 0.1 / 0.8 / 1.8% at 2 / 5 / 10% MCAR, an
# order of magnitude below the effect that was corrected. Unchanged at M66
# (its wording never mentioned the metric error), and stated where the SEs are
# shown rather than only in the help page, because that is where a reader is
# about to use them.
axes_fiml_se_caveat <- paste0(
  "Note: under missing = \"fiml\" the standard errors are ",
  "observed-information SEs on the standardized metric, conditional on the ",
  "FIML standardization constants; FIML assumes the data are missing at ",
  "random and multivariate normal."
)

# ---- methods ----------------------------------------------------------------

#' Print circumplex axes-reliability results
#'
#' Compact display of an [axes_reliability()] object: the per-axis reliability,
#' SEm, and Nunnally-Bernstein comparison, with the correlation-as-covariance
#' standard-error caveat.
#'
#' @param x A `circumplex_axes_reliability` object.
#' @param digits The number of decimal places to display (default = 3).
#' @param ... Not used.
#' @return `x`, invisibly.
#' @method print circumplex_axes_reliability
#' @export
print.circumplex_axes_reliability <- function(x, digits = 3, ...) {
  d <- x$details
  from_cormat <- identical(d$input, "cormat")
  # identical() on the read-back field, so an object built before M65 (no
  # `missing` field at all) reads as FALSE rather than erroring.
  is_fiml <- identical(d$missing, "fiml")
  cat(
    "\nCircumplex Axes Reliability (Strack, Jacobs & Grosse Holtforth, 2013)",
    "\nInput:        ", if (from_cormat) "correlation matrix" else "item data",
    "\nItems:        ", d$n_items, " (", d$n_scales, " scales)",
    # Three different N's, three different labels, because they are three
    # different quantities and a shared label would hide that. "Complete N"
    # names the listwise complete-case count; "Sample N" the size a supplied
    # correlation matrix was computed from; "Total N" every respondent in the
    # data, with the complete-case count beside it so the reader can see how
    # much of it listwise would have discarded (M65).
    #
    # "Total N" prints d$n_total, NOT d$n. The two differ whenever a row with no
    # observed item at all was dropped, and printing N_used under a label
    # reading "Total" named a number that was not the total -- caught at review
    # against AC8, which requires the total N. Where they differ the used count
    # is shown beside it rather than replaced by it, because N_used is what the
    # fit consumed and the reader needs both; where they agree it is left out,
    # so the common case gains no noise.
    if (from_cormat) {
      "\nSample N:     "
    } else if (is_fiml) {
      "\nTotal N:      "
    } else {
      "\nComplete N:   "
    },
    if (is_fiml) d$n_total else d$n,
    if (is_fiml) {
      paste0(
        " (",
        if (!identical(d$n_total, d$n)) paste0(d$n, " used, "),
        d$n_complete, " complete)"
      )
    },
    "\nSEm scale:    ", if (is.character(d$sd)) d$sd else "custom",
    "\n\n# Per-axis reliability\n\n",
    sep = ""
  )
  disp <- data.frame(
    Axis = x$results$Axis,
    item_n = x$results$item_n,
    Reliability = axes_fmt(x$results$reliability, digits),
    SEm = axes_fmt(x$results$sem, digits),
    NB_Reliability = axes_fmt(x$results$nb_reliability, digits),
    stringsAsFactors = FALSE
  )
  print(disp, row.names = FALSE, right = FALSE)
  if (isTRUE(d$boundary)) {
    cat("\n")
    # Names the CLASS of solution, not one disjunct: the flag is raised by an
    # axes variance outside (0, 1) OR any negative variance component, and
    # naming only the first told a user the wrong cause for the others
    # (M62 review, F2). The components table below already shows which.
    cat_prose(
      paste0(
        "Note: a boundary solution was reached (an axes variance outside",
        " (0, 1), or a negative variance component); reliability and SEm are",
        " NA rather than a clipped value. See the components table above."
      ),
      prefix = "  "
    )
  } else if (isTRUE(all.equal(
    x$results$reliability[[1]], x$results$reliability[[2]]
  ))) {
    # The two axes share one (equal-constrained) axes-variance estimate, so a
    # balanced instrument's rows match by construction (RR09) -- flag it so a
    # user does not read the identical rows as a bug.
    cat("\n")
    cat_prose(
      paste0(
        "Note: the two axes share one axes-variance estimate and, with equal",
        " items per axis, carry the same reliability. This is expected, not",
        " an error."
      ),
      prefix = "  "
    )
  }
  # RR09 sec. 7.4 and M61-D1: NA-with-reason, never silently dropped. `d$nb_reason`
  # is NULL when the comparison is available. The `from_cormat` fallback keeps the
  # note on objects built before M61 added the field.
  # `nb_reason` carries EVERY reason that applies, so both notes print when a
  # correlation matrix also has single-item scales -- membership, not identity.
  nb_reason <- d$nb_reason
  if (is.null(nb_reason) && from_cormat) nb_reason <- "cormat"
  if ("cormat" %in% nb_reason) {
    cat("\n")
    cat_prose(
      paste0(
        "Note: the Nunnally-Bernstein comparison needs the raw item scores",
        " (scale alphas and the axis-composite variance), so it is NA on the",
        " correlation-matrix path."
      ),
      prefix = "  "
    )
  }
  if ("fiml" %in% nb_reason) {
    cat("\n")
    cat_prose(
      paste0(
        "Note: the Nunnally-Bernstein comparison needs each scale's alpha and",
        " the axis-composite variance, both of which need items observed by",
        " every respondent, so it is NA under missing = \"fiml\" rather than",
        " computed from whatever cells happened to be answered."
      ),
      prefix = "  "
    )
  }
  if ("single_item" %in% nb_reason) {
    cat("\n")
    cat_prose(
      paste0(
        "Note: the Nunnally-Bernstein comparison needs each scale's alpha,",
        " which is undefined for a scale carrying only one item, so it is NA",
        " here. Strack et al. (2013) likewise leave it blank for such",
        " instruments."
      ),
      prefix = "  "
    )
  }
  # The correction-failure state gets its own note, like every other
  # NA-with-reason state above it (boundary, cormat, fiml, single_item) --
  # RR09 sec. 7.4's NA-with-reason doctrine, which the new state was otherwise
  # the only one to skip. Without it the SE column reads as all-NA with the
  # explanation reachable only from `details`, and the call-time warning() is
  # routinely muffled (this package's own harnesses wrap the call in
  # suppressWarnings()).
  #
  # It also SUPPRESSES the corrected-SE half of the caveat rather than printing
  # it beside the gap: that text asserts the standard errors "are corrected ...
  # and are calibrated", which is a claim about numbers that are not there. The
  # surviving half is still printed so nothing true is lost with it (M66 review,
  # F3). The two failures usually arrive together (both routes solve the same
  # sigma-hat) but need not, and neither note may speak for the other -- which
  # is why the opening below is a function of both flags even though only the SE
  # half is printed here; the fit half and its own failure note are emitted by
  # summary(), beside the numbers they qualify (axes_cat_fit_note()).
  se_live <- is.null(x$details$se_correction_failed)
  fit_live <- is.null(x$details$fit_scaling_failed)
  if (!se_live) {
    cat("\n")
    cat_prose(
      paste0(
        "Note: the component standard errors could not be computed (",
        x$details$se_correction_failed,
        ") and are NA. The point estimates, reliability, and SEm ",
        "are unaffected."
      ),
      prefix = "  "
    )
  }
  if (se_live || fit_live) {
    cat("\n")
    cat_prose(axes_metric_note(se_live, fit_live), prefix = "  ")
    if (se_live) cat_prose(axes_se_corrected_note, prefix = "  ")
    if (se_live && is_fiml) {
      cat("\n")
      cat_prose(axes_fiml_se_caveat, prefix = "  ")
    }
  }
  invisible(x)
}


# The scaled-fit note and its failure counterpart, printed by summary() under
# the fit line rather than by print() -- see the axes_fit_scaled_note comment
# above. Factored out so the two callers cannot drift apart.
axes_cat_fit_note <- function(x) {
  fit_failed <- x$details$fit_scaling_failed
  if (is.null(fit_failed)) {
    cat("\n")
    cat_prose(axes_fit_scaled_note, prefix = "  ")
  } else {
    cat("\n")
    cat_prose(
      paste0(
        "Note: the global fit statistics could not be scaled to the ",
        "correlation metric (", fit_failed, "), so chisq, pvalue, rmsea and ",
        "cfi are NA. What lavaan reported unscaled is in ",
        "details$fit_uncorrected; df and srmr are unaffected."
      ),
      prefix = "  "
    )
  }
  invisible(NULL)
}

#' Summarize circumplex axes-reliability results
#'
#' Fuller display of an [axes_reliability()] object: everything [print()] shows
#' plus the estimated variance components (with standard errors) and the global
#' fit indices.
#'
#' @param object A `circumplex_axes_reliability` object.
#' @param digits The number of decimal places to display (default = 3).
#' @param ... Not used.
#' @return `object`, invisibly.
#' @method summary circumplex_axes_reliability
#' @export
summary.circumplex_axes_reliability <- function(object, digits = 3, ...) {
  x <- object
  print(x, digits = digits)
  cat("\n# Variance components\n\n")
  comp <- data.frame(
    Component = x$components$Component,
    Estimate = axes_fmt(x$components$Estimate, digits),
    SE = axes_fmt(x$components$SE, digits),
    stringsAsFactors = FALSE
  )
  print(comp, row.names = FALSE, right = FALSE)

  cat("\n# Global fit\n\n")
  cat(
    "  chi-square(", axes_fmt(x$fit$df, 0), ") = ", axes_fmt(x$fit$chisq, 2),
    ",  RMSEA = ", axes_fmt(x$fit$rmsea, 3),
    ",  CFI = ", axes_fmt(x$fit$cfi, 3),
    "\n", sep = ""
  )
  # BC5's third surface, beside the line it qualifies rather than two blocks
  # above it.
  axes_cat_fit_note(x)
  invisible(x)
}

Try the circumplex package in your browser

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

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