Examples follow the book’s section numbers.
Chapter 1 covers the R groundwork these notes take for granted:
installing packages from CRAN, loading them, and the way the book’s data
sets are named. This chapter draws on one of them,
chapter_8_table_12, the blood pressure data of Table 8.12.
The population means of Section 8.3 and the abstract cell means of
Figures 8.2 through 8.5 are not data sets at all — they are entered here
directly from the printed tables and figures.
data(package = "AMCP") lists every data set in the
package.
# Load all of the packages used.
library(tidyverse) # data wrangling and plotting (dplyr, ggplot2, and friends)
library(AMCP) # the textbook's data sets
library(DMAR) # design, measurement, and analysis
library(broom) # tidy model output into data frames
library(effectsize) # effect sizes (eta-squared, omega-squared)
library(emmeans) # estimated marginal means and contrasts
library(gt) # polished HTML tables
library(gtsummary) # publication-ready regression / comparison tables
library(modelsummary) # side-by-side model-comparison tablesChapter 8 introduces higher-order between-subjects factorial designs, centering on the three-way ANOVA and the concept of a three-way interaction. A three-way interaction tests whether the nature of a two-way interaction changes across the levels of a third factor. When this interaction is statistically significant, it is a warning that interpreting overall main effects or two-way interactions in isolation can be misleading, because the outcome depends on the specific combination of all three variables. To unpack these conditional relationships, the omnibus test is decomposed into simple two-way interactions, then simple main effects, then comparisons of individual cell means. The underlying mathematics scales to any number of factors, but the chapter notes that designs with more than three factors are uncommon in the behavioral sciences, both because of sample-size requirements and because very high-order interactions are not terribly informative to describe.
The chapter begins with a 2 2 2 version of the hypertension study of Chapter 7, adding diet therapy as a third factor. The eight values in Table 8.1 are population means, so there is no sampling error: any nonzero difference is a true effect, and tests of statistical significance are beside the point. Following the book, the drug factor is A, biofeedback is B, and diet is C.
# Table 8.1 population means (blood pressure), entered from the book.
pop_8_1 <- expand.grid(
Drug = c("Present", "Absent"),
Biofeedback = c("Present", "Absent"),
Diet = c("Absent", "Present"),
stringsAsFactors = FALSE
) |>
mutate(
Drug = factor(Drug, levels = c("Present", "Absent")),
Biofeedback = factor(Biofeedback, levels = c("Present", "Absent")),
Diet = factor(Diet, levels = c("Absent", "Present")),
BP = c(180, 200, 205, 210, # diet absent: (DrugP/BioP, DrugA/BioP, DrugP/BioA, DrugA/BioA)
170, 185, 190, 190) # diet present: same order
)
pop_8_1 Drug Biofeedback Diet BP
1 Present Present Absent 180
2 Absent Present Absent 200
3 Present Absent Absent 205
4 Absent Absent Absent 210
5 Present Present Present 170
6 Absent Present Present 185
7 Present Absent Present 190
8 Absent Absent Present 190
A three-way interaction means that the two-way A B effect is not the same at every level of C. For these population means, that question becomes: is the Drug Biofeedback interaction the same when diet is absent as when diet is present? Table 8.5 lays the two 2 2 tables side by side and takes the difference of differences.
# The drug effect (absent minus present) within each Biofeedback x Diet combination,
# then the difference between those drug effects: the two-way interaction at each
# level of Diet. This is the arithmetic of Table 8.5.
drug_effects <- pop_8_1 |>
pivot_wider(names_from = Drug, values_from = BP, names_prefix = "Drug ") |>
mutate(`Drug effect` = `Drug Absent` - `Drug Present`)
drug_effects# A tibble: 4 × 5
Biofeedback Diet `Drug Present` `Drug Absent` `Drug effect`
<fct> <fct> <dbl> <dbl> <dbl>
1 Present Absent 180 200 20
2 Absent Absent 205 210 5
3 Present Present 170 185 15
4 Absent Present 190 190 0
#> drug effects 20, 5 (diet absent) and 15, 0 (diet present) match book Table 8.5, p. 391
drug_effects |>
group_by(Diet) |>
summarize(
`Drug x Biofeedback interaction` =
`Drug effect`[Biofeedback == "Present"] - `Drug effect`[Biofeedback == "Absent"],
.groups = "drop"
)# A tibble: 2 × 2
Diet `Drug x Biofeedback interaction`
<fct> <dbl>
1 Absent 15
2 Present 15
#> 15 and 15 — matches book p. 391: the two-way interaction is 15 at both levels
#> of diet, so the three-way interaction is exactly zeroWhen diet is absent the drug lowers blood pressure 20 points with biofeedback and 5 points without it, a difference of 15. When diet is present the corresponding drug effects are 15 and 0, again a difference of 15. Because the two-way interaction has the same value at both levels of diet, the three-way interaction is zero in this population.
Figure 8.1 plots the same eight means, with drug condition on the horizontal axis, a line for each biofeedback condition, and one panel per diet condition.
ggplot(pop_8_1, aes(Drug, BP, group = Biofeedback, color = Biofeedback)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
facet_wrap(~ Diet, labeller = labeller(
Diet = c(Absent = "(a) Diet absent", Present = "(b) Diet present"))) +
scale_color_brewer(palette = "Dark2") +
scale_y_continuous(limits = c(160, 220), breaks = seq(170, 210, by = 10)) +
labs(title = "Figure 8.1. Cell means by diet condition (no three-way interaction)",
x = "Drug", y = "Population mean blood pressure", color = "Biofeedback") +
theme(legend.position = "top")Figure 8.1. Plots of the Table 8.1 cell means as a function of diet condition: (a) diet absent and (b) diet present.
In both panels the lines are non-parallel, so biofeedback and drug interact at each level of diet. The book formalizes the correspondence between the two panels with four simple effects: and are the biofeedback effects when diet is absent (drug present and drug absent, respectively), and and are the same two effects when diet is present.
# The four simple effects marked by brackets in Figure 8.1: the biofeedback
# effect (absent minus present) within each Drug x Diet combination.
deltas <- pop_8_1 |>
pivot_wider(names_from = Biofeedback, values_from = BP,
names_prefix = "Biofeedback ") |>
mutate(`Biofeedback effect` = `Biofeedback Absent` - `Biofeedback Present`) |>
dplyr::select(Diet, Drug, `Biofeedback effect`)
deltas# A tibble: 4 × 3
Diet Drug `Biofeedback effect`
<fct> <fct> <dbl>
1 Absent Present 25
2 Absent Absent 10
3 Present Present 20
4 Present Absent 5
#> 25, 10, 20, 5 — matches book p. 392: Delta_1 = 25 (205 - 180), Delta_2 = 10
#> (210 - 200), Delta_3 = 20 (190 - 170), Delta_4 = 5 (190 - 185)
deltas |>
group_by(Diet) |>
summarize(`Delta difference` = `Biofeedback effect`[Drug == "Present"] -
`Biofeedback effect`[Drug == "Absent"],
.groups = "drop")# A tibble: 2 × 2
Diet `Delta difference`
<fct> <dbl>
1 Absent 15
2 Present 15
Underneath each panel of Figure 8.1 the book prints the parameters of two separate two-way full models, one per diet condition, using Equations 7.9–7.12. Those parameters make the equivalence of the two panels exact.
# Equations 7.9-7.12 applied separately within each level of diet: the grand
# mean, the two sets of main-effect parameters, and the four interaction
# parameters of a 2 x 2 full model.
two_way_parameters <- function(cells) {
mu <- mean(cells$BP) # grand mean
alpha <- tapply(cells$BP, cells$Drug, mean) - mu # drug (A)
beta <- tapply(cells$BP, cells$Biofeedback, mean) - mu # biofeedback (B)
alphabeta <- matrix(NA_real_, nrow = length(alpha), ncol = length(beta),
dimnames = list(Drug = names(alpha), Biofeedback = names(beta)))
for (j in names(alpha)) {
for (k in names(beta)) {
cell <- cells$BP[cells$Drug == j & cells$Biofeedback == k]
alphabeta[j, k] <- cell - (mu + alpha[j] + beta[k])
}
}
list(mu = mu, alpha = alpha, beta = beta, alphabeta = alphabeta)
}
params_absent <- two_way_parameters(filter(pop_8_1, Diet == "Absent"))
params_present <- two_way_parameters(filter(pop_8_1, Diet == "Present"))
params_absent$mu
[1] 198.75
$alpha
Present Absent
-6.25 6.25
$beta
Present Absent
-8.75 8.75
$alphabeta
Biofeedback
Drug Present Absent
Present -3.75 3.75
Absent 3.75 -3.75
#> mu = 198.75, alpha = -6.25 / 6.25, beta = -8.75 / 8.75,
#> alphabeta = -3.75, 3.75, 3.75, -3.75 — matches book Figure 8.1(a), p. 393
params_present$mu
[1] 183.75
$alpha
Present Absent
-3.75 3.75
$beta
Present Absent
-6.25 6.25
$alphabeta
Biofeedback
Drug Present Absent
Present -3.75 3.75
Absent 3.75 -3.75
#> mu = 183.75, alpha = -3.75 / 3.75, beta = -6.25 / 6.25,
#> alphabeta = -3.75, 3.75, 3.75, -3.75 — matches book Figure 8.1(b), p. 393
# The three-way interaction is zero exactly when the two sets of alpha-beta
# parameters agree.
all.equal(params_absent$alphabeta, params_present$alphabeta)[1] TRUE
#> TRUE — matches book p. 393: "each alpha-beta parameter has the same value
#> when diet is absent as when diet is present"The grand means and the main-effect parameters differ between the two panels (blood pressure is simply lower when diet is present), but every interaction parameter is identical. That identity is the absence of a three-way interaction.
Figure 8.1 showed a case with no three-way interaction. To show how a three-way interaction can hide inside an averaged two-way plot, the text works through two small abstract 2 2 2 examples on a generic outcome Y. These cell means are entered directly from the book’s figures; because they are population means, every difference shown is a true effect.
At the first level of C the A B lines cross, so A and B interact strongly. At the second level of C the two lines are flat and parallel, so A and B do not interact there. Because the A B interaction is present at C but absent at C, the two two-way interactions cannot be equal, which is exactly what a three-way interaction means.
# Cell means read from the book's Figure 8.2 panels.
ex1 <- expand.grid(A = c("A1", "A2"), B = c("B1", "B2"), C = c("C1", "C2"),
stringsAsFactors = FALSE) |>
mutate(
A = factor(A, levels = c("A1", "A2")),
B = factor(B, levels = c("B1", "B2")),
C = factor(C, levels = c("C1", "C2")),
Y = c(2, 6, 6, 2, # at C1: B1 rises 2 -> 6, B2 falls 6 -> 2 (lines cross)
5, 5, 3, 3) # at C2: B1 flat at 5, B2 flat at 3 (parallel)
)
#> cell means match book Figure 8.2, p. 395
a_labels <- c(A1 = "A₁", A2 = "A₂")
b_labels <- c(B1 = "B₁", B2 = "B₂")
ggplot(ex1, aes(A, Y, group = B, color = B)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
facet_wrap(~ C, labeller = labeller(
C = c(C1 = "(a) At the first level of C", C2 = "(b) At the second level of C"))) +
scale_x_discrete(labels = a_labels) +
scale_color_brewer(palette = "Dark2", labels = b_labels) +
scale_y_continuous(limits = c(1, 7), breaks = 1:7) +
labs(title = "Figure 8.2. A by B at each level of C (interaction at C₁ only)",
x = "Factor A", y = expression("Population cell mean of" ~ italic(Y)),
color = "Factor B") +
theme(legend.position = "top")Figure 8.2. Interaction plots for A and B at (a) the first level of C and (b) the second level of C.
The same example, now collapsed over C: each plotted mean averages the C and C values from Figure 8.2. The lines cross, so there is a non-zero A B interaction on average. But this averaged picture blends a strong interaction at C with no interaction at C. The better summary is that A and B interact at C but not at C, which is why a three-way interaction calls for interpreting simple effects rather than averaged effects.
# A tibble: 4 × 3
A B Y
<fct> <fct> <dbl>
1 A1 B1 3.5
2 A1 B2 4.5
3 A2 B1 5.5
4 A2 B2 2.5
#> 3.5, 5.5 (B1) and 4.5, 2.5 (B2) — matches book Figure 8.3, p. 396
ggplot(ex1_avg, aes(A, Y, group = B, color = B)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
scale_x_discrete(labels = a_labels) +
scale_color_brewer(palette = "Dark2", labels = b_labels) +
scale_y_continuous(limits = c(2, 6), breaks = seq(2, 6, by = 0.5)) +
labs(title = "Figure 8.3. A by B averaging over C (interaction survives the average)",
x = "Factor A", y = expression("Population marginal mean of" ~ italic(Y)),
color = "Factor B") +
theme(legend.position = "top")Figure 8.3. Interaction plot for A and B averaging over the levels of C (first example).
A second, unrelated abstract 2 2 2 example. Here A and B interact at both levels of C, but in opposite directions: at C the B line rises while B stays flat, and at C the B line falls while B stays flat. The two two-way interactions are equal in size and opposite in sign, so they cancel when averaged.
# Cell means read from the book's Figure 8.4 panels.
ex2 <- expand.grid(A = c("A1", "A2"), B = c("B1", "B2"), C = c("C1", "C2"),
stringsAsFactors = FALSE) |>
mutate(
A = factor(A, levels = c("A1", "A2")),
B = factor(B, levels = c("B1", "B2")),
C = factor(C, levels = c("C1", "C2")),
Y = c(2, 6, 5, 5, # at C1: B1 rises 2 -> 6, B2 flat at 5
6, 2, 3, 3) # at C2: B1 falls 6 -> 2, B2 flat at 3
)
#> cell means match book Figure 8.4, p. 397
ggplot(ex2, aes(A, Y, group = B, color = B)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
facet_wrap(~ C, labeller = labeller(
C = c(C1 = "(a) At the first level of C", C2 = "(b) At the second level of C"))) +
scale_x_discrete(labels = a_labels) +
scale_color_brewer(palette = "Dark2", labels = b_labels) +
scale_y_continuous(limits = c(1, 7), breaks = 1:7) +
labs(title = "Figure 8.4. A by B at each level of C (opposite interactions)",
x = "Factor A", y = expression("Population cell mean of" ~ italic(Y)),
color = "Factor B") +
theme(legend.position = "top")Figure 8.4. Interaction plots for A and B at (a) the first level of C and (b) the second level of C (second example).
Averaging the second example over C gives a population mean of 4 for every combination of A and B, so the B and B lines lie exactly on top of one another. After averaging there is no A main effect, no B main effect, and no A B interaction. Yet A and B interact at both levels of C; the two interactions simply cancel. Reading the averaged plot as “A and B do not interact” would obscure the real structure of the data, which is the central caution of the chapter.
# A tibble: 4 × 3
A B Y
<fct> <fct> <dbl>
1 A1 B1 4
2 A1 B2 4
3 A2 B1 4
4 A2 B2 4
#> all four means equal 4 — matches book p. 396, where the text gives the four
#> averaged cell means; the plot of them is Figure 8.5, p. 398
# The A1 - A2 difference within each B x C combination, which the averaged plot hides.
ex2 |>
pivot_wider(names_from = A, values_from = Y) |>
mutate(`A1 - A2` = A1 - A2) |>
select(B, C, `A1 - A2`)# A tibble: 4 × 3
B C `A1 - A2`
<fct> <fct> <dbl>
1 B1 C1 -4
2 B2 C1 0
3 B1 C2 4
4 B2 C2 0
#> -4, 0, 4, 0 for B1C1, B2C1, B1C2, B2C2 — matches book p. 397
ggplot(ex2_avg, aes(A, Y, group = B, color = B)) +
# Nudge the second line slightly so the coincident lines are both visible.
geom_line(aes(linetype = B),
linewidth = 1,
position = position_dodge(width = 0.04)) +
geom_point(aes(shape = B), size = 3,
position = position_dodge(width = 0.04)) +
scale_x_discrete(labels = a_labels) +
scale_color_brewer(palette = "Dark2", labels = b_labels) +
scale_linetype_discrete(labels = b_labels) +
scale_shape_discrete(labels = b_labels) +
scale_y_continuous(limits = c(2, 4.5), breaks = seq(2, 4.5, by = 0.5)) +
labs(title = "Figure 8.5. A by B averaging over C (interactions cancel)",
x = "Factor A", y = expression("Population marginal mean of" ~ italic(Y)),
color = "Factor B", linetype = "Factor B", shape = "Factor B") +
theme(legend.position = "top")Figure 8.5. Interaction plot for A and B averaging over C (second example): the two lines coincide.
The numerical example extends the Chapter 7 hypertension data by adding a diet factor: biofeedback (A, two levels), drug therapy (B, three levels), and diet (C, two levels), a 2 3 2 design with per cell and . The data for the diet-absent condition are the same scores used in Chapter 7.
'data.frame': 72 obs. of 4 variables:
$ BP : int 170 175 165 180 160 158 161 173 157 152 ...
$ Drug : int 1 1 1 1 1 1 1 1 1 1 ...
$ Biofeedback: int 2 2 2 2 2 2 2 2 2 2 ...
$ Diet : int 1 1 1 1 1 1 2 2 2 2 ...
# Spell out each numeric code, then set the level order to match the book's tables.
bp <- chapter_8_table_12 |>
as_tibble() |>
mutate(
Biofeedback = case_when(
Biofeedback == 1 ~ "Absent",
Biofeedback == 2 ~ "Present"
),
Drug = case_when(
Drug == 1 ~ "X",
Drug == 2 ~ "Y",
Drug == 3 ~ "Z"
),
Diet = case_when(
Diet == 1 ~ "Absent",
Diet == 2 ~ "Present"
)
) |>
mutate(
Biofeedback = factor(Biofeedback, levels = c("Present", "Absent")),
Drug = factor(Drug, levels = c("X", "Y", "Z")),
Diet = factor(Diet, levels = c("Absent", "Present"))
)
head(bp, 10)# A tibble: 10 × 4
BP Drug Biofeedback Diet
<int> <fct> <fct> <fct>
1 170 X Present Absent
2 175 X Present Absent
3 165 X Present Absent
4 180 X Present Absent
5 160 X Present Absent
6 158 X Present Absent
7 161 X Present Present
8 173 X Present Present
9 157 X Present Present
10 152 X Present Present
Table 8.13 reports the cell means, arranged as two 2 3 tables, one per diet condition.
# Cell means and standard deviations (the book's Table 8.13 shows the means).
bp |>
group_by(Diet, Biofeedback, Drug) |>
summarise(mean_BP = mean(BP), sd_BP = sd(BP), n = n(), .groups = "drop")# A tibble: 12 × 6
Diet Biofeedback Drug mean_BP sd_BP n
<fct> <fct> <fct> <dbl> <dbl> <int>
1 Absent Present X 168 8.60 6
2 Absent Present Y 204 12.7 6
3 Absent Present Z 189 12.6 6
4 Absent Absent X 188 10.9 6
5 Absent Absent Y 200 10.1 6
6 Absent Absent Z 209 14.4 6
7 Present Present X 169 14.8 6
8 Present Present Y 172 10.9 6
9 Present Present Z 173 11.7 6
10 Present Absent X 173 9.80 6
11 Present Absent Y 187 14.0 6
12 Present Absent Z 182 17.1 6
#> diet absent 168, 204, 189 / 188, 200, 209
#> diet present 169, 172, 173 / 173, 187, 182 — all match book Table 8.13, p. 405
# The marginal means that Table 8.13 also displays.
bp |> group_by(Diet, Biofeedback) |> summarise(mean_BP = mean(BP), .groups = "drop")# A tibble: 4 × 3
Diet Biofeedback mean_BP
<fct> <fct> <dbl>
1 Absent Present 187
2 Absent Absent 199
3 Present Present 171.
4 Present Absent 181.
#> 187, 199, 171.33, 180.67 — matches book Table 8.13, p. 405
bp |> group_by(Diet, Drug) |> summarise(mean_BP = mean(BP), .groups = "drop")# A tibble: 6 × 3
Diet Drug mean_BP
<fct> <fct> <dbl>
1 Absent X 178
2 Absent Y 202
3 Absent Z 199
4 Present X 171
5 Present Y 180.
6 Present Z 178.
#> 178, 202, 199, 171, 179.5, 177.5 — matches book Table 8.13, p. 405
bp |> group_by(Diet) |> summarise(mean_BP = mean(BP), .groups = "drop")# A tibble: 2 × 2
Diet mean_BP
<fct> <dbl>
1 Absent 193
2 Present 176
The design is balanced, so Type I, Type II, and Type III sums of
squares all agree and base R’s aov() reproduces the book’s
omnibus table directly.
# Three-way omnibus ANOVA (Table 8.14).
ANOVA_3Way <- aov(BP ~ Biofeedback * Drug * Diet, data = bp)
summary(ANOVA_3Way) Df Sum Sq Mean Sq F value Pr(>F)
Biofeedback 1 2048 2048 13.072 0.000615 ***
Drug 2 3675 1837 11.729 5.02e-05 ***
Diet 1 5202 5202 33.204 3.05e-07 ***
Biofeedback:Drug 2 259 129 0.827 0.442457
Biofeedback:Diet 1 32 32 0.204 0.652937
Drug:Diet 2 903 451 2.882 0.063815 .
Biofeedback:Drug:Diet 2 1075 537 3.431 0.038834 *
Residuals 60 9400 157
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> SS 2048, 3675, 5202, 259, 32, 903, 1075, 9400 and F 13.07, 11.73, 33.20,
#> 0.83, 0.20, 2.88, 3.43 — match book Table 8.14, p. 406. The book prints that
#> table's p column to two decimals, so its .440, .650, .060, and .040 are the
#> .4425, .6529, .0638, and .0388 computed here.The same ANOVA as an APA-style table. We build it from
broom::tidy() and format it with gt,
adding partial for each
effect alongside the sums of squares, , mean squares, , and :
aov_tbl <- broom::tidy(ANOVA_3Way)
ss_res <- aov_tbl$sumsq[aov_tbl$term == "Residuals"] # error SS, for partial eta^2
aov_tbl |>
transmute(
Source = term, SS = sumsq, df = df, MS = meansq,
Fstat = statistic, p = p.value,
# Partial eta-squared: each effect's SS over effect-plus-error SS.
peta = if_else(term == "Residuals", NA_real_, sumsq / (sumsq + ss_res))
) |>
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 8.14**. Three-Way Analysis of Variance for Blood Pressure")) |>
cols_align(align = "center", columns = -Source)| Table 8.14. Three-Way Analysis of Variance for Blood Pressure | ||||||
| Source | SS | df | MS | F | p | Partial η2 |
|---|---|---|---|---|---|---|
| Biofeedback | 2,048.00 | 1 | 2,048.00 | 13.07 | 0.0006 | 0.179 |
| Drug | 3,675.00 | 2 | 1,837.50 | 11.73 | 0.0001 | 0.281 |
| Diet | 5,202.00 | 1 | 5,202.00 | 33.20 | 0.0000 | 0.356 |
| Biofeedback:Drug | 259.00 | 2 | 129.50 | 0.83 | 0.4425 | 0.027 |
| Biofeedback:Diet | 32.00 | 1 | 32.00 | 0.20 | 0.6529 | 0.003 |
| Drug:Diet | 903.00 | 2 | 451.50 | 2.88 | 0.0638 | 0.088 |
| Biofeedback:Drug:Diet | 1,075.00 | 2 | 537.50 | 3.43 | 0.0388 | 0.103 |
| Residuals | 9,400.00 | 60 | 156.67 | |||
Table 8.14 is a summary of seven model comparisons. Section 8.4.2 spells out the restricted model for each effect (Table 8.10, p. 401) and Table 8.11 (p. 404) gives the resulting sums of squares and degrees of freedom. Working the comparisons explicitly shows where each line of the ANOVA table comes from: , and
Dropping a main effect from an R formula while its interactions
remain leaves the model spanning the same space, so
update() and anova() would report a comparison
with zero degrees of freedom. We therefore build each restricted model
from the full design matrix, deleting the columns that belong to the
effect being tested. Sum-to-zero coding makes those columns exactly the
parameters the book sets to zero in Table 8.10; in a balanced design the
resulting sums of squares do not depend on the coding.
# One design matrix for the full model, with sum-to-zero coding for all three factors.
X <- model.matrix(~ Biofeedback * Drug * Diet, data = bp,
contrasts.arg = list(Biofeedback = "contr.sum",
Drug = "contr.sum",
Diet = "contr.sum"))
effect_of_column <- attr(X, "assign") # 0 = intercept, 1-7 = the seven omnibus effects
effect_names <- attr(terms(BP ~ Biofeedback * Drug * Diet), "term.labels")
# Error sum of squares for the model that keeps only the named columns.
error_ss <- function(cols) sum(lm.fit(x = X[, cols, drop = FALSE], y = bp$BP)$residuals^2)
all_columns <- seq_len(ncol(X))
E_F <- error_ss(all_columns)
df_F <- nrow(X) - ncol(X)
map_dfr(seq_along(effect_names), function(j) {
dropped <- which(effect_of_column == j)
E_R <- error_ss(setdiff(all_columns, dropped))
df_diff <- length(dropped)
F_stat <- ((E_R - E_F) / df_diff) / (E_F / df_F)
tibble(Effect = effect_names[j],
E_R = E_R, E_F = E_F, SS = E_R - E_F, df = df_diff,
F_stat = F_stat,
p = pf(F_stat, df_diff, df_F, lower.tail = FALSE))
})# A tibble: 7 × 7
Effect E_R E_F SS df F_stat p
<chr> <dbl> <dbl> <dbl> <int> <dbl> <dbl>
1 Biofeedback 11448 9400 2048. 1 13.1 0.000615
2 Drug 13075 9400 3675. 2 11.7 0.0000502
3 Diet 14602 9400 5202. 1 33.2 0.000000305
4 Biofeedback:Drug 9659 9400 259. 2 0.827 0.442
5 Biofeedback:Diet 9432 9400 32.0 1 0.204 0.653
6 Drug:Diet 10303 9400 903. 2 2.88 0.0638
7 Biofeedback:Drug:Diet 10475. 9400 1075. 2 3.43 0.0388
The DMAR
package assembles the same table from a formula and adds, for every
effect, partial and partial
with noncentral- confidence intervals — quantities
aov() does not report.
| effect | SS | df | F_value | p_value | eta_squared_partial | eta_squared_partial_ci_lower | eta_squared_partial_ci_upper | omega_squared_partial | omega_squared_partial_ci_lower | omega_squared_partial_ci_upper |
|---|---|---|---|---|---|---|---|---|---|---|
| Biofeedback | 2048.000 | 1 | 13.072 | 0.0006 | 0.179 | 0.032 | 0.308 | 0.144 | 0.032 | 0.308 |
| Drug | 3675.000 | 2 | 11.729 | < 0.0001 | 0.281 | 0.082 | 0.397 | 0.230 | 0.082 | 0.397 |
| Diet | 5202.000 | 1 | 33.204 | < 0.0001 | 0.356 | 0.148 | 0.468 | 0.309 | 0.148 | 0.468 |
| Biofeedback:Drug | 259.000 | 2 | 0.827 | 0.4425 | 0.027 | 0.000 | 0.111 | 0.000 | 0.000 | 0.111 |
| Biofeedback:Diet | 32.000 | 1 | 0.204 | 0.6529 | 0.003 | 0.000 | 0.072 | 0.000 | 0.000 | 0.072 |
| Drug:Diet | 903.000 | 2 | 2.882 | 0.0638 | 0.088 | 0.000 | 0.200 | 0.050 | 0.000 | 0.200 |
| Biofeedback:Drug:Diet | 1075.000 | 2 | 3.431 | 0.0388 | 0.103 | 0.000 | 0.218 | 0.063 | 0.000 | 0.218 |
| Residuals | 9400.000 | 60 | NA | NA | NA | NA | NA | NA | NA | NA |
Sum of squares: Type III
Confidence level: 95%
All three main effects are highly statistically significant. None of the two-way interactions reaches the .05 level, although Drug Diet just misses it, and the three-way Biofeedback Drug Diet interaction is statistically significant.
The book supplements these tests with partial for each effect (Equation 7.46), treating the “off factors” as extrinsic.
# Partial omega-squared for each effect (Table 8.15).
effectsize::omega_squared(ANOVA_3Way, partial = TRUE)# Effect Size for ANOVA (Type I)
Parameter | Omega2 (partial) | 95% CI
-------------------------------------------------------
Biofeedback | 0.14 | [0.03, 1.00]
Drug | 0.23 | [0.08, 1.00]
Diet | 0.31 | [0.16, 1.00]
Biofeedback:Drug | 0.00 | [0.00, 1.00]
Biofeedback:Diet | 0.00 | [0.00, 1.00]
Drug:Diet | 0.05 | [0.00, 1.00]
Biofeedback:Drug:Diet | 0.06 | [0.00, 1.00]
- One-sided CIs: upper bound fixed at [1.00].
#> A .14, B .23, C .31, AB .00, AC .00, BC .05, ABC .06 — matches book Table 8.15,
#> p. 407. The A x B and A x C values are set to zero because their F values are
#> below 1, exactly as the book notes.A statistically significant three-way interaction means lower-order effects should not be interpreted in isolation: the two-way interactions, and the main effects, are not consistent across levels of the third factor. Judging the “best” drug from its marginal means is misleading here, because the drug’s effectiveness depends on the particular combination of diet and biofeedback. The decomposition in Section 8.5.2 replaces those average effects with conditional ones.
DMAR returns the same per-effect partial from a call that takes a
fitted model directly, and ci_omega_squared() attaches a
noncentral- confidence interval to
each one. We first build a parallel lm() fit of the same
three-way model; the numbers are identical to the aov() fit
above.
fit_full <- lm(BP ~ Biofeedback * Drug * Diet, data = bp)
DMAR::omega_squared(fit_full, estimand = "partial")| effect | omega_squared_partial | F_value | df_effect | df_error | N |
|---|---|---|---|---|---|
| Biofeedback | 0.144 | 13.072 | 1 | 60 | 72 |
| Drug | 0.230 | 11.729 | 2 | 60 | 72 |
| Diet | 0.309 | 33.204 | 1 | 60 | 72 |
| Biofeedback:Drug | 0.000 | 0.827 | 2 | 60 | 72 |
| Biofeedback:Diet | 0.000 | 0.204 | 1 | 60 | 72 |
| Drug:Diet | 0.050 | 2.882 | 2 | 60 | 72 |
| Biofeedback:Drug:Diet | 0.063 | 3.431 | 2 | 60 | 72 |
Sum of squares: Type III
#> .144, .230, .309, .000, .000, .050, .063 — the same values as
#> effectsize::omega_squared() above, and they match book Table 8.15, p. 407
DMAR::ci_omega_squared(fit_full, estimand = "partial", conf_level = 0.95)| effect | omega_squared_partial | omega_squared_partial_ci_lower | omega_squared_partial_ci_upper | F_value | df_effect | df_error | N |
|---|---|---|---|---|---|---|---|
| Biofeedback | 0.144 | 0.032 | 0.308 | 13.072 | 1 | 60 | 72 |
| Drug | 0.230 | 0.082 | 0.397 | 11.729 | 2 | 60 | 72 |
| Diet | 0.309 | 0.148 | 0.468 | 33.204 | 1 | 60 | 72 |
| Biofeedback:Drug | 0.000 | 0.000 | 0.111 | 0.827 | 2 | 60 | 72 |
| Biofeedback:Diet | 0.000 | 0.000 | 0.072 | 0.204 | 1 | 60 | 72 |
| Drug:Diet | 0.050 | 0.000 | 0.200 | 2.882 | 2 | 60 | 72 |
| Biofeedback:Drug:Diet | 0.063 | 0.000 | 0.218 | 3.431 | 2 | 60 | 72 |
Sum of squares: Type III
Confidence level: 95%
#> point estimates again match book Table 8.15, p. 407; the confidence intervals
#> are an addition — the book reports the point estimates onlyLike the book, DMAR clamps a negative estimate to zero, which is what happens whenever .
The lm() fit is the same model written as a regression,
where each coefficient is a deviation from the reference cell
(biofeedback present, Drug X, diet absent). A
modelsummary table lays out the estimates with standard
errors beneath them:
modelsummary(
list("Three-way factorial" = fit_full),
estimate = "{estimate}",
statistic = "({std.error})", # standard errors in parentheses, APA style
gof_map = c("nobs", "aic", "bic", "logLik"),
output = "gt" # gt backend, to match the gtsummary table below
)| Three-way factorial | |
|---|---|
| (Intercept) | 168.000 |
| (5.110) | |
| BiofeedbackAbsent | 20.000 |
| (7.226) | |
| DrugY | 36.000 |
| (7.226) | |
| DrugZ | 21.000 |
| (7.226) | |
| DietPresent | 1.000 |
| (7.226) | |
| BiofeedbackAbsent × DrugY | -24.000 |
| (10.220) | |
| BiofeedbackAbsent × DrugZ | 0.000 |
| (10.220) | |
| BiofeedbackAbsent × DietPresent | -16.000 |
| (10.220) | |
| DrugY × DietPresent | -33.000 |
| (10.220) | |
| DrugZ × DietPresent | -17.000 |
| (10.220) | |
| BiofeedbackAbsent × DrugY × DietPresent | 35.000 |
| (14.453) | |
| BiofeedbackAbsent × DrugZ × DietPresent | 5.000 |
| (14.453) | |
| Num.Obs. | 72 |
| AIC | 581.1 |
| BIC | 610.7 |
| Log.Lik. | -277.548 |
#> the intercept is the Biofeedback present / Drug X / Diet absent cell mean of
#> 168 — matches book Table 8.13, p. 405The same fit as a gtsummary
tbl_regression() table, with the 95% confidence interval
for each coefficient:
| Characteristic | Beta | 95% CI | p-value |
|---|---|---|---|
| (Intercept) | 168 | 158, 178 | <0.001 |
| Biofeedback | |||
| Present | — | — | |
| Absent | 20 | 5.5, 34 | 0.007 |
| Drug | |||
| X | — | — | |
| Y | 36 | 22, 50 | <0.001 |
| Z | 21 | 6.5, 35 | 0.005 |
| Diet | |||
| Absent | — | — | |
| Present | 1.0 | -13, 15 | 0.9 |
| Biofeedback * Drug | |||
| Absent * Y | -24 | -44, -3.6 | 0.022 |
| Absent * Z | 0.00 | -20, 20 | >0.9 |
| Biofeedback * Diet | |||
| Absent * Present | -16 | -36, 4.4 | 0.12 |
| Drug * Diet | |||
| Y * Present | -33 | -53, -13 | 0.002 |
| Z * Present | -17 | -37, 3.4 | 0.10 |
| Biofeedback * Drug * Diet | |||
| Absent * Y * Present | 35 | 6.1, 64 | 0.018 |
| Absent * Z * Present | 5.0 | -24, 34 | 0.7 |
| Abbreviation: CI = Confidence Interval | |||
Figure 8.6 (p. 409) gives a flowchart for analyzing a three-factor design. Since the three-way interaction is statistically significant, the first branch asks whether the Biofeedback Drug interaction is significant at each level of diet.
The obvious way to answer that question is to split the data by diet
and run two separate two-way ANOVAs, but then the error term comes from
only the 30 degrees of freedom inside that half of the design. The book
instead uses the pooled from
the full three-way model, on 60 degrees of freedom, under the assumption
of homogeneity of variance across all twelve cells; p. 410 sets out the
choice between the two error terms explicitly.
emmeans::joint_tests() carries the full model’s error term
into every simple effect, which is what makes it reproduce the book’s
tables.
# Simple interaction tests: Biofeedback x Drug at each level of Diet (Table 8.16).
# These use the pooled error term (MS_W = 156.7, df = 60) from ANOVA_3Way.
joint_tests(ANOVA_3Way, by = "Diet")Diet = Absent:
model term df1 df2 F.ratio p.value
Biofeedback 1 60 8.272 0.0056
Drug 2 60 13.098 <0.0001
Biofeedback:Drug 2 60 3.677 0.0312
Diet = Present:
model term df1 df2 F.ratio p.value
Biofeedback 1 60 5.004 0.0290
Drug 2 60 1.513 0.2286
Biofeedback:Drug 2 60 0.581 0.5625
#> Biofeedback:Drug F = 3.677 when diet is absent and 0.581 when diet is present
#> — matches book Table 8.16, p. 411 (F = 3.68, p = .03; F = 0.58, p = .56)The Biofeedback Drug interaction is significant when diet is absent () but not when diet is present (). That contrast is what the significant three-way interaction was pointing at, and it means the follow-up analysis must take a different path for each diet condition.
Path 1: diet absent (significant simple interaction). Because biofeedback and drug interact when diet is absent, the next step is simple, simple main effects: the biofeedback effect at each level of drug, and the drug effect at each level of biofeedback, with diet held at absent (Table 8.17).
# Simple, simple main effects of Biofeedback within each Drug (diet absent).
joint_tests(ANOVA_3Way, by = c("Diet", "Drug")) |>
filter(Diet == "Absent")Diet = Absent, Drug = X:
model term df1 df2 F.ratio p.value
Biofeedback 1 60 7.660 0.0075
Diet = Absent, Drug = Y:
model term df1 df2 F.ratio p.value
Biofeedback 1 60 0.306 0.5820
Diet = Absent, Drug = Z:
model term df1 df2 F.ratio p.value
Biofeedback 1 60 7.660 0.0075
#> F = 7.660 for Drug X, 0.306 for Drug Y, 7.660 for Drug Z — matches book
#> Table 8.17, p. 411 (7.66, 0.31, 7.66; p = .007, .582, .007)
# Simple, simple main effects of Drug within each Biofeedback level (diet absent).
joint_tests(ANOVA_3Way, by = c("Diet", "Biofeedback")) |>
filter(Diet == "Absent")Diet = Absent, Biofeedback = Present:
model term df1 df2 F.ratio p.value
Drug 2 60 12.523 <0.0001
Diet = Absent, Biofeedback = Absent:
model term df1 df2 F.ratio p.value
Drug 2 60 4.251 0.0188
#> F = 12.523 with biofeedback present and 4.251 with biofeedback absent —
#> matches book Table 8.17, p. 411 (12.52, p < .001; 4.25, p = .019)When diet is absent, drug matters whether or not biofeedback is present, but biofeedback matters only for patients on Drug X or Drug Z.
Biofeedback has only two levels, so no further comparison is needed once its simple, simple main effect is significant. Drug has three levels, so the book compares individual cell means within each biofeedback condition, defining a family as one combination of biofeedback and diet and using Tukey’s HSD to hold at .05 (Table 8.18).
# Pairwise comparisons of drugs within each Biofeedback x Diet combination.
drug_comparisons <- emmeans(ANOVA_3Way, specs = pairwise ~ Drug | Biofeedback * Diet)
drug_comparisons$contrasts |>
as.data.frame() |>
filter(Diet == "Absent") |>
mutate(F_equivalent = t.ratio^2) # for a 1-df test, F = t^2 contrast Biofeedback Diet estimate SE df t.ratio p.value
1 X - Y Present Absent -36 7.226494 60 -4.981669 1.672017e-05
2 X - Z Present Absent -21 7.226494 60 -2.905973 1.398245e-02
3 Y - Z Present Absent 15 7.226494 60 2.075695 1.035313e-01
4 X - Y Absent Absent -12 7.226494 60 -1.660556 2.287808e-01
5 X - Z Absent Absent -21 7.226494 60 -2.905973 1.398245e-02
6 Y - Z Absent Absent -9 7.226494 60 -1.245417 4.314940e-01
F_equivalent
1 24.817021
2 8.444681
3 4.308511
4 2.757447
5 8.444681
6 1.551064
#> F_equivalent 24.82, 8.44, 4.31 (biofeedback present) and 2.76, 8.44, 1.55
#> (biofeedback absent) — matches book Table 8.18, p. 412The book’s decision rule for this family is that an observed must exceed , where is the critical studentized
range for , , and three means (p. 411). We can
check that critical value directly, and emmeans applies the
equivalent Tukey adjustment to the values:
q F_criterion
3.398661 5.775449
Three of the six comparisons clear that criterion: when diet is absent, Drug X differs from Drug Y and from Drug Z with biofeedback present, and Drug X differs from Drug Z with biofeedback absent.
Path 2: diet present (non-significant simple interaction). Because biofeedback and drug do not interact when diet is present, their effects within that diet condition can be read from simple main effects — biofeedback averaging over drug, and drug averaging over biofeedback (Table 8.19).
Diet = Present:
model term df1 df2 F.ratio p.value
Biofeedback 1 60 5.004 0.0290
Drug 2 60 1.513 0.2286
Biofeedback:Drug 2 60 0.581 0.5625
#> Biofeedback F = 5.004, p = .0290 and Drug F = 1.513, p = .2286 — matches book
#> Table 8.19, p. 412 (F = 5.00, p = .029; F = 1.51, p = .229)Drug has no statistically significant effect when diet is present. Biofeedback does, and because biofeedback has only two levels, the marginal means (171.33 with biofeedback, 180.67 without) tell the whole story.
DMAR’s simple_effects_AB() reports a whole family of
simple main effects for a two-factor fit — both A-within-B and
B-within-A — together with partial and its confidence interval for
every row, which joint_tests() does not give. It takes a
two-factor model, so we fit Biofeedback * Drug separately
within each diet condition.
An important consequence: each fit pools error only within its own half of the design, so these tests use on 30 degrees of freedom rather than the 60 of the full model. That is the other error term the book explicitly raises on p. 410 — the one appropriate if heterogeneity of variance across levels of C is suspected — so the values below are deliberately not the ones in Table 8.17, even though the pattern of results is the same.
fit_diet_absent <- lm(BP ~ Biofeedback * Drug, data = filter(bp, Diet == "Absent"))
fit_diet_present <- lm(BP ~ Biofeedback * Drug, data = filter(bp, Diet == "Present"))
# Diet absent: both families of simple main effects, on the subset's own error term.
DMAR::simple_effects_AB(fit_diet_absent)| effect | focal_factor | conditioning_factor | conditioning_level | F_value | df_effect | df_error | p_value | p_adjusted | eta_squared_partial | ci_lower | ci_upper | n_at_level |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Biofeedback | Drug = X | Biofeedback | Drug | X | 8.785 | 1 | 30 | 0.0059 | 0.0059 | 0.227 | 0.019 | 0.414 | 12 |
| Biofeedback | Drug = Y | Biofeedback | Drug | Y | 0.351 | 1 | 30 | 0.5578 | 0.5578 | 0.012 | 0.000 | 0.152 | 12 |
| Biofeedback | Drug = Z | Biofeedback | Drug | Z | 8.785 | 1 | 30 | 0.0059 | 0.0059 | 0.227 | 0.019 | 0.414 | 12 |
| Drug | Biofeedback = Present | Drug | Biofeedback | Present | 14.363 | 2 | 30 | < 0.0001 | < 0.0001 | 0.489 | 0.180 | 0.618 | 18 |
| Drug | Biofeedback = Absent | Drug | Biofeedback | Absent | 4.876 | 2 | 30 | 0.0147 | 0.0147 | 0.245 | 0.010 | 0.419 | 18 |
Confidence level: 95%
Pooled-error partial eta squared with a noncentral-F interval.
#> df_error = 30, not the 60 of book Table 8.17, so the F values are larger
#> (8.785, 0.351, 8.785, 14.363, 4.876). The pattern is the book's: biofeedback
#> matters within Drug X and Drug Z but not Drug Y, and drug matters within both
#> biofeedback conditions.
# Diet present: the same family, where nothing reaches significance.
DMAR::simple_effects_AB(fit_diet_present)| effect | focal_factor | conditioning_factor | conditioning_level | F_value | df_effect | df_error | p_value | p_adjusted | eta_squared_partial | ci_lower | ci_upper | n_at_level |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Biofeedback | Drug = X | Biofeedback | Drug | X | 0.272 | 1 | 30 | 0.6061 | 0.6061 | 0.009 | 0.000 | 0.144 | 12 |
| Biofeedback | Drug = Y | Biofeedback | Drug | Y | 3.819 | 1 | 30 | 0.0600 | 0.0600 | 0.113 | 0.000 | 0.303 | 12 |
| Biofeedback | Drug = Z | Biofeedback | Drug | Z | 1.375 | 1 | 30 | 0.2502 | 0.2502 | 0.044 | 0.000 | 0.216 | 12 |
| Drug | Biofeedback = Present | Drug | Biofeedback | Present | 0.147 | 2 | 30 | 0.8638 | 0.8638 | 0.010 | 0.000 | 0.092 | 18 |
| Drug | Biofeedback = Absent | Drug | Biofeedback | Absent | 1.709 | 2 | 30 | 0.1982 | 0.1982 | 0.102 | 0.000 | 0.271 | 18 |
Confidence level: 95%
Pooled-error partial eta squared with a noncentral-F interval.
Comparing the two sets makes the decomposition concrete: when diet is absent the biofeedback effect is large within Drug X and Drug Z and negligible within Drug Y, and drug differences are large within each biofeedback level; when diet is present none of these simple effects is significant. That difference between the two diet conditions is the structural pattern driving the significant three-way interaction.
Table 8.20 repeats the Table 8.13 means and attaches superscripts marking which means within a row or a column differ. The pieces behind those superscripts are the cell means and marginal means already computed, assembled here in the layout the book uses.
# Cell means, diet absent (the top panel of Table 8.20).
bp |>
filter(Diet == "Absent") |>
group_by(Biofeedback, Drug) |>
summarise(Mean = mean(BP), .groups = "drop") |>
pivot_wider(names_from = Drug, values_from = Mean)# A tibble: 2 × 4
Biofeedback X Y Z
<fct> <dbl> <dbl> <dbl>
1 Present 168 204 189
2 Absent 188 200 209
#> 168, 204, 189 / 188, 200, 209 — matches book Table 8.20, p. 413
# Cell means, diet present (the bottom panel).
bp |>
filter(Diet == "Present") |>
group_by(Biofeedback, Drug) |>
summarise(Mean = mean(BP), .groups = "drop") |>
pivot_wider(names_from = Drug, values_from = Mean)# A tibble: 2 × 4
Biofeedback X Y Z
<fct> <dbl> <dbl> <dbl>
1 Present 169 172 173
2 Absent 173 187 182
#> 169, 172, 173 / 173, 187, 182 — matches book Table 8.20, p. 413
# The marginal means that carry the superscripts when diet is present.
emmeans(ANOVA_3Way, ~ Biofeedback | Diet) |>
as.data.frame() |>
dplyr::select(Diet, Biofeedback, emmean) Diet Biofeedback emmean
1 Absent Present 187.0000
2 Absent Absent 199.0000
3 Present Present 171.3333
4 Present Absent 180.6667
#> 187, 199 (diet absent) and 171.33, 180.67 (diet present) — matches book
#> Table 8.20, p. 413
emmeans(ANOVA_3Way, ~ Drug | Diet) |>
as.data.frame() |>
dplyr::select(Diet, Drug, emmean) Diet Drug emmean
1 Absent X 178.0
2 Absent Y 202.0
3 Absent Z 199.0
4 Present X 171.0
5 Present Y 179.5
6 Present Z 177.5
#> 178, 202, 199 (diet absent) and 171, 179.5, 177.5 (diet present) — matches
#> book Table 8.20, p. 413The cell means show what the tests found. When diet is absent, adding biofeedback drops blood pressure substantially for patients on Drug X (188 to 168) and Drug Z (209 to 189) but does little for patients on Drug Y (200 to 204). The marginal means mask that pattern, which is why the superscripts in the diet-absent half of Table 8.20 sit on cell means rather than on marginal means.
Plotting the cell means (Figure 8.7) is often the most direct way to see a three-way interaction: the shape of the two-way interaction changes across the levels of the third factor.
cell_means <- bp |>
group_by(Diet, Biofeedback, Drug) |>
summarise(mean_BP = mean(BP), .groups = "drop")
ggplot(cell_means, aes(Drug, mean_BP, group = Biofeedback, color = Biofeedback)) +
geom_line(linewidth = 1) +
geom_point(size = 3) +
facet_wrap(~ Diet, labeller = labeller(
Diet = c(Absent = "(a) Diet absent", Present = "(b) Diet present"))) +
scale_color_brewer(palette = "Dark2") +
scale_y_continuous(limits = c(165, 210), breaks = seq(165, 210, by = 5)) +
labs(title = "Figure 8.7. Cell means by diet condition (numerical example)",
x = "Drug", y = "Sample mean blood pressure", color = "Biofeedback") +
theme(legend.position = "top")Figure 8.7. Plot of the Table 8.13 cell means as a function of diet condition: (a) diet absent and (b) diet present.
The two panels look very different. When diet is absent the lines cross — a strong Biofeedback Drug interaction. When diet is present they are nearly parallel: biofeedback lowers blood pressure, but the drugs do not differ from one another. That the left panel looks so unlike the right panel is the three-way interaction, and it agrees with the tests of Table 8.16.
Rather than stopping at values, we can form confidence intervals for single-degree-of-freedom effects. For the biofeedback effect within Drug X when diet is absent, the standard error uses the pooled on 60 degrees of freedom and the of the two cells being compared, so from Table 5.17 the interval is
# 95% CIs for the biofeedback effect within each Drug (diet absent).
biofeedback_by_drug <- emmeans(ANOVA_3Way, specs = pairwise ~ Biofeedback | Drug * Diet)
confint(biofeedback_by_drug$contrasts) |>
as.data.frame() |>
filter(Diet == "Absent")Drug = X, Diet = Absent:
contrast estimate SE df lower.CL upper.CL
Present - Absent -20 7.226494 60 -34.45514 -5.544859
Drug = Y, Diet = Absent:
contrast estimate SE df lower.CL upper.CL
Present - Absent 4 7.226494 60 -10.45514 18.455141
Drug = Z, Diet = Absent:
contrast estimate SE df lower.CL upper.CL
Present - Absent -20 7.226494 60 -34.45514 -5.544859
Confidence level used: 0.95
#> Drug X: estimate -20.00, 95% CI [-34.46, -5.54] — matches the limits the book
#> quotes from Table 8.21 on p. 416. The Drug Y (+4.00) and Drug Z (-20.00) rows
#> are the other two intervals of Table 8.21; the book shows them in the table
#> but does not quote their limits in the text.The interval for Drug X is centered on , the difference between the two cell means in Table 8.13, and it excludes zero, consistent with the for that effect in Table 8.17.
The last line of Table 8.21 is the biofeedback effect when diet is present. It is not reported drug by drug, because biofeedback and drug do not interact there, so it averages over drug and is based on 18 observations per mean rather than 6:
biofeedback_marginal <- emmeans(ANOVA_3Way, specs = pairwise ~ Biofeedback | Diet)
confint(biofeedback_marginal$contrasts) |>
as.data.frame() |>
filter(Diet == "Present")Diet = Present:
contrast estimate SE df lower.CL upper.CL
Present - Absent -9.333333 4.172219 60 -17.67901 -0.9876537
Results are averaged over the levels of: Drug
Confidence level used: 0.95
#> estimate -9.33 = 171.33 - 180.67 — matches book p. 416; the limits reproduce
#> the p. 416 formula above
# The same interval, worked straight from the book's expression on p. 416.
-9.333333 + c(-1, 1) * sqrt(4.00) * sqrt((9400 / 60) * (1/18 + 1/18))[1] -17.677770 -0.988896
#> [-17.68, -0.99], the same limits confint() returns above; the book's sqrt(4.00)
#> is a rounded stand-in for t(.975, 60) = 2.0003, which is why it agrees "within
#> rounding error", exactly as the book saysThis interval is much narrower than the three diet-absent intervals, because averaging over drug raises the effective sample size behind each mean from 6 to 18.
These are 95% intervals one at a time. To cover a family of effects, the width uses a multiple-comparison multiplier instead. Table 8.22 does this for the drug comparisons of Table 8.18, with a simultaneous 95% level under Tukey’s HSD:
Biofeedback = Present, Diet = Absent:
contrast estimate SE df lower.CL upper.CL
X - Y -36 7.226494 60 -53.36683 -18.63317
X - Z -21 7.226494 60 -38.36683 -3.63317
Y - Z 15 7.226494 60 -2.36683 32.36683
Biofeedback = Absent, Diet = Absent:
contrast estimate SE df lower.CL upper.CL
X - Y -12 7.226494 60 -29.36683 5.36683
X - Z -21 7.226494 60 -38.36683 -3.63317
Y - Z -9 7.226494 60 -26.36683 8.36683
Confidence level used: 0.95
Conf-level adjustment: tukey method for comparing a family of 3 estimates
All three main effects are significant, so it is tempting to combine the best level of each: biofeedback present, Drug X, and diet present. Table 8.23 gives the marginal means for each main effect and their deviations from the grand mean, which are the estimated effect parameters.
[1] 184.5
#> 184.5 — matches book p. 417
marginal_means <- bind_rows(
as.data.frame(emmeans(ANOVA_3Way, ~ Biofeedback)) |>
transmute(Effect = paste("Biofeedback", tolower(Biofeedback)), emmean),
as.data.frame(emmeans(ANOVA_3Way, ~ Drug)) |>
transmute(Effect = paste("Drug", Drug), emmean),
as.data.frame(emmeans(ANOVA_3Way, ~ Diet)) |>
transmute(Effect = paste("Diet", tolower(Diet)), emmean)
) |>
mutate(`Deviation from grand mean` = emmean - grand_mean)
marginal_means Effect emmean Deviation from grand mean
1 Biofeedback present 179.1667 -5.333333
2 Biofeedback absent 189.8333 5.333333
3 Drug X 174.5000 -10.000000
4 Drug Y 190.7500 6.250000
5 Drug Z 188.2500 3.750000
6 Diet absent 193.0000 8.500000
7 Diet present 176.0000 -8.500000
#> 179.17, 189.83, 174.50, 190.75, 188.25, 193.00, 176.00 with deviations
#> -5.33, 5.33, -10.00, 6.25, 3.75, 8.50, -8.50 — matches book Table 8.23, p. 418If the three therapies combined additively, the cell mean for
biofeedback plus Drug X plus diet would be the grand mean plus the three
main-effect parameters. Equation 8.9 (p. 418) says the actual cell mean
also carries the three two-way interaction parameters and the three-way
parameter. model.tables() extracts all of them from the
fitted model.
effects_table <- model.tables(ANOVA_3Way, type = "effects")
# Main-effect parameters for this particular combination.
alpha_1 <- as.numeric(effects_table$tables$Biofeedback["Present"])
beta_1 <- as.numeric(effects_table$tables$Drug["X"])
gamma_2 <- as.numeric(effects_table$tables$Diet["Present"])
c(alpha_1 = alpha_1, beta_1 = beta_1, gamma_2 = gamma_2) alpha_1 beta_1 gamma_2
-5.333333 -10.000000 -8.500000
#> -5.33, -10.00, -8.50 — matches book p. 418
# What the cell mean would be if the three factors combined additively.
expected_BP <- grand_mean + alpha_1 + beta_1 + gamma_2
expected_BP[1] 160.6667
#> 160.67 — matches book p. 417
# The interaction parameters for the same combination.
ab_11 <- effects_table$tables$`Biofeedback:Drug`["Present", "X"]
ac_12 <- effects_table$tables$`Biofeedback:Diet`["Present", "Present"]
bc_12 <- effects_table$tables$`Drug:Diet`["X", "Present"]
abc_112 <- effects_table$tables$`Biofeedback:Drug:Diet`["Present", "X", "Present"]
c(ab_11 = ab_11, ac_12 = ac_12, bc_12 = bc_12, abc_112 = abc_112) ab_11 ac_12 bc_12 abc_112
-0.6666667 0.6666667 5.0000000 3.3333333
#> -0.67, 0.67, 5.00, 3.33 — matches book p. 418
# Equation 8.9: the full model reproduces the observed cell mean exactly.
actual_BP <- expected_BP + ab_11 + ac_12 + bc_12 + abc_112
actual_BP[1] 169
Had the therapies combined additively, this group’s mean would have been 160.67. The observed mean is 169. Two parameters account for the whole of that 8.33-point gap, because the other two cancel: the Drug X diet combination is 5 points less effective than their separate effects imply — an antagonistic two-way interaction — and the three-way combination costs a further 3.33 points.
# The lowest cell mean in the design, and the diet-present alternatives.
cell_means |> arrange(mean_BP) |> head(4)# A tibble: 4 × 4
Diet Biofeedback Drug mean_BP
<fct> <fct> <fct> <dbl>
1 Absent Present X 168
2 Present Present X 169
3 Present Present Y 172
4 Present Present Z 173
#> 168 for biofeedback present / Drug X / diet absent is the lowest mean in the
#> design — matches book p. 418Because of those interactions, the most effective combination in these data is biofeedback with Drug X and without the diet (mean 168), and combining the diet with Drug Y or Drug Z under biofeedback is only slightly higher. As the book cautions, a clinical choice would also weigh side effects, cost, and compliance; the statistical point is that treatments in combination can behave differently from what their separate effects suggest.
Power in a three-way design works exactly as in earlier chapters: it depends on , the numerator and denominator degrees of freedom, and the noncentrality parameter of a noncentral . Equations 8.10–8.16 (p. 422) give one noncentrality parameter per omnibus effect, each of the form , so seven effects can carry seven different power values. Equation 3.104 adds that , which is why an effect with larger numerator tends to have lower power at the same .
The book works the details through end-of-chapter exercises rather than a numerical illustration, so what follows applies its formulas to this design rather than reproducing a printed value. Take the three-way interaction in this 2 3 2 design, with Cohen’s medium (p. 423) and the study’s own . Base R needs nothing beyond the distribution functions:
N <- 72 # total sample size, 6 per cell in 12 cells
f <- 0.25 # Cohen's medium value of f (p. 423)
df_effect <- 2 # (a - 1)(b - 1)(c - 1) = 1 x 2 x 1
df_error <- N - 2 * 3 * 2 # N - abc
lambda <- N * f^2 # Equation 8.16 / 8.19
lambda[1] 4.5
[1] 3.25
#> 3.25
# Power: the chance of exceeding the central-F critical value when the
# noncentral F with this lambda is the true distribution.
F_crit <- qf(0.95, df1 = df_effect, df2 = df_error)
pf(F_crit, df1 = df_effect, df2 = df_error, ncp = lambda, lower.tail = FALSE)[1] 0.4408624
So a medium three-way interaction would be detected only about 44% of the time at per cell — a concrete version of the chapter’s warning that three-way interactions are often the hardest effect in the design to detect.
ss_power_factorial_anova() does the same calculation
from the design description rather than from degrees of freedom worked
out by hand, and it will also solve for the per cell that reaches a target power —
which the base R call does not do.
# Power at the design actually used: n = 6 per cell.
DMAR::ss_power_factorial_anova(
factor_levels = c(2, 3, 2), # biofeedback, drug, diet
effect_indices = c(1, 2, 3), # the three-way interaction
true_f = 0.25,
alpha_level = 0.05,
specified_n_per_cell = 6
)| term | value |
|---|---|
| specified_n_per_cell | 6 |
| total_N | 72 |
| df_effect | 2 |
| df_error | 60 |
| noncentral_f_parm | 4.500 |
| actual_power | 0.441 |
#> noncentral_f_parm 4.500 and power 0.441 — the same lambda and power as the
#> base R calculation above
# The n per cell needed for .80 power on that same interaction.
DMAR::ss_power_factorial_anova(
factor_levels = c(2, 3, 2),
effect_indices = c(1, 2, 3),
true_f = 0.25,
desired_power = 0.80,
alpha_level = 0.05
)| term | value |
|---|---|
| necessary_n_per_cell | 14 |
| total_N | 168 |
| df_effect | 2 |
| df_error | 156 |
| noncentral_f_parm | 10.500 |
| actual_power | 0.827 |
The logic of the chapter extends to any number of factors. Two
counting results are useful: a completely crossed design with factors has omnibus effects, and the number
of effects of order is (Equation 8.21,
p. 424). That second expression is just a binomial coefficient, so
choose() evaluates it directly.
# Table 8.24: number of effects of each order in a completely crossed design.
tibble(Factors = 2:5) |>
mutate(
`Main effects` = choose(Factors, 1),
`Two-way` = choose(Factors, 2),
`Three-way` = choose(Factors, 3),
`Four-way` = choose(Factors, 4),
`Five-way` = choose(Factors, 5),
`Total omnibus effects` = 2^Factors - 1
) |>
gt() |>
tab_spanner(label = "Interactions",
columns = c(`Two-way`, `Three-way`, `Four-way`, `Five-way`)) |>
tab_header(title = md("**Table 8.24**. Number of Effects of Each Order in a Completely Crossed Factorial Design")) |>
cols_align(align = "center")| Table 8.24. Number of Effects of Each Order in a Completely Crossed Factorial Design | ||||||
| Factors | Main effects |
Interactions
|
Total omnibus effects | |||
|---|---|---|---|---|---|---|
| Two-way | Three-way | Four-way | Five-way | |||
| 2 | 2 | 1 | 0 | 0 | 0 | 3 |
| 3 | 3 | 3 | 1 | 0 | 0 | 7 |
| 4 | 4 | 6 | 4 | 1 | 0 | 15 |
| 5 | 5 | 10 | 10 | 5 | 1 | 31 |
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 8: Higher-Order Between-Subjects Factorial Designs (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.
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.
Ben-Shachar M, Makowski D, Lüdecke D, Patil I, Wiernik B, Thériault R, Waggoner P (2026). effectsize: Indices of Effect Size. doi:10.32614/CRAN.package.effectsize https://doi.org/10.32614/CRAN.package.effectsize. R package version 1.0.3, https://CRAN.R-project.org/package=effectsize.
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.
Lenth R, Piaskowski J (2026). emmeans: Estimated Marginal Means, aka Least-Squares Means. doi:10.32614/CRAN.package.emmeans https://doi.org/10.32614/CRAN.package.emmeans. R package version 2.0.4, https://CRAN.R-project.org/package=emmeans.
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/.
Robinson D, Hayes A, Couch S, Hvitfeldt E (2026). broom: Convert Statistical Objects into Tidy Tibbles. doi:10.32614/CRAN.package.broom https://doi.org/10.32614/CRAN.package.broom. R package version 1.0.13, https://CRAN.R-project.org/package=broom.
Sjoberg D, Larmarange J, Curry M, de la Rua E, Lavery J, Whiting K, Zabor E (2026). gtsummary: Presentation-Ready Data Summary and Analytic Result Tables. doi:10.32614/CRAN.package.gtsummary https://doi.org/10.32614/CRAN.package.gtsummary. R package version 2.5.1, https://CRAN.R-project.org/package=gtsummary.
Wickham H (2023). tidyverse: Easily Install and Load the ‘Tidyverse’. doi:10.32614/CRAN.package.tidyverse https://doi.org/10.32614/CRAN.package.tidyverse. R package version 2.0.0, https://CRAN.R-project.org/package=tidyverse.