srh.simple.posthoc: Simple-effects post hoc (Dunn) with Bonferroni adjustment

View source: R/srh.simple.posthoc.R

srh.simple.posthocR Documentation

Simple-effects post hoc (Dunn) with Bonferroni adjustment

Description

Computes Dunn's pairwise comparisons for simple effects of one target factor (compare) within levels of the remaining conditioning factors (by). Adjustment can be done within each conditioning table (SPSS-like) or globally across all tests.

Usage

srh.simple.posthoc(
  formula,
  data,
  compare = NULL,
  scope = c("within", "global"),
  digits = 3
)

Arguments

formula

A formula of the form y ~ A + B (+ C ...); requires at least two RHS factors to define a simple effect.

data

A data.frame containing variables in formula.

compare

Character; the factor to compare pairwise. By default, the first factor on the RHS of formula.

scope

"within" (default) applies Bonferroni adjustment within each by-table; "global" applies one Bonferroni across all pairwise tests produced for all by-tables combined.

digits

Number of digits for rounding numeric columns (Z, P.unadj, P.adj). Default 3.

Details

The data are subset to complete cases on y and all RHS factors. All RHS variables are coerced to factor. The table is split by all factors except compare and Dunn's test (FSA::dunnTest) is run per split. With scope = "within", the Bonferroni correction is applied separately in each split (with m.tests = choose(k,2) for that split). With scope = "global", P.adj is re-computed once with stats::p.adjust(..., method = "bonferroni") across all pairwise tests from all splits (and m.tests is set to the total number of tests).

Value

A data.frame with columns:

  • conditioning factor columns (one value repeated per split),

  • Comparison, Z, P.unadj, P.adj,

  • m.tests (number of tests used for Bonferroni),

  • adj.note (human-readable note).

Attributes: "adjustment" (one-line description) and "call".

Examples

data(mimicry, package = "factorH")

# Two factors: pairwise comparisons for 'gender' within levels of 'condition'.
# By default, compare = first RHS factor ('gender' here).
# p.adj uses Bonferroni within each by-table (scope = "within").
tab1 <- srh.simple.posthoc(liking ~ gender + condition, data = mimicry)
head(tab1); attr(tab1, "adjustment")

# One global family of tests (global Bonferroni across all subgroup tests):
tab2 <- srh.simple.posthoc(liking ~ gender + condition, data = mimicry,
                           scope = "global")
head(tab2); attr(tab2, "adjustment")

# Three factors: compare 'gender' within each condition × age_cat cell.
tab3 <- srh.simple.posthoc(liking ~ gender + condition + age_cat, data = mimicry)
head(tab3)

# Choose a different target factor to compare: here 'condition'
# (within each gender × age_cat cell).
tabA <- srh.simple.posthoc(liking ~ gender + condition + age_cat, data = mimicry,
                           compare = "condition")
head(tabA)

# Global Bonferroni variants (less common, but sometimes requested):
tabG  <- srh.simple.posthoc(liking ~ gender + condition + age_cat, data = mimicry,
                            scope = "global")
tabG2 <- srh.simple.posthoc(liking ~ condition + gender, data = mimicry)
tabG3 <- srh.simple.posthoc(liking ~ condition + gender, data = mimicry,
                            scope = "global")
head(tabG); head(tabG2); head(tabG3)

factorH documentation built on Sept. 11, 2025, 9:09 a.m.