R/notes.R

Defines functions model_cluster_sizes model_n_sentence n_sentence nbsp_comparisons link_name add_note_footer note_text parameters_note_sentences merge_parameters_note_records parameters_note_record describe_vcov cfa_intro_sentences factor_cor_note_sentences loadings_note_sentences fa_method_sentence anova_marked anova_heading_type or_list anova_note_sentences abbreviation_sentence combine_abbreviations column_abbreviations anova_note_record apa_note

Documented in apa_note

# Writing table notes ----

#' Write a table note
#'
#' Makes the text of an APA-style table note from what a table function
#' recorded about how the table was made. The text is a starting point:
#' edit it or add to it as needed.
#'
#' For [apa_anova()] tables, the note can say
#'
#' * which type of sums of squares was used,
#' * which effects have sphericity-corrected degrees of freedom (for `afex`
#'   models, only effects involving a within-subjects factor with more than
#'   two levels are corrected),
#' * what the effect size (and *MSE*, for `afex` models) stands for, and
#' * what stars or bold values mean.
#'
#' For [apa_loadings()] tables, the note can say
#'
#' * how the factors were extracted and rotated (e.g., "Principal axis
#'   factoring with oblimin rotation"), and that loadings after an oblique
#'   rotation are pattern coefficients,
#' * which loadings are omitted (`min_loading`) or bolded (`bold_max`),
#' * what the *h*^2^, *u*^2^, and Complexity columns stand for, and
#' * that the cumulative proportion of variance is approximate when the
#'   factors are correlated.
#'
#' For confirmatory factor analyses fit with `lavaan`, the note instead gives
#' the sample size and estimator, says that the loadings are standardized, and
#' says that blank cells are loadings fixed at 0.
#'
#' For [apa_descriptives()] tables, the note says which statistics are
#' shown, lists missing values, and names the tests of group differences.
#'
#' For [apa_means()], [apa_contrasts()], and [apa_slopes()] tables, the note
#' says what the values are, how the other predictors were held constant, on
#' which scale the estimates are, which level was subtracted from which, and
#' how *p*-values were adjusted for multiple comparisons.
#'
#' For [apa_collinearity()] tables, the note names terms with generalized
#' *VIF*s, explains the *VIF*s of interaction terms, defines tolerance, and
#' says which *VIF*s are bolded.
#'
#' For [apa_reliability()] tables, the note gives the number of respondents,
#' says which items were reverse-scored and how scale scores were computed,
#' and defines the statistics.
#'
#' For [apa_factor_cor()] tables, the note says how the factors were
#' extracted and rotated (for `lavaan` models, the sample size, the
#' estimator, and that the correlations are standardized).
#'
#' For [apa_htest()] tables, the note can say
#'
#' * which tests were done (e.g., Welch's *t* tests) and what the groups were
#'   compared with or the variables were correlated with,
#' * that *p*-values are one-sided, if they are,
#' * how Cohen's *d* (or Hedges' *g*) was standardized,
#' * what the effect size and its confidence interval stand for, and
#' * what stars or bold values mean.
#'
#' For [apa_parameters()] tables of regression models (`lm`, `glm`, `polr`,
#' `clm`, `multinom`, `zeroinfl`, `hurdle`, `lme4`, `glmmTMB`, and `nlme` models, or a list of models), the note can say
#'
#' * which robust standard errors were used (e.g., `vcov = "HC3"`),
#' * how the degrees of freedom of mixed-model *t* tests were approximated
#'   (Kenward-Roger or Satterthwaite),
#' * that ordinal-model thresholds are not tested,
#' * the scale of the zero-inflation and dispersion coefficients of `glmmTMB`
#'   models,
#' * the residual correlation structure and variance function of `nlme::lme`
#'   models,
#' * the reference category of multinomial models (`nnet::multinom`),
#' * the count distribution and the meaning of the zero model of zero-inflated and hurdle models (`pscl`),
#' * the effect-size measure and model of meta-analyses (`metafor::rma`) and, for tables of studies, the test of the pooled estimate, its prediction interval, and the test of heterogeneity,
#' * what the standardized coefficient (\eqn{\beta}{beta}), exponentiated coefficient
#'   (e.g., *OR*), confidence interval, and model-comparison (e.g.,
#'   \eqn{\Delta R^2}{Delta R^2}) columns stand for, and
#' * what stars or bold values mean.
#'
#' Abbreviations are defined from the `abbreviation` slot of each column's
#' [column_format()], so a note defines every abbreviated header that has a
#' definition. To define another header, or to change a definition, set the
#' slot in the `column_formats` passed to the table function (or set the
#' default with [apa7_defaults()]):
#'
#' ```r
#' cf <- column_formats()
#' cf$Sum_Squares@abbreviation <- "sum of squares"
#' apa_anova(fit, anova_parameters = c("Parameter", "Sum_Squares", "df", "F", "p"),
#'           column_formats = cf)
#' ```
#'
#' The sentences are in APA order: general notes first, then probability notes.
#' The text does not start with "*Note*." because apaquarto adds it. With
#' [apaquarto](https://wjschne.github.io/apaquarto/), make the table in one
#' chunk and display it in another, using the note as the `apa-note` chunk
#' option. Write `apa7::apa_note()` there, because in documents with cached
#' chunks, chunk options can be evaluated before `library(apa7)` takes effect:
#'
#' ````
#' ```{r}
#' #| label: anova-fit
#' tbl <- apa_anova(fit, type = 3)
#' ```
#'
#' ```{r}
#' #| label: tbl-anova
#' #| tbl-cap: Analysis of Variance
#' #| apa-note: !expr apa7::apa_note(tbl)
#' tbl
#' ```
#' ````
#'
#' The information is stored in an `"apa_note"` attribute of the table. dplyr
#' functions such as `mutate()` and `filter()` keep it, but some operations
#' (e.g., `bind_rows()` and joins) drop it, so call `apa_note()` on the
#' table that the table function returned.
#'
#' @param x a table made by an apa7 table function: [apa_anova()],
#'   [apa_parameters()], [apa_performance()] (for `lavaan` and `metafor`
#'   models), [apa_htest()], [apa_chisq()], [apa_cor()], [apa_descriptives()], [apa_means()],
#'   [apa_contrasts()], [apa_slopes()], [apa_loadings()], [apa_factor_cor()],
#'   [apa_n_factors()], [apa_reliability()], [apa_collinearity()],
#'   [apa_publication_bias()], or [apa_subgroups()]. Call `apa_note()` before
#'   changing the table, because some operations (e.g., [dplyr::bind_rows()])
#'   drop the information it needs.
#' @param collapse string that joins the sentences. Use `NULL` to get a character vector with one sentence per element.
#' @param citations how articles cited in the note are written: `"auto"`,
#'   `"keys"` (Pandoc citation keys, e.g., `[@duvalTrimFillSimple2000]`), or
#'   `"text"` (e.g., "(Duval & Tweedie, 2000)"). If `NULL` (default), the
#'   `apa7.citations` option is used, which is `"auto"` unless set. See
#'   [apa7_write_bib()].
#'
#' @returns character. Comparison signs (e.g., "=" in "*N* = 32") have
#'   non-breaking spaces (U+00A0) on both sides, so that a statistic is
#'   never split across lines.
#' @export
#' @examples
#' d <- mtcars
#' d$cyl <- factor(d$cyl)
#' d$am <- factor(d$am)
#' fit <- aov(mpg ~ cyl * am, data = d)
#' tbl <- apa_anova(fit, starred = "F")
#' apa_note(tbl)
#'
#' # Descriptive statistics with tests of group differences
#' d$Transmission <- factor(d$am, labels = c("Automatic", "Manual"))
#' apa_descriptives(d, variables = c(mpg, hp), by = Transmission, test = "auto") |>
#'   apa_note()
#'
#' # Factor analysis
#' fit_fa <- psych::fa(Harman74.cor$cov, 4, fm = "pa", rotate = "varimax")
#' apa_loadings(fit_fa, min_loading = .3, communality = TRUE) |>
#'   apa_note()
#'
#' # Regression
#' lm(mpg ~ cyl + wt, data = mtcars) |>
#'   apa_parameters(starred = "p") |>
#'   apa_note()
apa_note <- function(x, collapse = " ", citations = NULL) {
  citations <- citations_mode(citations)
  record <- attr(x, "apa_note", exact = TRUE)
  if (is.null(record)) {
    cli::cli_abort(c(
      "{.arg x} has no table note information.",
      i = "{.fn apa_note} works with tables made by {.fn apa_anova}, {.fn apa_htest}, {.fn apa_parameters}, {.fn apa_loadings}, {.fn apa_factor_cor}, {.fn apa_n_factors}, {.fn apa_reliability}, {.fn apa_collinearity}, {.fn apa_publication_bias}, {.fn apa_subgroups}, {.fn apa_descriptives}, {.fn apa_chisq}, {.fn apa_cor}, {.fn apa_means}, {.fn apa_contrasts}, {.fn apa_slopes}, and {.fn apa_performance} (for lavaan and metafor models).",
      i = "The information is stored in an attribute that some operations (e.g., {.fn dplyr::bind_rows} and joins) drop. Call {.fn apa_note} on the table before changing it."
    ))
  }

  sentences <- switch(
    record$table,
    anova = anova_note_sentences(record),
    loadings = loadings_note_sentences(record),
    factor_cor = factor_cor_note_sentences(record),
    reliability = reliability_note_sentences(record),
    collinearity = collinearity_note_sentences(record),
    n_factors = n_factors_note_sentences(record),
    meta = meta_note_sentences(record),
    meta_fit = meta_fit_note_sentences(record),
    publication_bias = publication_bias_note_sentences(record),
    meta_subgroups = meta_subgroups_note_sentences(record),
    descriptives = descriptives_note_sentences(record),
    chisq = chisq_note_sentences(record),
    cor = cor_note_sentences(record),
    modelbased = modelbased_note_sentences(record),
    parameters = parameters_note_sentences(record),
    htest = htest_note_sentences(record),
    fit = fit_note_sentences(record),
    sem = sem_note_sentences(record),
    cli::cli_abort("Unknown table type {.val {record$table}}.") # nocov
  )

  sentences <- resolve_citations(sentences, citations) |>
    nbsp_comparisons()

  if (is.null(collapse)) {
    return(sentences)
  }
  paste(sentences, collapse = collapse)
}

# ANOVA table note records ----

#' Record what an ANOVA table's note needs
#'
#' @param ss_type 1, 2, 3, or NA
#' @param es_column effect size column shown in the table (or NULL)
#' @param column_formats column_formats used for the table
#' @param starred,bolded whether stars or bolding were applied
#' @param correction sphericity correction for afex models ("GG", "HF", "none")
#' @param corrected_factors formatted names of within-subjects factors with
#'   more than two levels
#' @param abbreviations definitions of the abbreviated headers shown (from
#'   column_abbreviations())
#' @keywords internal
#' @noRd
anova_note_record <- function(
  ss_type = NA,
  abbreviations = character(0),
  starred = FALSE,
  bolded = FALSE,
  correction = NULL,
  corrected_factors = character(0),
  mixed = FALSE,
  df_method = NA,
  effect_size_header = NULL,
  model_sentences = NULL
) {
  list(
    table = "anova",
    model_sentences = model_sentences,
    ss_type = ss_type,
    correction = correction,
    corrected_factors = corrected_factors,
    mixed = mixed,
    df_method = df_method,
    effect_size_header = effect_size_header,
    abbreviations = abbreviations,
    starred = starred,
    bolded = bolded
  )
}

# Abbreviations ----

#' Definitions of the abbreviated headers of columns
#'
#' @param columns column format names of the columns shown, in table order
#' @param column_formats column_formats (or a named list of column_format
#'   objects) used for the table
#' @returns character vector of definitions named by the term defined (the
#'   header, unless the abbreviation is named), without duplicate terms
#' @keywords internal
#' @noRd
column_abbreviations <- function(columns, column_formats) {
  out <- character(0)
  for (name in columns) {
    cf <- column_formats[[name]]
    if (is.null(cf)) {
      next
    }
    abbreviation <- cf@abbreviation
    if (length(abbreviation) == 0 || is.na(abbreviation) || !nzchar(abbreviation)) {
      next
    }
    term <- names(abbreviation)
    if (is.null(term) || is.na(term) || !nzchar(term)) {
      term <- cf@header
    }
    if (!(term %in% names(out))) {
      out[[term]] <- unname(abbreviation)
    }
  }
  out
}

#' Combine definitions, keeping the first definition of each term
#' @keywords internal
#' @noRd
combine_abbreviations <- function(...) {
  out <- c(...)
  if (length(out) == 0) {
    return(character(0))
  }
  out[!duplicated(names(out))]
}

#' "*MSE* = mean square error; CI = confidence interval."
#' @keywords internal
#' @noRd
abbreviation_sentence <- function(abbreviations) {
  if (length(abbreviations) == 0) {
    return(NULL)
  }
  paste0(
    paste(names(abbreviations), abbreviations, sep = " = ", collapse = "; "),
    "."
  )
}

# ANOVA table note sentences ----

#' @keywords internal
#' @noRd
anova_note_sentences <- function(record) {
  # Sentences about the model (e.g., multivariate tests)
  sentences <- as.character(record$model_sentences)

  # Sums of squares (mixed models: type of tests)
  if (isTRUE(record$mixed) && !is.null(record$ss_type) && !is.na(record$ss_type)) {
    sentences <- c(
      sentences,
      paste0(
        "*F* tests of fixed effects are Type ",
        c("I", "II", "III")[record$ss_type],
        "."
      )
    )
  } else if (!is.null(record$ss_type) && !is.na(record$ss_type)) {
    sentences <- c(
      sentences,
      switch(
        as.character(record$ss_type),
        `1` = "Sums of squares are sequential (Type I).",
        `2` = "Type II sums of squares.",
        `3` = "Type III sums of squares."
      )
    )
  }

  # Denominator degrees of freedom of mixed models
  if (isTRUE(record$mixed) && isTRUE(record$df_method %in% c("kenward", "satterthwaite"))) {
    sentences <- c(
      sentences,
      paste0(
        "Denominator degrees of freedom are ",
        switch(
          record$df_method,
          kenward = paste0("Kenward", intToUtf8(8211), "Roger"),
          satterthwaite = "Satterthwaite"
        ),
        " approximations ",
        df_method_citation(record$df_method),
        "."
      )
    )
  }
  if (isTRUE(record$mixed) && !is.null(record$effect_size_header)) {
    sentences <- c(
      sentences,
      paste0(
        record$effect_size_header,
        " is computed from *F* and its degrees of freedom, which only approximates it in mixed models."
      )
    )
  }

  # Sphericity
  if (length(record$corrected_factors) > 0 && !is.null(record$correction)) {
    correction_name <- switch(
      record$correction,
      GG = paste0("Greenhouse", intToUtf8(8211), "Geisser"),
      HF = paste0("Huynh", intToUtf8(8211), "Feldt"),
      NA_character_
    )
    if (is.na(correction_name)) {
      sentences <- c(
        sentences,
        "Degrees of freedom are not corrected for violations of sphericity."
      )
    } else {
      sentences <- c(
        sentences,
        paste0(
          "Degrees of freedom and *p*-values for effects involving ",
          or_list(record$corrected_factors),
          " are ",
          correction_name,
          " corrected ",
          cite(switch(
            record$correction,
            GG = "greenhouseMethodsAnalysisProfile1959",
            HF = "huynhEstimationBoxCorrection1976"
          )),
          "."
        )
      )
    }
  }

  # Abbreviations
  sentences <- c(sentences, abbreviation_sentence(record$abbreviations))

  # Probability notes
  if (isTRUE(record$starred)) {
    sentences <- c(sentences, paste0(apa_p_star_note(), "."))
  }
  if (isTRUE(record$bolded)) {
    sentences <- c(
      sentences,
      paste0(
        "Bold values are statistically significant at *p*",
        intToUtf8(c(160, 8804, 160)),
        ".05."
      )
    )
  }

  sentences
}

#' "A", "A or B", "A, B, or C"
#' @keywords internal
#' @noRd
or_list <- function(x) {
  n <- length(x)
  if (n <= 2) {
    return(paste(x, collapse = " or "))
  }
  paste0(paste(x[-n], collapse = ", "), ", or ", x[n])
}

#' Type of sums of squares from the heading of an ANOVA table
#' @keywords internal
#' @noRd
anova_heading_type <- function(tab) {
  heading <- paste(attr(tab, "heading", exact = TRUE), collapse = " ")
  if (grepl("Type III", heading, fixed = TRUE)) {
    return(3L)
  }
  if (grepl("Type II", heading, fixed = TRUE)) {
    return(2L)
  }
  if (grepl("Analysis of Variance Table", heading, fixed = TRUE)) {
    return(1L)
  }
  NA_integer_
}

#' Whether stars or bolding were applied to columns in the table
#' @keywords internal
#' @noRd
anova_marked <- function(marked, anova_parameters) {
  marked <- marked[!is.na(marked) & marked != ""]
  length(intersect(marked, anova_parameters)) > 0
}

# Factor analysis notes ----

#' "Principal axis factoring with oblimin rotation."
#' @keywords internal
#' @noRd
fa_method_sentence <- function(record) {
  method <- if (is.null(record$method)) NULL else fa_method_labels[[record$method]]
  if (is.null(method)) {
    # Methods without a known name are given as psych calls them
    method <- paste0("Factor analysis (", record$method, " extraction)")
  }
  rotation <- if (is.null(record$rotation)) "none" else record$rotation
  # A single factor cannot be rotated, so rotation is not worth mentioning
  if (identical(record$nfactors, 1L)) {
    return(paste0(method, "."))
  }
  if (identical(rotation, "none")) {
    return(paste0(method, " without rotation."))
  }
  rotation_label <- fa_rotation_labels[[rotation]]
  if (is.null(rotation_label)) {
    rotation_label <- rotation
  }
  paste0(method, " with ", rotation_label, " rotation.")
}

#' @keywords internal
#' @noRd
fa_method_labels <- list(
  pca = "Principal components analysis",
  pa = "Principal axis factoring",
  minres = "Minimum residual factor analysis",
  uls = "Unweighted least squares factor analysis",
  ols = "Ordinary least squares factor analysis",
  wls = "Weighted least squares factor analysis",
  gls = "Generalized least squares factor analysis",
  ml = "Maximum likelihood factor analysis",
  minchi = "Minimum chi-square factor analysis",
  minrank = "Minimum rank factor analysis",
  alpha = "Alpha factor analysis"
)

#' @keywords internal
#' @noRd
fa_rotation_labels <- list(
  geominT = "geomin (orthogonal)",
  geominQ = "geomin (oblique)",
  bentlerT = "Bentler (orthogonal)",
  bentlerQ = "Bentler (oblique)",
  targetT = "target (orthogonal)",
  targetQ = "target (oblique)",
  TargetT = "target (orthogonal)",
  TargetQ = "target (oblique)",
  Promax = "promax",
  cluster = "cluster",
  biquartimin = "biquartimin"
)

#' @keywords internal
#' @noRd
loadings_note_sentences <- function(record) {
  is_cfa <- identical(record$method, "lavaan")
  is_pca <- identical(record$method, "pca")
  if (is_cfa) {
    sentences <- c(
      cfa_intro_sentences(record),
      "Loadings are fully standardized estimates from a confirmatory factor analysis."
    )
    if (isTRUE(record$fixed_zero)) {
      sentences <- c(sentences, "Blank cells are loadings fixed at 0.")
    }
  } else {
    sentences <- fa_method_sentence(record)
  }

  if (isTRUE(record$oblique) && !is_cfa) {
    sentences <- c(sentences, "Loadings are pattern coefficients.")
  }

  if (!is.null(record$min_loading) && !is.na(record$min_loading)) {
    cutoff <- sub("^0", "", format(record$min_loading, nsmall = 2))
    sentences <- c(
      sentences,
      paste0("Loadings smaller than ", cutoff, " in absolute value are omitted.")
    )
  }

  if (isTRUE(record$bold_max)) {
    sentences <- c(
      sentences,
      "The largest loading for each variable is in bold."
    )
  }

  sentences <- c(sentences, abbreviation_sentence(record$abbreviations))

  if (isTRUE(record$variance) && isTRUE(record$oblique)) {
    sentences <- c(
      sentences,
      paste0(
        "Because the ",
        if (is_pca) "components" else "factors",
        " are correlated, the variance they explain overlaps, and the cumulative proportion is approximate."
      )
    )
  }

  sentences
}

#' @keywords internal
#' @noRd
factor_cor_note_sentences <- function(record) {
  if (!identical(record$method, "lavaan")) {
    return(fa_method_sentence(record))
  }
  c(
    cfa_intro_sentences(record),
    "Correlations among the latent variables are from the fully standardized solution.",
    if (isTRUE(record$structural)) {
      "Correlations between latent variables linked by regression paths are implied by the model."
    }
  )
}

#' Sample size, estimator, and missing-data sentences for lavaan models
#' @keywords internal
#' @noRd
cfa_intro_sentences <- function(record) {
  sentences <- character(0)
  if (length(record$n) == 1 && !is.na(record$n)) {
    sentences <- n_sentence(record$n)
  }
  if (length(record$estimator) > 0) {
    sentences <- c(sentences, paste0("Estimation used ", record$estimator, "."))
  }
  if (any(record$missing %in% c("ml", "ml.x", "fiml", "direct"))) {
    sentences <- c(
      sentences,
      "Missing data were handled with full information maximum likelihood."
    )
  }
  sentences
}

# Regression table notes ----

#' Sentence about the covariance matrix behind the standard errors
#' @param vcov the `vcov` argument passed to parameters::model_parameters()
#' @keywords internal
#' @noRd
describe_vcov <- function(vcov) {
  if (is.null(vcov)) {
    return(NULL)
  }
  if (!is.character(vcov)) {
    return("Standard errors are from a user-supplied covariance matrix.")
  }
  v <- vcov[1]
  type <- if (grepl("^HC[0-9]", v)) {
    paste0("heteroskedasticity-consistent (", v, ")")
  } else if (v %in% c("vcovHC", "HC")) {
    "heteroskedasticity-consistent"
  } else if (grepl("^CR[0-9]", v)) {
    paste0("cluster-robust (", v, ")")
  } else if (v %in% c("vcovCL", "vcovCR", "CR")) {
    "cluster-robust"
  } else if (v %in% c("vcovHAC", "HAC", "NeweyWest", "vcovPL", "kernHAC")) {
    "heteroskedasticity- and autocorrelation-consistent"
  } else if (v %in% c("vcovBS", "BS", "bootstrap")) {
    "bootstrapped"
  } else {
    paste0("robust (", v, ")")
  }
  paste0("Standard errors are ", type, ".")
}

#' Record what a regression table's note needs
#' @param note list from an apa_parameters method (vcov, df_method,
#'   thresholds)
#' @param headers headers of the columns shown, named by column format name
#' @param abbreviations definitions of the abbreviated headers shown
#' @param starred,bolded whether stars or bolding were applied
#' @keywords internal
#' @noRd
parameters_note_record <- function(
  note,
  headers,
  abbreviations = character(0),
  starred = FALSE,
  bolded = FALSE
) {
  list(
    table = "parameters",
    headers = headers,
    # Symbols of random effects (e.g., *SD* and *r*) are defined, too
    abbreviations = combine_abbreviations(abbreviations, note$random_abbreviations),
    vcov = note$vcov,
    df_method = note$df_method,
    thresholds = note$thresholds,
    components = note$components,
    residual = note$residual,
    multinomial = note$multinomial,
    count_zero = note$count_zero,
    meta = note$meta,
    gam = note$gam,
    n = note$n,
    # Sentences written by the method for its kind of model
    model_sentences = note$model_sentences,
    robust_low = note$robust_low,
    starred = isTRUE(starred),
    bolded = isTRUE(bolded),
    performance = NULL,
    performance_starred = FALSE
  )
}

#' Combine the note records of the models in a list of models
#' @keywords internal
#' @noRd
merge_parameters_note_records <- function(
  records,
  performance = NULL,
  performance_starred = FALSE,
  column_formats = NULL
) {
  if (is.null(column_formats)) {
    column_formats <- the$column_formats
  }
  records <- purrr::compact(records)
  headers <- unlist(unname(purrr::map(records, "headers")))
  headers <- headers[!duplicated(names(headers))]

  pick <- function(field) {
    unique(unlist(purrr::map(records, field)))
  }

  performance_headers <- NULL
  performance_abbreviations <- character(0)
  if (length(performance) > 0) {
    performance <- performance[!is.na(performance)]
    performance_headers <- purrr::map_chr(
      performance,
      \(m) if (is.null(column_formats[[m]])) m else column_formats[[m]]@header
    )
    names(performance_headers) <- performance
    performance_abbreviations <- column_abbreviations(performance, column_formats)
    # In mixed models, R2 is the marginal R2, so it needs a definition
    if (
      all(c("R2", "R2_conditional") %in% performance) &&
        !(performance_headers[["R2"]] %in% names(performance_abbreviations))
    ) {
      performance_abbreviations <- combine_abbreviations(
        stats::setNames(
          paste0("variance explained by fixed effects ", cite("nakagawaGeneralSimpleMethod2013")),
          performance_headers[["R2"]]
        ),
        performance_abbreviations
      )
    }
  }

  list(
    table = "parameters",
    headers = headers,
    abbreviations = combine_abbreviations(
      unlist(unname(purrr::map(records, "abbreviations"))),
      performance_abbreviations
    ),
    vcov = pick("vcov"),
    df_method = pick("df_method"),
    thresholds = pick("thresholds"),
    components = pick("components"),
    residual = pick("residual"),
    multinomial = purrr::compact(purrr::map(records, "multinomial"))[1][[1]],
    count_zero = merge_count_zero_records(purrr::compact(purrr::map(records, "count_zero"))),
    meta = purrr::compact(purrr::map(records, "meta"))[1][[1]],
    gam = purrr::compact(purrr::map(records, "gam"))[1][[1]],
    # N only when the models share it
    n = if (length(pick("n")) == 1) pick("n"),
    model_sentences = pick("model_sentences"),
    robust_low = unlist(purrr::map(records, "robust_low")),
    starred = any(purrr::map_lgl(records, "starred")),
    bolded = any(purrr::map_lgl(records, "bolded")),
    performance = performance_headers,
    performance_starred = isTRUE(performance_starred)
  )
}

#' @keywords internal
#' @noRd
parameters_note_sentences <- function(record) {
  sentences <- character(0)
  headers <- record$headers

  # Standard errors
  sentences <- c(sentences, record$vcov)

  # Degrees of freedom of mixed-model t tests
  if ("t" %in% names(headers) && length(record$df_method) > 0) {
    df_names <- c(
      kenward = paste0("Kenward", intToUtf8(8211), "Roger"),
      satterthwaite = "Satterthwaite"
    )
    known <- intersect(record$df_method, names(df_names))
    if ("nlme" %in% record$df_method) {
      sentences <- c(
        sentences,
        "Degrees of freedom are computed by nlme from the grouping levels at which each effect varies."
      )
    }
    if (length(known) > 0) {
      sentences <- c(
        sentences,
        paste0(
          "Degrees of freedom are ",
          paste(df_names[known], collapse = " and "),
          " approximations ",
          paste(vapply(known, df_method_citation, character(1)), collapse = " "),
          "."
        )
      )
    }
  }

  # Ordinal thresholds
  if (length(record$thresholds) > 0) {
    sentences <- c(
      sentences,
      paste0(
        "Thresholds (cutpoints) are on the ",
        link_name(record$thresholds[1]),
        " scale and are not tested."
      )
    )
  }

  # Residual correlation structures and variance functions of nlme models
  sentences <- c(sentences, record$residual)

  # Reference category of multinomial models
  sentences <- c(sentences, multinomial_note_sentence(record$multinomial))

  # Count and zero models of zero-inflated and hurdle models (pscl)
  sentences <- c(sentences, count_zero_note_sentences(record$count_zero))

  # Smoothing parameters and smooth-term tests of GAMs (mgcv)
  sentences <- c(sentences, gam_note_sentences(record$gam))

  # Sample size (NOTE-6), after the sentences that describe the model
  sentences <- c(sentences, record$n)

  # Sentences written by the method (e.g., Cox and quantile regression)
  sentences <- c(sentences, record$model_sentences)
  sentences <- c(sentences, robust_low_sentence(record$robust_low))

  # Measure and model of meta-regressions (metafor)
  sentences <- c(sentences, meta_note_sentences(record$meta))

  # Zero-inflation and dispersion parts of glmmTMB models
  if ("zero_inflated" %in% record$components) {
    sentences <- c(
      sentences,
      "Zero-inflation coefficients are log odds of an excess zero (a zero not produced by the conditional model)."
    )
  }
  if ("dispersion" %in% record$components) {
    sentences <- c(
      sentences,
      "Dispersion coefficients are on the log scale."
    )
  }

  # Abbreviations, in column order
  sentences <- c(sentences, abbreviation_sentence(record$abbreviations))

  # Probability notes
  c(
    sentences,
    probability_note_sentences(
      starred = isTRUE(record$starred) || isTRUE(record$performance_starred),
      bolded = record$bolded
    )
  )
}

# Adding notes to flextables ----

#' Note text for apa_flextable(note = ...)
#' @param data the data passed to apa_flextable()
#' @param note `NULL`, `FALSE`, `TRUE`, or a character string
#' @returns note text, or NULL for no note
#' @keywords internal
#' @noRd
note_text <- function(data, note) {
  if (is.null(note) || isFALSE(note)) {
    return(NULL)
  }
  if (isTRUE(note)) {
    if (is.null(attr(data, "apa_note", exact = TRUE))) {
      cli::cli_abort(c(
        "{.code note = TRUE} needs a table made by {.fn apa_anova}, {.fn apa_htest}, {.fn apa_parameters}, {.fn apa_loadings}, {.fn apa_factor_cor}, {.fn apa_reliability}, {.fn apa_collinearity}, {.fn apa_publication_bias}, {.fn apa_subgroups}, {.fn apa_descriptives}, {.fn apa_chisq}, {.fn apa_cor}, {.fn apa_means}, {.fn apa_contrasts}, or {.fn apa_slopes}.",
        i = "The information for the note is stored in an attribute that some operations (e.g., {.fn dplyr::bind_rows} and joins) drop.",
        i = "To write your own note, pass it as text (e.g., {.code note = \"Data are from ...\"})."
      ))
    }
    text <- apa_note(data)
  } else if (is.character(note)) {
    text <- paste(note[!is.na(note)], collapse = " ")
  } else {
    cli::cli_abort(
      "{.arg note} must be {.code TRUE}, {.code FALSE}, {.code NULL}, or a character string, not {.obj_type_friendly {note}}."
    )
  }
  if (length(text) == 0 || !nzchar(trimws(text))) {
    return(NULL)
  }
  text
}

#' Add a table note below a flextable, styled like apa_style() footers but
#' single-spaced
#' @keywords internal
#' @noRd
add_note_footer <- function(
  ft,
  text,
  font_family = NULL,
  font_size = NULL,
  text_color = "black",
  horizontal_padding = 3
) {
  if (is.null(font_size)) {
    font_size <- the$font_size
  }
  if (is.null(text)) {
    return(ft)
  }
  if (is.null(font_family)) {
    font_family <- the$font_family
  }
  n_before <- flextable::nrow_part(ft, "footer")
  ft <- flextable::add_footer_lines(
    ft,
    values = ftExtra::as_paragraph_md(paste0("*Note*. ", text))
  )
  i <- n_before + 1
  ft |>
    flextable::font(i = i, part = "footer", fontname = font_family) |>
    flextable::fontsize(i = i, part = "footer", size = font_size) |>
    flextable::color(i = i, part = "footer", color = text_color) |>
    flextable::line_spacing(i = i, part = "footer", space = 1) |>
    flextable::align(i = i, part = "footer", align = "left") |>
    flextable::valign(i = i, part = "footer", valign = "top") |>
    flextable::padding(
      i = i,
      part = "footer",
      padding.top = 2,
      padding.bottom = 0,
      padding.left = horizontal_padding,
      padding.right = horizontal_padding
    )
}

# Shared note wording ----

#' Link names as written in notes (e.g., "cloglog" becomes "complementary
#' log-log"); names not listed are kept as R gives them
#' @param link character vector of R's link names
#' @keywords internal
#' @noRd
link_name <- function(link) {
  names_written <- c(
    logit = "logit",
    probit = "probit",
    cloglog = "complementary log-log",
    loglog = "log-log",
    cauchit = "Cauchit",
    log = "log",
    identity = "identity",
    inverse = "inverse",
    sqrt = "square root",
    "1/mu^2" = "inverse squared"
  )
  ifelse(link %in% names(names_written), names_written[link], link) |>
    unname()
}

#' Put non-breaking spaces around comparison signs (NOTE-7), so that
#' "*N* = 32" or "*p* < .05" is never split across lines
#' @param x character vector of note sentences
#' @keywords internal
#' @noRd
nbsp_comparisons <- function(x) {
  gsub(
    " ([=<>\u2264\u2265\u2260\u2248]) ",
    "\u00A0\\1\u00A0",
    x,
    perl = TRUE
  )
}

#' The sample-size sentence of a model note: "*N* = 66." or, with details,
#' "*N* = 228, with 165 events." or "*N* = 2,148 observations in 537
#' clusters (4 per cluster)."
#' @param n number of observations
#' @param details text placed after the number (e.g., ", with 165 events")
#' @keywords internal
#' @noRd
n_sentence <- function(n, details = "") {
  paste0("*N* = ", format(n, big.mark = ","), details, ".")
}

#' The sample-size sentence of a model whose method writes no model
#' sentences: "*N* = 32." or, for mixed models, "*N* = 180 observations
#' (Subject: 18 clusters)."
#' @keywords internal
#' @noRd
model_n_sentence <- function(fit) {
  n <- tryCatch(stats::nobs(fit), error = function(e) insight::n_obs(fit))
  if (length(n) != 1 || is.na(n)) {
    return(NULL)
  }
  clusters <- model_cluster_sizes(fit)
  if (length(clusters) == 0) {
    return(n_sentence(n))
  }
  labels <- purrr::map_chr(names(clusters), \(g) cluster_label(g, the$variable_name_formatter))
  n_sentence(
    n,
    paste0(
      " observations (",
      paste0(labels, ": ", format(clusters, big.mark = ",", trim = TRUE), " clusters", collapse = "; "),
      ")"
    )
  )
}

#' Number of clusters of each grouping factor of a mixed model, named by
#' the grouping factor (outermost first for nlme), or NULL
#' @keywords internal
#' @noRd
model_cluster_sizes <- function(fit) {
  sizes <- if (inherits(fit, "merMod")) {
    lme4::ngrps(fit)
  } else if (inherits(fit, "glmmTMB")) {
    vapply(fit$modelInfo$reTrms$cond$flist, nlevels, integer(1))
  } else if (inherits(fit, "lme")) {
    rev(fit$dims$ngrps[seq_len(fit$dims$Q)])
  } else if (inherits(fit, "clmm")) {
    fit$dims$nlev.gf
  }
  if (length(sizes) == 0) {
    return(NULL)
  }
  stats::setNames(as.integer(sizes), names(sizes))
}

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.