R Code and Instructions
On this page
- 5.3 — Multiple Comparisons
- 5.3.1 — Experimentwise and Per-Comparison Error Rates
- 5.4 — Simultaneous Confidence Intervals
- 5.5 — Multiple Planned Comparisons
- 5.5.1 — Bonferroni Adjustment
- 5.5.2 — Modification of the Bonferroni Approach with Unequal Variances
- 5.5.3 — Numerical Example
- 5.6 — Pairwise Comparisons
- 5.6.1 — Tukey’s HSD Procedure
- 5.6.2 — Modifications of Tukey’s HSD
- 5.6.3 — Numerical Example
- 5.7 — Post Hoc Complex Comparisons
- 5.7.2 — Comparison of Scheffé to Bonferroni and Tukey
- 5.7.3 — Modifications of Scheffé’s Method
- 5.7.4 — Numerical Example
- 5.8 — Other Multiple-Comparison Procedures
- 5.8.1 — Dunnett’s Procedure for Comparisons with a Control
- 5.8.2 — Numerical Example
- 5.8.3 — Procedures for Comparisons With the Best
- 5.8.4 — Numerical Example
- 5.8.5 — Fisher’s LSD (Protected t)
- 5.9 — False Discovery Rate
- 5.11 — Statistical Power
- How to Cite This Page
- References
Examples follow the book’s section numbers.
Chapter 5 takes up the multiple-comparisons problem: any time several tests are performed on the same data, the probability of at least one Type I error somewhere in the experiment exceeds the alpha level used for each single test. The chapter develops methods that control that experimentwise rate, and matches each method to the kind of question being asked — Bonferroni for a planned set of contrasts, Tukey’s HSD for all pairwise comparisons, Scheffé for complex contrasts chosen after seeing the data, Dunnett for comparisons against a control, Hsu’s modification of Dunnett for identifying the best treatment, and the false discovery rate as a different way of defining error control altogether. It closes by showing what each of these choices costs in statistical power.
These notes build on Chapters 1, 3, and 4; see Chapter 1 for how to
obtain the book’s data sets from the AMCP package and how
those data sets are named. Chapter 5 uses one of them,
chapter_5_table_4, the blood pressure data of Table
5.4.
HideShow
# Load all of the packages used.
library(dplyr) # data manipulation
library(ggplot2) # figures
library(AMCP) # the textbook's data sets
library(DMAR) # design, measurement, and analysis
library(multcomp) # multiple-comparison procedures (glht)
library(gt) # polished HTML tables
library(modelsummary) # side-by-side model-comparison tables5.3 — Multiple Comparisons
5.3.1 — Experimentwise and Per-Comparison Error Rates
The book’s starting point is the four-treatment hypertension study with three orthogonal contrasts, each tested at . Equation 5.1 gives the probability of at least one Type I error across an orthogonal set of contrasts,
HideShow
[1] 0.142625HideShow
[1] 0.15Three quantities that are easy to confuse have three different values here: the per-comparison rate is .05, the expected number of errors per experiment is .15, and the experimentwise rate is .143. Only one of them can be set to .05, and the book’s preference is to control . In single-factor designs and the familywise rate are identical, so this chapter works with throughout; the distinction between them waits until Chapter 7.
The figure below is a companion illustration of why the uncorrected rate is a problem: as the number of orthogonal contrasts grows, the probability of at least one spurious “significant” result climbs away from the nominal .05.
HideShow
error_inflation <- data.frame(
C = 1:20,
alpha_EW = 1 - (1 - 0.05)^(1:20)
)
ggplot(error_inflation, aes(x = C, y = alpha_EW)) +
geom_hline(yintercept = 0.05, linetype = "dashed", colour = "#0C2340") +
annotate("text", x = 11, y = 0.10, label = "alpha[plain(PC)] == 0.05",
parse = TRUE, colour = "#0C2340") +
geom_line() +
geom_point() +
scale_y_continuous(limits = c(0, 1)) +
labs(x = expression("Number of orthogonal contrasts" ~ (italic(C))),
y = expression("Experimentwise error rate" ~ (alpha[plain(EW)])),
title = "Inflation of the experimentwise Type I error rate")Pushing the logic to its limit, the book quotes R. G. Miller’s “ultraconservative statistician” who treats every test of a lifetime — say 1,000 of them — as a single family. Equation 5.2 solves for the per-comparison alpha that such a statistician would need.
HideShow
[1] 5.129198e-05The price of that protection is power, which the next companion figure traces. Each point holds at .05 across tests and asks what power remains for a two-sample comparison with per group and a population standardized mean difference of .
HideShow
C_values <- seq(1, 1000, by = 10)
# Equation 5.2 again: the per-comparison alpha that holds alpha_EW at .05.
alpha_PC_adjusted <- 1 - (1 - 0.05)^(1 / C_values)
power_values <- vapply(
alpha_PC_adjusted,
function(a) power.t.test(n = 64,
delta = 0.5,
sd = 1,
sig.level = a,
type = "two.sample",
alternative = "two.sided",
strict = TRUE)$power,
numeric(1)
)
# Power at C = 1, the uncorrected single-test benchmark.
power_values[1]HideShow
[1] 0.8014596HideShow
power_cost <- data.frame(C = C_values, power = power_values)
ggplot(power_cost, aes(x = C, y = power)) +
geom_hline(yintercept = power_values[1], linetype = "dashed",
colour = "#0C2340") +
annotate("text", x = 600, y = power_values[1] + 0.04,
label = "Power with no correction", colour = "#0C2340") +
geom_line(linewidth = 1) +
scale_y_continuous(limits = c(0, 0.9)) +
labs(x = expression("Number of tests" ~ (italic(C))),
y = "Power for a single contrast",
title = "The power cost of controlling a very large family")5.4 — Simultaneous Confidence Intervals
Every procedure in this chapter is built from one formula. Equation 5.3 writes a confidence interval for a contrast under homogeneity of variance as
and Equation 5.12 states the same thing in words: estimate (critical value)(estimated standard error). What separates Bonferroni from Tukey from Scheffé from Dunnett is only the critical value . So the estimate and its standard error are built once here, and each later section supplies its own .
The data of Table 5.4
HideShow
data(chapter_5_table_4)
# Table 5.4 names the four groups, so each code is mapped to its treatment
# explicitly; factor() is then used only to fix the order of the levels.
blood_pressure <- chapter_5_table_4 |>
mutate(Treatment = case_when(
group == 1 ~ "Drug Therapy",
group == 2 ~ "Biofeedback",
group == 3 ~ "Diet",
group == 4 ~ "Combination"
)) |>
mutate(Treatment = factor(
Treatment,
levels = c("Drug Therapy", "Biofeedback", "Diet", "Combination")
))
blood_pressureHideShow
group sbp Treatment
1 1 84 Drug Therapy
2 1 95 Drug Therapy
3 1 93 Drug Therapy
4 1 104 Drug Therapy
5 1 99 Drug Therapy
6 1 106 Drug Therapy
7 2 81 Biofeedback
8 2 84 Biofeedback
9 2 92 Biofeedback
10 2 101 Biofeedback
11 2 80 Biofeedback
12 2 108 Biofeedback
13 3 98 Diet
14 3 95 Diet
15 3 86 Diet
16 3 87 Diet
17 3 94 Diet
18 3 101 Diet
19 4 91 Combination
20 4 78 Combination
21 4 85 Combination
22 4 80 Combination
23 4 81 Combination
24 4 76 CombinationHideShow
Treatment mean sd s2 n
1 Drug Therapy 96.83333 8.035339 64.56667 6
2 Biofeedback 91.00000 11.489125 132.00000 6
3 Diet 93.50000 5.958188 35.50000 6
4 Combination 81.83333 5.419102 29.36667 6The omnibus analysis of variance and its error term
The contrasts themselves are the point of the chapter, but the omnibus analysis supplies the pooled error term and its degrees of freedom, which every procedure that assumes homogeneity of variance uses.
HideShow
Analysis of Variance Table
Response: sbp
Df Sum Sq Mean Sq F value Pr(>F)
Treatment 3 744.79 248.264 3.7985 0.02637 *
Residuals 20 1307.17 65.358
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1HideShow
[1] 65.35833HideShow
df_between df_error
3 20 The book does not print the omnibus test for the Table 5.4 data, so only is checked against the printed page here; the remaining entries of the table follow from it.
This is a model-comparison text, so it is worth making the comparison behind that table explicit. The restricted model gives every participant the grand mean; the full model gives each treatment its own mean. The from comparing them is the omnibus above.
HideShow
Analysis of Variance Table
Model 1: sbp ~ 1
Model 2: sbp ~ Treatment
Res.Df RSS Df Sum of Sq F Pr(>F)
1 23 2052.0
2 20 1307.2 3 744.79 3.7985 0.02637 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1The same omnibus analysis as an APA-style table, built with
gt so the formatting carries over to HTML. The numbers
are exactly those in the anova() printout above.
HideShow
anova_table <- data.frame(
Source = c("Treatment (between groups)", "Within (error)"),
SS = ANOVA_Result$"Sum Sq",
df = ANOVA_Result$"Df",
MS = ANOVA_Result$"Mean Sq",
Fstat = ANOVA_Result$"F value",
p = ANOVA_Result$"Pr(>F)"
)
# Partial eta-squared: the effect's share of effect-plus-error variance.
anova_table$peta <- c(
anova_table$SS[1] / (anova_table$SS[1] + anova_table$SS[2]),
NA_real_
)
anova_table |>
gt() |>
fmt_number(columns = c(SS, MS, Fstat), decimals = 2) |>
fmt_number(columns = peta, decimals = 3) |>
fmt_number(columns = p, decimals = 4) |>
sub_missing(missing_text = "") |>
cols_label(Fstat = md("*F*"), p = md("*p*"),
peta = md("Partial η<sup>2</sup>")) |>
tab_header(title = md("**Table 1**. Analysis of Variance for Systolic Blood Pressure")) |>
cols_align(align = "center", columns = -Source)| Table 1. Analysis of Variance for Systolic Blood Pressure | ||||||
| Source | SS | df | MS | F | p | Partial η2 |
|---|---|---|---|---|---|---|
| Treatment (between groups) | 744.79 | 3 | 248.26 | 3.80 | 0.0264 | 0.363 |
| Within (error) | 1,307.17 | 20 | 65.36 | |||
A side-by-side modelsummary table places the two models in adjacent columns, with standard errors in parentheses (APA style) and fit statistics at the foot:
| Grand mean | Group means | |
|---|---|---|
| (Intercept) | 90.792 | 96.833 |
| (1.928) | (3.300) | |
| TreatmentBiofeedback | -5.833 | |
| (4.668) | ||
| TreatmentDiet | -3.333 | |
| (4.668) | ||
| TreatmentCombination | -15.000 | |
| (4.668) | ||
| Num.Obs. | 24 | 24 |
| AIC | 178.9 | 174.1 |
| BIC | 181.2 | 179.9 |
| Log.Lik. | -87.436 | -82.025 |
Per-interval confidence intervals, the baseline
Choosing gives Equation 4.40 from Chapter 4, which controls confidence at the per-interval level rather than simultaneously. Note the numerator degrees of freedom: a contrast is a single-degree-of-freedom question, so , and the multiplier is equivalently a central quantile because .
HideShow
from_F from_t
2.085963 2.085963 HideShow
psi_hat se lower upper
Drug - Biofeedback 5.833333 4.667559 -3.903025 15.569692
Drug - Diet 3.333333 4.667559 -6.403025 13.069692
Biofeedback - Diet -2.500000 4.667559 -12.236358 7.236358
Mean(1, 2, 3) - Combin 11.944444 3.811046 3.994741 19.894148HideShow
DMAR’s ci_c() also gives the 95% interval for each
contrast from the group means, sample sizes, and pooled within-group
standard deviation.
| term | value |
|---|---|
| ci_lower | -3.903 |
| contrast | 5.833 |
| ci_upper | 15.570 |
Confidence level: 95%
HideShow
#> -3.903 to 15.570 — agrees with the hand computation.
# Collect all four contrasts in one table.
bind_rows(lapply(Contrasts, function(cw) {
fit <- ci_c(means = Summary_5_4$mean, s_anova = sqrt(MS_W),
c_weights = cw, n_per_group = Summary_5_4$n,
N = sum(Summary_5_4$n), conf_level = 0.95)
rows <- generics::tidy(fit)
tibble::tibble(estimate = rows$estimate[rows$term == "contrast"],
lower = rows$estimate[rows$term == "ci_lower"],
upper = rows$estimate[rows$term == "ci_upper"])
}), .id = "comparison")HideShow
# A tibble: 4 × 4
comparison estimate lower upper
<chr> <dbl> <dbl> <dbl>
1 Drug - Biofeedback 5.83 -3.90 15.6
2 Drug - Diet 3.33 -6.40 13.1
3 Biofeedback - Diet -2.5 -12.2 7.24
4 Mean(1, 2, 3) - Combin 11.9 3.99 19.9 5.5 — Multiple Planned Comparisons
5.5.1 — Bonferroni Adjustment
The Bonferroni adjustment sets . The book illustrates it with the strategy-training study: four groups, 11 participants per group, three planned contrasts, and an observed of 4.23 on 1 and 40 degrees of freedom.
HideShow
[1] 0.01666667HideShow
[1] 4.084746HideShow
[1] 6.24428HideShow
[1] 2.498856Checking the Bonferroni inequality
Equation 5.5 is an inequality, , so the actual experimentwise rate under the Bonferroni adjustment could in principle fall well below .05. Table 5.3 shows that for orthogonal contrasts it does not: the inequality is an equality for practical purposes, and the adjustment is therefore not conservative here.
HideShow
C Actual_Probability Bonferroni_Limit
1 1 0.050000 0.05
2 2 0.049375 0.05
3 3 0.049171 0.05
4 4 0.049070 0.05
5 5 0.049010 0.05
6 10 0.048890 0.05
7 20 0.048830 0.05
8 50 0.048794 0.055.5.2 — Modification of the Bonferroni Approach with Unequal Variances
When the group variances are not pooled, the contrast’s standard error uses each group’s own variance (Equation 5.10) and its denominator degrees of freedom come from the Welch–Satterthwaite expression given as Equation 5.14 and in the footnote to Table 5.17. Both are written out below as functions of the -weights.
HideShow
# Standard error of a contrast without pooling (Equations 5.10 and 5.11).
contrast_se_heterogeneous <- function(c_weights, s2, n) {
sqrt(sum(c_weights^2 * s2 / n))
}
# Denominator degrees of freedom for that standard error (Equation 5.14; the
# same expression appears as Chapter 4's Equation 4.34).
contrast_df_heterogeneous <- function(c_weights, s2, n) {
components <- c_weights^2 * s2 / n
sum(components)^2 / sum(components^2 / (n - 1))
}
se_heterogeneous <- vapply(Contrasts, contrast_se_heterogeneous, numeric(1),
s2 = Summary_5_4$s2, n = Summary_5_4$n)
df_heterogeneous <- vapply(Contrasts, contrast_df_heterogeneous, numeric(1),
s2 = Summary_5_4$s2, n = Summary_5_4$n)
round(rbind(se = se_heterogeneous, df = df_heterogeneous), 3)HideShow
Drug - Biofeedback Drug - Diet Biofeedback - Diet Mean(1, 2, 3) - Combin
se 5.724 4.084 5.284 3.032
df 8.947 9.222 7.508 13.2885.5.3 — Numerical Example
The contrast tests of Table 5.5
With the estimates and both kinds of standard error in hand, the tests at the top of Table 5.5 follow directly. The Bonferroni decision rule is to compare each value with , and only the fourth contrast clears it, whether or not homogeneity is assumed.
HideShow
psi_hat <- vapply(Contrasts, function(cw) sum(cw * Summary_5_4$mean), numeric(1))
se_pooled <- vapply(Contrasts, function(cw) sqrt(MS_W * sum(cw^2 / Summary_5_4$n)),
numeric(1))
contrast_tests <- data.frame(
psi_hat = round(psi_hat, 3),
se_equal = round(se_pooled, 3),
t_equal = round(psi_hat / se_pooled, 3),
df_equal = df_error,
p_equal = round(2 * pt(-abs(psi_hat / se_pooled), df = df_error), 3),
se_unequal = round(se_heterogeneous, 3),
t_unequal = round(psi_hat / se_heterogeneous, 3),
df_unequal = round(df_heterogeneous, 3),
p_unequal = round(2 * pt(-abs(psi_hat / se_heterogeneous),
df = df_heterogeneous), 3)
)
contrast_testsHideShow
psi_hat se_equal t_equal df_equal p_equal se_unequal
Drug - Biofeedback 5.833 4.668 1.250 20 0.226 5.724
Drug - Diet 3.333 4.668 0.714 20 0.483 4.084
Biofeedback - Diet -2.500 4.668 -0.536 20 0.598 5.284
Mean(1, 2, 3) - Combin 11.944 3.811 3.134 20 0.005 3.032
t_unequal df_unequal p_unequal
Drug - Biofeedback 1.019 8.947 0.335
Drug - Diet 0.816 9.222 0.435
Biofeedback - Diet -0.473 7.508 0.650
Mean(1, 2, 3) - Combin 3.940 13.288 0.002Simultaneous intervals assuming homogeneity of variance
For the Bonferroni method the critical value is with .
HideShow
F_value CV
7.531602 2.744376 HideShow
#> F 7.5316 — the 7.53 of Table 5.5 (book p. 221) and the df = 20, C = 4
#> entry of Appendix Table A.3 (book p. 985); CV 2.7444
bonferroni_homogeneous <- do.call(rbind, lapply(
Contrasts, contrast_ci,
means = Summary_5_4$mean, n = Summary_5_4$n, MS_W = MS_W,
critical_value = CV_bonferroni
))
round(bonferroni_homogeneous, 2)HideShow
psi_hat se lower upper
Drug - Biofeedback 5.83 4.67 -6.98 18.64
Drug - Diet 3.33 4.67 -9.48 16.14
Biofeedback - Diet -2.50 4.67 -15.31 10.31
Mean(1, 2, 3) - Combin 11.94 3.81 1.49 22.40The first two lower limits print as and where the book shows and . The difference is where the rounding happens, not in the method: Table 5.5 does the arithmetic from the rounded intermediate values it displays, , while the code above carries the unrounded critical value and standard error through to the end. Substituting the book’s own displayed values reproduces its printed endpoints exactly.
HideShow
[1] -6.974989 18.641589Only the fourth interval excludes zero, which is the same conclusion the values gave: the combination treatment differs from the average of the three single treatments, and nothing else is established.
Simultaneous intervals without assuming homogeneity of variance
Equation 5.11 keeps the Bonferroni critical value but evaluates it at each contrast’s own heterogeneous degrees of freedom. Two versions are worth seeing. The first uses the exact, non-integer degrees of freedom, which is what the book recommends when a program is available (p. 222). The second rounds those degrees of freedom down to the integers that Appendix Table A.3 is indexed by, which is what produces the printed intervals.
HideShow
contrast_ci_heterogeneous <- function(c_weights, means, s2, n, critical_value) {
psi_hat <- sum(c_weights * means)
se <- contrast_se_heterogeneous(c_weights, s2, n)
data.frame(
psi_hat = psi_hat,
se = se,
lower = psi_hat - critical_value * se,
upper = psi_hat + critical_value * se
)
}
# Exact (non-integer) denominator degrees of freedom.
CV_exact <- sqrt(qf(1 - 0.05 / 4, df1 = 1, df2 = df_heterogeneous))
round(CV_exact^2, 2)HideShow
Drug - Biofeedback Drug - Diet Biofeedback - Diet
9.71 9.57 10.66
Mean(1, 2, 3) - Combin
8.33 HideShow
psi_hat se lower upper
Drug - Biofeedback 5.83 5.72 -12.00 23.66
Drug - Diet 3.33 4.08 -9.30 15.97
Biofeedback - Diet -2.50 5.28 -19.75 14.75
Mean(1, 2, 3) - Combin 11.94 3.03 3.19 20.70HideShow
Drug - Biofeedback Drug - Diet Biofeedback - Diet
10.28 9.68 11.12
Mean(1, 2, 3) - Combin
8.39 HideShow
#> 10.28, 9.68, 11.12, 8.39 — match the critical F values shown in the lower
#> half of Table 5.5, book p. 221
bonferroni_table_df <- do.call(rbind, Map(
function(cw, cv) contrast_ci_heterogeneous(
cw, Summary_5_4$mean, Summary_5_4$s2, Summary_5_4$n, cv),
Contrasts, CV_table
))
round(bonferroni_table_df, 2)HideShow
psi_hat se lower upper
Drug - Biofeedback 5.83 5.72 -12.52 24.18
Drug - Diet 3.33 4.08 -9.37 16.04
Biofeedback - Diet -2.50 5.28 -20.12 15.12
Mean(1, 2, 3) - Combin 11.94 3.03 3.16 20.73As the book notes on p. 223, relaxing homogeneity makes little practical difference in these data: two intervals widen appreciably, two narrow slightly, and the fourth contrast remains the only one that excludes zero.
The same multiplier and intervals in DMAR
The Bonferroni multiplier is a central- quantile at , and cv_t()
reports both of its tails. Passing to ci_c() as the confidence level
gives the simultaneous interval from the summary statistics.
| term | value | area_less | area_greater |
|---|---|---|---|
| lower_cv | -2.744 | 0.006 | 0.994 |
| upper_cv | 2.744 | 0.994 | 0.006 |
HideShow
| term | value |
|---|---|
| ci_lower | -6.976 |
| contrast | 5.833 |
| ci_upper | 18.643 |
Confidence level: 98.75%
5.6 — Pairwise Comparisons
5.6.1 — Tukey’s HSD Procedure
Tukey’s HSD judges every pairwise difference against the sampling distribution of the largest one. That distribution is the studentized range, written , and the critical value for an observed is ; equivalently an observed is compared with . Note that the familywise alpha is not split between the two tails here, because the studentized range is already a distribution of an absolute largest difference.
HideShow
[1] 4HideShow
[1] 3.958293HideShow
critical_t critical_F
2.798936 7.834044 Table 5.6 sets the Tukey critical value beside the unadjusted and the Bonferroni critical values for all pairwise comparisons, holding at 12. For groups there are pairs, and that count is the the Bonferroni column uses.
HideShow
groups <- 2:6
comparisons <- groups * (groups - 1) / 2
data.frame(
Number_of_Groups = groups,
Number_of_Comparisons = comparisons,
Per_Comparison = round(qf(1 - 0.05, df1 = 1, df2 = 12), 2),
Tukey = round(qtukey(1 - 0.05, nmeans = groups, df = 12)^2 / 2, 2),
Bonferroni = round(qf(1 - 0.05 / comparisons, df1 = 1, df2 = 12), 2)
)HideShow
Number_of_Groups Number_of_Comparisons Per_Comparison Tukey Bonferroni
1 2 1 4.75 4.75 4.75
2 3 3 4.75 7.12 7.73
3 4 6 4.75 8.81 9.94
4 5 10 4.75 10.16 11.75
5 6 15 4.75 11.28 13.31Whenever the Tukey critical value is smaller than the Bonferroni value, which is the sense in which Tukey’s HSD is the more powerful way to test all pairwise comparisons.
5.6.2 — Modifications of Tukey’s HSD
With unequal but pooled variances, Kramer’s modification simply computes each observed with the general unequal- formula and keeps the critical value. When variances are heterogeneous the book recommends Dunnett’s T3 for small samples, which compares the observed of Equation 5.13 — on the degrees of freedom of Equation 5.14 — with a critical value from the studentized maximum modulus distribution.
SPSS’s “Tamhane” option is Tamhane’s T2, the close relative of T3 that uses a Sidak-adjusted rather than the maximum modulus, and that is what the lower half of Table 5.7 reports. Both pieces are base R: a Welch standard error and degrees of freedom per pair, then a Sidak-adjusted quantile over the pairs.
HideShow
pairs_index <- combn(a, 2)
pair_labels <- paste(levels(blood_pressure$Treatment)[pairs_index[1, ]],
levels(blood_pressure$Treatment)[pairs_index[2, ]],
sep = " - ")
g <- pairs_index[1, ]
h <- pairs_index[2, ]
difference <- Summary_5_4$mean[g] - Summary_5_4$mean[h]
# Equation 5.13, as a standard error rather than an F.
se_pair <- sqrt(Summary_5_4$s2[g] / Summary_5_4$n[g] +
Summary_5_4$s2[h] / Summary_5_4$n[h])
# Equation 5.14.
df_pair <- (Summary_5_4$s2[g] / Summary_5_4$n[g] +
Summary_5_4$s2[h] / Summary_5_4$n[h])^2 /
((Summary_5_4$s2[g] / Summary_5_4$n[g])^2 / (Summary_5_4$n[g] - 1) +
(Summary_5_4$s2[h] / Summary_5_4$n[h])^2 / (Summary_5_4$n[h] - 1))
C_pairwise <- a * (a - 1) / 2
alpha_sidak <- 1 - (1 - 0.05)^(1 / C_pairwise)
CV_tamhane <- qt(1 - alpha_sidak / 2, df = df_pair)
data.frame(
Comparison = pair_labels,
Difference = round(difference, 2),
Std_Error = round(se_pair, 3),
Sig = round(1 - (1 - 2 * pt(-abs(difference / se_pair), df = df_pair))^C_pairwise, 3),
Lower = round(difference - CV_tamhane * se_pair, 2),
Upper = round(difference + CV_tamhane * se_pair, 2)
)HideShow
Comparison Difference Std_Error Sig Lower Upper
1 Drug Therapy - Biofeedback 5.83 5.724 0.913 -13.38 25.04
2 Drug Therapy - Diet 3.33 4.084 0.967 -10.26 16.93
3 Drug Therapy - Combination 15.00 3.957 0.027 1.65 28.35
4 Biofeedback - Diet -2.50 5.284 0.998 -21.17 16.17
5 Biofeedback - Combination 9.17 5.186 0.535 -9.49 27.83
6 Diet - Combination 11.67 3.288 0.032 0.91 22.42HideShow
#> differences 5.83, 3.33, 15.00, -2.50, 9.17, 11.67 with standard errors
#> 5.724, 4.084, 3.957, 5.284, 5.186, 3.288, significance values .913, .967,
#> .027, .998, .535, .032, and intervals -13.38 to 25.04, -10.26 to 16.93,
#> 1.65 to 28.35, -21.17 to 16.17, -9.49 to 27.83, 0.91 to 22.42 — all match
#> the Tamhane half of Table 5.7, book p. 228Relaxing homogeneity makes one more pairwise difference statistically significant here: the combination treatment is now distinguishable from diet as well as from drug therapy, exactly as the book reports on p. 227.
DMAR supplies the critical value that Dunnett’s T3 needs, through
cv_smm(). Its n_comparisons argument is the
number of pairs, and it can be checked against Appendix Table A.5, whose
columns are indexed by the number of groups.
| term | value | area_less | area_greater |
|---|---|---|---|
| upper_cv | 2.897 | 0.950 | 0.050 |
| term | value | area_less | area_greater |
|---|---|---|---|
| upper_cv | 3.365 | 0.950 | 0.050 |
5.6.3 — Numerical Example
TukeyHSD() is base R and needs only the fitted
aov() object. It reports every pairwise difference with its
simultaneous interval and its adjusted value.
HideShow
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = sbp ~ Treatment, data = blood_pressure)
$Treatment
diff lwr upr p adj
Biofeedback-Drug Therapy -5.833333 -18.89753 7.230868 0.6038383
Diet-Drug Therapy -3.333333 -16.39753 9.730868 0.8903064
Combination-Drug Therapy -15.000000 -28.06420 -1.935799 0.0208593
Diet-Biofeedback 2.500000 -10.56420 15.564201 0.9493084
Combination-Biofeedback -9.166667 -22.23087 3.897534 0.2345096
Combination-Diet -11.666667 -24.73087 1.397534 0.0905811HideShow
#> differences -5.833, -3.333, -15.000, 2.500, -9.167, -11.667 with intervals
#> -18.90 to 7.23, -16.40 to 9.73, -28.06 to -1.94, -10.56 to 15.56, -22.23
#> to 3.90, -24.73 to 1.40 and adjusted p .604, .890, .021, .949, .235, .091
#> — match the Tukey HSD half of Table 5.7, book p. 228. R subtracts the
#> earlier level from the later one, so each difference carries the opposite
#> sign from the book's (I) minus (J) row.Under the homogeneity assumption the only statistically significant pairwise difference is between drug therapy and the combination treatment. The interval runs from roughly 2 to 28 units, so although the combination therapy is confidently better, the data say little about by how much.
The same intervals as a figure. An interval that crosses the dashed line at zero leaves the direction of that difference unresolved.
HideShow
tukey_df <- as.data.frame(Tukey_Result$Treatment)
tukey_df$Comparison <- factor(rownames(tukey_df),
levels = rev(rownames(tukey_df)))
ggplot(tukey_df, aes(x = diff, y = Comparison)) +
geom_vline(xintercept = 0, linetype = "dashed", colour = "#0C2340") +
geom_pointrange(aes(xmin = lwr, xmax = upr)) +
labs(x = "Difference in mean systolic blood pressure",
y = NULL,
title = "Tukey HSD 95% simultaneous confidence intervals")The same intervals in DMAR
TukeyHSD() works from a fitted model;
ci_tukey_kramer() takes a response vector and a grouping
vector instead, and reports the studentized-range statistic for each
pair alongside the interval.
| contrast | mean_difference | se | q_statistic | ci_lower | ci_upper | p_adjusted |
|---|---|---|---|---|---|---|
| Biofeedback – Drug Therapy | -5.833 | 3.300 | -1.767 | -18.898 | 7.231 | 0.6038 |
| Diet – Drug Therapy | -3.333 | 3.300 | -1.010 | -16.398 | 9.731 | 0.8903 |
| Combination – Drug Therapy | -15.000 | 3.300 | -4.545 | -28.064 | -1.936 | 0.0209 |
| Diet – Biofeedback | 2.500 | 3.300 | 0.757 | -10.564 | 15.564 | 0.9493 |
| Combination – Biofeedback | -9.167 | 3.300 | -2.777 | -22.231 | 3.898 | 0.2345 |
| Combination – Diet | -11.667 | 3.300 | -3.535 | -24.731 | 1.398 | 0.0906 |
Confidence level: 95%, simultaneous interval (Tukey)
| term | value | area_less | area_greater |
|---|---|---|---|
| upper_cv | 2.799 | 0.950 | 0.050 |
5.7 — Post Hoc Complex Comparisons
Scheffé’s method judges a contrast against the sampling distribution of the largest obtainable from any contrast, pairwise or complex. Because (Section 5.7.1), that distribution is times a central on and degrees of freedom, so the critical value of Equation 5.22 is . The price of being allowed to test infinitely many data-suggested contrasts is that this critical value is large.
HideShow
[1] 8.7668325.7.2 — Comparison of Scheffé to Bonferroni and Tukey
Scheffé’s cost is fixed; Bonferroni’s grows with every contrast added. Table 5.8 shows where the two cross for and .
HideShow
a_illustration <- 4
df_error_illustration <- 30
C_values <- 1:10
scheffe_cv <- (a_illustration - 1) *
qf(1 - 0.05, df1 = a_illustration - 1, df2 = df_error_illustration)
table_5_8 <- data.frame(
C = C_values,
Bonferroni = round(qf(1 - 0.05 / C_values, df1 = 1,
df2 = df_error_illustration), 2),
Scheffe = round(scheffe_cv, 2)
)
table_5_8HideShow
C Bonferroni Scheffe
1 1 4.17 8.77
2 2 5.57 8.77
3 3 6.43 8.77
4 4 7.06 8.77
5 5 7.56 8.77
6 6 7.98 8.77
7 7 8.34 8.77
8 8 8.65 8.77
9 9 8.93 8.77
10 10 9.18 8.77HideShow
[1] 8So up to eight planned contrasts can be tested with a smaller critical value under Bonferroni than under Scheffé. Only a researcher who might test more than eight, or who cannot specify the contrasts in advance, gains from Scheffé here. And for pairwise comparisons specifically, Tukey beats both.
5.7.3 — Modifications of Scheffé’s Method
The Brown–Forsythe separate-variances modification keeps the form of Equation 5.22 and substitutes the heterogeneous denominator degrees of freedom already computed in Section 5.5.2.
5.7.4 — Numerical Example
The same four contrasts are now treated as having been chosen after seeing the data, so Scheffé is the method of choice. Nothing about the estimates or their standard errors changes — only the critical value.
HideShow
F_value CV
9.295174 3.048799 HideShow
psi_hat se lower upper
Drug - Biofeedback 5.83 4.67 -8.40 20.06
Drug - Diet 3.33 4.67 -10.90 17.56
Biofeedback - Diet -2.50 4.67 -16.73 11.73
Mean(1, 2, 3) - Combin 11.94 3.81 0.33 23.56Every Scheffé interval is wider than the corresponding Bonferroni interval of Section 5.5.3, which is what choosing contrasts post hoc costs. The conclusion is unchanged: only the complex contrast supports a confident statement of direction.
Without the homogeneity assumption, the critical value becomes on each contrast’s own degrees of freedom. As in Section 5.5.3 the book reads from an appendix table at integer degrees of freedom — its footnote to Table 5.10 spells this out, giving for the first contrast — so rounding the degrees of freedom down reproduces the printed values.
HideShow
Drug - Biofeedback Drug - Diet Biofeedback - Diet
11.62 11.47 12.58
Mean(1, 2, 3) - Combin
10.17 HideShow
psi_hat se lower upper
Drug - Biofeedback 5.83 5.72 -13.67 25.34
Drug - Diet 3.33 4.08 -10.50 17.17
Biofeedback - Diet -2.50 5.28 -21.24 16.24
Mean(1, 2, 3) - Combin 11.94 3.03 2.28 21.61HideShow
Drug - Biofeedback Drug - Diet Biofeedback - Diet
12.20 11.59 13.04
Mean(1, 2, 3) - Combin
10.23 HideShow
psi_hat se lower upper
Drug - Biofeedback 5.83 5.72 -14.16 25.82
Drug - Diet 3.33 4.08 -10.57 17.23
Biofeedback - Diet -2.50 5.28 -21.58 16.58
Mean(1, 2, 3) - Combin 11.94 3.03 2.25 21.64The same intervals in DMAR
cv_scheffe() returns the multiplier, and
ci_scheffe() builds the whole set of simultaneous intervals
from the raw data given a matrix whose columns are the -weights.
| term | value | area_less | area_greater |
|---|---|---|---|
| upper_cv | 2.961 | 0.950 | 0.050 |
HideShow
Drug - Biofeedback Drug - Diet Biofeedback - Diet Mean(1, 2, 3) - Combin
[1,] 1 1 0 0.3333333
[2,] -1 0 1 0.3333333
[3,] 0 -1 -1 0.3333333
[4,] 0 0 0 -1.0000000| contrast | estimate | se | F_value | ci_lower | ci_upper | p_adjusted |
|---|---|---|---|---|---|---|
| Drug – Biofeedback | 5.833 | 4.668 | 0.521 | -8.397 | 20.064 | 0.6730 |
| Drug – Diet | 3.333 | 4.668 | 0.170 | -10.897 | 17.564 | 0.9154 |
| Biofeedback – Diet | -2.500 | 4.668 | 0.096 | -16.730 | 11.730 | 0.9616 |
| Mean(1, 2, 3) – Combin | 11.944 | 3.811 | 3.274 | 0.325 | 23.564 | 0.0424 |
Confidence level: 95%, simultaneous interval (Scheffé)
5.8 — Other Multiple-Comparison Procedures
5.8.1 — Dunnett’s Procedure for Comparisons with a Control
When every test of interest compares one group with each of the other groups, Dunnett’s procedure exploits the correlation among those comparisons and so uses a smaller critical value than either Bonferroni or Tukey would.
5.8.2 — Numerical Example
Here the combination treatment plays the role of the control, and the
three single treatments are each compared with it. glht()
from multcomp
takes the fitted aov() object; its Dunnett contrasts are
formed against the first level of the factor, so the
combination treatment is released to that position first. The adjusted
values and simultaneous limits
are obtained by numerical integration over a multivariate , which involves random sampling, so the
seed is set on the page.
HideShow
Simultaneous Tests for General Linear Hypotheses
Multiple Comparisons of Means: Dunnett Contrasts
Fit: aov(formula = sbp ~ Treatment, data = blood_pressure_control)
Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
Drug Therapy - Combination == 0 15.000 4.668 3.214 0.0117 *
Biofeedback - Combination == 0 9.167 4.668 1.964 0.1527
Diet - Combination == 0 11.667 4.668 2.500 0.0544 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Adjusted p values reported -- single-step method)HideShow
Simultaneous Confidence Intervals
Multiple Comparisons of Means: Dunnett Contrasts
Fit: aov(formula = sbp ~ Treatment, data = blood_pressure_control)
Quantile = 2.5391
95% family-wise confidence level
Linear Hypotheses:
Estimate lwr upr
Drug Therapy - Combination == 0 15.0000 3.1484 26.8516
Biofeedback - Combination == 0 9.1667 -2.6849 21.0183
Diet - Combination == 0 11.6667 -0.1849 23.5183The Dunnett intervals are narrower than the corresponding Tukey intervals of Table 5.7, which is the gain from giving up all comparisons among the three single treatments.
The same intervals in DMAR
cv_dunnett() gives the critical value, which can be
checked against the book’s appendix. ci_dunnett() builds
the intervals from the raw data with the control named rather than
released, and evaluates the Dunnett quantile deterministically, so its
limits match the book’s SAS output to three decimals rather than to
simulation error.
| term | value | area_less | area_greater |
|---|---|---|---|
| upper_cv | 2.540 | 0.950 | 0.050 |
| contrast | mean_difference | se | t_value | ci_lower | ci_upper | p_adjusted |
|---|---|---|---|---|---|---|
| Drug Therapy – Combination | 15.000 | 4.668 | 3.214 | 3.143 | 26.857 | 0.0117 |
| Biofeedback – Combination | 9.167 | 4.668 | 1.964 | -2.691 | 21.024 | 0.1528 |
| Diet – Combination | 11.667 | 4.668 | 2.500 | -0.191 | 23.524 | 0.0544 |
Confidence level: 95%, simultaneous interval (Dunnett)
5.8.3 — Procedures for Comparisons With the Best
Hsu’s procedure answers a different question: which treatment is best? It reuses the Dunnett critical value, but the group that plays the role of the control is not known before the data are seen — it is the group with the most desirable sample mean. For blood pressure, low is best.
5.8.4 — Numerical Example
Unconstrained intervals
The steps are those in the lower half of Table 5.12. Identify the group with the smallest sample mean, form two-sided Dunnett intervals comparing every other group with it, and then obtain an interval for the best-looking group itself by reversing the signs of the limits of the interval for the group with the second smallest mean.
HideShow
Combination Biofeedback Diet Drug Therapy
81.83333 91.00000 93.50000 96.83333 HideShow
[1] "Combination"HideShow
[1] "Biofeedback"| contrast | mean_difference | se | t_value | ci_lower | ci_upper | p_adjusted |
|---|---|---|---|---|---|---|
| Drug Therapy – Combination | 15.000 | 4.668 | 3.214 | 3.143 | 26.857 | 0.0117 |
| Biofeedback – Combination | 9.167 | 4.668 | 1.964 | -2.691 | 21.024 | 0.1528 |
| Diet – Combination | 11.667 | 4.668 | 2.500 | -0.191 | 23.524 | 0.0544 |
Confidence level: 95%, simultaneous interval (Dunnett)
HideShow
# Step 5: reverse the signs of the second-best group's limits to obtain an
# interval for the group with the smallest mean.
unconstrained_frame <- as.data.frame(Unconstrained)
second_row <- grep(second_best, unconstrained_frame$contrast)
round(-rev(c(unconstrained_frame$ci_lower[second_row],
unconstrained_frame$ci_upper[second_row])), 3)HideShow
[1] -21.024 2.691That interval contains zero, so biofeedback, diet, and the combination treatment all remain candidates for best; only drug therapy is ruled out. The book’s reading of the same numbers is that the combination therapy is at most 2.69 units worse than whichever treatment is truly best, while biofeedback and diet could be as much as 21.02 and 23.52 units worse.
Constrained intervals
If there is no interest in estimating by how much the best treatment is best, the constrained intervals of Table 5.13 are available instead: one-sided Dunnett upper limits, one-sided lower limits, and then a lower limit of zero substituted wherever the one-sided lower limit came out positive.
| contrast | mean_difference | se | t_value | ci_lower | ci_upper | p_adjusted |
|---|---|---|---|---|---|---|
| Drug Therapy – Combination | 15.000 | 4.668 | 3.214 | -Inf | 25.233 | 0.9999 |
| Biofeedback – Combination | 9.167 | 4.668 | 1.964 | -Inf | 19.399 | 0.9971 |
| Diet – Combination | 11.667 | 4.668 | 2.500 | -Inf | 21.899 | 0.9994 |
Confidence level: 95%, simultaneous interval (Dunnett)
| contrast | mean_difference | se | t_value | ci_lower | ci_upper | p_adjusted |
|---|---|---|---|---|---|---|
| Drug Therapy – Combination | 15.000 | 4.668 | 3.214 | 4.767 | Inf | 0.0059 |
| Biofeedback – Combination | 9.167 | 4.668 | 1.964 | -1.066 | Inf | 0.0765 |
| Diet – Combination | 11.667 | 4.668 | 2.500 | 1.434 | Inf | 0.0272 |
Confidence level: 95%, simultaneous interval (Dunnett)
HideShow
upper <- as.data.frame(Upper_Limits)
lower <- as.data.frame(Lower_Limits)
# Step 4: a positive one-sided lower limit becomes a lower limit of zero.
final_limits <- data.frame(
Group = sub(paste0(" - ", best_group), "", upper$contrast),
Lower = pmin(lower$ci_lower, 0),
Upper = upper$ci_upper
)
# Step 5: the interval for the group with the smallest mean, from the
# second-smallest group's reversed limits.
second_row <- grep(second_best, final_limits$Group)
final_limits <- rbind(
final_limits,
data.frame(Group = best_group,
Lower = -final_limits$Upper[second_row],
Upper = -final_limits$Lower[second_row])
)
final_limits[, c("Lower", "Upper")] <- round(final_limits[, c("Lower", "Upper")], 2)
final_limitsHideShow
Group Lower Upper
1 Drug Therapy 0.00 25.23
2 Biofeedback -1.07 19.40
3 Diet 0.00 21.90
4 Combination -19.40 1.07Both the combination and the biofeedback intervals contain zero, so one of those two is the best treatment with 95% confidence. The constrained intervals rule out more treatments than the unconstrained ones did, at the cost of saying nothing about how much better the best treatment is.
5.8.5 — Fisher’s LSD (Protected )
Fisher’s LSD tests the omnibus hypothesis first and, if it is rejected, tests individual contrasts at . The book’s objection is that the omnibus test protects only the complete null hypothesis. Its illustration (p. 244) imagines eleven groups of which ten share a common population mean: the omnibus test is then rejected almost always, after which the ten identical groups receive no protection at all, and the probability of at least one spurious difference among them is given as approximately .60 rather than .05.
The companion simulation below puts numbers on that second stage directly: ten groups drawn from one population, all 45 pairwise comparisons tested at against the pooled error term. The book states the approximate value without fixing a sample size, so the figure below is an illustration of the magnitude rather than a reproduction of a printed quantity.
HideShow
set.seed(113)
n_replications <- 2000
groups_identical <- 10
n_per_group <- 10
df_error_lsd <- groups_identical * (n_per_group - 1)
pairs_lsd <- combn(groups_identical, 2)
critical_t <- qt(1 - 0.05 / 2, df = df_error_lsd)
any_significant <- replicate(n_replications, {
y <- matrix(rnorm(groups_identical * n_per_group), ncol = groups_identical)
mean_j <- colMeans(y)
MS_W_j <- mean(apply(y, 2, var))
t_obs <- abs(mean_j[pairs_lsd[1, ]] - mean_j[pairs_lsd[2, ]]) /
sqrt(MS_W_j * 2 / n_per_group)
any(t_obs > critical_t)
})
c(number_of_pairs = ncol(pairs_lsd), rate = mean(any_significant))HideShow
number_of_pairs rate
45.0000 0.6175 The rate is an order of magnitude above .05 and in the neighbourhood of the .60 the book reports, which is the point: the second stage of the LSD leaves the true part of a partially true null hypothesis unprotected.
5.9 — False Discovery Rate
Benjamini and Hochberg’s Linear Step Up (LSU) procedure compares the ordered values with a sliding scale of critical values . The book works an example with and the values .029, .026, .167, .048, and .001 (p. 248).
HideShow
Rank_i P_Value Critical_Value Below_Critical
1 1 0.001 0.01 TRUE
2 2 0.026 0.02 FALSE
3 3 0.029 0.03 TRUE
4 4 0.048 0.04 FALSE
5 5 0.167 0.05 FALSEHideShow
[1] 3Reading the table from the bottom up, as the procedure prescribes: .167 is not below .05, .048 is not below .040, and .029 is below .030, so and testing stops. Step 5 then rejects every hypothesis of rank 3 or lower — the second-ranked value of .026 is rejected too, even though it is not itself below its own critical value of .020. That is why the last column above is labelled by what it compares rather than by a decision.
The same decision from base R’s p.adjust()
The LSU procedure is exactly the Benjamini–Hochberg adjustment, which
base R implements directly. DMAR does not re-wrap it, so
p.adjust() is the route to adjusted values for a reader who wants them
rather than a step-up table.
HideShow
[1] 0.04833333 0.04833333 0.16700000 0.06000000 0.00500000HideShow
[1] 35.11 — Statistical Power
Nothing about a contrast’s observed changes when other contrasts are tested alongside it. What changes is the critical value, and therefore the power. Section 5.11 computes that power from first principles, and the book prints the R code that does it (p. 253). The planning scenario is a follow-up to the Kroes et al. electroconvulsive therapy study: three groups with presumed means of 25, 34, and 37, a population standard deviation of 12, and 25 participants per group.
HideShow
a_power <- 3
n_power <- 25
N_total <- a_power * n_power
sigma <- 12
alpha_EW <- 0.05
mu <- c(A = 25, B = 34, C = 37)
# Equation 4.56 for f-squared, then Equation 4.49 for the noncentrality
# parameter lambda.
noncentrality <- function(psi, sum_c_squared) {
f_squared <- psi^2 / (a_power * sigma^2 * sum_c_squared)
N_total * f_squared
}
planned_contrasts <- list(
"A vs B" = list(psi = mu["A"] - mu["B"], sum_c_squared = 2),
"A vs C" = list(psi = mu["A"] - mu["C"], sum_c_squared = 2),
"B vs C" = list(psi = mu["B"] - mu["C"], sum_c_squared = 2),
"A vs B and C" = list(psi = mu["A"] - mean(mu[c("B", "C")]),
sum_c_squared = 1 + 0.25 + 0.25)
)
lambda <- vapply(planned_contrasts,
function(z) noncentrality(z$psi, z$sum_c_squared),
numeric(1))
round(lambda, 2)HideShow
A vs B A vs C B vs C A vs B and C
7.03 12.50 0.78 12.76 The three critical values are the ones developed earlier in the chapter, now evaluated for this design. Only Bonferroni’s depends on .
HideShow
CV_bonferroni_power <- function(C) qf(1 - alpha_EW / C, df1 = 1,
df2 = N_total - a_power)
CV_tukey_power <- qtukey(1 - alpha_EW, nmeans = a_power,
df = N_total - a_power)^2 / 2
CV_scheffe_power <- (a_power - 1) * qf(1 - alpha_EW, df1 = a_power - 1,
df2 = N_total - a_power)
round(c(Bonferroni_C2 = CV_bonferroni_power(2),
Bonferroni_C3 = CV_bonferroni_power(3),
Bonferroni_C4 = CV_bonferroni_power(4),
Tukey = CV_tukey_power,
Scheffe = CV_scheffe_power), 2)HideShow
Bonferroni_C2 Bonferroni_C3 Bonferroni_C4 Tukey Scheffe
5.24 6.01 6.56 5.73 6.25 HideShow
#> 5.24, 6.01, 6.56, 5.73, 6.25 — match the critical values of Tables 5.18,
#> 5.19, and 5.20, book pp. 254 and 255
# Power is the probability that a noncentral F exceeds the critical value.
power_contrast <- function(critical_value, lambda) {
1 - pf(critical_value, df1 = 1, df2 = N_total - a_power, ncp = lambda)
}
power_table <- function(which_contrasts, C, tukey = TRUE) {
l <- lambda[which_contrasts]
cv <- CV_bonferroni_power(C)
out <- rbind(
Lambda = round(l, 2),
"CV for Bonferroni" = round(cv, 2),
"Power for Bonferroni" = round(power_contrast(cv, l), 2),
"CV for Tukey" = if (tukey) round(CV_tukey_power, 2) else NA,
"Power for Tukey" = if (tukey) round(power_contrast(CV_tukey_power, l), 2) else NA,
"CV for Scheffe" = round(CV_scheffe_power, 2),
"Power for Scheffe" = round(power_contrast(CV_scheffe_power, l), 2)
)
colnames(out) <- which_contrasts
out
}Two planned comparisons, Table 5.18
HideShow
A vs B A vs C
Lambda 7.03 12.50
CV for Bonferroni 5.24 5.24
Power for Bonferroni 0.64 0.89
CV for Tukey 5.73 5.73
Power for Tukey 0.60 0.87
CV for Scheffe 6.25 6.25
Power for Scheffe 0.56 0.85With only two planned pairwise comparisons the Bonferroni critical value is the smallest of the three, so Bonferroni gives the most power. Power for the Group A versus Group C comparison is good; power for Group A versus Group B is marginal at best.
All pairwise comparisons, Table 5.19
HideShow
A vs B A vs C B vs C
Lambda 7.03 12.50 0.78
CV for Bonferroni 6.01 6.01 6.01
Power for Bonferroni 0.58 0.86 0.06
CV for Tukey 5.73 5.73 5.73
Power for Tukey 0.60 0.87 0.07
CV for Scheffe 6.25 6.25 6.25
Power for Scheffe 0.56 0.85 0.06Three things change. Power for Groups B versus C is dismal, because their mean difference corresponds to a standardized mean difference of only . Tukey now beats Bonferroni, because has risen to 3 while the Tukey critical value does not depend on . And the Tukey and Scheffé columns are identical to those in Table 5.18 for exactly that reason.
All pairwise comparisons plus a complex contrast, Table 5.20
HideShow
A vs B A vs C B vs C A vs B and C
Lambda 7.03 12.50 0.78 12.76
CV for Bonferroni 6.56 6.56 6.56 6.56
Power for Bonferroni 0.54 0.83 0.05 0.84
CV for Tukey NA NA NA NA
Power for Tukey NA NA NA NA
CV for Scheffe 6.25 6.25 6.25 6.25
Power for Scheffe 0.56 0.85 0.06 0.86The complex contrast has the largest noncentrality parameter and so the largest power. Bonferroni’s critical value has risen again with , and Scheffé is now the better choice — and it would be the only legitimate choice had these four contrasts been chosen after looking at the data.
Complete, minimal, and proportional power
Every power value above is individual power: the probability that one particular contrast reaches significance. Section 5.11 also distinguishes complete power (all false null hypotheses rejected, also called all-pairs power), minimal power (at least one rejected, also called any-pairs power), and proportional power (the expected proportion of false null hypotheses rejected). Complete and minimal power have no textbook closed form, so the book obtains them by simulation, and Table 5.21 reports the results of 10,000 replications of a SAS macro.
The scenario is a Solomon four-group design with 30 participants per condition, population means of 54, 54, 46, and 46, a population standard deviation of 10, and Tukey’s method controlling at .05. Two of the six pairwise comparisons are true null hypotheses; power concerns only the other four.
The book does not print code for this table, and no function in base R or in DMAR computes these three quantities, so this is a place where following along means building the calculation. We do it twice: first by simulation, the way the book does, and then exactly, which this particular design turns out to allow. The second route is what makes the first one interpretable.
HideShow
set.seed(113)
n_replications <- 10000 # the same number of replications the book used
n_per_group <- 30
mu_solomon <- c(54, 54, 46, 46)
sigma_solomon <- 10
group_solomon <- factor(rep(1:4, each = n_per_group))
# The four false null hypotheses are the high-versus-low pairs.
false_nulls <- c("3-1", "4-1", "3-2", "4-2")
simulation <- t(replicate(n_replications, {
y <- rnorm(length(group_solomon),
mean = rep(mu_solomon, each = n_per_group),
sd = sigma_solomon)
p_adjusted <- TukeyHSD(aov(y ~ group_solomon))$group_solomon[, "p adj"]
rejected <- p_adjusted[false_nulls] < 0.05
c(complete = all(rejected), minimal = any(rejected),
proportional = mean(rejected))
}))
estimates <- colMeans(simulation)
round(estimates, 4)HideShow
complete minimal proportional
0.3620 0.9333 0.6824 HideShow
#> .3620, .9333, .6824; Table 5.21 prints .35930, .94620, and .68773,
#> book p. 256
# A simulated power is an estimate, so it has a standard error. Report it.
# Complete and minimal power are proportions, so theirs is the binomial
# sqrt(p(1 - p)/reps). Proportional power is not a proportion: each replication
# contributes 0, .25, .5, .75 or 1, so the binomial formula overstates it by
# half. The column standard deviation is right for all three.
round(apply(simulation, 2, sd) / sqrt(n_replications), 4)HideShow
complete minimal proportional
0.0048 0.0025 0.0030 Table 5.21 reports .35930 for complete power with a simulation interval of .350 to .369, .94620 for minimal power with an interval of .942 to .951, and .68773 for proportional power with an interval of .682 to .694 (book p. 256). Complete power and proportional power from the run above fall inside those intervals. Minimal power does not: it comes out about .013 lower than the book’s figure.
It is tempting to read that as a discrepancy between SAS and R and go looking for a difference in how the Tukey decision is made. The Monte Carlo standard errors printed above suggest a prior question. Both figures are estimates from 10,000 replications, and the standard error of such an estimate is about .0024. How much of the third decimal can one run of this size actually support? The rest of this section answers that, and the answer makes the two published figures easier to understand than any difference in software would.
The same experiment, at a fraction of the cost
Answering that question means running the whole study many times over, and the chunk above is too slow for that: it builds 10,000 data sets of 120 observations and fits 10,000 analyses of variance. Almost none of that work is needed. In a balanced one-way design the group means and are sufficient for every pairwise comparison, so the group means can be drawn straight from their sampling distribution, , and straight from its scaled chi-square distribution, independently of them. Tukey’s rule then rejects a pair when the absolute difference of two drawn means exceeds . It is the same experiment, simulated at the level of its sufficient statistics rather than observation by observation, and it runs about a thousand times faster.
HideShow
# Complete, minimal, and proportional power for a balanced one-way design,
# simulated from the sufficient statistics. The procedure argument supplies
# the critical value on the mean-difference scale, in units of
# sqrt(MS_within / n), so "bonferroni" and "scheffe" are available too.
mcp_power <- function(means, n, sigma, alpha = 0.05,
procedure = c("tukey", "bonferroni", "scheffe", "none"),
reps = 10000) {
procedure <- match.arg(procedure)
a <- length(means)
df_within <- a * (n - 1)
pairs <- combn(a, 2, simplify = FALSE)
false_null <- Filter(function(p) means[p[1]] != means[p[2]], pairs)
true_null <- Filter(function(p) means[p[1]] == means[p[2]], pairs)
critical <- switch(procedure,
tukey = qtukey(1 - alpha, nmeans = a, df = df_within),
bonferroni = sqrt(2) * qt(1 - alpha / (2 * length(pairs)), df = df_within),
scheffe = sqrt(2 * (a - 1) * qf(1 - alpha, df1 = a - 1, df2 = df_within)),
none = sqrt(2) * qt(1 - alpha / 2, df = df_within))
# One row per replication: the group means, plus that run's own MS_within.
means_sim <- matrix(rnorm(reps * a, mean = rep(means, each = reps),
sd = sigma / sqrt(n)),
nrow = reps, ncol = a)
MS_within <- sigma^2 * rchisq(reps, df = df_within) / df_within
threshold <- critical * sqrt(MS_within / n)
n_rejected <- integer(reps)
for (p in false_null)
n_rejected <- n_rejected +
(abs(means_sim[, p[1]] - means_sim[, p[2]]) > threshold)
any_true_rejected <- rep(FALSE, reps)
for (p in true_null)
any_true_rejected <- any_true_rejected |
(abs(means_sim[, p[1]] - means_sim[, p[2]]) > threshold)
estimate <- c(complete = mean(n_rejected == length(false_null)),
minimal = mean(n_rejected > 0),
proportional = mean(n_rejected) / length(false_null),
true_FWE = mean(any_true_rejected))
# If every population mean is the same there is no false null hypothesis to
# reject, so the three power quantities are undefined rather than 1, 0, and
# 0/0. The familywise error rate in the last row is still exactly the
# quantity one wants in that case.
if (length(false_null) == 0)
estimate[c("complete", "minimal", "proportional")] <- NA_real_
# The Monte Carlo standard error of a mean is the standard deviation of the
# quantity being averaged over the square root of the number of replications.
# For complete, minimal and the familywise rate that reduces to the binomial
# formula; for proportional power, which averages 0, .25, .5, .75 and 1, it
# does not, and the binomial formula would overstate it.
mc_se <- c(complete = stats::sd(n_rejected == length(false_null)),
minimal = stats::sd(n_rejected > 0),
proportional = stats::sd(n_rejected / max(1, length(false_null))),
true_FWE = stats::sd(any_true_rejected)) / sqrt(reps)
if (length(false_null) == 0)
mc_se[c("complete", "minimal", "proportional")] <- NA_real_
data.frame(quantity = names(estimate),
estimate = unname(estimate),
MC_SE = unname(mc_se))
}
set.seed(113)
mcp_power(means = mu_solomon, n = n_per_group, sigma = sigma_solomon,
procedure = "tukey", reps = n_replications)HideShow
quantity estimate MC_SE
1 complete 0.37190 0.004833361
2 minimal 0.93790 0.002413492
3 proportional 0.69005 0.003023700
4 true_FWE 0.02090 0.001430568Three sets of numbers are now in hand for one scenario: the book’s .35930, .94620, .68773; the raw-data run’s .3620, .9333, .6824; and this run’s .37190, .93790, .69005. They are three estimates of the same three quantities, differing only in the random stream each used. The fourth row is a bonus the book’s table does not report — the proportion of replications in which at least one of the two true null hypotheses was rejected, about .021, which is Tukey’s method being conservative, as it is designed to be.
The exact answer, for this design
Complete and minimal power have no general closed form, which is why the book simulates. For this scenario they can nonetheless be computed exactly, because the design is balanced and the four population means take only two values. Work in units of the standard error of a group mean, , and condition on , so that the rejection threshold is a fixed number . Condition also on the two low-group means. A high-group mean then rejects against both of them when a single standard normal variable lands below the smaller or above the larger — or, should the two low means happen to lie more than apart, in the gap between them — and it rejects against neither when it lands in the overlap of the two acceptance windows, which is non-empty exactly when the two low means lie closer than . Each window is a difference of two normal distribution functions. The two high-group means are independent and carry the same two probabilities, so the chance that all four false null hypotheses are rejected is the first probability squared, and one minus the square of the second is minimal power. Averaging over the two low-group means and over leaves a three-fold integral, and because each integrand is smooth and bounded, an equal-probability grid evaluates it to more decimal places than anyone needs.
HideShow
# Exact complete, minimal, and proportional power by quadrature, for the
# pattern of this scenario: four groups, two at a high mean and two at a low
# one, balanced, Tukey at alpha. No simulation and so no Monte Carlo error;
# the only error is the grid's, which is past the fourth decimal here.
exact_mcp <- function(means, n, sigma, alpha = 0.05,
n_grid_MS = 400, n_grid_mean = 320) {
a <- length(means)
stopifnot(a == 4, length(unique(means)) == 2,
sum(means == max(means)) == 2)
df_within <- a * (n - 1)
SE_mean <- sigma / sqrt(n)
critical <- qtukey(1 - alpha, nmeans = a, df = df_within)
# MS_within / sigma^2 on an equal-probability grid of its chi-square law,
# turned into the Tukey threshold in units of the standard error of a mean.
MS_scaled <- qchisq((seq_len(n_grid_MS) - 0.5) / n_grid_MS,
df = df_within) / df_within
tau <- critical * sqrt(MS_scaled)
delta <- diff(range(means)) / SE_mean # mean separation, same units
# The two low-group means, also on an equal-probability grid.
z <- qnorm((seq_len(n_grid_mean) - 0.5) / n_grid_mean)
upper <- outer(z, z, pmax)
lower <- outer(z, z, pmin)
by_threshold <- vapply(tau, function(t) {
hi <- t - delta # how far a high-group mean must sit above the larger one
lo <- t + delta # and how far below the smaller one, to reject
# The window strictly between the two low means and the window in which
# neither comparison rejects are the two signs of one difference: whichever
# is non-empty, the other is not.
between <- pnorm(upper - lo) - pnorm(lower + hi)
reject_both <- pnorm(lower - lo) + pnorm(upper + hi, lower.tail = FALSE) +
pmax(0, between)
reject_neither <- pmax(0, -between)
c(complete = mean(reject_both^2),
nothing = mean(reject_neither^2),
proportional = pnorm(hi / sqrt(2), lower.tail = FALSE) +
pnorm(-lo / sqrt(2)),
true_FWE = 1 - (1 - 2 * pnorm(t / sqrt(2), lower.tail = FALSE))^2)
}, numeric(4))
c(complete = mean(by_threshold["complete", ]),
minimal = 1 - mean(by_threshold["nothing", ]),
proportional = mean(by_threshold["proportional", ]),
true_FWE = mean(by_threshold["true_FWE", ]))
}
exact_solomon <- exact_mcp(means = mu_solomon, n = n_per_group,
sigma = sigma_solomon)
round(exact_solomon, 4)HideShow
complete minimal proportional true_FWE
0.3655 0.9405 0.6880 0.0205 Proportional power needs no grid at all, and it is worth seeing why. It is the expected proportion of the four false null hypotheses rejected, which by linearity of expectation is just the individual power of one such comparison. A single mean difference divided by its estimated standard error follows a noncentral distribution, and Tukey’s rule compares it to , so one line of base R settles it.
HideShow
[1] 0.6880257What one simulation run can support
With exact values in hand the earlier numbers can be placed. The exact minimal power is .9405. The book’s run returned .94620, which is 2.4 Monte Carlo standard errors above it — far enough that Table 5.21’s own interval for minimal power, (.942, .951), does not reach down to the value it brackets. That is not a flaw in the table: an interval of that kind misses about one time in twenty, and this is one of those times. The raw-data run at the top of this section returned .9333, which is 3.0 standard errors below the exact value and well out in the other tail — a run lands that low only about once in several hundred tries — but a tail is part of a distribution, not evidence of a different method. The way to see the distribution both figures come from is to run the whole 10,000-replication study over and over, which the sufficient-statistic version makes cheap enough to do on this page.
HideShow
set.seed(113)
# Two thousand independent repetitions of the book's 10,000-replication study:
# 20 million simulated experiments in a few seconds.
repeat_runs <- t(replicate(2000, {
mcp_power(means = mu_solomon, n = n_per_group, sigma = sigma_solomon,
procedure = "tukey", reps = n_replications)$estimate
}))
colnames(repeat_runs) <- c("complete", "minimal", "proportional", "true_FWE")
round(rbind(mean = colMeans(repeat_runs),
SD = apply(repeat_runs, 2, sd),
min = apply(repeat_runs, 2, min),
max = apply(repeat_runs, 2, max)), 5)HideShow
complete minimal proportional true_FWE
mean 0.36559 0.94050 0.68808 0.02052
SD 0.00480 0.00233 0.00296 0.00143
min 0.35020 0.93310 0.67672 0.01550
max 0.38190 0.94790 0.69900 0.02480The means recover the exact values and the standard deviations are the Monte Carlo standard errors the formula predicts, so the simulation is doing exactly what it should. What it cannot do is pin the third decimal. The 2,000 runs of minimal power range from .9331 to .9479, and that range contains both published figures: the book’s .94620 and this page’s .9333 are both inside the spread of honest runs. Neither is evidence of anything but the random stream behind it.
HideShow
ggplot(data.frame(minimal = repeat_runs[, "minimal"]), aes(x = minimal)) +
geom_histogram(binwidth = 0.001, fill = "grey80", colour = "white") +
geom_vline(xintercept = exact_solomon[["minimal"]], linewidth = 1,
colour = "#0C2340") +
geom_vline(xintercept = 0.94620, linetype = "dashed", colour = "#0C2340") +
geom_vline(xintercept = estimates[["minimal"]], linetype = "dotted",
colour = "#0C2340") +
annotate("text", x = exact_solomon[["minimal"]], y = 410, hjust = 1.1,
label = "Exact .9405", colour = "#0C2340") +
annotate("text", x = 0.94620, y = 330, hjust = 1.1,
label = "Table 5.21 .94620", colour = "#0C2340") +
annotate("text", x = estimates[["minimal"]], y = 330, hjust = -0.1,
label = "Simulation above .9333", colour = "#0C2340") +
labs(x = "Estimated minimal (any-pairs) power per 10,000-replication run",
y = "Number of runs",
title = "Two thousand runs of the same 10,000-replication simulation")One more number makes the point concrete. To pin minimal power to within .001 with 95% confidence takes about 215,000 replications, not 10,000.
HideShow
[1] 214968Two habits follow. Report a Monte Carlo standard error, or an interval, with any simulated power, as Table 5.21 does and as the first chunk of this section does; a power quoted to five decimals from 10,000 replications states several digits more than the run behind it can support. And where the design admits an exact calculation, prefer it: the quadrature above takes a few seconds, has no standard error at all, and is what tells us that both published figures are ordinary draws.
Is 30 per group enough?
The practical reading is the book’s. Thirty participants per
condition give a high probability of finding something — a
statistically significant pairwise difference somewhere — and only about
a one-in-three chance of finding everything that is really
there, which is a recipe for apparent failures to replicate. The book
adds that PASS puts the sample size needed for all-pairs power of .80 at
52 per group (p. 257). The exact calculation can check that figure
directly, since exact_mcp() takes as an argument.
HideShow
50 51 52 53 54
0.771 0.785 0.799 0.812 0.824 A reader who wants the smallest whose exact all-pairs power reaches .80 on the nose would take 53. The difference is immaterial for planning, and it is the same lesson once more: PASS reaches 52 by simulation, so its answer carries a simulation error of its own.
How to Cite This Page
The book. Maxwell, S. E., Delaney, H. D., & Kelley, K. (2027). Designing Experiments and Analyzing Data: A Model Comparison Perspective (4th ed.). New York, NY: Routledge.
This page. Maxwell, S. E., Delaney, H. D., & Kelley, K. (2027). Chapter 5: Testing Several Contrasts: The Multiple-Comparisons Problem (R code and instructions). Companion materials for Designing Experiments and Analyzing Data: A Model Comparison Perspective (4th ed.). https://DesigningExperiments.com
These examples use DMAR 1.2.0. The printed book predates DMAR.
References
Arel-Bundock V (2026). modelsummary: Summary Tables and Plots for Statistical Models and Data: Beautiful, Customizable, and Publication-Ready. doi:10.32614/CRAN.package.modelsummary https://doi.org/10.32614/CRAN.package.modelsummary. R package version 2.6.0, https://CRAN.R-project.org/package=modelsummary.
Hothorn T, Bretz F, Westfall P (2026). multcomp: Simultaneous Inference in General Parametric Models. doi:10.32614/CRAN.package.multcomp https://doi.org/10.32614/CRAN.package.multcomp. R package version 1.4-31, https://CRAN.R-project.org/package=multcomp.
Iannone R, Cheng J, Schloerke B, Haughton S, Hughes E, Lauer A, François R, Seo J, Brevoort K, Roy O (2026). gt: Easily Create Presentation-Ready Display Tables. doi:10.32614/CRAN.package.gt https://doi.org/10.32614/CRAN.package.gt. R package version 1.3.0, https://CRAN.R-project.org/package=gt.
Kelley K (2026). DMAR: Design, Measurement, and Analysis in R (DMAR). R package version 1.2.0, https://kenkelley.org.
Maxwell S, Delaney H, Kelley K (2026). AMCP: Data Sets to Accompany Designing Experiments and Analyzing Data: A Model Comparison Perspective (Maxwell, Delaney, and Kelley, 2027, 4th Edition). doi:10.32614/CRAN.package.AMCP https://doi.org/10.32614/CRAN.package.AMCP. R package version 2.0.0, https://CRAN.R-project.org/package=AMCP.
Maxwell, S. E., Delaney, H. D., & Kelley, K. (2027). Designing Experiments and Analyzing Data: A Model Comparison Perspective (4th ed.). New York, NY: Routledge.
R Core Team (2026). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. doi:10.32614/R.manuals https://doi.org/10.32614/R.manuals. https://www.R-project.org/.
Wickham H, Chang W, Henry L, Pedersen T, Takahashi K, Wilke C, Woo K, Yutani H, Dunnington D, van den Brand T (2026). ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. doi:10.32614/CRAN.package.ggplot2 https://doi.org/10.32614/CRAN.package.ggplot2. R package version 4.0.3, https://CRAN.R-project.org/package=ggplot2.
Wickham H, François R, Henry L, Müller K, Vaughan D (2026). dplyr: A Grammar of Data Manipulation. doi:10.32614/CRAN.package.dplyr https://doi.org/10.32614/CRAN.package.dplyr. R package version 1.2.1, https://CRAN.R-project.org/package=dplyr.