Learn R Programming

DMAR (version 1.0.0)

pairwise_within: Paired Pairwise Comparisons With Multiple-Comparison Adjustment

Description

Computes all pairwise paired-t comparisons among the levels of a within-subjects factor and returns the mean difference, paired SD, paired t-statistic, degrees of freedom, raw and adjusted p-values, and a confidence interval on the mean difference, all in tidy long form. p-values are adjusted across comparisons by the user-specified method (Bonferroni, Holm, Hochberg, Hommel, BH, BY, or none).

Usage

pairwise_within(
  data,
  subject = NULL,
  condition = NULL,
  outcome = NULL,
  adjust = c("holm", "bonferroni", "hochberg", "hommel", "BH", "BY", "none"),
  conf_level = 0.95,
  bonferroni_ci = FALSE
)

Value

A data.frame with one row per pair. Columns:

contrast (the labeled difference, e.g.

"B - A"), mean_difference, sd_difference,

t_statistic, df, p_value (raw),

p_adjusted, lower_limit, upper_limit,

n_pairs.

Arguments

data

Either an \(n \times k\) wide numeric matrix / data.frame (one row per subject, one column per condition), or a long-format data.frame together with subject, condition, and outcome column names.

subject

Long-format only: character name of the subject-id column.

condition

Long-format only: character name of the within- subjects factor column.

outcome

Long-format only: character name of the response column.

adjust

Multiple-comparison adjustment method. One of "holm" (default), "bonferroni", "hochberg", "hommel", "BH", "BY", or "none". Passed to stats::p.adjust().

conf_level

Family-wise confidence level for the per-pair CIs. Default 0.95. CIs are computed at the per-pair nominal level (\(1 - \alpha/m\) under Bonferroni; conf_level otherwise).

bonferroni_ci

Logical. If TRUE, the CIs use a per-pair confidence level of \(1 - (1 - \mathrm{conf\_level}) / m\) to give simultaneous conf_level coverage across the \(m\) comparisons (Bonferroni-corrected CIs). Default FALSE.

Author

Ken Kelley [email protected]

Details

Why a paired pairwise. stats::pairwise.t.test() returns a square matrix of p-values, which doesn't compose with the rest of the DMAR pipeline. This function returns one row per comparison, matching the data.frame(term, value) style used elsewhere.

CI scale. CIs are on the mean-difference scale (unstandardized). When bonferroni_ci = TRUE, the per-pair confidence level is \(1 - (1 - \mathrm{conf\_level}) / m\), giving Bonferroni-style simultaneous coverage. The CI is built from the paired-t distribution with \(n - 1\) degrees of freedom.

References

Maxwell, S. E., Delaney, H. D., & Kelley, K. (2027). Designing experiments and analyzing data: A model comparison perspective (4th ed.). Routledge. (See Chapter 11.)

Holm, S. (1979). A simple sequentially rejective multiple test procedure. Scandinavian Journal of Statistics, 6(2), 65--70.

See Also

pairwise.t.test, anova_within

Other within-subjects analysis: anova_within(), anova_within_two_way(), epsilon_corrections(), mauchly_test(), plot_trajectories_fitted()

Other hypothesis tests: adjusted_means(), ancova(), anova_within(), ci_dunnett(), ci_scheffe(), ci_tukey_kramer(), compare_cov_structures(), contrast_test(), correlations_test(), equivalence_r(), equivalence_smd(), factorial_anova(), manova_split_plot(), mauchly_test(), mixed_anova(), obrien_test(), randomization_test(), randomization_test_paired(), regions_of_significance(), simple_effects_AB(), summary_t_test(), welch_t()

Examples

Run this code
# 1. Wide-format input: 4 timepoints x 10 subjects.
set.seed(113)
n <- 10; k <- 4
Y <- matrix(rnorm(n * k, 0, 1), n, k) +
     matrix(rep(seq(0, 0.9, length.out = k), n), n, k, byrow = TRUE) +
     rnorm(n, 0, 1.5)
colnames(Y) <- paste0("T", 1:k)
pairwise_within(Y)

# 2. Long-format input:
long <- data.frame(
  subject = factor(rep(1:n, times = k)),
  time    = factor(rep(paste0("T", 1:k), each = n)),
  y       = as.vector(Y)
)
pairwise_within(long, subject = "subject", condition = "time", outcome = "y")

Run the code above in your browser using DataLab