Chapter 14: Higher Order Designs with Within-Subjects Factors: The Multivariate Approach

R Code and Instructions

Scott E. Maxwell, Harold D. Delaney, and Ken Kelley

Code
Results
On this page

Examples follow the book’s section numbers.

Chapter 14 extends the multivariate approach to higher-order experimental designs, specifically focusing on designs with multiple within-subjects factors and mixed (split-plot) designs containing both within- and between-subjects factors. By transforming repeated measures into composite difference (DD) variables, the text demonstrates how to test main effects and interactions without relying on the restrictive sphericity assumption required by the traditional univariate mixed-model approach. Once omnibus multivariate tests establish statistical significance, the chapter details how to rigorously probe these findings using simple effects tests, interaction contrasts, and confidence intervals. It also addresses the unique analytical needs of longitudinal randomized designs, emphasizing the superiority of MANCOVA over repeated measures ANOVA for evaluating treatment effects over time while controlling for baseline scores. Finally, the chapter equips researchers with practical tools for power analysis and sample size planning, demonstrating how different methodological choices and contrast specifications impact statistical power in these complex designs.

Chapter 1 explains how the book’s data sets are named in AMCP and covers the operational details of R that we do not repeat here. This chapter uses four of those data sets: chapter_14_table_1 (the 2×22 \times 2 perceptual experiment), chapter_14_table_4 (the 2×32 \times 3 version of the same experiment), chapter_14_table_7 (the split-plot design with two angle conditions), and chapter_14_table_9 (the split-plot design with three angle conditions).

HideShow
# 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(MASS)           # mvrnorm(): multivariate normal data for the power simulations
library(car)            # Anova() and Manova() for Type II/III tests
library(broom)          # tidy model output into data frames
library(gt)             # polished HTML tables
library(gtsummary)      # publication-ready regression / comparison tables
library(modelsummary)   # side-by-side model-comparison tables

14.2 — Two Within-Subjects Factors, Each with Two Levels

To explore the multivariate approach for a 2×22 \times 2 within-subjects design, we rely on the perceptual experiment data introduced in the text (Table 14.1, p. 768). In this study, 10 participants are measured on their reaction times to recognize a target letter. There are two within-subjects factors: Noise (Absent vs. Present) and Angle (0 degrees vs. 8 degrees).

Let’s begin by loading the data for this example.

HideShow
data(chapter_14_table_1)
str(chapter_14_table_1)
HideShow
'data.frame':   10 obs. of  4 variables:
 $ Absent0 : int  420 420 480 420 540 360 480 480 540 480
 $ Absent8 : int  480 480 540 540 540 360 600 660 540 540
 $ Present0: int  480 360 660 480 480 360 540 540 480 540
 $ Present8: int  780 600 780 900 720 540 840 900 780 780
HideShow
chapter_14_table_1
HideShow
   Absent0 Absent8 Present0 Present8
1      420     480      480      780
2      420     480      360      600
3      480     540      660      780
4      420     540      480      900
5      540     540      480      720
6      360     360      360      540
7      480     600      540      840
8      480     660      540      900
9      540     540      480      780
10     480     540      540      780
HideShow
# Cell means, to check the data against the book.
colMeans(chapter_14_table_1)
HideShow
 Absent0  Absent8 Present0 Present8 
     462      528      492      762 
HideShow
#> 462, 528, 492, 762 — matches the Mean row of Table 14.1 (p. 768) and the
#> cell means of Table 14.2 (p. 769).

Table 14.2 (p. 769) also reports the marginal means, which are what the two main effects compare. They follow from the cell means.

HideShow
# Marginal means for Angle (averaging over Noise) and for Noise (averaging over Angle).
with(chapter_14_table_1, c(
  Angle0 = mean(c(Absent0, Present0)),
  Angle8 = mean(c(Absent8, Present8))
))
HideShow
Angle0 Angle8 
   477    645 
HideShow
#> 477 and 645 — matches Table 14.2 (p. 769).

with(chapter_14_table_1, c(
  Noise_Absent  = mean(c(Absent0, Absent8)),
  Noise_Present = mean(c(Present0, Present8))
))
HideShow
 Noise_Absent Noise_Present 
          495           627 
HideShow
#> 495 and 627 — matches Table 14.2 (p. 769).

Because both factors have exactly two levels, any main effect or interaction will only have 1 degree of freedom. This means we can test each omnibus effect individually by constructing a single difference score (DD variable) for each one.

  • D1D_1 (Main Effect of Angle): Averages across the Noise factor and subtracts the 0-degree angle scores from the 8-degree angle scores.

  • D2D_2 (Main Effect of Noise): Averages across the Angle factor and subtracts the Noise-Absent scores from the Noise-Present scores.

  • D3D_3 (Angle ×\times Noise Interaction): Measures how the Angle effect changes depending on the presence or absence of Noise.

14.2.1 — Formation of Main-Effect DD Variables

HideShow
# Create difference scores, per the rationale given in the book.
# D1 is Equation 14.1 (p. 770) and D2 is Equation 14.9 (p. 771).
chapter_14_table_1$D1 <- .5*(chapter_14_table_1$Absent8 + chapter_14_table_1$Present8) - .5*(chapter_14_table_1$Absent0 + chapter_14_table_1$Present0)

chapter_14_table_1$D2 <- .5*(chapter_14_table_1$Present0 + chapter_14_table_1$Present8) - .5*(chapter_14_table_1$Absent0 + chapter_14_table_1$Absent8)

14.2.2 — Formation of Interaction DD Variables

The interaction difference score is the difference between the Angle effect when Noise is present and the Angle effect when Noise is absent (Equation 14.10, p. 771).

HideShow
chapter_14_table_1$D3 <- (chapter_14_table_1$Present8 - chapter_14_table_1$Absent8) - (chapter_14_table_1$Present0 - chapter_14_table_1$Absent0)

chapter_14_table_1
HideShow
   Absent0 Absent8 Present0 Present8  D1  D2  D3
1      420     480      480      780 180 180 240
2      420     480      360      600 150  30 180
3      480     540      660      780  90 210  60
4      420     540      480      900 270 210 300
5      540     540      480      720 120  60 240
6      360     360      360      540  90  90 180
7      480     600      540      840 210 150 180
8      480     660      540      900 270 150 180
9      540     540      480      780 150  90 300
10     480     540      540      780 150 150 180

We can summarize these new difference scores to check the means and variability.

HideShow
# dplyr is loaded above as part of tidyverse.
chapter_14_table_1_Summary <- chapter_14_table_1 %>%
  summarize(
    Mean_D1 = mean(D1),
    Mean_D2 = mean(D2),
    Mean_D3 = mean(D3),
    SD_D1 = sd(D1),
    SD_D2 = sd(D2),
    SD_D3 = sd(D3)
  )

chapter_14_table_1_Summary
HideShow
  Mean_D1 Mean_D2 Mean_D3    SD_D1    SD_D2    SD_D3
1     168     132     204 65.11528 61.96773 70.42727
HideShow
#> Means 168, 132, and 204 — match the Mean row of Table 14.3 (p. 769).

# Or more simply using base R
summary(chapter_14_table_1[, c("D1", "D2", "D3")])
HideShow
       D1              D2              D3     
 Min.   : 90.0   Min.   : 30.0   Min.   : 60  
 1st Qu.:127.5   1st Qu.: 90.0   1st Qu.:180  
 Median :150.0   Median :150.0   Median :180  
 Mean   :168.0   Mean   :132.0   Mean   :204  
 3rd Qu.:202.5   3rd Qu.:172.5   3rd Qu.:240  
 Max.   :270.0   Max.   :210.0   Max.   :300  
Testing the hypotheses

The null hypothesis for each effect stipulates that the population mean of its respective DD variable is equal to zero (μ=0\mu = 0).

One way of testing the DD variables is a one-sample tt-test of each difference score against a null value of 0. Because each effect has 1 numerator degree of freedom, each tt here is the square root of the corresponding FF reported in the book.

HideShow
t.test(
  x           = chapter_14_table_1$D1,
  mu          = 0,
  alternative = "two.sided",
  conf.level  = 0.95
)
HideShow

    One Sample t-test

data:  chapter_14_table_1$D1
t = 8.1588, df = 9, p-value = 1.891e-05
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 121.4193 214.5807
sample estimates:
mean of x 
      168 
HideShow
#> t = 8.1588, and 8.1588^2 = 66.57 — matches the F on p. 771.

t.test(
  x           = chapter_14_table_1$D2,
  mu          = 0,
  alternative = "two.sided",
  conf.level  = 0.95
)
HideShow

    One Sample t-test

data:  chapter_14_table_1$D2
t = 6.7361, df = 9, p-value = 8.498e-05
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
  87.67095 176.32905
sample estimates:
mean of x 
      132 
HideShow
#> t = 6.7361, and 6.7361^2 = 45.375 — matches the F on p. 771.

t.test(
  x           = chapter_14_table_1$D3,
  mu          = 0,
  alternative = "two.sided",
  conf.level  = 0.95
)
HideShow

    One Sample t-test

data:  chapter_14_table_1$D3
t = 9.1599, df = 9, p-value = 7.391e-06
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 153.6194 254.3806
sample estimates:
mean of x 
      204 
HideShow
#> t = 9.1599, and 9.1599^2 = 83.90 — matches the F on p. 771.

Equivalently, the book computes each effect by hand with Equation 14.8 (p. 770), F=nD‾2/sD2F = n\bar{D}^2 / s^2_{D}. Plugging the actual sample values for each DD variable into that formula reproduces the book’s FF values exactly:

HideShow
n <- nrow(chapter_14_table_1)

# The ingredients the book reports for D1 on p. 771.
c(n = n, Dbar = mean(chapter_14_table_1$D1), s2 = var(chapter_14_table_1$D1))
HideShow
   n Dbar   s2 
  10  168 4240 
HideShow
#> n = 10, Dbar = 168, s^2 = 4,240 — matches p. 771.

# Equation 14.8 applied to each D variable.
F_D1 <- n * mean(chapter_14_table_1$D1)^2 / var(chapter_14_table_1$D1)
F_D2 <- n * mean(chapter_14_table_1$D2)^2 / var(chapter_14_table_1$D2)
F_D3 <- n * mean(chapter_14_table_1$D3)^2 / var(chapter_14_table_1$D3)

c(Angle_D1 = F_D1, Noise_D2 = F_D2, Interaction_D3 = F_D3)
HideShow
      Angle_D1       Noise_D2 Interaction_D3 
      66.56604       45.37500       83.90323 
HideShow
#> Angle  D1: F = 66.57 — matches book p. 771.
#> Noise  D2: F = 45.375 — matches book p. 771.
#> Inter. D3: F = 83.90 — matches book p. 771.

# Each observed F is referred to a critical F with 1 and n - 1 degrees of freedom.
qf(p = 1 - .05, df1 = 1, df2 = n - 1)
HideShow
[1] 5.117355
HideShow
#> 5.12 — matches the critical value quoted on p. 771.

The textbook explicitly frames these hypothesis tests as a comparison between a full model and a restricted model. We can map this framework onto R code.

Below, we fit two linear models for the Angle main effect (D1D_1). The first is the Full Model (which includes an intercept to estimate the mean), and the second is the Restricted Model (which forces the intercept to 0). We then compare the two using the anova() function to reproduce the FF-test in the book.

HideShow
# Full Model
D1_Model_Mean <- lm(D1 ~ 1, data=chapter_14_table_1)
summary(D1_Model_Mean)
HideShow

Call:
lm(formula = D1 ~ 1, data = chapter_14_table_1)

Residuals:
   Min     1Q Median     3Q    Max 
 -78.0  -40.5  -18.0   34.5  102.0 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   168.00      20.59   8.159 1.89e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 65.12 on 9 degrees of freedom
HideShow
# Restricted Model
D1_Model_Null <- lm(D1 ~ 0, data=chapter_14_table_1)

# Compare the restricted model to the full model
anova(D1_Model_Null, D1_Model_Mean)
HideShow
Analysis of Variance Table

Model 1: D1 ~ 0
Model 2: D1 ~ 1
  Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
1     10 320400                                  
2      9  38160  1    282240 66.566 1.891e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> The two RSS columns are E(R) and E(F) of Equations 14.6 and 14.5 (p. 770),
#> and the model comparison gives F = 66.57 — matches book p. 771.

Now that we have confirmed the main effect of Angle (D1), we can apply this exact same full-versus-restricted model comparison logic to test the main effect of Noise (D2) and the Angle x Noise interaction (D3).

HideShow
# Models for D2 (Noise Main Effect)
D2_Model_Mean <- lm(D2 ~ 1, data=chapter_14_table_1)
D2_Model_Null <- lm(D2 ~ 0, data=chapter_14_table_1)
anova(D2_Model_Null, D2_Model_Mean)
HideShow
Analysis of Variance Table

Model 1: D2 ~ 0
Model 2: D2 ~ 1
  Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
1     10 208800                                  
2      9  34560  1    174240 45.375 8.498e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 45.375 for the Noise main effect — matches book p. 771.

# Models for D3 (Angle x Noise Interaction)
D3_Model_Mean <- lm(D3 ~ 1, data=chapter_14_table_1)
D3_Model_Null <- lm(D3 ~ 0, data=chapter_14_table_1)
anova(D3_Model_Null, D3_Model_Mean)
HideShow
Analysis of Variance Table

Model 1: D3 ~ 0
Model 2: D3 ~ 1
  Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
1     10 460800                                  
2      9  44640  1    416160 83.903 7.391e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 83.90 for the Angle x Noise interaction — matches book p. 771.

14.2.3 — Relationship to the Mixed-Model Approach

It is important to note that because this is a 2×22 \times 2 design, the main effects and the interaction all have exactly 1 numerator degree of freedom. Consequently, the sphericity assumption required by the traditional mixed-model approach is necessarily satisfied. The book makes the point explicitly on p. 772: when every factor has only two levels, the multivariate and mixed-model approaches are identical, provided the mixed-model approach uses an error term of the form MSeffect×SMS_{\text{effect} \times S}. The degrees of freedom agree as well, so there is nothing to choose between the two approaches here.

14.3 — Multivariate Analysis of Two-Way a×ba \times b Within-Subjects Designs

To motivate the analysis of a general a×ba \times b within-subjects design, we continue with the perceptual experiment, but we now reinstate the 4-degree angle condition. This gives us a 2×32 \times 3 design (Noise: Absent vs. Present; Angle: 0, 4, and 8 degrees). Following the book, Angle is the AA factor, with a=3a = 3 levels, and Noise is the BB factor, with b=2b = 2 levels. These are the data of Table 14.4 (p. 774), which are the same data as Table 12.1 so that the multivariate and mixed-model analyses can be compared.

HideShow
data(chapter_14_table_4)
str(chapter_14_table_4)
HideShow
'data.frame':   10 obs. of  6 variables:
 $ Absent0 : int  420 420 480 420 540 360 480 480 540 480
 $ Absent4 : int  420 480 480 540 660 420 480 600 600 420
 $ Absent8 : int  480 480 540 540 540 360 600 660 540 540
 $ Present0: int  480 360 660 480 480 360 540 540 480 540
 $ Present4: int  600 480 780 780 660 480 720 720 720 660
 $ Present8: int  780 600 780 900 720 540 840 900 780 780
HideShow
chapter_14_table_4
HideShow
   Absent0 Absent4 Absent8 Present0 Present4 Present8
1      420     420     480      480      600      780
2      420     480     480      360      480      600
3      480     480     540      660      780      780
4      420     540     540      480      780      900
5      540     660     540      480      660      720
6      360     420     360      360      480      540
7      480     480     600      540      720      840
8      480     600     660      540      720      900
9      540     600     540      480      720      780
10     480     420     540      540      660      780
HideShow
# Cell means, to check the data against the book.
colMeans(chapter_14_table_4)
HideShow
 Absent0  Absent4  Absent8 Present0 Present4 Present8 
     462      510      528      492      660      762 
HideShow
#> 462, 510, 528, 492, 660, 762 — matches the Mean row of Table 14.4 (p. 774).

The six cell means are worth looking at before any test is run, because the whole argument of the section is about the pattern they form.

HideShow
cell_means <- chapter_14_table_4 |>
  summarize(across(Absent0:Present8, mean)) |>
  pivot_longer(everything(), names_to = "cell", values_to = "Mean_RT") |>
  mutate(
    Noise = if_else(str_starts(cell, "Absent"), "Absent", "Present"),
    Angle = as.numeric(str_remove(cell, "Absent|Present"))
  ) |>
  mutate(Noise = factor(Noise, levels = c("Absent", "Present")))

cell_means
HideShow
# A tibble: 6 × 4
  cell     Mean_RT Noise   Angle
  <chr>      <dbl> <fct>   <dbl>
1 Absent0      462 Absent      0
2 Absent4      510 Absent      4
3 Absent8      528 Absent      8
4 Present0     492 Present     0
5 Present4     660 Present     4
6 Present8     762 Present     8
HideShow
ggplot(cell_means, aes(x = Angle, y = Mean_RT, color = Noise, shape = Noise)) +
  geom_line(linewidth = 0.8) +
  geom_point(size = 3) +
  scale_x_continuous(breaks = c(0, 4, 8)) +
  labs(
    x     = "Angle (degrees off center)",
    y     = "Mean reaction time (ms)",
    color = "Noise",
    shape = "Noise"
  )
Sample cell means for the \(2 \times 3\) perceptual experiment (Table 14.4, p. 774).

Sample cell means for the 2×32 \times 3 perceptual experiment (Table 14.4, p. 774).

14.3.1 — Formation of Main-Effect DD Variables

Because the Angle factor has three levels, we must form a−1=2a – 1 = 2 separate DD variables to test its main effect. Since Angle is quantitative, we will use coefficients for linear (-1, 0, 1) and quadratic (1, -2, 1) trends to create D1D_1 and D2D_2, respectively, averaging across the Noise factor (Equations 14.14 and 14.15, p. 774).

Because the Noise factor only has two levels, we only need b−1=1b – 1 = 1 DD variable (D3D_3) to test its main effect, which simply finds the difference between the average Noise Present and Noise Absent scores (Equation 14.16, p. 775).

HideShow
# Define Angle Main Effect D variables (Linear and Quadratic Trends)
chapter_14_table_4$D1 <- -1*(.5*(chapter_14_table_4$Absent0 + chapter_14_table_4$Present0)) + 0*(.5*(chapter_14_table_4$Absent4 + chapter_14_table_4$Present4)) + 1*(.5*(chapter_14_table_4$Absent8 + chapter_14_table_4$Present8))

chapter_14_table_4$D2 <- 1*(.5*(chapter_14_table_4$Absent0 + chapter_14_table_4$Present0)) - 2*(.5*(chapter_14_table_4$Absent4 + chapter_14_table_4$Present4)) + 1*(.5*(chapter_14_table_4$Absent8 + chapter_14_table_4$Present8))

# Define Noise Main Effect D variable
chapter_14_table_4$D3 <- (1/3)*(chapter_14_table_4$Present0 + chapter_14_table_4$Present4 + chapter_14_table_4$Present8) - (1/3)*(chapter_14_table_4$Absent0 + chapter_14_table_4$Absent4 + chapter_14_table_4$Absent8)

14.3.2 — Formation of Interaction DD Variables

The interaction requires (a−1)(b−1)=2(a – 1)(b – 1) = 2 DD variables. We create D4D_4 and D5D_5 by cross-multiplying the coefficients of the Angle variables (D1D_1, D2D_2) with the Noise variable (D3D_3), which is Equation 14.23 and Equation 14.24 (p. 776).

HideShow
# Define Interaction D variables (Cross-products of Main Effect coefficients)
chapter_14_table_4$D4 <- 1*chapter_14_table_4$Absent0 + 0*chapter_14_table_4$Absent4 - 1*chapter_14_table_4$Absent8 -1*chapter_14_table_4$Present0 + 0*chapter_14_table_4$Present4 + 1*chapter_14_table_4$Present8

chapter_14_table_4$D5 <- -1*chapter_14_table_4$Absent0 + 2*chapter_14_table_4$Absent4 -1*chapter_14_table_4$Absent8 + 1*chapter_14_table_4$Present0 - 2*chapter_14_table_4$Present4 + 1*chapter_14_table_4$Present8

# Review the resulting Difference variables
chapter_14_table_4[, c("D1", "D2", "D3", "D4", "D5")]
HideShow
    D1   D2  D3  D4   D5
1  180   60 180 240    0
2  150  -30  20 180   60
3   90  -30 240  60 -180
4  270 -150 220 300  -60
5  120 -180  40 240  120
6   90  -90  80 180   60
7  210   30 180 180 -180
8  270  -30 140 180   60
9  150 -150 100 300  -60
10 150   90 180 180 -180
HideShow
# Their means are the Mean row of Table 14.5.
colMeans(chapter_14_table_4[, c("D1", "D2", "D3", "D4", "D5")])
HideShow
 D1  D2  D3  D4  D5 
168 -48 138 204 -36 
HideShow
#> 168, -48, 138, 204, -36 — matches Table 14.5 (p. 775).

14.3.3 — Omnibus Tests—Multivariate Significance Tests

To test the main effect of Angle, we must test the null hypothesis that both D1D_1 and D2D_2 equal zero simultaneously. The book does this by building the error matrices E(F)E(F) and E(R)E(R) for the full and restricted models and comparing their determinants with Equation 14.28 (p. 777). Nothing but base R is needed for that, and it is worth doing once so the machinery is visible:

HideShow
D_A <- as.matrix(chapter_14_table_4[, c("D1", "D2")])
n   <- nrow(D_A)

# E(F): deviations from the sample means of D1 and D2.
E_F_A <- crossprod(sweep(D_A, 2, colMeans(D_A)))

# E(R): deviations from zero, which is what the null hypothesis asserts.
E_R_A <- crossprod(D_A)

E_F_A
HideShow
      D1    D2
D1 38160  3240
D2  3240 77760
HideShow
#> 38,160 and 77,760 on the diagonal, 3,240 off it — matches E(F) on p. 778.
E_R_A
HideShow
       D1     D2
D1 320400 -77400
D2 -77400 100800
HideShow
#> 320,400 and 100,800 on the diagonal, -77,400 off it — matches E(R) on p. 778.

c(det_E_F = det(E_F_A), det_E_R = det(E_R_A))
HideShow
    det_E_F     det_E_R 
 2956824000 26305560000 
HideShow
#> 2,956,824,000 and 26,305,560,000 — match the determinants on p. 778.

# Equation 14.28, with a = 3 levels of Angle.
a <- 3
F_A <- ((det(E_R_A) - det(E_F_A)) / (a - 1)) / (det(E_F_A) / (n - a + 1))
c(F_A = F_A, p = pf(F_A, df1 = a - 1, df2 = n - a + 1, lower.tail = FALSE))
HideShow
         F_A            p 
31.586237125  0.000159629 
HideShow
#> F = 31.59 with 2 and 8 df, p = .0002 — matches book p. 778.

R’s manova() does the same determinant arithmetic for us, and anova() on the fitted object reports it as Wilks’s Lambda with the same approximate FF.

HideShow
# Multivariate test for the Main Effect of Angle (A)
MANOVA_A <- manova(cbind(D1, D2) ~ 1, data=chapter_14_table_4)
anova(MANOVA_A, test="Wilks")
HideShow
Analysis of Variance Table

            Df  Wilks approx F num Df den Df    Pr(>F)    
(Intercept)  1 0.1124   31.586      2      8 0.0001596 ***
Residuals    9                                            
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> approx F = 31.586 with 2 and 8 df — matches the hand computation above and
#> book p. 778. Wilks's Lambda is det E(F) / det E(R) = 0.1124.

Here we test the main effect of Noise (B). Because there is only a single D3D_3 variable for that effect, the matrices have one row and one column, their determinants are just the single numbers they contain, and the multivariate test collapses to a univariate one. A model comparison or a one-sample tt-test does the job.

HideShow
# Approach 1: Model Comparison (ANOVA)
D3_Model_Mean <- aov(D3 ~ 1, data=chapter_14_table_4)
D3_Model_Null <- aov(D3 ~ 0, data=chapter_14_table_4)
anova(D3_Model_Null, D3_Model_Mean)
HideShow
Analysis of Variance Table

Model 1: D3 ~ 0
Model 2: D3 ~ 1
  Res.Df    RSS Df Sum of Sq      F   Pr(>F)    
1     10 241200                                 
2      9  50760  1    190440 33.766 0.000256 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> RSS of 241,200 and 50,760 are E(R) = [241,200] and E(F) = [50,760] on p. 778,
#> and F = 33.77 — matches book p. 778.

# Approach 2: One-Sample t-test
t.test(
  x           = chapter_14_table_4$D3,
  mu          = 0,
  alternative = "two.sided",
  conf.level  = 0.95
)
HideShow

    One Sample t-test

data:  chapter_14_table_4$D3
t = 5.8108, df = 9, p-value = 0.000256
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
  84.27674 191.72326
sample estimates:
mean of x 
      138 
HideShow
#> t = 5.8108, and 5.8108^2 = 33.77 — matches the F on p. 778.

Testing the Angle ×\times Noise interaction requires testing D4D_4 and D5D_5 simultaneously, so we return to manova() and Equation 14.30 (p. 778).

HideShow
# Multivariate test for the Interaction
MANOVA_Interaction <- manova(cbind(D4, D5) ~ 1, data=chapter_14_table_4)
anova(MANOVA_Interaction, test="Wilks")
HideShow
Analysis of Variance Table

            Df    Wilks approx F num Df den Df    Pr(>F)    
(Intercept)  1 0.081777   44.914      2      8 4.472e-05 ***
Residuals    9                                              
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> approx F = 44.914 with 2 and 8 df, p = .00004 — matches book p. 778.

Alternatively, we can use the Anova() function from the car package (loaded above), which reports the same tests with Pillai’s trace as its default statistic. With a single group every multivariate criterion gives the same FF, so the two routes agree exactly.

HideShow
# Notice we use lm() instead of manova() when passing to car::Anova()
Model_A_lm <- lm(cbind(D1, D2) ~ 1, data=chapter_14_table_4)
Model_B_lm <- lm(D3 ~ 1, data=chapter_14_table_4)
Model_Int_lm <- lm(cbind(D4, D5) ~ 1, data=chapter_14_table_4)

Anova(Model_A_lm, type="III")
HideShow

Type III MANOVA Tests: Pillai test statistic
            Df test stat approx F num Df den Df    Pr(>F)    
(Intercept)  1    0.8876   31.586      2      8 0.0001596 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> approx F = 31.586 — matches book p. 778.
Anova(Model_B_lm, type="III")
HideShow
Anova Table (Type III tests)

Response: D3
            Sum Sq Df F value   Pr(>F)    
(Intercept) 190440  1  33.766 0.000256 ***
Residuals    50760  9                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 33.766 — matches book p. 778.
Anova(Model_Int_lm, type="III")
HideShow

Type III MANOVA Tests: Pillai test statistic
            Df test stat approx F num Df den Df    Pr(>F)    
(Intercept)  1   0.91822   44.914      2      8 4.472e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> approx F = 44.914 — matches book p. 778.

14.4 — Measures of Effect

As noted in the text, while the multivariate approach is recommended for hypothesis testing, the mixed-model formulation of omega squared (ω̂2\hat{\omega}^2) is still preferred for estimating effect sizes because it treats individual differences between subjects as a source of total variance, just as between-subjects designs do. Those components do not come from the DD variables; they come from the mixed-model analysis of Chapter 12, which we can fit here with aov() and an Error() stratum.

HideShow
# Long format, with Noise and Angle spelled out level by level.
chapter_14_table_4$Subject <- factor(seq_len(nrow(chapter_14_table_4)))

within_long <- chapter_14_table_4 |>
  select(Subject, Absent0:Present8) |>
  pivot_longer(Absent0:Present8, names_to = "cell", values_to = "Score") |>
  mutate(Noise = case_when(
    str_starts(cell, "Absent")  ~ "Absent",
    str_starts(cell, "Present") ~ "Present"
  )) |>
  mutate(Angle = case_when(
    str_ends(cell, "0") ~ "0",
    str_ends(cell, "4") ~ "4",
    str_ends(cell, "8") ~ "8"
  )) |>
  mutate(
    Noise = factor(Noise, levels = c("Absent", "Present")),
    Angle = factor(Angle, levels = c("0", "4", "8"))
  )

# The mixed-model (univariate) analysis of these same data: Angle is A, Noise is B.
within_aov <- aov(Score ~ Angle * Noise + Error(Subject / (Angle * Noise)),
                  data = within_long)
summary(within_aov)
HideShow

Error: Subject
          Df Sum Sq Mean Sq F value Pr(>F)
Residuals  9 292140   32460               

Error: Subject:Angle
          Df Sum Sq Mean Sq F value   Pr(>F)    
Angle      2 289920  144960   40.72 2.09e-07 ***
Residuals 18  64080    3560                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Error: Subject:Noise
          Df Sum Sq Mean Sq F value   Pr(>F)    
Noise      1 285660  285660   33.77 0.000256 ***
Residuals  9  76140    8460                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Error: Subject:Angle:Noise
            Df Sum Sq Mean Sq F value   Pr(>F)    
Angle:Noise  2 105120   52560   45.31 9.42e-08 ***
Residuals   18  20880    1160                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> A = 289,920 / 2 / 144,960 / F = 40.72;  A x S = 64,080 / 18 / 3,560
#> B = 285,660 / 1 / 285,660 / F = 33.77;  B x S = 76,140 /  9 / 8,460
#> A x B = 105,120 / 2 / 52,560 / F = 45.31;  A x B x S = 20,880 / 18 / 1,160
#> — matches Table 12.5 (p. 650). The subjects stratum gives SS_S = 292,140 and
#> MS_S = 32,460, the two extra values the book supplies on p. 779.

Equation 12.6, repeated on p. 779, is ω̂2=dfeffect(MSeffect−MSeffect×S)/(SSeffect+SSeffect×S+SSS+MSS)\hat{\omega}^2 = df_{\text{effect}}(MS_{\text{effect}} – MS_{\text{effect} \times S}) / (SS_{\text{effect}} + SS_{\text{effect} \times S} + SS_{S} + MS_{S}). Pulling the pieces out of the fitted object gives the book’s three values.

HideShow
within_sum <- summary(within_aov)

# Subjects stratum, whose single line is the subjects effect.
SS_S <- within_sum$`Error: Subject`[[1]][1, "Sum Sq"]
MS_S <- within_sum$`Error: Subject`[[1]][1, "Mean Sq"]
c(SS_S = SS_S, MS_S = MS_S)
HideShow
  SS_S   MS_S 
292140  32460 
HideShow
#> 292,140 and 32,460 — matches the values stated on p. 779.

# One stratum per within-subjects effect, each with its own effect x S error line.
omega_squared_within <- function(stratum, df_effect) {
  tab <- within_sum[[stratum]][[1]]
  eff <- 1L                       # the effect is the first row of its stratum
  err <- nrow(tab)                # the effect x S term is the Residuals row
  df_effect * (tab[eff, "Mean Sq"] - tab[err, "Mean Sq"]) /
    (tab[eff, "Sum Sq"] + tab[err, "Sum Sq"] + SS_S + MS_S)
}

c(
  Angle_A       = omega_squared_within("Error: Subject:Angle", df_effect = 2),
  Noise_B       = omega_squared_within("Error: Subject:Noise", df_effect = 1),
  Angle_x_Noise = omega_squared_within("Error: Subject:Angle:Noise", df_effect = 2)
)
HideShow
      Angle_A       Noise_B Angle_x_Noise 
    0.4167403     0.4038462     0.2281403 
HideShow
#> .42, .40, and .23 — matches the omega squared values on p. 779.
In DMAR: the sphericity-adjusted tests in one table

The mixed-model FF values above assume sphericity, which is exactly the assumption the multivariate approach of this chapter avoids. aov() does not report the Greenhouse–Geisser or Huynh–Feldt corrections, so a reader comparing the two approaches has to compute ε̂\hat{\varepsilon} separately. The DMAR function anova_within_two_way() reports the uncorrected test and the three adjusted versions together, along with partial η2\eta^2, from the same long data frame.

HideShow
anova_within_two_way(
  data     = as.data.frame(within_long),
  outcome  = "Score",
  factor_A = "Angle",
  factor_B = "Noise",
  subject  = "Subject"
)
effect adjustment F_value df_1 df_2 p_value epsilon eta_squared_partial
A none 40.719 2 18 < 0.0001 NA 0.819
A Greenhouse-Geisser 40.719 1.923 17.309 < 0.0001 0.962 0.819
A Huynh-Feldt 40.719 2 18 < 0.0001 1.000 0.819
A lower_bound 40.719 1 9 0.0001 0.500 0.819
B none 33.766 1 9 0.0003 NA 0.790
B Greenhouse-Geisser 33.766 1 9 0.0003 1.000 0.790
B Huynh-Feldt 33.766 1 9 0.0003 1.000 0.790
B lower_bound 33.766 1 9 0.0003 1.000 0.790
A:B none 45.310 2 18 < 0.0001 NA 0.834
A:B Greenhouse-Geisser 45.310 1.808 16.272 < 0.0001 0.904 0.834
A:B Huynh-Feldt 45.310 2 18 < 0.0001 1.000 0.834
A:B lower_bound 45.310 1 9 < 0.0001 0.500 0.834
HideShow
#> The "none" rows give F = 40.719 (A), 33.766 (B), and 45.310 (A:B), the same
#> values as the aov() output above and as Table 12.5 (p. 650). The adjusted
#> rows are what aov() does not provide.

The contrast with the multivariate tests of Section 14.3 is the point of the chapter: for the Noise main effect, which has 1 numerator degree of freedom, the two approaches agree exactly (F=33.77F = 33.77). For the Angle main effect and the interaction, which have 2, they do not (31.5931.59 versus 40.7240.72, and 44.9144.91 versus 45.3145.31), because the multivariate approach buys its freedom from the sphericity assumption with a different error structure.

With the effect sizes in hand, we follow up the significant omnibus tests with contrasts and simple effects.

14.4.1 — Further Investigation of Main Effects

Because the Angle main effect was significant and has three levels, we may want to test specific comparisons of its marginal means. In Section 14.3, we already created a D2D_2 variable that represents the quadratic trend of the Angle main effect.

We can test it individually by comparing it to a null model (where the intercept is 0).

HideShow
# Test the quadratic trend of Angle (D2)
D2_Model_Mean <- lm(D2 ~ 1, data=chapter_14_table_4)
D2_Model_Null <- lm(D2 ~ 0, data=chapter_14_table_4)
anova(D2_Model_Null, D2_Model_Mean)
HideShow
Analysis of Variance Table

Model 1: D2 ~ 0
Model 2: D2 ~ 1
  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1     10 100800                           
2      9  77760  1     23040 2.6667 0.1369
HideShow
#> F = 2.67 — matches book p. 779.

# The ingredients the book reports for this contrast on p. 779.
c(Dbar = mean(chapter_14_table_4$D2), s2 = var(chapter_14_table_4$D2))
HideShow
Dbar   s2 
 -48 8640 
HideShow
#> Dbar = -48 and s^2 = 8,640 — match p. 779, and 10(-48)^2 / 8,640 = 2.67.

# Critical value for a single planned comparison, 1 and n - 1 df.
qf(p = 1 - .05, df1 = 1, df2 = nrow(chapter_14_table_4) - 1)
HideShow
[1] 5.117355
HideShow
#> 5.12 — matches the critical value on p. 780, so the quadratic trend is
#> not statistically significant.

The observed FF of 2.67 is not statistically significant, and it is the same FF the mixed-model approach of Chapter 12 produces for the quadratic angle trend when a separate error term is used (p. 779).

14.4.2 — Further Investigation of an Interaction—Simple Effects

Because we found a significant Angle ×\times Noise interaction, the next logical step is to test simple effects. First, we will test the simple effect of Noise at each individual level of Angle (0, 4, and 8 degrees).

To do this, we create three new DD variables (D6D_6, D7D_7, and D8D_8), each representing the difference between Noise Present and Noise Absent at a specific angle.

HideShow
# Simple effects of Noise at specific Angles
chapter_14_table_4$D6 <- chapter_14_table_4$Present0 - chapter_14_table_4$Absent0
chapter_14_table_4$D7 <- chapter_14_table_4$Present4 - chapter_14_table_4$Absent4
chapter_14_table_4$D8 <- chapter_14_table_4$Present8 - chapter_14_table_4$Absent8 

# Test each simple effect of Noise
anova(lm(D6 ~ 0, data=chapter_14_table_4), lm(D6 ~ 1, data=chapter_14_table_4))
HideShow
Analysis of Variance Table

Model 1: D6 ~ 0
Model 2: D6 ~ 1
  Res.Df   RSS Df Sum of Sq      F Pr(>F)
1     10 61200                           
2      9 52200  1      9000 1.5517 0.2443
HideShow
#> F = 1.55 — matches book p. 781.
anova(lm(D7 ~ 0, data=chapter_14_table_4), lm(D7 ~ 1, data=chapter_14_table_4))
HideShow
Analysis of Variance Table

Model 1: D7 ~ 0
Model 2: D7 ~ 1
  Res.Df    RSS Df Sum of Sq      F   Pr(>F)   
1     10 327600                                
2      9 102600  1    225000 19.737 0.001617 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 19.74 — matches book p. 781.
anova(lm(D8 ~ 0, data=chapter_14_table_4), lm(D8 ~ 1, data=chapter_14_table_4))
HideShow
Analysis of Variance Table

Model 1: D8 ~ 0
Model 2: D8 ~ 1
  Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
1     10 586800                                  
2      9  39240  1    547560 125.59 1.376e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 125.59 — matches book p. 781.

The output matches the text: the effect of Noise at 0 degrees (F=1.55F = 1.55) is not significant, but the effect of Noise intensifies at 4 degrees (F=19.74F = 19.74) and 8 degrees (F=125.59F = 125.59), all three on p. 781.

Next, we test the simple effect of Angle at each individual level of Noise. Because Angle has three levels, we must form two DD variables for each noise condition. We will use linear and quadratic coefficients again, but this time we will not average across the Noise conditions.

HideShow
# Simple effect of Angle when Noise is absent
chapter_14_table_4$D9 <- -1*chapter_14_table_4$Absent0 + 0*chapter_14_table_4$Absent4 + 1*chapter_14_table_4$Absent8
chapter_14_table_4$D10 <- 1*chapter_14_table_4$Absent0 - 2*chapter_14_table_4$Absent4 + 1*chapter_14_table_4$Absent8

# Simple effect of Angle when Noise is present
chapter_14_table_4$D11 <- -1*chapter_14_table_4$Present0 + 0*chapter_14_table_4$Present4 + 1*chapter_14_table_4$Present8
chapter_14_table_4$D12 <- 1*chapter_14_table_4$Present0 - 2*chapter_14_table_4$Present4 + 1*chapter_14_table_4$Present8

We test these simple effects simultaneously using a multivariate approach with the Anova() function from the car package.

HideShow
# Test D9 and D10 simultaneously (Angle effect | Noise Absent)
MANOVA_D9D10_Full <- lm(cbind(D9, D10) ~ 1, data=chapter_14_table_4)
Anova(MANOVA_D9D10_Full, type="III")
HideShow

Type III MANOVA Tests: Pillai test statistic
            Df test stat approx F num Df den Df  Pr(>F)  
(Intercept)  1   0.64407   7.2381      2      8 0.01605 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> approx F = 7.238 with 2 and 8 df, p = .016 — matches book p. 781.

# Test D11 and D12 simultaneously (Angle effect | Noise Present)
MANOVA_D11D12_Full <- lm(cbind(D11, D12) ~ 1, data=chapter_14_table_4)
Anova(MANOVA_D11D12_Full, type="III")
HideShow

Type III MANOVA Tests: Pillai test statistic
            Df test stat approx F num Df den Df    Pr(>F)    
(Intercept)  1   0.91849   45.072      2      8 4.415e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> approx F = 45.072 with 2 and 8 df, p = .00004 — matches book p. 781.

These results confirm the textbook’s findings: there is a significant simple effect of Angle when Noise is absent (F=7.24F = 7.24) and a highly significant simple effect of Angle when Noise is present (F=45.07F = 45.07), both on p. 781.

Having found a significant simple effect of Angle in each noise condition, the book goes one step further and tests the two DD variables for Noise Absent individually, which is how the nature of that simple effect is identified.

HideShow
# The individual cell mean comparisons within Noise Absent.
anova(lm(D9 ~ 0, data=chapter_14_table_4), lm(D9 ~ 1, data=chapter_14_table_4))
HideShow
Analysis of Variance Table

Model 1: D9 ~ 0
Model 2: D9 ~ 1
  Res.Df   RSS Df Sum of Sq      F   Pr(>F)   
1     10 75600                                
2      9 32040  1     43560 12.236 0.006745 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 12.24 for the linear trend of Angle when Noise is absent — matches p. 782.
anova(lm(D10 ~ 0, data=chapter_14_table_4), lm(D10 ~ 1, data=chapter_14_table_4))
HideShow
Analysis of Variance Table

Model 1: D10 ~ 0
Model 2: D10 ~ 1
  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1     10 162000                           
2      9 153000  1      9000 0.5294 0.4854
HideShow
#> F = 0.53 for the quadratic trend of Angle when Noise is absent — matches p. 782.

14.4.3 — Interaction Contrasts

An alternative to testing simple effects is to test interaction contrasts. The omnibus interaction is represented collectively by D4D_4 and D5D_5 (which we created in Section 14.3).

  • D4D_4 represents the interaction of Noise with the linear trend of Angle.

  • D5D_5 represents the interaction of Noise with the quadratic trend of Angle.

We can test these 1-degree-of-freedom interaction contrasts individually to see exactly what is driving the omnibus interaction.

HideShow
# Test Interaction Contrast: Noise x Linear trend of Angle (D4)
anova(lm(D4 ~ 0, data=chapter_14_table_4), lm(D4 ~ 1, data=chapter_14_table_4))
HideShow
Analysis of Variance Table

Model 1: D4 ~ 0
Model 2: D4 ~ 1
  Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
1     10 460800                                  
2      9  44640  1    416160 83.903 7.391e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 83.90 — matches book p. 782.

# Test Interaction Contrast: Noise x Quadratic trend of Angle (D5)
anova(lm(D5 ~ 0, data=chapter_14_table_4), lm(D5 ~ 1, data=chapter_14_table_4))
HideShow
Analysis of Variance Table

Model 1: D5 ~ 0
Model 2: D5 ~ 1
  Res.Df    RSS Df Sum of Sq  F Pr(>F)
1     10 129600                       
2      9 116640  1     12960  1 0.3434
HideShow
#> F = 1.00 — matches book p. 782.

As the text reports on p. 782, the interaction contrast for D4D_4 is highly significant (F=83.90F = 83.90), while D5D_5 is not (F=1.00F = 1.00). This indicates that the Noise effect grows stronger in a linear fashion as the angle deviates further from 0 degrees.

14.5 — Confidence Intervals for Contrasts

Just as in the one-way within-subjects design, we can form confidence intervals for contrasts in the two-way design using the general formula:

D‾±(CV)(seD‾)\bar{D} \pm (\sqrt{CV})(se_{\bar{D}})

Where CVCV is the critical value (which changes depending on whether the contrast is uncorrected, a planned Bonferroni, or a post hoc Roy-Bose/Scheffé) and seD‾se_{\bar{D}} is the standard error of the mean of the DD variable.

Because most statistical software only outputs uncorrected confidence intervals by default, we often need to calculate the adjusted critical values ourselves to control the familywise alpha level. Below we define the parameters of the design as the book does (n=10n = 10; Angle is AA with a=3a = 3 levels; Noise is BB with b=2b = 2 levels) and calculate the critical values for uncorrected, Bonferroni-adjusted, and post hoc tests.

Critical values for main-effect contrasts

Uncorrected critical values. With a per-comparison alpha level, the critical value for a single DD variable is read from the FF distribution with 1 and n−1n – 1 degrees of freedom (p. 780).

HideShow
n <- nrow(chapter_14_table_4)
a <- 3  # Angle, the A factor
b <- 2  # Noise, the B factor
alpha_FW <- .05

# A single planned comparison of either factor's marginal means.
CV_Single_Comparison <- qf(p = 1 - alpha_FW, df1 = 1, df2 = n - 1)
CV_Single_Comparison
HideShow
[1] 5.117355
HideShow
#> 5.12 — the critical value used on pp. 780 and 782.

Bonferroni adjusted critical values.

If a set of CC planned comparisons is tested, each contrast is tested at αFW/C\alpha_{FW} / C (p. 780). The Angle factor has a−1=2a – 1 = 2 degrees of freedom, so testing both its linear and quadratic trends as planned comparisons means C=2C = 2; the Noise factor has only b−1=1b – 1 = 1 degree of freedom, so C=1C = 1 there.

HideShow
# Number of planned contrasts within each factor (family)
C_for_A_Factor <- 2
C_for_B_Factor <- 1

CV_A_Factor_Bonferroni <- qf(p = 1 - alpha_FW/C_for_A_Factor, df1 = 1, df2 = n - 1)
CV_B_Factor_Bonferroni <- qf(p = 1 - alpha_FW/C_for_B_Factor, df1 = 1, df2 = n - 1)

CV_A_Factor_Bonferroni
HideShow
[1] 7.209283
HideShow
CV_B_Factor_Bonferroni
HideShow
[1] 5.117355

Dividing the familywise alpha level by the number of planned contrasts raises the critical value for the Angle family from 5.12 to 7.21. With a single planned contrast (C=1C = 1), as for Noise, αFW/C\alpha_{FW}/C is just .05.05, so the Bonferroni and uncorrected critical values coincide.

Roy–Bose (multivariate Scheffé) critical values.

For post hoc complex comparisons in a multivariate design, the text gives the Roy–Bose critical value in Equation 14.31 for contrasts of the AA marginal means and Equation 14.32 for contrasts of the BB marginal means (p. 780). This is the most conservative adjustment.

HideShow
CV_A_Factor_Scheffe <- (n-1)*(a-1)*qf(p=1-alpha_FW, df1=a-1, df2=n-a+1)/(n-a+1)
CV_B_Factor_Scheffe <- (n-1)*(b-1)*qf(p=1-alpha_FW, df1=b-1, df2=n-b+1)/(n-b+1)

CV_A_Factor_Scheffe   # Angle: Equation 14.31
HideShow
[1] 10.03268
HideShow
CV_B_Factor_Scheffe   # Noise: Equation 14.32
HideShow
[1] 5.117355

Tested post hoc, the quadratic trend of Angle would be compared against 10.03 rather than 5.12, so the conclusion of Section 14.4.1 does not change. For the Noise factor, which contributes a single DD variable, the Roy–Bose value reduces to the uncorrected 5.12, as it must.

Critical values for interaction contrasts

The general form for these critical values applies to interactions as well, using the interaction degrees of freedom dfeffect=(a−1)(b−1)df_{effect} = (a-1)(b-1), which is the general expression of Equation 14.34 (p. 782).

HideShow
# Omnibus degrees of freedom for the interaction
df_interaction <- (a-1)*(b-1)

# A single planned interaction contrast is still a 1 df test.
qf(p = 1 - alpha_FW, df1 = 1, df2 = n - 1)
HideShow
[1] 5.117355
HideShow
#> 5.12 — the value the book uses for D4 and D5 on p. 782.

# Bonferroni, with C = (a - 1)(b - 1) = 2 planned interaction contrasts
C_Interaction_Contrasts <- df_interaction
CV_Interaction_Bonferroni <- qf(p=1-alpha_FW/C_Interaction_Contrasts, df1=1, df2=n-1)
CV_Interaction_Bonferroni
HideShow
[1] 7.209283
HideShow
# Roy-Bose (Scheffe) Post Hoc Interaction CV (Equations 14.33 and 14.34)
CV_Interaction_Scheffe <- (n-1)*df_interaction*qf(p=1-alpha_FW, df1=df_interaction, df2=n-df_interaction)/(n-df_interaction)
CV_Interaction_Scheffe
HideShow
[1] 10.03268

The observed FF of 83.90 for D4D_4 exceeds even the post hoc critical value of 10.03, while the observed FF of 1.00 for D5D_5 falls short of the uncorrected 5.12 — which is exactly what the book concludes on p. 782.

Reproducing Table 14.6: estimates and standard errors

Now that we have the mathematical framework for our critical values, we need the sample estimates (D‾\bar{D}) and the standard errors (seD‾se_{\bar{D}}). The textbook explores four specific contrasts in Table 14.6 (p. 784):

  • D1: Angle Linear (Main Effect)

  • D9: Angle Linear (Simple Effect: Noise Absent)

  • D11: Angle Linear (Simple Effect: Noise Present)

  • Interaction Contrast: The difference between the two simple effects (D11 – D9)

By fitting intercept-only models in R, we can extract these values. The default confint() method provides the uncorrected 95% confidence intervals, which is what the book’s Table 14.6 reports: in all four cases 95% confidence pertains to a single comparison rather than a collection of them.

HideShow
# Create the specific Interaction Contrast variable from Table 14.6
chapter_14_table_4$NA_NP_0_8 <- chapter_14_table_4$D11 - chapter_14_table_4$D9

# Fit intercept-only models for each contrast
model_D1 <- lm(D1 ~ 1, data=chapter_14_table_4)
model_D9 <- lm(D9 ~ 1, data=chapter_14_table_4)
model_D11 <- lm(D11 ~ 1, data=chapter_14_table_4)
model_Interaction <- lm(NA_NP_0_8 ~ 1, data=chapter_14_table_4)

# Combine Estimates, Standard Errors, and Uncorrected 95% Confidence Intervals
results_table <- data.frame(
  Contrast = c("D1 (Angle Linear Main)", "D9 (Angle Linear | Noise Absent)",
               "D11 (Angle Linear | Noise Present)", "Interaction (D11 - D9)"),
  Estimate = c(coef(model_D1), coef(model_D9), coef(model_D11), coef(model_Interaction)),
  Std_Error = c(summary(model_D1)$coefficients[1, 2], summary(model_D9)$coefficients[1, 2],
                summary(model_D11)$coefficients[1, 2], summary(model_Interaction)$coefficients[1, 2]),
  Lower_95_Uncorrected = c(confint(model_D1)[1], confint(model_D9)[1], confint(model_D11)[1], confint(model_Interaction)[1]),
  Upper_95_Uncorrected = c(confint(model_D1)[2], confint(model_D9)[2], confint(model_D11)[2], confint(model_Interaction)[2])
)

results_table |>
  gt() |>
  fmt_number(columns = -Contrast, decimals = 3) |>
  cols_label(
    Std_Error            = "Std. Error",
    Lower_95_Uncorrected = "Lower 95%",
    Upper_95_Uncorrected = "Upper 95%"
  ) |>
  tab_header(title = md("Confidence intervals for four contrasts in the reaction time data")) |>
  cols_align(align = "center", columns = -Contrast)
Confidence intervals for four contrasts in the reaction time data
Contrast Estimate Std. Error Lower 95% Upper 95%
D1 (Angle Linear Main) 168.000 20.591 121.419 214.581
D9 (Angle Linear | Noise Absent) 66.000 18.868 23.318 108.682
D11 (Angle Linear | Noise Present) 270.000 27.203 208.463 331.537
Interaction (D11 – D9) 204.000 22.271 153.619 254.381
HideShow
#> D1:        168.000, se 20.591, [121.419, 214.581] — matches Table 14.6 (p. 784).
#> D9:         66.000, se 18.868, [ 23.318, 108.682] — matches Table 14.6 (p. 784).
#> D11:       270.000, se 27.203, [208.463, 331.537] — matches Table 14.6 (p. 784).
#> NA_NP_0_8: 204.000, se 22.271, [153.619, 254.381] — matches Table 14.6 (p. 784).

As the book points out (p. 784), the mean of the interaction contrast is exactly the difference between the means of D11D_{11} and D9D_9: 270−66=204270 – 66 = 204. To widen any of these intervals for multiple comparisons, multiply the standard error by the square root of one of the critical values computed above instead of by the tt quantile confint() uses.

14.6 — One Within-Subjects Factor and One Between-Subjects Factor in the Same Design

14.6.1 — Split-Plot Design with Two Levels of the Within-Subjects Factor

A split-plot design (often referred to as a mixed ANOVA) contains both a between-subjects factor and a within-subjects factor.

To illustrate the multivariate approach to this design, we will revisit the perceptual psychology example from Chapter 12: * Between-Subjects Factor (A): Age (Young vs. Old) * Within-Subjects Factor (B): Angle (0° and 8°)

To keep things simple initially, we are only looking at two levels of the within-subjects factor (0° and 8°). Let’s load and format the data of Table 14.7 (p. 786).

HideShow
data(chapter_14_table_7)

# Spell out the Age factor level by level, then set the level order.
chapter_14_table_7 <- chapter_14_table_7 |>
  mutate(Age = case_when(
    Group == 1 ~ "Young",
    Group == 2 ~ "Old"
  )) |>
  mutate(Age = factor(Age, levels = c("Young", "Old")))

head(chapter_14_table_7)
HideShow
  Angle0 Angle8 Group   Age
1    450    630     1 Young
2    390    540     1 Young
3    570    660     1 Young
4    450    720     1 Young
5    510    630     1 Young
6    360    450     1 Young
HideShow
# Cell means, to check the data against the book.
chapter_14_table_7 |>
  group_by(Age) |>
  summarize(Angle0 = mean(Angle0), Angle8 = mean(Angle8))
HideShow
# A tibble: 2 × 3
  Age   Angle0 Angle8
  <fct>  <dbl>  <dbl>
1 Young    477    645
2 Old      543    792
HideShow
#> Young 477 and 645; Old 543 and 792 — matches the Mean rows of Table 14.7 (p. 786).

In any two-factor design, we have three omnibus effects to test:

  1. The main effect of Age (Between-Subjects)

  2. The main effect of Angle (Within-Subjects)

  3. The Age ×\times Angle interaction (Within-Subjects)

14.6.2 — Main Effect of Between-Subjects Factor

To test the main effect of our between-subjects factor (Age), we need to average over the within-subjects factor. We do this by creating a new transformed variable, MM, which is simply the average of the 0° and 8° scores for each participant (Equation 14.37, p. 787).

Mij=(Y1ij+Y2ij)/2M_{ij} = (Y_{1ij} + Y_{2ij}) / 2 Once we compute MM for every participant, testing the main effect of Age is identical to conducting a standard one-way between-subjects ANOVA with MM as the dependent variable, which is Equation 14.40 (p. 787).

HideShow
# Create the M variable (Average of the two angles)
chapter_14_table_7$M <- (chapter_14_table_7$Angle0 + chapter_14_table_7$Angle8) / 2

# Group means on M, the marginal means the test compares.
tapply(chapter_14_table_7$M, chapter_14_table_7$Age, mean)
HideShow
Young   Old 
561.0 667.5 
HideShow
#> 561 and 667.5 — matches the Mean rows of Table 14.8 (p. 788).

# Test the main effect of Age using a one-way ANOVA on M
anova_age_main <- aov(M ~ Age, data = chapter_14_table_7)
summary(anova_age_main)
HideShow
            Df Sum Sq Mean Sq F value  Pr(>F)   
Age          1  56711   56711   9.242 0.00704 **
Residuals   18 110452    6136                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> Residual SS = 110,452.5, the denominator sum the book reports on p. 788, and
#> F = 9.242 with 1 and 18 df, p = .007 — matches book p. 788.

The output gives us an FF value of 9.24 and a pp value of .007. The main effect of Age is statistically significant. Averaging across the angles, older participants have significantly slower reaction times than younger participants.

The same one-way ANOVA on MM as an APA-style table, with sums of squares, df, F, p, and partial η2\eta^2. (apaTables::apa.aov.table() produces only plain text that reads no better than base R in HTML, so the table is built with gt, which formats it properly.)

HideShow
age_tbl <- broom::tidy(anova_age_main)
ss_res  <- age_tbl$sumsq[age_tbl$term == "Residuals"]   # error SS, for eta^2
age_tbl |>
  transmute(
    Source = if_else(term == "Age", "Age", "Within (S/A)"),
    SS = sumsq, df = df, MS = meansq,
    Fstat = statistic, p = p.value,
    # Partial eta-squared: the effect's share of effect-plus-error variance.
    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 &eta;<sup>2</sup>")) |>
  tab_header(title = md("Analysis of variance for the Age main effect (marginal mean *M*)")) |>
  cols_align(align = "center", columns = -Source)
Analysis of variance for the Age main effect (marginal mean M)
Source SS df MS F p Partial η2
Age 56,711.25 1 56,711.25 9.24 0.0070 0.339
Within (S/A) 110,452.50 18 6,136.25


14.6.3 — Within-Subjects Effects

The remaining two effects (Angle main effect and Age ×\times Angle interaction) both involve the difference between the 0° and 8° conditions. To test these, we create another transformed variable, DD, representing the difference score for each participant (Equation 14.41, p. 788).

Dij=Y2ij−Y1ijD_{ij} = Y_{2ij} – Y_{1ij}

HideShow
# Create the D variable (Difference between 8° and 0°)
chapter_14_table_7$D <- chapter_14_table_7$Angle8 - chapter_14_table_7$Angle0

# Displaying M and D to mirror Table 14.8
chapter_14_table_7[, c("Age", "M", "D")]
HideShow
     Age   M   D
1  Young 540 180
2  Young 465 150
3  Young 615  90
4  Young 585 270
5  Young 570 120
6  Young 405  90
7  Young 615 210
8  Young 645 270
9  Young 585 150
10 Young 585 150
11   Old 555 270
12   Old 705 210
13   Old 570 240
14   Old 705 150
15   Old 600 360
16   Old 735 270
17   Old 750 240
18   Old 600 240
19   Old 795 210
20   Old 660 300
HideShow
# Group means on D.
tapply(chapter_14_table_7$D, chapter_14_table_7$Age, mean)
HideShow
Young   Old 
  168   249 
HideShow
#> 168 and 249 — matches the Mean rows of Table 14.8 (p. 788).

Notice that every participant has a positive DD score, meaning everyone was slower at 8° than at 0°. Also notice that the DD scores seem generally larger for the Old group than the Young group.

14.6.4 — Test of the Interaction

The Age ×\times Angle interaction tests whether the effect of Angle (our DD variable) differs depending on Age. If there is no interaction, the mean DD score should be the same for both Young and Old participants.

We can test this by running a standard one-way ANOVA on the DD variable.

HideShow
# Test the interaction by running an ANOVA on the difference score (D)
# This is Equation 14.44 (p. 789).
anova_interaction <- aov(D ~ Age, data = chapter_14_table_7)
summary(anova_interaction)
HideShow
            Df Sum Sq Mean Sq F value  Pr(>F)   
Age          1  32805   32805   8.807 0.00825 **
Residuals   18  67050    3725                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> Residual SS = 67,050, the denominator sum reported on p. 790, and F = 8.807
#> with 1 and 18 df, p = .008 — matches book p. 790.

The FF value is 8.81 with a pp value of .008. The interaction is statistically significant. The angle effect is significantly stronger for older participants (D‾2=249\bar{D}_2 = 249) than for younger ones (D‾1=168\bar{D}_1 = 168), as the book notes on p. 790.

14.6.5 — Within-Subjects Main Effect

The final test is the main effect of Angle. This tests whether the grand mean of DD (D‾\bar{D}) is significantly different from zero, averaging across Age.

The book proves that the FF statistic for this main effect can be calculated by comparing the sum of squares for the angle main effect (ND‾2N\bar{D}^2, Equation 14.47, p. 791) to the error term from our full model (which happens to be the exact same error term used in the interaction test above).

Let’s compute this exactly as Equation 14.48 (p. 792) defines it:

HideShow
# Calculate N, a, and grand mean of D
N <- nrow(chapter_14_table_7)
a <- length(levels(chapter_14_table_7$Age))
D_bar <- mean(chapter_14_table_7$D)

# Extract the Sum of Squared Errors from our previous interaction model
# This is the denominator sum of squares: sum((Dij - Dj_bar)^2)
SS_error <- sum(resid(anova_interaction)^2)
df_error <- N - a
MS_error <- SS_error / df_error

# Calculate the Sum of Squares for the Angle Main Effect
SS_angle <- N * (D_bar^2)

# Compute the F-statistic
F_angle <- SS_angle / MS_error

# Compute the p-value
p_angle <- pf(F_angle, df1 = 1, df2 = df_error, lower.tail = FALSE)

# Display results
cat("Sum of Squares (Angle):", SS_angle,
    "\nF-statistic:", F_angle,
    "\np-value:", p_angle, "\n")
HideShow
Sum of Squares (Angle): 869445 
F-statistic: 233.4081 
p-value: 9.479516e-12 
HideShow
#> Dbar = 208.5 and SS = 869,445 — match p. 791; F = 233.41 with 1 and 18 df
#> — matches book p. 792.

The by-hand calculation gives us an FF value of 233.41, matching the text. Participants responded significantly more slowly in the 8° condition compared to the 0° condition.

Notice a structural detail here: the error term for the within-subjects main effect is identical to the error term for the interaction. Both tests use the full-model variance of the DD scores as their denominator, because both use Equation 14.42 as the full model (p. 792).

While the by-hand calculation makes the algebra visible, we can bypass it entirely in practice. Because the Angle main effect tests if the grand mean of DD is zero, we can extract this directly from the (Intercept) of a linear model predicting DD.

However, by default, R uses dummy coding, meaning the intercept tests the mean of the reference group (e.g., Young participants). To test the grand mean, we must change the Age factor to use sum-to-zero contrasts (effect coding).

HideShow
# Change the contrast coding for Age to sum-to-zero
contrasts(chapter_14_table_7$Age) <- contr.sum

# Fit the linear model predicting the Difference score (D) by Age
model_D <- lm(D ~ Age, data = chapter_14_table_7)

# Use car's Anova() (loaded above) to get Type III Sums of Squares
Anova(model_D, type = 3)
HideShow
Anova Table (Type III tests)

Response: D
            Sum Sq Df  F value   Pr(>F)    
(Intercept) 869445  1 233.4081 9.48e-12 ***
Age          32805  1   8.8067 0.008246 ** 
Residuals    67050 18                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> (Intercept) SS = 869,445 — matches book p. 791; F = 233.41 — matches p. 792.
#> Age F = 8.81 — matches book p. 790.

In the output above, the (Intercept) row gives us an FF value of 233.41, which matches the by-hand calculation for the Angle main effect. Additionally, the Age row gives us an FF value of 8.81, which matches the earlier test for the Age ×\times Angle interaction.

This single lm(D ~ Age) model therefore carries both within-subjects effects: its intercept is the grand mean of DD (the Angle main effect) and its Age coefficient is the between-group difference in DD (the Age ×\times Angle interaction). A publication-quality table makes those two pieces easy to read. With modelsummary (the estimate with its standard error beneath, fit statistics at the foot):

HideShow
modelsummary(
  list("D = Angle8 - Angle0" = model_D),
  estimate  = "{estimate}",
  statistic = "({std.error})",   # standard errors in parentheses, APA style
  gof_map   = c("nobs", "aic", "bic", "logLik"),
  stars     = TRUE,
  output    = "gt"               # gt backend, to match the gtsummary table below
)
D = Angle8 – Angle0
(Intercept) 208.500
(13.647)
Age1 -40.500
(13.647)
Num.Obs. 20
AIC 225.1
BIC 228.1
Log.Lik. -109.553
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

The same model as a gtsummary tbl_regression() table, with the intercept shown so the Angle main effect is visible:

HideShow
tbl_regression(model_D, intercept = TRUE) |>
  modify_caption("**Regression of the difference score *D* on Age**")
Regression of the difference score D on Age
Characteristic Beta 95% CI p-value
(Intercept) 209 180, 237 <0.001
Age


    Young -41 -69, -12 0.008
    Old — —
Abbreviation: CI = Confidence Interval
In DMAR: all three split-plot effects in one table

We assembled the three split-plot effects above by hand: an ANOVA on the marginal mean MM for the between-subjects factor, an ANOVA on the difference score DD for the interaction, and an intercept (or by-hand) test for the within-subjects main effect. The DMAR function manova_split_plot() packages this as a single call: it takes the wide data, the names of the within-subjects columns, and the name of the between-subjects factor, and returns the four classical multivariate test statistics (Wilks’s Lambda, Pillai’s trace, Hotelling-Lawley, and Roy’s largest root) for the between-subjects effect (A), the within-subjects effect (B), and their interaction, all in one table.

HideShow
# DMAR is loaded above. Build the wide two-level split-plot data, spelling
# out the between-subjects factor level by level.
mix <- chapter_14_table_7 |>
  as_tibble() |>
  mutate(Age = case_when(
    Group == 1 ~ "Young",
    Group == 2 ~ "Old"
  )) |>
  mutate(Age = factor(Age, levels = c("Young", "Old")))

split_plot_tests <- manova_split_plot(
  data    = mix,
  within  = c("Angle0", "Angle8"),
  between = "Age",
  ss_type = 3
)

# The returned data frame carries the sums-of-squares type as a final row
# rather than as an attribute, so we keep only the rows that are tests.
subset(split_plot_tests, effect != "sum_of_squares_type")
effect statistic_name statistic_value F_approx df_1 df_2 p_value
A Pillai 0.339 9.242 1 18 0.0070
A Wilks 0.661 9.242 1 18 0.0070
A Hotelling-Lawley 0.513 9.242 1 18 0.0070
A Roy 0.513 9.242 1 18 0.0070
B Pillai 0.928 233.408 1 18 < 0.0001
B Wilks 0.072 233.408 1 18 < 0.0001
B Hotelling-Lawley 12.967 233.408 1 18 < 0.0001
B Roy 12.967 233.408 1 18 < 0.0001
interaction Pillai 0.329 8.807 1 18 0.0082
interaction Wilks 0.671 8.807 1 18 0.0082
interaction Hotelling-Lawley 0.489 8.807 1 18 0.0082
interaction Roy 0.489 8.807 1 18 0.0082

Sum of squares: Type III

HideShow
#> A (Age) F = 9.24 matches book p. 788; B (Angle) F = 233.41 matches
#> p. 792 (Equation 14.48); interaction F = 8.81 matches p. 790.

Because the within-subjects factor here has only two levels, s=1s = 1 and the four multivariate statistics all yield the same FF. The A rows reproduce the Age main effect (F=9.24F = 9.24, book p. 788), the B rows reproduce the Angle main effect of Equation 14.48 (F=233.41F = 233.41, p. 792, the value of the by-hand computation above), and the interaction rows reproduce the Age ×\times Angle interaction (F=8.81F = 8.81, p. 790). With equal group sizes, as here, the Type III test that ss_type = 3 asks for and the Type II test agree; with unequal sizes, the Type III test is the test of the unweighted mean of the difference scores, which the book prefers (Chapter 14, note 6, p. 854).

14.6.7 — General a×ba \times b Split-Plot Design

The principles we just used for a design with two within-subjects levels apply when there are more levels, but the analysis becomes truly multivariate. When we only had 0° and 8° conditions, we could get away with a single difference variable (DD). Now we reinstate the 4° condition so that our Angle factor has three levels, giving a design with a=2a = 2 levels of the between-subjects factor and b=3b = 3 levels of the within-subjects factor.

Because our within-subjects factor now has b=3b = 3 levels, we will need to form b−1b – 1 (or 2) DD variables to capture the within-subjects effects, requiring us to test them simultaneously using matrices.

Let’s load the data of Table 14.9 (p. 794).

HideShow
data(chapter_14_table_9)

# Spell out the Age factor level by level, then set the level order.
chapter_14_table_9 <- chapter_14_table_9 |>
  mutate(Age = case_when(
    Group == 1 ~ "Young",
    Group == 2 ~ "Old"
  )) |>
  mutate(Age = factor(Age, levels = c("Young", "Old")))

head(chapter_14_table_9)
HideShow
  Angle0 Angle4 Angle8 Group   Age
1    450    510    630     1 Young
2    390    480    540     1 Young
3    570    630    660     1 Young
4    450    660    720     1 Young
5    510    660    630     1 Young
6    360    450    450     1 Young
HideShow
# Cell means, to check the data against the book.
chapter_14_table_9 |>
  group_by(Age) |>
  summarize(across(Angle0:Angle8, mean))
HideShow
# A tibble: 2 × 4
  Age   Angle0 Angle4 Angle8
  <fct>  <dbl>  <dbl>  <dbl>
1 Young    477    585    645
2 Old      543    654    792
HideShow
#> Young 477, 585, 645; Old 543, 654, 792 — matches the Mean rows of
#> Table 14.9 (p. 794).

14.6.8 — Between-Subjects Main Effect

Just like before, to test the main effect of Age, we collapse across the repeated measures. Regardless of how many within-subjects levels there are, we always form exactly one MM variable representing each participant’s overall mean score:

Mij=(Y1ij+Y2ij+Y3ij)/3M_{ij} = (Y_{1ij} + Y_{2ij} + Y_{3ij}) / 3

HideShow
# Compute M (Mean across all three angles); Equation 14.49, p. 793.
chapter_14_table_9$M <- (chapter_14_table_9$Angle0 + chapter_14_table_9$Angle4 + chapter_14_table_9$Angle8) / 3

# Group means and grand mean on M.
c(tapply(chapter_14_table_9$M, chapter_14_table_9$Age, mean),
  Grand = mean(chapter_14_table_9$M))
HideShow
Young   Old Grand 
  569   663   616 
HideShow
#> 569, 663, and 616 — matches the Group Mean and Grand Mean rows of
#> Table 14.10 (p. 795).

# Test the Age Main Effect using a one-way ANOVA on M
anova_age_main_3 <- aov(M ~ Age, data = chapter_14_table_9)
summary(anova_age_main_3)
HideShow
            Df Sum Sq Mean Sq F value Pr(>F)  
Age          1  44180   44180   7.276 0.0147 *
Residuals   18 109300    6072                 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 7.276 with 1 and 18 df, p = .0147 — matches book p. 794.

The output gives us an FF value of 7.28 and a pp value of .0147, indicating a statistically significant main effect of Age. This is the same FF value the mixed-model (univariate) approach of Chapter 12 produces, as the book notes on p. 795. Because testing the between-subjects effect involves averaging over the within-subjects factor (bypassing sphericity concerns), the multivariate and univariate approaches are mathematically identical here.

14.6.9 — Within-Subjects Effects

To test the within-subjects effects (Angle main effect and Age ×\times Angle interaction), we must form b−1b – 1 difference variables. Since Angle is a quantitative factor with 3 levels, it is highly logical to use orthogonal polynomial contrasts:

  • D1 (Linear Trend): −1(0∘)+0(4∘)+1(8∘)-1(0^\circ) + 0(4^\circ) + 1(8^\circ)

  • D2 (Quadratic Trend): 1(0∘)−2(4∘)+1(8∘)1(0^\circ) – 2(4^\circ) + 1(8^\circ)

HideShow
# Create D1 (Linear) and D2 (Quadratic)
chapter_14_table_9$D1 <- -1*chapter_14_table_9$Angle0 + 0*chapter_14_table_9$Angle4 + 1*chapter_14_table_9$Angle8
chapter_14_table_9$D2 <-  1*chapter_14_table_9$Angle0 - 2*chapter_14_table_9$Angle4 + 1*chapter_14_table_9$Angle8

# Display M, D1, and D2 to mirror Table 14.10
head(chapter_14_table_9[, c("Age", "M", "D1", "D2")])
HideShow
    Age   M  D1   D2
1 Young 530 180   60
2 Young 470 150  -30
3 Young 620  90  -30
4 Young 610 270 -150
5 Young 600 120 -180
6 Young 420  90  -90
HideShow
# Group means and grand means on the two D variables.
rbind(
  tapply(chapter_14_table_9$D1, chapter_14_table_9$Age, mean),
  tapply(chapter_14_table_9$D2, chapter_14_table_9$Age, mean)
)
HideShow
     Young Old
[1,]   168 249
[2,]   -48  27
HideShow
c(D1_grand = mean(chapter_14_table_9$D1), D2_grand = mean(chapter_14_table_9$D2))
HideShow
D1_grand D2_grand 
   208.5    -10.5 
HideShow
#> D1: 168 (Young), 249 (Old), grand 208.5; D2: -48, 27, grand -10.5
#> — matches Table 14.10 (p. 795).

14.6.10 — Within-Subjects Main Effect

The null hypothesis for the Angle main effect states that the grand means of both D1D1 and D2D2 equal zero simultaneously. Because we have multiple DD variables, we can no longer use simple sums of squares; we must compute matrices representing the Sum of Squares and Cross-Products (SSCP).

We need two matrices:E(F)E(F) (Full Model Matrix):

  1. The error matrix capturing how much participants deviate from their own group means on D1D1 and D2D2.

  2. E(R)E(R) (Restricted Model Matrix): The error matrix capturing how much participants deviate from the grand mean assumption (where the true grand mean is hypothesized to be zero).

The code below constructs these matrices directly from the definitions on pp. 797–798.

HideShow
# Extract Group Means and Grand Means for D1 and D2
D1_grand <- mean(chapter_14_table_9$D1)
D2_grand <- mean(chapter_14_table_9$D2)

# Calculate the Full Model Error Matrix E(F)
# e(F) = Participant Score - Group Mean
e1_F <- chapter_14_table_9$D1 - ave(chapter_14_table_9$D1, chapter_14_table_9$Age)
e2_F <- chapter_14_table_9$D2 - ave(chapter_14_table_9$D2, chapter_14_table_9$Age)

# Crossprod creates the SSCP matrix: Sum of squares on diagonal, cross-products off-diagonal
E_F <- crossprod(cbind(e1_F, e2_F))
rownames(E_F) <- colnames(E_F) <- c("D1", "D2")

# Full Model Matrix E(F)
print(E_F)
HideShow
      D1     D2
D1 67050  -9090
D2 -9090 125370
HideShow
#> 67,050 and 125,370 on the diagonal, -9,090 off it — matches E(F) on p. 797.

# Calculate the Restricted Model Matrix E(R)
# The text establishes a shortcut formula: E(R) = E(F) + N * (Grand Mean Matrix)
N <- nrow(chapter_14_table_9)
E_R <- matrix(c(
  E_F[1,1] + N * (D1_grand^2),             # E11(R)
  E_F[1,2] + N * (D1_grand * D2_grand),    # E12(R)
  E_F[2,1] + N * (D1_grand * D2_grand),    # E21(R)
  E_F[2,2] + N * (D2_grand^2)              # E22(R)
), nrow=2)
rownames(E_R) <- colnames(E_R) <- c("D1", "D2")

# Restricted Model Matrix E(R)
print(E_R)
HideShow
       D1     D2
D1 936495 -52875
D2 -52875 127575
HideShow
#> 936,495 and 127,575 on the diagonal, -52,875 off it — matches E(R) on p. 798.

Both matrices match the text. To conduct the statistical test, we compare the “volume” of these matrices using their determinants. The formula for the test statistic (Equation 14.54, p. 798) is:

F=(|E(R)|−|E(F)|)/(b−1)|E(F)|/(N−a−b+2)F = \frac{(|E(R)| – |E(F)|) / (b – 1)}{|E(F)| / (N – a – b + 2)}

HideShow
# Calculate Determinants
det_EF <- det(E_F)
det_ER <- det(E_R)

cat("Determinant of E(F):", format(det_EF, scientific=FALSE), "\n")
HideShow
Determinant of E(F): 8323430400 
HideShow
cat("Determinant of E(R):", format(det_ER, scientific=FALSE), "\n")
HideShow
Determinant of E(R): 116677584000 
HideShow
# Compute the F-statistic
a <- length(levels(chapter_14_table_9$Age)) # 2 levels of Age
b <- 3 # 3 angles

df_num <- b - 1
df_den <- N - a - b + 2

F_angle_multiv <- ((det_ER - det_EF) / df_num) / (det_EF / df_den)
p_angle_multiv <- pf(F_angle_multiv, df1 = df_num, df2 = df_den, lower.tail = FALSE)

cat(
  "\nAngle Main Effect (Multivariate F):\n",
  "F(", df_num, ", ", df_den, ") = ", F_angle_multiv, "\n",
  "p-value = ", p_angle_multiv, "\n",
  sep = ""
)
HideShow

Angle Main Effect (Multivariate F):
F(2, 17) = 110.6527
p-value = 1.791335e-10
HideShow
#> |E(F)| = 8,323,430,400 and |E(R)| = 116,677,584,000 — match p. 798.
#> F = 110.65 with 2 and 17 df, p < .0001 — matches book p. 798.

By dividing the determinants, we arrive at an FF value of 110.65 with 2 and 17 degrees of freedom (p<.0001p < .0001). Thus, the main effect of Angle is highly statistically significant when evaluated via the multivariate matrix approach.

14.6.11 — Test of the Interaction

The final omnibus effect is the interaction between Age and Angle. Like the main effect, this test involves comparing full and restricted models. The full models (E(F)E(F)) are identical to the ones we used for the within-subjects main effect, because they simply represent how much each participant deviates from their own specific group mean on D1D1 and D2D2.

The restricted models (E(R)E(R)), however, change. The interaction null hypothesis states that there are no group differences on the within-subjects differences. This means our restricted model simply predicts that every participant’s score on a DD variable is equal to the grand mean of that variable:

D1ij=μ1+ε1ijD_{1ij} = \mu_1 + \varepsilon_{1ij}

D2ij=μ2+ε2ijD_{2ij} = \mu_2 + \varepsilon_{2ij}

Let’s compute the new E(R)E(R) matrix for the interaction and compare it to the E(F)E(F) matrix we already computed.

HideShow
# Calculate the Restricted Model Errors for the Interaction
# e(R) = Participant Score - Grand Mean
e1_R_int <- chapter_14_table_9$D1 - D1_grand
e2_R_int <- chapter_14_table_9$D2 - D2_grand

# Calculate the Restricted Model Matrix E(R) for Interaction
E_R_int <- crossprod(cbind(e1_R_int, e2_R_int))
rownames(E_R_int) <- colnames(E_R_int) <- c("D1", "D2")

# Restricted Model Matrix E(R) for Interaction
print(E_R_int)
HideShow
      D1     D2
D1 99855  21285
D2 21285 153495
HideShow
#> 99,855 and 153,495 on the diagonal, 21,285 off it — matches the E(R) for the
#> interaction on p. 799.

This matrix differs from the E(R)E(R) used for the main effect, because each participant’s error is taken from the grand mean of the DD variable rather than from the group mean minus the grand mean (p. 799).

The special case F test (a=2a = 2)

Because our between-subjects factor (Age) only has two levels (a=2a = 2), we can use a simplified formula to calculate the FF statistic directly from the determinants of our two matrices (Equation 14.58, p. 800):

F=(|E(R)|−|E(F)|)/(b−1)|E(F)|/(N−b)F = \frac{(|E(R)| – |E(F)|) / (b – 1)}{|E(F)| / (N – b)}

HideShow
# Calculate Determinant of the new E(R)
det_ER_int <- det(E_R_int)

cat(
  "Determinant of E(F): ", format(det_EF, scientific = FALSE), "\n",
  "Determinant of E(R): ", format(det_ER_int, scientific = FALSE), "\n",
  sep = ""
)
HideShow
Determinant of E(F): 8323430400
Determinant of E(R): 14874192000
HideShow
# Compute the F-statistic for the interaction (a = 2 special case)
F_int_special <- ((det_ER_int - det_EF) / (b - 1)) / (det_EF / (N - b))
p_int_special <- pf(F_int_special, df1 = b - 1, df2 = N - b, lower.tail = FALSE)

cat(
  "\nInteraction Effect F-test (a = 2):\n",
  "F(", b - 1, ", ", N - b, ") = ", F_int_special, "\n",
  "p-value = ", p_int_special, "\n",
  sep = ""
)
HideShow

Interaction Effect F-test (a = 2):
F(2, 17) = 6.689727
p-value = 0.007192605
HideShow
#> |E(R)| = 14,874,192,000 — matches p. 801.
#> F = 6.69 with 2 and 17 df, p = .0072 — matches book p. 801.

The output yields an FF value of 6.69 with a pp value of .0072, indicating a significant Age ×\times Angle interaction.

General Multivariate Statistics (Wilks’s Lambda and Pillai–Bartlett)

When a design has more than two levels for the between-subjects factor (a>2a > 2), the simple determinant ratio breaks down, and we must rely on general multivariate test statistics. The book restricts its discussion to two of the four: Wilks’s Lambda (Λ\Lambda) and the Pillai–Bartlett trace (VV).

  1. Wilks’s Lambda (Λ\Lambda)

Wilks’s Lambda is the ratio of the full model’s determinant to the restricted model’s determinant, Λ=|E(F)|/|E(R)|\Lambda = |E(F)| / |E(R)| (Equation 14.60, p. 801). Values closer to 0 indicate larger effects, while values closer to 1 indicate smaller ones.

HideShow
# Calculate Wilks' Lambda
wilks_lambda <- det_EF / det_ER_int
cat("Wilks' Lambda:", wilks_lambda, "\n")
HideShow
Wilks' Lambda: 0.5595887 
  1. Pillai–Bartlett Trace (VV)

This statistic is based on subtracting the full matrix from the restricted matrix to get a Hypothesis matrix (H=E(R)−E(F)H = E(R) – E(F)). We then multiply HH by the inverse of the Total matrix (T=E(R)T = E(R)). The trace (sum of the diagonal elements) of this resulting matrix is Pillai’s VV.

HideShow
# Calculate Pillai-Bartlett Trace
H_matrix <- E_R_int - E_F
T_matrix <- E_R_int

# H multiplied by the inverse of T
HT_inv <- H_matrix %*% solve(T_matrix)

# H * T^-1 Matrix
print(HT_inv)
HideShow
          D1        D2
D1 0.2950662 0.1569726
D2 0.2732095 0.1453450
HideShow
#> .2951, .1570, .2732, .1453 — matches the matrix on p. 803.

# The trace is the sum of the diagonal elements
pillai_V <- sum(diag(HT_inv))
cat("\nPillai-Bartlett Trace (V):", pillai_V, "\n")
HideShow

Pillai-Bartlett Trace (V): 0.4404113 
HideShow
#> V = 0.4404 — matches book p. 804, where Equation 14.63 converts it to the
#> same F of 6.69 with 2 and 17 df.

Built-in Functions

We can bypass the matrix algebra using R’s built-in manova() function. Because the interaction hypothesis states that there are no group differences on the within-subjects differences, running a one-way MANOVA predicting our difference variables (D1D1 and D2D2) by the between-subjects factor (Age) tests the interaction.

HideShow
# Fit the MANOVA model predicting the difference scores by Age
interaction_manova <- manova(cbind(D1, D2) ~ Age, data = chapter_14_table_9)

# Request Wilks' Lambda
summary(interaction_manova, test = "Wilks")
HideShow
          Df   Wilks approx F num Df den Df   Pr(>F)   
Age        1 0.55959   6.6897      2     17 0.007193 **
Residuals 18                                           
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> Wilks = 0.55959 and approx F = 6.69 with 2 and 17 df, p = .0072
#> — matches the by-hand values above and book p. 801.

# Request Pillai-Bartlett Trace
summary(interaction_manova, test = "Pillai")
HideShow
          Df  Pillai approx F num Df den Df   Pr(>F)   
Age        1 0.44041   6.6897      2     17 0.007193 **
Residuals 18                                           
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> Pillai = 0.44041 — matches V = 0.4404 on p. 804, with the same F of 6.69.

Notice how the manova() summary outputs match our by-hand calculations exactly.

Because our design only has two levels for the between-subjects factor, the parameter ss (the smaller of a−1a-1 and b−1b-1) equals 1. Whenever s=1s = 1, all four multivariate test statistics (Wilks’s Lambda, Pillai–Bartlett, Hotelling–Lawley, and Roy’s greatest characteristic root) necessarily agree, which the book establishes on p. 804. This is why the specialized FF test above captures the interaction without the approximations that larger designs require.

14.7 — Measures of Effect

Even though we just used the multivariate (MANOVA) approach to test our within-subjects effects, when it comes to calculating effect sizes, the text recommends reverting to the mixed-model (univariate) formulation of omega squared (ω̂2\hat{\omega}^2).

This is because the mixed-model approach treats individual differences between subjects (SSS/ASS_{S/A}) as a meaningful source of total variance, ensuring our effect sizes reflect the reality of the repeated measures design.

The formulas for the split-plot design are identical to those from Chapter 12, repeated as Equations 12.22 through 12.24 on p. 805:

Main Effect of Between-Subjects Factor (A): ω̂A2=SSA−(a−1)MSS/ASSA+SSS/A+MSS/A\hat{\omega}^2_A = \frac{SS_A – (a-1)MS_{S/A}}{SS_A + SS_{S/A} + MS_{S/A}}

Main Effect of Within-Subjects Factor (B): ω̂B2=(b−1)(MSB−MSB×S/A)SSB+SSB×S/A+SSS/A+MSS/A\hat{\omega}^2_B = \frac{(b-1)(MS_B – MS_{B \times S/A})}{SS_B + SS_{B \times S/A} + SS_{S/A} + MS_{S/A}}

Interaction (AB): ω̂AB2=(a−1)(b−1)(MSAB−MSB×S/A)SSAB+SSB×S/A+SSS/A+MSS/A\hat{\omega}^2_{AB} = \frac{(a-1)(b-1)(MS_{AB} – MS_{B \times S/A})}{SS_{AB} + SS_{B \times S/A} + SS_{S/A} + MS_{S/A}}

While multivariate software doesn’t always output these mixed-model components directly, we can extract them in R by fitting a traditional univariate repeated measures ANOVA model.

HideShow
# Reshape the data to "long" format for univariate ANOVA
chapter_14_long <- reshape(
  as.data.frame(chapter_14_table_9),
  varying = c("Angle0", "Angle4", "Angle8"),
  v.names = "Score",
  timevar = "Angle",
  times = c(0, 4, 8),
  direction = "long"
)
chapter_14_long$Angle <- factor(chapter_14_long$Angle)
chapter_14_long$Participant <- factor(chapter_14_long$id)

# Run the mixed-model ANOVA
univariate_aov <- aov(Score ~ Age * Angle + Error(Participant/Angle), data = chapter_14_long)

# Save the summary object so we can extract values from it
aov_summary <- summary(univariate_aov)

# Extract the components dynamically
# The summary object is a list. [[1]] is the Between-Subjects part, [[2]] is the Within-Subjects part.

# Between-Subjects components (from Error: Participant)
# Row 1 is Age, Row 2 is Residuals (S/A)
SS_A  <- aov_summary$`Error: Participant`[[1]][1, "Sum Sq"]
SS_SA <- aov_summary$`Error: Participant`[[1]][2, "Sum Sq"]
MS_SA <- aov_summary$`Error: Participant`[[1]][2, "Mean Sq"]

# Within-Subjects components (from Error: Participant:Angle)
# Row 1 is Angle, Row 2 is Age:Angle, Row 3 is Residuals (BxS/A)
SS_B   <- aov_summary$`Error: Participant:Angle`[[1]][1, "Sum Sq"]
MS_B   <- aov_summary$`Error: Participant:Angle`[[1]][1, "Mean Sq"]
SS_AB  <- aov_summary$`Error: Participant:Angle`[[1]][2, "Sum Sq"]
MS_AB  <- aov_summary$`Error: Participant:Angle`[[1]][2, "Mean Sq"]
SS_BSA <- aov_summary$`Error: Participant:Angle`[[1]][3, "Sum Sq"]
MS_BSA <- aov_summary$`Error: Participant:Angle`[[1]][3, "Mean Sq"]

# Calculate Omega Squared Values
a <- length(levels(chapter_14_long$Age))   # Dynamically count levels of Age (2)
b <- length(levels(chapter_14_long$Angle)) # Dynamically count levels of Angle (3)

omega_sq_A  <- (SS_A - (a-1)*MS_SA) / (SS_A + SS_SA + MS_SA)
omega_sq_B  <- ((b-1)*(MS_B - MS_BSA)) / (SS_B + SS_BSA + SS_SA + MS_SA)
omega_sq_AB <- ((a-1)*(b-1)*(MS_AB - MS_BSA)) / (SS_AB + SS_BSA + SS_SA + MS_SA)

cat(
  "Omega Squared (Age): ", round(omega_sq_A, 2), "\n",
  "Omega Squared (Angle): ", round(omega_sq_B, 2), "\n",
  "Omega Squared (Age x Angle): ", round(omega_sq_AB, 2), "\n",
  sep = ""
)
HideShow
Omega Squared (Age): 0.24
Omega Squared (Angle): 0.52
Omega Squared (Age x Angle): 0.04
HideShow
#> .24, .52, and .04 — matches the omega squared values on p. 805.

As the text notes on p. 805, the Age (.24) and Angle (.52) main effects account for considerably more variance than the interaction (.04), but the interaction might still be the most theoretically interesting finding.

14.7.1 — Further Investigation of Main Effects

If we want to dig into the within-subjects main effect, we investigate specific DD variables.

For example, our D2D_2 variable represents the quadratic trend of the Angle factor. Let’s isolate the FF test for just the quadratic trend by looking specifically at the D2D_2 components of the E(F)E(F) and E(R)E(R) matrices we computed earlier in Section 14.6.10, which is exactly how the book proceeds on p. 806.

Recall Equation 14.48 for testing a specific DD variable:

F=ND‾2∑j=1a∑i=1n(Dij−D‾j)2/(N−a) F = \frac{N \bar{D}^2}{\sum_{j=1}^a \sum_{i=1}^n (D_{ij} – \bar{D}_j)^2 / (N – a)}

HideShow
# E_F and E_R from section 14.6.10 contain the Error components for D1 and D2
# The [2,2] elements correspond to D2 (the quadratic trend)
E_F_D2 <- E_F[2, 2]
E_R_D2 <- E_R[2, 2]

# The numerator is N * (Grand Mean of D2)^2, which is also E_R - E_F
num_D2 <- E_R_D2 - E_F_D2
cat("EF for D2:", E_F_D2, "\n")
HideShow
EF for D2: 125370 
HideShow
cat("ER for D2:", E_R_D2, "\n")
HideShow
ER for D2: 127575 
HideShow
cat("N * Dbar^2:", num_D2, "\n")
HideShow
N * Dbar^2: 2205 
HideShow
#> 125,370; 127,575; and 2,205 — all three match book p. 806.

# Calculate the F-statistic
df_num_D2 <- 1
df_den_D2 <- N - a

F_D2 <- num_D2 / (E_F_D2 / df_den_D2)
cat("\nF-statistic for Quadratic Trend (D2):", F_D2, "\n")
HideShow

F-statistic for Quadratic Trend (D2): 0.3165829 
HideShow
#> F = 0.32 with 1 and 18 df — matches book p. 806.

# Critical values: per comparison, then the post hoc Roy-Bose value of
# Equation 14.64 (p. 806).
qf(p = 1 - .05, df1 = 1, df2 = N - a)
HideShow
[1] 4.413873
HideShow
#> 4.41 — matches the critical value quoted on p. 807.
(N - a) * (b - 1) * qf(p = 1 - .05, df1 = b - 1, df2 = N - a - b + 2) /
  (N - a - b + 2)
HideShow
[1] 7.605594
HideShow
#> 7.60 — matches the post hoc critical value computed on p. 807.

The FF value is 0.32. Against either critical value it is far from significant, confirming there is no meaningful quadratic trend in the data.

We can bypass the by-hand arithmetic entirely with base R’s linear modeling. When software performs a multivariate test, it is simultaneously running univariate ANOVAs on each individual DD variable; the book notes on p. 807 that this is how statistical packages report the univariate FF of 233.41 for D1D_1 and 0.32 for D2D_2 alongside the multivariate FF of 110.65.

To see these in R, we can wrap our multivariate linear model in summary.aov(). However, there is a crucial distinction to keep in mind:

  1. To test the Interaction (Age ×\times Angle components): We look at the Age effect output by summary.aov().

  2. To test the Within-Subjects Main Effect (Angle components): We must look at the Intercept of these models, because testing if the grand mean of a difference score equals zero is exactly what an intercept test evaluates.

Let’s illustrate this. We will fit the linear model and explicitly ask for the Intercept test to find our 0.32 F-value.

HideShow
# Sum-to-zero coding for Age, so that the Intercept is the unweighted grand
# mean of each D variable rather than the mean of the first group. This is set
# on the Age column itself, visibly, because it changes what the output means.
contrasts(chapter_14_table_9$Age) <- contr.sum

# Fit the multivariate linear model predicting D1 and D2
multivariate_lm <- lm(cbind(D1, D2) ~ Age, data = chapter_14_table_9)

# View the univariate ANOVA for D2 (Quadratic Trend)
# We use summary() to see the t-test for the Intercept.
# Remember: F = t^2 for tests with 1 numerator degree of freedom!
summary(multivariate_lm)$`Response D2`
HideShow

Call:
lm(formula = D2 ~ Age, data = chapter_14_table_9)

Residuals:
   Min     1Q Median     3Q    Max 
-132.0  -64.5   18.0   63.0  138.0 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)   -10.50      18.66  -0.563   0.5806  
Age1          -37.50      18.66  -2.009   0.0597 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 83.46 on 18 degrees of freedom
Multiple R-squared:  0.1832,    Adjusted R-squared:  0.1379 
F-statistic: 4.038 on 1 and 18 DF,  p-value: 0.05972
HideShow
# The intercept t, squared, is the F for the quadratic trend.
summary(multivariate_lm)$`Response D2`$coefficients["(Intercept)", "t value"]^2
HideShow
[1] 0.3165829
HideShow
#> 0.32 — matches book p. 806.

In the output for Response D2, the (Intercept) row has a tt value of about −0.563-0.563, and squaring it returns the 0.32 we computed from the matrices.

If we wanted to test the univariate simple effects for the interaction instead, we would just run:

HideShow
# This will output the ANOVA tables showing the Age effect on D1 and D2
summary.aov(multivariate_lm)
HideShow
 Response D1 :
            Df Sum Sq Mean Sq F value   Pr(>F)   
Age          1  32805   32805  8.8067 0.008246 **
Residuals   18  67050    3725                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

 Response D2 :
            Df Sum Sq Mean Sq F value  Pr(>F)  
Age          1  28125   28125   4.038 0.05972 .
Residuals   18 125370    6965                  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> The Age line of Response D2 gives F = 4.038, p = .0597, the interaction
#> contrast the book reports on p. 814.

This breaks the omnibus multivariate models down into specific contrast tests without cross-multiplying any error matrices by hand.

14.7.2 — Further Investigation of an Interaction—Simple Effects

Because our Age ×\times Angle interaction was significant, we must break it down. We can test the between-subjects factor at fixed levels of the within-subjects factor (Age within a specific Angle), or the within-subjects factor at fixed levels of the between-subjects factor (Angle within a specific Age).

14.7.3 — Between-Subjects Effects at a Fixed Level of Within-Subjects Factor

If we want to know whether Young and Old participants differ specifically at the 0° Angle, the within-subjects nature of the design drops out: only one score per participant is involved, so this is a one-way between-subjects ANOVA on the Angle0 column (p. 807). The book describes the procedure here rather than reporting a value for this particular test, so there is nothing to confirm against the printed page.

HideShow
# Simple effect of Age at 0 degrees
simple_effect_age_at_0 <- aov(Angle0 ~ Age, data = chapter_14_table_9)
summary(simple_effect_age_at_0)
HideShow
            Df Sum Sq Mean Sq F value Pr(>F)  
Age          1  21780   21780   3.161 0.0923 .
Residuals   18 124020    6890                 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

As the book stresses on p. 808, this is a test on an original YY variable, not on a DD variable, and the error term it uses is MSS/A at BkMS_{S/A \text{ at } B_k} — the error term that does not require sphericity, and the one SAS, SPSS, and R all use by default.

14.7.4 — Within-Subjects Effects at a Fixed Level of Between-Subjects Factor

This calculation is more complex in a multivariate framework. Let’s test the Angle effect only for the Younger participants. We treat this as a one-way within-subjects design using just the Young group.

Let’s compute the E(F)1E(F)_1 and E(R)1E(R)_1 matrices specifically for the Young participants.

HideShow
# Filter data for Young participants
young_data <- subset(chapter_14_table_9, Age == "Young")
n1 <- nrow(young_data)

# Get Means for Young participants
D1_mean_young <- mean(young_data$D1)
D2_mean_young <- mean(young_data$D2)

# Calculate E(F)_1 (Deviations from Young means)
e1_F_young <- young_data$D1 - D1_mean_young
e2_F_young <- young_data$D2 - D2_mean_young
E_F_1 <- crossprod(cbind(e1_F_young, e2_F_young))

# Calculate E(R)_1 (Deviations from zero, since null is no effect)
# Mathematically: E(R)_1 = E(F)_1 + n1 * (Mean Vector * Mean Vector^T)
E_R_1 <- matrix(c(
  E_F_1[1,1] + n1 * (D1_mean_young^2),
  E_F_1[1,2] + n1 * (D1_mean_young * D2_mean_young),
  E_F_1[2,1] + n1 * (D1_mean_young * D2_mean_young),
  E_F_1[2,2] + n1 * (D2_mean_young^2)
), nrow=2)

# E(F)_1 for Young Participants
print(E_F_1)
HideShow
           e1_F_young e2_F_young
e1_F_young      38160       3240
e2_F_young       3240      77760
HideShow
#> 38,160 and 77,760 on the diagonal, 3,240 off it — matches E(F)_1 on p. 809.

# E(R)_1 for Young Participants
print(E_R_1)
HideShow
       [,1]   [,2]
[1,] 320400 -77400
[2,] -77400 100800
HideShow
#> 320,400 and 100,800 on the diagonal, -77,400 off it — matches E(R)_1 on p. 809.

Both matrices match the book. The book also points out (p. 809) that the overall E(F)E(F) is the sum of the separate within-group matrices, which is worth checking because it is what makes the pooled error term below possible.

HideShow
old_data <- subset(chapter_14_table_9, Age == "Old")
E_F_2 <- crossprod(cbind(old_data$D1 - mean(old_data$D1),
                         old_data$D2 - mean(old_data$D2)))
print(E_F_2)
HideShow
       [,1]   [,2]
[1,]  28890 -12330
[2,] -12330  47610
HideShow
#> 28,890 and 47,610 on the diagonal, -12,330 off it — matches E(F)_2 on p. 809.

# E(F) = E(F)_1 + E(F)_2
E_F_1 + E_F_2
HideShow
           e1_F_young e2_F_young
e1_F_young      67050      -9090
e2_F_young      -9090     125370
HideShow
#> 67,050 and 125,370 on the diagonal, -9,090 off it: the E(F) of Section
#> 14.6.10, as the book states on p. 809.

Now we calculate the FF test. There are two approaches to do so.

  1. Separate Error Term (Equation 14.65, p. 809)

F=(|E(R)1|−|E(F)1|)/(b−1)|E(F)1|/(n1−b+1) F = \frac{(|E(R)_1| – |E(F)_1|) / (b – 1)}{|E(F)_1| / (n_1 – b + 1)}

HideShow
det_EF1 <- det(E_F_1)
det_ER1 <- det(E_R_1)

c(det_E_F_1 = det_EF1, det_E_R_1 = det_ER1)
HideShow
  det_E_F_1   det_E_R_1 
 2956824000 26305560000 
HideShow
#> 2,956,824,000 and 26,305,560,000 — match the determinants on p. 809.

F_sep <- ((det_ER1 - det_EF1) / (b - 1)) / (det_EF1 / (n1 - b + 1))
p_sep <- pf(F_sep, df1 = b - 1, df2 = n1 - b + 1, lower.tail = FALSE)

cat("F-test (Separate Error Term): F = ", F_sep, ", p = ", p_sep, "\n")
HideShow
F-test (Separate Error Term): F =  31.58624 , p =  0.000159629 
HideShow
#> F = 31.59 with 2 and 8 df, p = .0002 — matches book p. 809.

The result (F=31.59F = 31.59, p=.0002p = .0002) shows a highly significant Angle effect for the Young group.

  1. Pooled Error Term (Alternative)

If we assume the covariance matrices are equal in the population (Homogeneity of Covariance), we gain statistical power by using the overall E(F)E(F) matrix – calculated from all participants – as our error term.

The formula for the pooled FF statistic (Equation 14.66, p. 810) substitutes our targeted hypothesis matrix into the overall error space:

F=(|E(F)+E(R)j−E(F)j|−|E(F)|)/(b−1)|E(F)|/(N−a−b+2) F = \frac{(|E(F) + E(R)_j – E(F)_j| – |E(F)|) / (b – 1)}{|E(F)| / (N – a – b + 2)}

HideShow
# E_F and det_EF are the full-sample matrix and determinant from Section 14.6.10.

# The text defines a specific matrix addition/subtraction:
Pooled_Hypothesis_Matrix <- E_F + E_R_1 - E_F_1

print(Pooled_Hypothesis_Matrix)
HideShow
       D1     D2
D1 349290 -89730
D2 -89730 148410
HideShow
#> 349,290 and 148,410 on the diagonal, -89,730 off it — matches p. 810.

det_pooled_hyp <- det(Pooled_Hypothesis_Matrix)
cat("Determinant of |E(F) + E(R)1 - E(F)1|:", format(det_pooled_hyp, scientific=FALSE), "\n")
HideShow
Determinant of |E(F) + E(R)1 - E(F)1|: 43786656000 
HideShow
#> 43,786,656,000 — matches book p. 810.

# Calculate the F-statistic using the overall det_EF computed in 14.6.10
F_pooled <- ((det_pooled_hyp - det_EF) / (b - 1)) / (det_EF / (N - a - b + 2))
p_pooled <- pf(F_pooled, df1 = b - 1, df2 = N - a - b + 2, lower.tail = FALSE)

cat("F-test (Pooled Error Term): F = ", F_pooled, ", p = ", p_pooled, "\n")
HideShow
F-test (Pooled Error Term): F =  36.21553 , p =  7.433013e-07 
HideShow
#> F = 36.22 with 2 and 17 df, p < .0001 — matches book p. 810.

Using the pooled error term gives us F=36.22F = 36.22 with p<.0001p < .0001, which is somewhat more powerful because of the additional denominator degrees of freedom (17 instead of 8). However, as the textbook warns on pp. 810–811, the pooled test is not robust to violations of the homogeneity of covariance matrices assumption, and Box’s MM test is of little practical help in deciding. With samples of perhaps 40 or more per level of AA, the separate error term is preferable because the extra degrees of freedom from pooling are then inconsequential.

14.7.5 — Cell Mean Comparisons

Because we found a significant overall Angle effect for the younger participants, it makes sense to drill down even further. Let’s test a specific contrast (the quadratic trend) strictly for the younger participants.

This test is essentially a one-way within-subjects contrast. However, just like in the previous section, we must choose between a separate error term (using only the data from the younger participants) and a pooled error term (using error data across all participants).

Method 1: separate error term

The FF statistic for testing a specific DD variable at a specific level of AA (AjA_j) using a separate error term is Equation 14.67 (p. 812): F=njD‾2j2sD2j2F = \frac{n_j \bar{D}_{2j}^2}{s_{D2j}^2}

Let’s calculate this in R using the young_data we created earlier.

HideShow
# Get the sample size, mean, and variance of D2 for Young participants
n1 <- nrow(young_data)
Dbar_21 <- mean(young_data$D2)
s2_D21 <- var(young_data$D2)

cat("Mean of D2 (Young):", Dbar_21, "\n",
    "Variance of D2 (Young):", s2_D21, "\n")
HideShow
Mean of D2 (Young): -48 
 Variance of D2 (Young): 8640 
HideShow
#> -48 and 8,640 — match book p. 812.

# Calculate the F-statistic
F_cell_sep <- (n1 * Dbar_21^2) / s2_D21
p_cell_sep <- pf(F_cell_sep, df1 = 1, df2 = n1 - 1, lower.tail = FALSE)

cat("\nF-statistic (Separate Error):", round(F_cell_sep, 2), "\n")
HideShow

F-statistic (Separate Error): 2.67 
HideShow
cat("p-value:", round(p_cell_sep, 4), "\n")
HideShow
p-value: 0.1369 
HideShow
#> F = 2.67 with 1 and 9 df — matches book p. 812.

# The book also reads these numbers off the diagonals of E(F)_1 and E(R)_1:
# the numerator is the difference of the [2, 2] elements and the denominator
# divides the E(F)_1 element by n1 - 1.
c(numerator = E_R_1[2, 2] - E_F_1[2, 2], denominator = E_F_1[2, 2] / (n1 - 1))
HideShow
  numerator denominator 
      23040        8640 
HideShow
#> 23,040 and 8,640 — match book p. 812, and their ratio is again 2.67.

Testing whether the mean of a difference score equals zero for a single group is the definition of a one-sample tt-test, so the same result is available directly.

HideShow
t.test(
  x           = young_data$D2,
  mu          = 0,
  alternative = "two.sided",
  conf.level  = 0.95
)
HideShow

    One Sample t-test

data:  young_data$D2
t = -1.633, df = 9, p-value = 0.1369
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -114.49357   18.49357
sample estimates:
mean of x 
      -48 
HideShow
#> t = -1.633, and (-1.633)^2 = 2.67 — matches the F on p. 812.
Method 2: pooled error term

If we assume homogeneity of variance across the age groups, we can pool the variance of D2D_2 across both groups. We substitute the separate variance with the pooled variance, sD2p2s_{D2p}^2 (also known as the mean square within for the DD variable, MSS/A(D)MS_{S/A(D)}). This is Equation 14.68 (p. 812).

F=njD‾2j2sD2p2 F = \frac{n_j \bar{D}_{2j}^2}{s_{D2p}^2}

HideShow
# The Older participants' variance on D2 (old_data was built above)
s2_D22 <- var(old_data$D2)

# Calculate pooled variance (unweighted average, since n is equal)
s2_D2p <- (s2_D21 + s2_D22) / 2
cat("Variance of D2 (Old):", s2_D22, "\n")
HideShow
Variance of D2 (Old): 5290 
HideShow
cat("Pooled Variance of D2:", s2_D2p, "\n")
HideShow
Pooled Variance of D2: 6965 
HideShow
#> 5,290 and 6,965 — match book p. 812.

# The same pooled variance is the [2, 2] element of E(F) divided by N - a.
E_F[2, 2] / (N - a)
HideShow
[1] 6965
HideShow
#> 6,965 — the equivalence the book points out on p. 812.

# Calculate the F-statistic
F_cell_pool <- (n1 * Dbar_21^2) / s2_D2p
# Denominator df is N - a (20 - 2 = 18)
p_cell_pool <- pf(F_cell_pool, df1 = 1, df2 = N - a, lower.tail = FALSE)

cat(
  "\nF-statistic (Pooled Error):", round(F_cell_pool, 2), "\n",
  "p-value:", round(p_cell_pool, 4), "\n"
)
HideShow

F-statistic (Pooled Error): 3.31 
 p-value: 0.0856 
HideShow
#> F = 3.31 with 1 and 18 df — matches book p. 812.

Using the pooled error gives us an FF value of 3.31. As discussed previously, deciding which critical value to use for these tests depends on whether they were planned (Bonferroni) or post hoc, in which case the Roy–Bose values of Equations 14.69 and 14.70 (p. 813) apply, the first for the separate error term and the second for the pooled one.

14.7.6 — Interaction Contrasts

Instead of looking at the simple effect of the quadratic trend within one group, we can evaluate if the quadratic trend is significantly different between the younger and older participants with an interaction contrast.

Because an interaction contrast effectively reduces the within-subjects factor down to a single DD variable (the quadratic trend difference), the within-subjects nature of the design vanishes! We are left comparing two independent groups on a single score.

The formula from Chapter 4 for testing a specific between-group comparison applies directly, which the book gives as Equation 14.73 (p. 814):

F=ψ̂2MSS/A(D)∑j=1a(cj2/nj) F = \frac{\hat{\psi}^2}{MS_{S/A}(D) \sum_{j=1}^a (c_j^2/n_j)}

Let’s define our contrast (ψ̂\hat{\psi}) as the difference between the mean D2D_2 score for younger participants and the mean D2D_2 score for older participants (c1=1c_1 = 1, c2=−1c_2 = -1).

HideShow
# Get the mean D2 score for Older participants
Dbar_22 <- mean(old_data$D2)

# Calculate the contrast (psi_hat)
psi_hat <- Dbar_21 - Dbar_22
cat("Contrast (Psi Hat):", psi_hat, "\n")
HideShow
Contrast (Psi Hat): -75 
HideShow
#> -48 - 27 = -75 — matches book p. 814.

MS_SA_D <- s2_D2p # We already calculated MS_S/A(D) as the pooled variance (s2_D2p)


# Calculate the F-statistic
# Contrast weights: c1 = 1, c2 = -1. n1 = 10, n2 = 10.
sum_cj2_nj <- (1^2 / 10) + ((-1)^2 / 10)

F_interaction_contrast <- (psi_hat^2) / (MS_SA_D * sum_cj2_nj)
p_interaction_contrast <- pf(F_interaction_contrast, df1 = 1, df2 = N - a, lower.tail = FALSE)

cat(
  "\nF-statistic (Interaction Contrast):", round(F_interaction_contrast, 2), "\n",
  "p-value:", round(p_interaction_contrast, 4), "\n"
)
HideShow

F-statistic (Interaction Contrast): 4.04 
 p-value: 0.0597 
HideShow
#> F = 4.04 with 1 and 18 df, p = .0597 — matches book p. 814.
The ANOVA shortcut

The text notes that testing an interaction contrast when AA has two levels is identical to running a standard one-way ANOVA (or independent samples tt-test) on the DD variable (p. 814). We can show this by running aov() on the D2D_2 column using Age as the predictor:

HideShow
# Run a standard univariate ANOVA on the D2 variable
interaction_contrast_aov <- aov(D2 ~ Age, data = chapter_14_table_9)
summary(interaction_contrast_aov)
HideShow
            Df Sum Sq Mean Sq F value Pr(>F)  
Age          1  28125   28125   4.038 0.0597 .
Residuals   18 125370    6965                 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
#> F = 4.038 with 1 and 18 df, p = .0597 — matches book p. 814, and the
#> residual mean square of 6,965 is MS_S/A(D) again.

Here the FF value is 4.04, the same as the contrast computation above, with p=.0597p = .0597. Treating the difference score as an ordinary dependent variable removes the need for the algebra altogether. For a post hoc test the critical value would come from Equation 14.75 (p. 814), which is the a=2a = 2 form of the Roy–Bose value.

14.8 — Confidence Intervals for Contrasts

Now that we have covered hypothesis testing in split-plot designs using the multivariate approach, we turn to forming confidence intervals. The general form of a confidence interval here is identical to what we have seen in previous chapters:

ψ̂±(CV)(seψ̂)\hat{\psi} \pm (\sqrt{CV})(se_{\hat{\psi}})

Where: * ψ̂\hat{\psi} is the estimated value of the contrast. * CVCV is the appropriate critical value (often an FF or t2t^2 value). * seψ̂se_{\hat{\psi}} is the standard error of the contrast.

The complexities in a split-plot design arise because we can base our contrast on different types of variables: an M variable (marginal means), a D variable (difference scores), or a Y variable (a single column/level of the within-subjects factor).

Depending on the research question, we might want to form confidence intervals for: 1. A contrast among levels of the between-subjects marginal mean.

  1. A contrast among levels of the within-subjects marginal mean.

  2. A contrast among levels of the within-subjects factor at a specific level of the between-subjects factor (Simple effect of within).

  3. A contrast among levels of the between-subjects factor at a specific level of the within-subjects factor (Simple effect of between).

  4. An interaction contrast.

Let’s reproduce the five specific confidence intervals the book shows in Table 14.12 (p. 816) and discusses one at a time on pp. 817–819. All five assume a per-comparison alpha level of .05.

First, we need to set up the proper dependent variables. The text operationalizes the quadratic trend with coefficients of −0.5-0.5, 1.01.0, and −0.5-0.5 (p. 818), which is the scaling its reported estimates use.

HideShow
# Define the specific Quadratic D variable used in Table 14.12
# Coefficients: 0 degrees (-0.5), 4 degrees (1.0), 8 degrees (-0.5)
chapter_14_table_9$D_quad <-
  -0.5 * chapter_14_table_9$Angle0 +
   1.0 * chapter_14_table_9$Angle4 -
   0.5 * chapter_14_table_9$Angle8

# Put "Old" first so that every two-group contrast below is Old minus Young,
# the direction in which the book reports these intervals. Re-creating the
# factor resets its contrasts, so the sum-to-zero coding is set again.
chapter_14_table_9$Age <- factor(chapter_14_table_9$Age, levels = c("Old", "Young"))
contrasts(chapter_14_table_9$Age) <- contr.sum
  1. Levels of Between-Subjects Marginal Means

Question: What is the age difference in reaction time, averaging over all angles?

Variable: MM (The mean across the three angles for each participant).

Because we are looking at the MM variable, we have effectively collapsed our data into a simple between-subjects design. We can run a standard independent samples tt-test, which is Equation 4.40 applied to MM (p. 817).

HideShow
# Between-Subjects Marginal Mean (Age diff on M)
ci_1 <- t.test(M ~ Age, data = chapter_14_table_9,
               var.equal = TRUE, alternative = "two.sided", conf.level = 0.95)

cat(
  "Estimate:", round(ci_1$estimate[1] - ci_1$estimate[2], 2), "\n",
  "Standard Error:", round(ci_1$stderr, 2), "\n",
  "t-value:", round(abs(ci_1$statistic), 2), "\n",
  "95% CI: [", round(ci_1$conf.int[1], 2), ",",
  round(ci_1$conf.int[2], 2), "]\n"
)
HideShow
Estimate: 94 
 Standard Error: 34.85 
 t-value: 2.7 
 95% CI: [ 20.79 , 167.21 ]
HideShow
#> Estimate 94, t = 2.70, 95% CI [20.79, 167.21] — matches book p. 817.

The estimate is 94 (663−569=94663 – 569 = 94). The tt value squared (2.702=7.292.70^2 = 7.29) agrees within rounding error with the FF value of 7.28 from our Age main effect test, which is the first of the two verification checks the book recommends on p. 817.

  1. Levels of Within-Subjects Marginal Means

Question: What is the quadratic trend for the angle factor, averaging over age?

Variable: DquadD_{quad} (Evaluated against a population mean of 0).

Averaging over groups, we are testing whether the grand mean of this difference score is zero. The error term for that test is not the total variance of DquadD_{quad}: as Equation 14.48 shows, it is the within-group variance, because the full model for a DD variable in a split-plot design keeps the between-subjects effect in it. With sum-to-zero coding, the intercept of lm(D_quad ~ Age) is the unweighted grand mean and its standard error is MSS/A(D)/N\sqrt{MS_{S/A(D)}/N}, so confint() gives the interval directly, with N−a=18N – a = 18 degrees of freedom.

HideShow
# Within-Subjects Marginal Mean (Quadratic trend on D_quad), pooled error
model_D_quad <- lm(D_quad ~ Age, data = chapter_14_table_9)

summary(model_D_quad)$coefficients["(Intercept)", ]
HideShow
  Estimate Std. Error    t value   Pr(>|t|) 
 5.2500000  9.3307288  0.5626570  0.5806094 
HideShow
confint(model_D_quad)["(Intercept)", ]
HideShow
    2.5 %    97.5 % 
-14.35313  24.85313 
HideShow
#> Estimate 5.25, se 9.33, t = 0.56, 95% CI [-14.35, 24.85] — matches book p. 818.

# The marginal means of Angle that produce the estimate of 5.25.
colMeans(chapter_14_table_9[, c("Angle0", "Angle4", "Angle8")])
HideShow
Angle0 Angle4 Angle8 
 510.0  619.5  718.5 
HideShow
#> 510, 619.5, and 718.5 — matches the marginal means on p. 818, and
#> -0.5(510) + 1(619.5) - 0.5(718.5) = 5.25.

The interval [−14.35,24.85][-14.35, 24.85] contains 0, consistent with the nonsignificant t=0.56t = 0.56, whose square is the FF of 0.32 we reported for the marginal quadratic trend. A one-sample tt-test on D_quad would use the total variance instead of the within-group variance and so would not reproduce the book’s interval.

  1. Within-Subjects Factor at a Specified Level of Between-Subjects Factor

Question: What is the quadratic trend specifically for younger participants?

Variable: DquadD_{quad} (Evaluated only for the “Young” group, but using a pooled error term).

As the book explains on p. 818, the standard error here can be based on the younger participants alone or on all participants if homogeneity of variance is assumed; Table 14.12 reports the version that uses all participants. We extract the pooled mean square within (MSWMS_W) from the same linear model as above.

HideShow
# Simple effect of within (Quadratic angle for Young, pooled error)
# Extract data for just the young group
young_D_quad <- subset(chapter_14_table_9, Age == "Young")$D_quad

# Get pooled variance from a linear model
lm_pooled <- lm(D_quad ~ Age, data = chapter_14_table_9)
MSW_pooled <- anova(lm_pooled)["Residuals", "Mean Sq"]

# Calculate SE and CI directly, to enforce the pooled error
estimate_3 <- mean(young_D_quad)
n_young <- length(young_D_quad)
se_3 <- sqrt(MSW_pooled / n_young)
t_val_3 <- estimate_3 / se_3
df_pooled <- lm_pooled$df.residual
tcrit <- qt(0.975, df = df_pooled)

cat(
  "Estimate:", round(estimate_3, 2), "\n",
  "Standard Error:", round(se_3, 2), "\n",
  "t-value:", round(t_val_3, 2), "\n",
  "95% CI: [", round(estimate_3 - tcrit * se_3, 2), ",",
  round(estimate_3 + tcrit * se_3, 2), "]\n"
)
HideShow
Estimate: 24 
 Standard Error: 13.2 
 t-value: 1.82 
 95% CI: [ -3.72 , 51.72 ]
HideShow
#> Estimate 24, t = 1.82, 95% CI [-3.72, 51.72] — matches book p. 818, and
#> 1.82^2 is the F of 3.31 reported for this effect on p. 812.
  1. Between-Subjects Factor at a Specified Level of Within-Subjects Factor

Question: What is the age difference specifically at the 8° angle?

Variable: YY (The raw Angle8 column).

Because we are only looking at one specific angle condition, the within-subjects nature of the design drops out entirely. We test this using a standard independent samples tt-test on that single column.

HideShow
# Simple effect of between (Age diff at 8 degrees)
ci_4 <- t.test(Angle8 ~ Age, data = chapter_14_table_9,
               var.equal = TRUE, alternative = "two.sided", conf.level = 0.95)

cat(
  "Estimate:", round(ci_4$estimate[1] - ci_4$estimate[2], 2), "\n",
  "Standard Error:", round(ci_4$stderr, 2), "\n",
  "t-value:", round(abs(ci_4$statistic), 2), "\n",
  "95% CI: [", round(ci_4$conf.int[1], 2), ",",
  round(ci_4$conf.int[2], 2), "]\n"
)
HideShow
Estimate: 147 
 Standard Error: 38.07 
 t-value: 3.86 
 95% CI: [ 67.03 , 226.97 ]
HideShow
#> Estimate 147 (792 - 645), 95% CI [67.03, 226.97] — matches book p. 818.

Interpretation: we are 95% confident that at 8°, older adults take between 67.03 ms and 226.97 ms longer to react than younger adults. The estimate of 147 is the difference between the mean values of 792 and 645 in Table 14.9, which is the book’s second verification check — the contrast estimate is the intended linear combination of cell means.

  1. Interaction Contrast

Question: Is the linear trend for angle different for older vs. younger participants?

Variable: D1D_1 (The linear difference score, tested across the Age groups).

An interaction contrast effectively reduces the within-subjects repeated measures down to a single difference score (D1D_1). We then compare the two independent groups on this score.

HideShow
# 5. Interaction Contrast (Age diff in linear trend D1)
ci_5 <- t.test(D1 ~ Age, data = chapter_14_table_9,
               var.equal = TRUE, alternative = "two.sided", conf.level = 0.95)

cat(
  "Estimate:", round(ci_5$estimate[1] - ci_5$estimate[2], 2), "\n",
  "Standard Error:", round(ci_5$stderr, 2), "\n",
  "t-value:", round(abs(ci_5$statistic), 2), "\n",
  "95% CI: [", round(ci_5$conf.int[1], 2), ",",
  round(ci_5$conf.int[2], 2), "]\n"
)
HideShow
Estimate: 81 
 Standard Error: 27.29 
 t-value: 2.97 
 95% CI: [ 23.66 , 138.34 ]
HideShow
#> Estimate 81 (249 - 168), 95% CI [23.66, 138.34] — matches book p. 819.

Interpretation: the interval [23.66,138.34][23.66, 138.34] does not contain zero, confirming a significant interaction. Increasing the angle from 0° to 8° causes a significantly steeper increase in reaction time for older participants than it does for younger participants, by somewhere between about 24 ms and 138 ms.

14.11 — Longitudinal Randomized Designs

Designs combining a between-subjects factor and a within-subjects factor are incredibly common. A frequent special case is the longitudinal randomized design, where the between-subjects factor is a randomly assigned treatment condition (e.g., Treatment vs. Control) and the within-subjects factor is time.

Crucially, in these designs, the first time point is often a baseline (pretest) measure taken before the intervention begins.

The book’s example is a study of 100 participants, 44 assigned to an active treatment and 56 to a control group, measured at a baseline and two post-intervention occasions (p. 821). Table 14.13 gives the sample means, which are all of the raw data the book reports for this example, so we cannot reproduce its test statistics from the printed page. What we can do is build a data set that has exactly those group means, carry out each analysis the section describes on it, and state the book’s own results beside them. The standard deviations and correlations below are illustrative, not the book’s, and the text is explicit (p. 821) that those are what determine whether the mean pattern is statistically significant.

HideShow
# MASS (mvrnorm) and car (Manova) are loaded above.
set.seed(113)

# Means from Table 14.13 (p. 821)
mu_Control   <- c(98.71, 97.90, 99.49)
mu_Treatment <- c(98.14, 105.78, 107.48)

# An illustrative covariance matrix: SD = 22 at each occasion, r = .5 between
# occasions. The book reports no standard deviations for this example; this
# spread was chosen so that the mean pattern of Table 14.13 is about as hard
# to detect here as it is in the book's own data.
sigma <- matrix(c(484, 242, 242,
                  242, 484, 242,
                  242, 242, 484), nrow = 3)

# Generate data, then shift each group so that its sample means are exactly
# the means printed in Table 14.13.
n_Control   <- 56
n_Treatment <- 44

match_means <- function(Y, mu) sweep(sweep(Y, 2, colMeans(Y)), 2, mu, "+")

data_Control   <- match_means(mvrnorm(n_Control,   mu_Control,   sigma), mu_Control)
data_Treatment <- match_means(mvrnorm(n_Treatment, mu_Treatment, sigma), mu_Treatment)

# Combine into a single data frame, spelling out the condition level by level.
df_longitudinal <- data.frame(
  Condition = factor(rep(c("Control", "Treatment"), c(n_Control, n_Treatment)),
                     levels = c("Control", "Treatment")),
  Time1 = c(data_Control[, 1], data_Treatment[, 1]), # Baseline
  Time2 = c(data_Control[, 2], data_Treatment[, 2]), # Post-intervention 1
  Time3 = c(data_Control[, 3], data_Treatment[, 3])  # Post-intervention 2
)

head(df_longitudinal)
HideShow
  Condition     Time1     Time2     Time3
1   Control 103.69661 102.91241  93.54411
2   Control  88.08470  81.45296  63.69278
3   Control  72.41065  77.80831 116.77315
4   Control 131.64773 125.59953 119.81621
5   Control 119.83545  91.78545 125.83002
6   Control 137.76032 135.18206 127.75664
HideShow
# The cell means now reproduce Table 14.13 exactly.
df_longitudinal |>
  group_by(Condition) |>
  summarize(across(Time1:Time3, mean))
HideShow
# A tibble: 2 × 4
  Condition Time1 Time2 Time3
  <fct>     <dbl> <dbl> <dbl>
1 Control    98.7  97.9  99.5
2 Treatment  98.1 106.  107. 
HideShow
#> Control 98.71, 97.90, 99.49; Treatment 98.14, 105.78, 107.48
#> — matches Table 14.13 (p. 821).
The “naive” approach: a 2 x 3 repeated measures analysis

Because the data look just like the split-plot designs we analyzed earlier in the chapter, our first instinct might be to run a 2 (Condition) ×\times 3 (Time) analysis treating baseline simply as level 1 of the Time factor. By the logic of Section 14.6, the Condition ×\times Time interaction is a MANOVA comparing the groups on the two gain scores from baseline, which the book states explicitly on p. 822.

HideShow
# D variables are gain scores from baseline: this MANOVA IS the interaction test.
df_longitudinal$Gain1 <- df_longitudinal$Time2 - df_longitudinal$Time1
df_longitudinal$Gain2 <- df_longitudinal$Time3 - df_longitudinal$Time1

rm_interaction <- manova(cbind(Gain1, Gain2) ~ Condition, data = df_longitudinal)
summary(rm_interaction)
HideShow
          Df   Pillai approx F num Df den Df  Pr(>F)  
Condition  1 0.049074   2.5029      2     97 0.08712 .
Residuals 98                                          
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

For the book’s own data this test gives F(2,97)=2.516F(2, 97) = 2.516 with p=.086p = .086 (Table 14.14, p. 821), and none of the three omnibus effects reaches the .05 level: Condition is F(1,98)=1.824F(1, 98) = 1.824, p=.180p = .180, and Time is F(2,97)=2.583F(2, 97) = 2.583, p=.081p = .081. Testing the simple effects at each post-intervention occasion does not help either — the book reports pp values of .095 and .084 (p. 822) — and the linear component of the interaction misses as well, at p=.066p = .066 (p. 822). The researcher is left unable to conclude that the treatment worked. The illustrative data above behave the same way: the interaction test does not reach the .05 level, even though the treatment group’s means rise by more than 9 points while the control group’s barely move.

Let’s look at why this happens and what we should do instead.

The better approach: MANCOVA (and ANCOVA)

As we learned in Chapter 9 for pretest-posttest designs, analyzing the data using Analysis of Covariance (ANCOVA) is almost always superior to using repeated measures ANOVA or analyzing simple gain scores.

Because we have two post-intervention measures here (Time 2 and Time 3), we generalize ANCOVA to Multivariate Analysis of Covariance (MANCOVA).

Random assignment balances baseline scores in expectation, although sample means can differ by chance. When baseline predicts the post-test outcomes, adjusting for it can reduce residual variation.

Here is how we run a MANCOVA in R, predicting the post-intervention scores from Condition, while covarying Baseline:

HideShow
# Fit the multivariate linear model using Time1 as a covariate
mancova_model <- lm(cbind(Time2, Time3) ~ Condition + Time1, data = df_longitudinal)

# Run the MANOVA on the model
Manova(mancova_model)
HideShow

Type II MANOVA Tests: Pillai test statistic
          Df test stat approx F num Df den Df    Pr(>F)    
Condition  1  0.063026   3.2288      2     96   0.04395 *  
Time1      1  0.302107  20.7785      2     96 3.176e-08 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

For the book’s data, the MANCOVA test of the Condition effect gives F=3.110F = 3.110 with p=.049p = .049 (p. 823) — statistically significant where the 2 ×\times 3 analysis of the same data was not. The same reversal happens with the illustrative data above, for the same reason: covarying baseline removes variance from the post-test scores that the gain-score analysis leaves in the error term. The exact FF here is not the book’s, because the book reports only the means for this example, and these standard deviations and correlations are ours.

The adjusted condition effect compares the groups on their post-test outcomes at a common baseline score.

We have bypassed the underpowered repeated measures analysis and reached a conclusion the data can support, using the same participants and the same measurements.

Follow-up tests: univariate ANCOVAs

If the omnibus MANCOVA is significant, we can test whether the groups differ at specific measurement occasions using standard univariate ANCOVAs.

HideShow
# Test group difference at Time 2, covarying Baseline (Time 1)
ancova_time2 <- lm(Time2 ~ Condition + Time1, data = df_longitudinal)
car::Anova(ancova_time2, type = "III") # Type III recommended for ANCOVA with unbalanced designs
HideShow
Anova Table (Type III tests)

Response: Time2
            Sum Sq Df F value    Pr(>F)    
(Intercept)   8702  1 20.2651 1.877e-05 ***
Condition     1646  1  3.8321   0.05315 .  
Time1        10853  1 25.2730 2.276e-06 ***
Residuals    41655 97                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
# Test group difference at Time 3, covarying Baseline (Time 1)
ancova_time3 <- lm(Time3 ~ Condition + Time1, data = df_longitudinal)
car::Anova(ancova_time3, type = "III")
HideShow
Anova Table (Type III tests)

Response: Time3
            Sum Sq Df F value    Pr(>F)    
(Intercept)   9444  1 28.0040 7.513e-07 ***
Condition     1689  1  5.0095    0.0275 *  
Time1        10693  1 31.7071 1.745e-07 ***
Residuals    32711 97                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

By accounting for the baseline variance, these ANCOVAs provide more power and precision than the simple effects tests derived from the 2 ×\times 3 analysis, for the reason Equation 9.49 gives: they use the correlation between baseline and each post-intervention measure, which the simple effects tests ignore.

The row to read in each table is Condition. For the book’s data these two ANCOVAs give F=4.691F = 4.691 with p=.033p = .033 at the first post-intervention occasion and F=4.141F = 4.141 with p=.045p = .045 at the second (p. 823) — both significant, where the corresponding simple effects tests in the 2 ×\times 3 analysis gave p=.095p = .095 and p=.084p = .084. That is not a contradiction but the power difference at work.

The two follow-up ANCOVAs are easiest to read side by side. With modelsummary, each model occupies its own column with fit statistics at the foot:

HideShow
modelsummary(
  list("Time 2 (covary baseline)" = ancova_time2,
       "Time 3 (covary baseline)" = ancova_time3),
  estimate  = "{estimate}",
  statistic = "({std.error})",   # standard errors in parentheses, APA style
  gof_map   = c("nobs", "aic", "bic", "logLik"),
  stars     = TRUE,
  output    = "gt"
)
Time 2 (covary baseline) Time 3 (covary baseline)
(Intercept) 47.136 49.102
(10.471) (9.279)
ConditionTreatment 8.173 8.281
(4.175) (3.700)
Time1 0.514 0.510
(0.102) (0.091)
Num.Obs. 100 100
AIC 895.0 870.8
BIC 905.4 881.2
Log.Lik. -443.494 -431.409
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

The same comparison as a gtsummary table, each model under its own spanning header:

HideShow
t_time2 <- tbl_regression(ancova_time2, intercept = TRUE)
t_time3 <- tbl_regression(ancova_time3, intercept = TRUE)
tbl_merge(list(t_time2, t_time3),
          tab_spanner = c("**Time 2**", "**Time 3**"))
Characteristic
Time 2
Time 3
Beta 95% CI p-value Beta 95% CI p-value
(Intercept) 47 26, 68 <0.001 49 31, 68 <0.001
Condition





    Control — —
— —
    Treatment 8.2 -0.11, 16 0.053 8.3 0.94, 16 0.027
Time1 0.51 0.31, 0.72 <0.001 0.51 0.33, 0.69 <0.001
Abbreviation: CI = Confidence Interval
An alternative formulation: MANCOVA on gain scores

Some researchers prefer to analyze the data in a way that makes “change” more explicit.

The book notes (p. 823) that running a MANCOVA predicting the raw post-test scores while covarying baseline is mathematically identical to running a MANCOVA predicting the gain scores while covarying baseline. That identity does not depend on the particular data, so it is something we can demonstrate exactly here.

HideShow
# The gain scores from baseline, created above for the interaction test.

# Fit MANCOVA on the Gain scores, covarying Baseline
mancova_gain_model <- lm(cbind(Gain1, Gain2) ~ Condition + Time1, data = df_longitudinal)

# Results
Manova(mancova_gain_model)
HideShow

Type II MANOVA Tests: Pillai test statistic
          Df test stat approx F num Df den Df    Pr(>F)    
Condition  1  0.063026   3.2288      2     96   0.04395 *  
Time1      1  0.282237  18.8745      2     96 1.222e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The Condition row is identical to the one in the MANCOVA on the raw post-tests. Base R will state the comparison as a model comparison, which isolates the Condition test and makes the equality explicit:

HideShow
# The Condition effect as a comparison of full and restricted models.
mancova_reduced      <- lm(cbind(Time2, Time3) ~ Time1, data = df_longitudinal)
mancova_gain_reduced <- lm(cbind(Gain1, Gain2) ~ Time1, data = df_longitudinal)

post_test_route <- anova(mancova_reduced, mancova_model, test = "Pillai")
gain_score_route <- anova(mancova_gain_reduced, mancova_gain_model, test = "Pillai")

post_test_route
HideShow
Analysis of Variance Table

Model 1: cbind(Time2, Time3) ~ Time1
Model 2: cbind(Time2, Time3) ~ Condition + Time1
  Res.Df Df Gen.var.   Pillai approx F num Df den Df  Pr(>F)  
1     98      362.75                                          
2     97 -1   354.75 0.063026   3.2288      2     96 0.04395 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
gain_score_route
HideShow
Analysis of Variance Table

Model 1: cbind(Gain1, Gain2) ~ Time1
Model 2: cbind(Gain1, Gain2) ~ Condition + Time1
  Res.Df Df Gen.var.   Pillai approx F num Df den Df  Pr(>F)  
1     98      362.75                                          
2     97 -1   354.75 0.063026   3.2288      2     96 0.04395 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
HideShow
# The two routes give the same test, to numerical precision.
all.equal(post_test_route[["approx F"]], gain_score_route[["approx F"]])
HideShow
[1] TRUE

The two analyses give the same Pillai statistic, the same FF, and the same pp value, exactly as the book says they must (p. 823). Some researchers prefer the gain-score form because it makes the question about change explicit, and because it lends itself to testing change within each group separately.

14.12 — Power Analysis and Sample Size Planning

In this section, we transition to sample size planning for the multivariate approach to within-subjects and mixed designs. When sphericity is violated (as is common in longitudinal data), traditional univariate power formulas break down.

While the textbook relies on SAS’s proc glmpower to evaluate these matrices analytically, we can leverage R’s computational flexibility to automate this via Monte Carlo simulation. This approach allows us to define any means and covariance structure, simulate thousands of experiments in seconds, and empirically calculate statistical power (the proportion of simulations that correctly reject the null hypothesis).

14.12.1 — Designs with Two Within-Subjects Factors

Let’s test the 2×32 \times 3 within-subjects design. The textbook asks: if we want power of .80 (α=.05\alpha = .05) to detect the AA main effect, the BB main effect, and the A×BA \times B interaction, given the cell means of Table 14.15, the correlation matrix of Table 14.16, and a population standard deviation of 20 in each cell, how many participants do we need?

The GLMPOWER output summarized on p. 825 gives these required total sample sizes for the multivariate test: * Factor A: 54 participants * Factor B: 20 participants * A ×\times B Interaction: 27 participants

Let’s write a simulation function to check that N=27N = 27 yields approximately .80 power for the interaction.

HideShow
# MASS (mvrnorm) is loaded above.

# Define Population Parameters: the cell means of Table 14.15 (p. 824), in the
# order A1B1, A1B2, A1B3, A2B1, A2B2, A2B3, and the correlations of
# Table 14.16 (p. 825).
mu_2x3 <- c(100, 118, 109, 114, 118, 116)
sd_2x3 <- 20
cor_matrix_2x3 <- matrix(c(
  1.0, 0.6, 0.6, 0.4, 0.3, 0.3,
  0.6, 1.0, 0.6, 0.3, 0.4, 0.3,
  0.6, 0.6, 1.0, 0.3, 0.3, 0.4,
  0.4, 0.3, 0.3, 1.0, 0.6, 0.6,
  0.3, 0.4, 0.3, 0.6, 1.0, 0.6,
  0.3, 0.3, 0.4, 0.6, 0.6, 1.0
), nrow=6, byrow=TRUE)

# Convert correlation matrix to covariance matrix
sigma_2x3 <- cor_matrix_2x3 * (sd_2x3^2)

# Simulation Function for 2x3 Within-Subjects Interaction
sim_power_interaction <- function(n, iterations = 2000) {
  p_values <- numeric(iterations)
  
  for(i in 1:iterations) {
    # Generate data for n participants
    Y <- mvrnorm(n, mu = mu_2x3, Sigma = sigma_2x3)
    
    # To test the AxB interaction via the multivariate approach, 
    # we create interaction D-variables (Difference of Differences)
    # A difference (A1 - A2) across the B contrasts (B1-B2, B2-B3)
    D1 <- (Y[,1] - Y[,4]) - (Y[,2] - Y[,5]) # A(1-2) at B(1-2)
    D2 <- (Y[,2] - Y[,5]) - (Y[,3] - Y[,6]) # A(1-2) at B(2-3)
    
    # MANOVA testing if the grand mean of these D variables is 0
    # The intercept p-value is the interaction test!
    fit <- manova(cbind(D1, D2) ~ 1)
    
    # Extract Pillai's trace p-value for the intercept
    p_values[i] <- summary(fit, intercept = TRUE)$stats["(Intercept)", "Pr(>F)"]
  }
  
  # Power is the proportion of significant tests
  return(mean(p_values < 0.05))
}

# Test the power for N = 27, the book's answer for the interaction.
set.seed(113)
power_N27 <- sim_power_interaction(n = 27)
cat("Simulated Power for AxB Interaction at N = 27:", power_N27, "\n")
HideShow
Simulated Power for AxB Interaction at N = 27: 0.8055 

The simulation returns a power of 80.6% at N=27N = 27, which is the sample size the book’s analytic calculation gives for power of .80 (p. 825). Two routes, two sets of assumptions about how the computation is done, one answer.

14.12.2 — Designs with One Between-Subjects Factor and One Within-Subjects Factor

Now we look at a mixed design: 2 groups (between-subjects) measured on 4 occasions (within-subjects), with the cell means of Table 14.18 and the correlations of Table 14.19 (both p. 828) and a population standard deviation of 10. The GLMPOWER output on p. 829 reports that the multivariate test of the omnibus interaction needs a total NN of 212, while a test of the condition by linear trend interaction needs only 174.

Let’s simulate the power for the linear trend contrast at N=174N = 174 (which means n=87n = 87 per group). We can do this efficiently in R by applying the contrast weights directly to the simulated data.

HideShow
# Define Parameters: cell means from Table 14.18 and correlations from
# Table 14.19 (both p. 828).
mu_cond1 <- c(104, 108, 110, 112)
mu_cond2 <- c(100, 102, 102, 104)
sd_2x4 <- 10

cor_matrix_2x4 <- matrix(c(
  1.00, 0.60, 0.54, 0.49,
  0.60, 1.00, 0.60, 0.54,
  0.54, 0.60, 1.00, 0.60,
  0.49, 0.54, 0.60, 1.00
), nrow=4, byrow=TRUE)

sigma_2x4 <- cor_matrix_2x4 * (sd_2x4^2)

# Linear Contrast Simulation
sim_linear_trend_power <- function(n_per_group, iterations = 2000) {
  p_values <- numeric(iterations)
  linear_weights <- c(-3, -1, 1, 3) # Appendix Table A.10, as on p. 829
  
  for(i in 1:iterations) {
    # Generate data
    Y1 <- mvrnorm(n_per_group, mu = mu_cond1, Sigma = sigma_2x4)
    Y2 <- mvrnorm(n_per_group, mu = mu_cond2, Sigma = sigma_2x4)
    
    # Apply linear contrast to each subject's scores
    # This reduces the 4 timepoints down to a single "Linear Slope" score
    slope1 <- Y1 %*% linear_weights 
    slope2 <- Y2 %*% linear_weights
    
    # Test if the groups differ on this slope (Interaction Contrast!)
    fit <- t.test(slope1, slope2, var.equal = TRUE)
    p_values[i] <- fit$p.value
  }
  
  return(mean(p_values < 0.05))
}

# Test power for Total N = 174 (n = 87 per group)
set.seed(113)
power_linear <- sim_linear_trend_power(n_per_group = 87)
cat("Simulated Power for Linear Trend Interaction at Total N=174:", power_linear, "\n")
HideShow
Simulated Power for Linear Trend Interaction at Total N=174: 0.8005 

The simulation returns a power of 80% at a total NN of 174, the sample size the book’s calculation gives for power of .80 (p. 829). This returns us to a recurring theme in experimental design: specificity buys statistical power.

Testing a broad omnibus interaction requires a larger sample (N=212N = 212) because the test has to look for any possible pattern of differences across the four occasions. By collapsing those occasions into a single linear slope with contrast weights, we focus all the power on one targeted question: do the linear trajectories differ? That focused test reaches .80 power with 38 fewer participants.

Comparing analysis approaches: repeated measures versus MANCOVA

The chapter closes with a demonstration of statistical efficiency. For a longitudinal randomized design (baseline plus three post-tests) with the population means of Table 14.21, a standard 2×42 \times 4 analysis treating baseline as a level of time has power of only .413 to detect the group by time interaction, while a MANCOVA on the three post-tests with baseline as the covariate has power of about .63 (both on p. 831).

Let’s reproduce that comparison by simulating the design with exactly 50 participants (n=25n = 25 per group) and calculating the empirical power of both analyses head to head.

HideShow
# Parameters for Longitudinal Design: population means from Table 14.21 and
# the correlation matrix on p. 830.
mu_ctrl <- c(50, 51, 51, 52)
mu_trt  <- c(50, 56, 58, 56)
sd_long <- 10

cor_long <- matrix(c(
  1.00, 0.40, 0.35, 0.30,
  0.40, 1.00, 0.40, 0.35,
  0.35, 0.40, 1.00, 0.40,
  0.30, 0.35, 0.40, 1.00
), nrow=4, byrow=TRUE)

sigma_long <- cor_long * (sd_long^2)

# Head-to-Head Simulation
sim_mancova_vs_rm <- function(n_per_group=25, iterations = 2000) {
  p_val_rm <- numeric(iterations)
  p_val_mancova <- numeric(iterations)
  
  Condition <- factor(rep(c("Control", "Treatment"), each = n_per_group))
  
  for(i in 1:iterations) {
    Y_ctrl <- mvrnorm(n_per_group, mu = mu_ctrl, Sigma = sigma_long)
    Y_trt  <- mvrnorm(n_per_group, mu = mu_trt, Sigma = sigma_long)
    Y_all  <- rbind(Y_ctrl, Y_trt)
    
    # Model 1: Repeated Measures ANOVA (Multivariate approach)
    # Tested by comparing groups on D variables from baseline
    D1 <- Y_all[,2] - Y_all[,1]
    D2 <- Y_all[,3] - Y_all[,1]
    D3 <- Y_all[,4] - Y_all[,1]
    fit_rm <- manova(cbind(D1, D2, D3) ~ Condition)
    p_val_rm[i] <- summary(fit_rm)$stats["Condition", "Pr(>F)"]
    
    # Model 2: MANCOVA 
    # Predict Post-tests from Condition, covarying Baseline (Y[,1])
    Y2 <- Y_all[,2]; Y3 <- Y_all[,3]; Y4 <- Y_all[,4]; Y1 <- Y_all[,1]
    fit_mancova <- manova(cbind(Y2, Y3, Y4) ~ Condition + Y1)
    p_val_mancova[i] <- summary(fit_mancova)$stats["Condition", "Pr(>F)"]
  }
  
  return(c(
    Power_Repeated_Measures = mean(p_val_rm < 0.05),
    Power_MANCOVA = mean(p_val_mancova < 0.05)
  ))
}

# Run the comparison; the book's values are .413 and about .63.
set.seed(113)
power_comparison <- sim_mancova_vs_rm()
print(power_comparison)
HideShow
Power_Repeated_Measures           Power_MANCOVA 
                 0.4060                  0.6265 

The simulation estimates power as 0.406 for the interaction and 0.626 for MANCOVA. The interaction compares changes from baseline. MANCOVA compares post-test means at a common baseline and assumes the same baseline slope in both groups for each outcome. For Table 14.21, base R can also evaluate the noncentral FF probabilities analytically, averaging over chance baseline imbalance for MANCOVA. The resulting powers are .4130 and .6355, respectively, consistent with the book’s GLIMMPSE discussion on p. 831.

Neither value is a comfortable place to run a study from, which is the book’s point in using this example: the MANCOVA is the better analysis of the two, and a sample of 50 is still too small for either.

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 14: Higher Order Designs with Within-Subjects Factors: The Multivariate Approach (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

Scroll to Top