Chapter 2: Drawing Valid Inferences from Experiments

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 1 covers the R groundwork these notes take for granted: installing packages from CRAN, loading them, and the way the book’s data sets are named. Chapter 2 is conceptual: it carries no numerical worked example, and AMCP ships no chapter_2_table_* data for it, so everything below is computed from scratch rather than read out of a data set.

HideShow
# Load all of the packages used.
library(tidyverse)      # data wrangling and plotting (dplyr, tidyr, ggplot2, and friends)
library(pwr)            # power analysis for common designs
library(DMAR)           # design, measurement, and analysis

What this companion covers

Chapter 2 is conceptual. It distinguishes the four types of validity — statistical conclusion, internal, construct, and external (Section 2.3.1, p. 54) — surveys the threats to each, sets out a general strategy for conceptualizing and controlling those threats (Section 2.3.6, p. 62), previews the designs developed in the rest of the book (Section 2.4, p. 64), and closes on the growth of experimentation, online experiments included (Section 2.5, p. 68). There is no data set to analyze and no printed table of results to reproduce.

Two kinds of chapter content do carry over into R, and they are what this page contains:

  • The quantitative claims behind Section 2.3.2. The chapter argues that low power is the most pervasive threat to statistical conclusion validity, that too-small samples and unreliable dependent variables are its leading causes, and that inflated Type I error rates come primarily from testing the same general hypothesis repeatedly (pp. 56–57, summarized in Table 2.1, p. 57). Each of those claims can be made quantitative in a few lines of base R.
  • The chapter’s design schematics. Figures 2.1–2.4 (pp. 62–64) recast threats to validity as missing cells of a larger factorial design, and Figures 2.5–2.7 (pp. 66–68) introduce the design vocabulary — one-way versus two-way, crossed versus nested, between-subjects versus within-subjects — that organizes the remaining chapters. All seven are drawn below in ggplot2, cell for cell as printed.

The numbers this page reports are computed here, not copied from the chapter: Chapter 2 prints none. Every page citation points at the sentence, table, or figure in the book that the code is making concrete.

2.3 — Threats to the Validity of Inferences from Experiments

2.3.2 — Statistical Conclusion Validity

Statistical conclusion validity asks whether the original statistical inference was correct (p. 55). Because the Type I error rate α\alpha is fixed by convention within a discipline, the chapter identifies power — the probability of rejecting a false null hypothesis, equal to 1−Pr⁡(Type II error)1 – \Pr(\text{Type II error}) — as the critical issue, and low power as the most pervasive threat to it (p. 56).

Power as a function of per-group nn (base R)

The chapter’s first stated cause of low power is that sample sizes are too small for the situation, and it notes that increasing the number of participants is conceptually the simplest remedy (p. 56). power.t.test(), in base R’s stats package, makes that trade-off explicit for the two-independent-groups case. We tabulate power against per-group nn at three illustrative population standardized mean differences, δ=0.2,0.5,0.8\delta = 0.2, 0.5, 0.8. The common population standard deviation is 1 here, so each mean difference entered in power.t.test() equals its standardized value.

HideShow
power_grid <- expand_grid(
  delta = c(0.2, 0.5, 0.8),
  n     = seq(10, 400, by = 10)
) |>
  mutate(
    power = power.t.test(
      n = n, delta = delta, sd = 1, sig.level = 0.05,
      type = "two.sample", alternative = "two.sided",
      strict = TRUE  # count rejections in both tails
    )$power
  )

power_grid
HideShow
# A tibble: 120 × 3
   delta     n  power
   <dbl> <dbl>  <dbl>
 1   0.2    10 0.0708
 2   0.2    20 0.0946
 3   0.2    30 0.119 
 4   0.2    40 0.143 
 5   0.2    50 0.168 
 6   0.2    60 0.192 
 7   0.2    70 0.217 
 8   0.2    80 0.242 
 9   0.2    90 0.266 
10   0.2   100 0.291 
# ℹ 110 more rows
HideShow
ggplot(power_grid, aes(x = n, y = power, color = factor(delta))) +
  geom_hline(yintercept = c(0.80, 0.95), linetype = "dotted",
             color = "#0C2340") +
  geom_line(linewidth = 0.9) +
  scale_color_discrete(
    breaks = c("0.2", "0.5", "0.8"),
    labels = expression(italic("δ") == 0.2, italic("δ") == 0.5, italic("δ") == 0.8)
  ) +
  scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = 0.1)) +
  labs(
    x     = expression("Sample size per group," ~ italic(n)),
    y     = "Power",
    color = NULL
  )
Power of the two-sample \(t\) test as a function of per-group \(n\) at three population standardized mean differences (two-sided, \(\alpha = .05\)). Dotted lines mark 80% and 95% power.

Power of the two-sample tt test as a function of per-group nn at three population standardized mean differences (two-sided, α=.05\alpha = .05). Dotted lines mark 80% and 95% power.

For 80% power, δ=0.8\delta = 0.8 requires 26 participants per group, whereas δ=0.2\delta = 0.2 requires 394.

The same answer from pwr, and from DMAR

Solving the same equation for nn instead of for power is the sample-size question. Base R and the pwr package answer it identically; the agreement is worth seeing, because it means nothing in the result depends on which package a reader reaches for.

HideShow
base_n <- power.t.test(
  delta       = 0.5,
  sd          = 1,
  power       = 0.80,
  sig.level   = 0.05,
  type        = "two.sample",
  alternative = "two.sided",
  strict      = TRUE
)$n

pwr_n <- pwr.t.test(
  d           = 0.5,  # pwr calls the assumed population effect d
  power       = 0.80,
  sig.level   = 0.05,
  type        = "two.sample",
  alternative = "two.sided"
)$n

c(power.t.test = base_n, pwr.t.test = pwr_n)
HideShow
power.t.test   pwr.t.test 
    63.76561     63.76561 
HideShow
#> 63.77 from both routes: base R and pwr agree to five significant digits.

Neither call above rounds its answer to a whole number of participants, or says what power that whole number buys. ss_power_smd() in DMAR reports both — the requirement rounded up, and the power actually achieved there, which is never exactly the power requested — along with the noncentrality parameter that generated them. Note the argument name: the planner takes true_smd, the assumed population standardized mean difference, rather than an estimate from data.

HideShow
ss_power_smd(
  true_smd      = 0.50,
  desired_power = 0.80,
  alpha_level   = 0.05,
  directional   = FALSE
)
term value
necessary_n_per_group 64
actual_power 0.801
noncentral_t_parm 2.828
true_smd 0.500
desired_power 0.800
alpha_level 0.050
tails 2
HideShow
#> necessary_n_per_group = 64, the ceiling of the 63.77 above, and
#> actual_power = 0.801 at that n rather than the 0.800 requested.
Required nn for 80% and 95% power

Collecting the base-R answers across the three benchmark effect sizes shows how steeply the requirement grows as the anticipated effect shrinks.

HideShow
n_needed <- function(delta, target_power) {
  power.t.test(
    delta       = delta,
    sd          = 1,
    power       = target_power,
    sig.level   = 0.05,
    type        = "two.sample",
    alternative = "two.sided",
    strict      = TRUE
  )$n
}

tibble(delta = c(0.2, 0.5, 0.8)) |>
  mutate(
    n_for_80 = vapply(delta, n_needed, numeric(1), target_power = 0.80),
    n_for_95 = vapply(delta, n_needed, numeric(1), target_power = 0.95)
  )
HideShow
# A tibble: 3 × 3
  delta n_for_80 n_for_95
  <dbl>    <dbl>    <dbl>
1   0.2    393.     651. 
2   0.5     63.8    105. 
3   0.8     25.5     41.6
HideShow
#> 393 / 63.8 / 25.5 per group for 80% power; 651 / 105 / 41.6 for 95%.
#> Dividing the population effect by four calls for about sixteen times as many participants.
Confirming the power curve by simulation

The analytic power above is not an approximation, but simulating the test it describes is a useful check on what “power” means operationally: it is the proportion of samples in which the test rejects. We draw samples under a true δ=0.5\delta = 0.5 and count rejections.

HideShow
set.seed(113)
n_replicates <- 2000

simulate_power <- function(n_per_group, true_smd) {
  rejects <- vapply(seq_len(n_replicates), function(i) {
    x <- rnorm(n_per_group, mean = 0,        sd = 1)
    y <- rnorm(n_per_group, mean = true_smd, sd = 1)
    t.test(x, y,
      alternative = "two.sided",
      var.equal   = TRUE,
      conf.level  = 0.95
    )$p.value < 0.05
  }, logical(1))
  mean(rejects)
}

tibble(n = c(10, 30, 50, 100, 200)) |>
  mutate(
    empirical_power  = vapply(n, simulate_power, numeric(1), true_smd = 0.5),
    analytic_power   = vapply(n, function(k) {
      power.t.test(
        n = k, delta = 0.5, sd = 1, sig.level = 0.05,
        type = "two.sample", alternative = "two.sided", strict = TRUE
      )$power
    }, numeric(1)),
    type2_error_rate = 1 - analytic_power
  )
HideShow
# A tibble: 5 × 4
      n empirical_power analytic_power type2_error_rate
  <dbl>           <dbl>          <dbl>            <dbl>
1    10           0.190          0.185          0.815  
2    30           0.488          0.478          0.522  
3    50           0.692          0.697          0.303  
4   100           0.937          0.940          0.0596 
5   200           0.998          0.999          0.00123
HideShow
#> Simulated and analytic power agree to within Monte Carlo error at every n,
#> and the Type II error rate falls as n grows — the point made on p. 56.

set.seed(113) is the seed used throughout these notes, so the figures above reproduce exactly on re-render; n_replicates is set in this chunk and reused by the simulation further down.

Why an unreliable dependent variable costs power

The chapter’s second stated cause of low power is an unreliable dependent variable, and its stated remedy is to lengthen the test, because longer tests are more reliable (pp. 56–57; the pairing appears in Table 2.1, p. 57). Both halves of that argument are short formulas, so the cost of unreliability can be read off directly in base R.

Lengthening a test by a factor kk changes its reliability by the Spearman–Brown relation. If δ\delta is the effect standardized by the true-score standard deviation, the population effect on the fallible measure is δρYY′\delta\sqrt{\rho_{YY'}}. The example uses δ=0.5\delta = 0.5 and a short-form reliability of 0.50.

HideShow
true_smd <- 0.50
rel_one_form <- 0.50

tibble(test_length_factor = c(1, 2, 4, 8)) |>
  mutate(
    reliability    = (test_length_factor * rel_one_form) /
                    (1 + (test_length_factor - 1) * rel_one_form),
    fallible_delta = true_smd * sqrt(reliability),
    n_for_80       = vapply(fallible_delta, n_needed, numeric(1),
                           target_power = 0.80)
  )
HideShow
# A tibble: 4 × 4
  test_length_factor reliability fallible_delta n_for_80
               <dbl>       <dbl>          <dbl>    <dbl>
1                  1       0.5            0.354    127. 
2                  2       0.667          0.408     95.2
3                  4       0.8            0.447     79.5
4                  8       0.889          0.471     71.6
HideShow
#> Reliability 0.50 attenuates the population effect from 0.50 to 0.354 and pushes the requirement to
#> 127 per group for 80% power; quadrupling test length raises reliability to
#> 0.80, raises that effect to 0.447, and brings the requirement down to 79.5.

The table makes the chapter’s caveat visible too: the returns diminish. Going from one form to two saves about 31 participants per group; going from four forms to eight saves fewer than 8 more. That is why the chapter frames lengthening the posttest and adding a pretest as complementary strategies whose relative value depends on how reliable the measure already is (p. 57).

Why repeated tests inflate the Type I error rate

Turning from conservative to liberal bias: the chapter identifies the primary cause of Type I error rates above the nominal level as the investigator having performed multiple tests of the same general hypothesis, and points to adjusted test procedures as the remedy (p. 57, and the first row of the liberal-bias half of Table 2.1, p. 57). With kk independent tests each at α=.05\alpha = .05, the probability that at least one is significant when every null is true is 1−(1−α)k1 – (1 – \alpha)^{k} — a one-line calculation in base R, which we then confirm by simulation and repair with p.adjust().

HideShow
set.seed(113)

one_family <- function(n_tests, n_per_group = 30) {
  p_values <- vapply(seq_len(n_tests), function(j) {
    x <- rnorm(n_per_group, mean = 0, sd = 1)
    y <- rnorm(n_per_group, mean = 0, sd = 1)
    t.test(x, y,
      alternative = "two.sided",
      var.equal   = TRUE,
      conf.level  = 0.95
    )$p.value
  }, numeric(1))
  c(
    unadjusted = any(p_values < 0.05),
    holm       = any(p.adjust(p_values, method = "holm") < 0.05)
  )
}

bind_rows(lapply(c(1, 3, 5, 10), function(k) {
  hits <- vapply(seq_len(n_replicates), function(i) one_family(k), logical(2))
  tibble(
    tests_per_family     = k,
    analytic_unadjusted  = 1 - (1 - 0.05)^k,
    empirical_unadjusted = mean(hits["unadjusted", ]),
    empirical_holm       = mean(hits["holm", ])
  )
}))
HideShow
# A tibble: 4 × 4
  tests_per_family analytic_unadjusted empirical_unadjusted empirical_holm
             <dbl>               <dbl>                <dbl>          <dbl>
1                1              0.0500                0.049         0.049 
2                3              0.143                 0.136         0.0475
3                5              0.226                 0.226         0.044 
4               10              0.401                 0.407         0.0555
HideShow
#> With every null true, the unadjusted familywise error rate climbs from .05
#> at one test toward .40 at ten, tracking 1 - (1 - .05)^k; Holm's adjustment
#> holds the familywise rate at about .05 throughout.

Holm’s procedure is used here because it is available in base R through p.adjust() and controls the familywise rate without assuming independence. The multiple-comparison procedures the book develops for the comparisons that actually arise in a one-way design — Tukey, Dunnett, Scheffé, and the rest — are the subject of Chapter 5, and appear in that chapter’s companion page rather than here.

2.3.6 — Conceptualizing and Controlling for Threats to Validity

Section 2.3.6 makes the organizing point of the chapter: most of the artifacts surveyed above can be thought of as incomplete designs, or as designs with more factors than were originally planned (p. 62). A two-group study in which selection bias operates is really two of the four cells of a treatment × population design; a two-group study whose treatment arrives with incidental features the control condition lacks — the vehicle that delivers the treatment — is really two of the four cells of a treatment × vehicle design. Figures 2.1 through 2.4 draw those designs. Each is a small grid, so each is a geom_tile() plot.

Figure 2.1 — the confounded two-group design

In the book’s Figure 2.1 (p. 62) all four cells of the 2 × 2 are drawn, and two of them are annotated as not included in the design. The two that are used pair treatment-present with vehicle-present and treatment-absent with vehicle-absent, which is exactly why a treatment effect and a vehicle effect cannot be told apart.

HideShow
two_by_two_cells <- function(included) {
  expand_grid(
    treatment = c("Present", "Absent"),
    vehicle   = c("Present", "Absent")
  ) |>
    mutate(in_design = paste(treatment, vehicle) %in% included) |>
    mutate(
      treatment = factor(treatment, levels = c("Absent", "Present")),
      vehicle   = factor(vehicle,   levels = c("Present", "Absent"))
    )
}

figure_2_1 <- two_by_two_cells(
  included = c("Present Present", "Absent Absent")
)
figure_2_1
HideShow
# A tibble: 4 × 3
  treatment vehicle in_design
  <fct>     <fct>   <lgl>    
1 Present   Present TRUE     
2 Present   Absent  FALSE    
3 Absent    Present FALSE    
4 Absent    Absent  TRUE     
HideShow
#> Present/Present and Absent/Absent in the design; Absent/Present and
#> Present/Absent marked "Not included in design" — matches Figure 2.1, p. 62.

ggplot(figure_2_1, aes(x = vehicle, y = treatment, fill = in_design)) +
  geom_tile(color = "black", linewidth = 0.6) +
  geom_text(
    data = \(d) filter(d, !in_design),
    aes(label = "Not included\nin design"),
    color = "black", size = 3.6
  ) +
  scale_fill_manual(
    values = c(`TRUE` = "#0C2340", `FALSE` = "white"),
    guide  = "none"
  ) +
  scale_x_discrete(expand = expansion(0), position = "top") +
  scale_y_discrete(expand = expansion(0)) +
  labs(x = "Vehicle (instrumental incidentals)", y = "\"Pure\" treatment")
Figure 2.1 (book p. 62): a two-group study seen as two cells of a 2 × 2 'pure' treatment × vehicle design. Shaded cells are in the design.

Figure 2.1 (book p. 62): a two-group study seen as two cells of a 2 × 2 ‘pure’ treatment × vehicle design. Shaded cells are in the design.

Figure 2.2 — the two preferred designs

The repair is to make the two groups differ along one dimension only. Either the control group is expanded to include the incidental features that had been unique to the experimental group — the sham-surgery control the chapter describes — or the experimental group is purified by stripping those features out (p. 63). Panel (a) of Figure 2.2 therefore holds the vehicle present in both groups; panel (b) holds it absent in both.

HideShow
figure_2_2 <- bind_rows(
  expand_grid(
    panel     = "(a) Expanded control group",
    treatment = c("Present", "Absent"),
    vehicle   = "Present"
  ) |>
    mutate(label = if_else(treatment == "Absent",
                           "Expanded\ncontrol group", "")),
  expand_grid(
    panel     = "(b) Purified experimental group",
    treatment = c("Present", "Absent"),
    vehicle   = "Absent"
  ) |>
    mutate(label = if_else(treatment == "Present",
                           "Purified\nexperimental group", ""))
) |>
  mutate(treatment = factor(treatment, levels = c("Absent", "Present")))
figure_2_2
HideShow
# A tibble: 4 × 4
  panel                           treatment vehicle label                       
  <chr>                           <fct>     <chr>   <chr>                       
1 (a) Expanded control group      Present   Present ""                          
2 (a) Expanded control group      Absent    Present "Expanded\ncontrol group"   
3 (b) Purified experimental group Present   Absent  "Purified\nexperimental gro…
4 (b) Purified experimental group Absent    Absent  ""                          
HideShow
#> One column per panel: vehicle Present in (a), Absent in (b), each with both
#> levels of treatment — matches Figure 2.2, p. 63.

ggplot(figure_2_2, aes(x = vehicle, y = treatment)) +
  geom_tile(fill = "#0C2340", color = "black", linewidth = 0.6) +
  geom_text(aes(label = label), color = "white", size = 3.4) +
  facet_wrap(~ panel, scales = "free_x") +
  scale_x_discrete(expand = expansion(0), position = "top") +
  scale_y_discrete(expand = expansion(0)) +
  labs(x = "Vehicle (instrumental incidentals)", y = "\"Pure\" treatment")
Figure 2.2 (book p. 63): the two preferred designs. (a) holds the vehicle present in both conditions; (b) holds it absent in both. In each panel the two groups differ only in the treatment.

Figure 2.2 (book p. 63): the two preferred designs. (a) holds the vehicle present in both conditions; (b) holds it absent in both. In each panel the two groups differ only in the treatment.

Figures 2.3 and 2.4 — expanding across subpopulations

The same move in the other direction serves generality rather than identifiability. A two-group study run in one subpopulation (Figure 2.3, p. 63) becomes a crossed design when a second subpopulation of interest is added (Figure 2.4, p. 64), and the treatment effect can then be examined in both.

HideShow
figure_2_3_2_4 <- bind_rows(
  expand_grid(
    panel         = "Figure 2.3 — Original design",
    condition     = c("Treatment", "Control"),
    subpopulation = "A"
  ),
  expand_grid(
    panel         = "Figure 2.4 — Expanded design",
    condition     = c("Treatment", "Control"),
    subpopulation = c("A", "B")
  )
) |>
  mutate(condition = factor(condition, levels = c("Control", "Treatment")))
figure_2_3_2_4
HideShow
# A tibble: 6 × 3
  panel                        condition subpopulation
  <chr>                        <fct>     <chr>        
1 Figure 2.3 — Original design Treatment A            
2 Figure 2.3 — Original design Control   A            
3 Figure 2.4 — Expanded design Treatment A            
4 Figure 2.4 — Expanded design Treatment B            
5 Figure 2.4 — Expanded design Control   A            
6 Figure 2.4 — Expanded design Control   B            
HideShow
#> Subpopulation A only in Figure 2.3; A and B in Figure 2.4, both crossed with
#> Treatment and Control — matches pp. 63--64.

ggplot(figure_2_3_2_4, aes(x = subpopulation, y = condition)) +
  geom_tile(fill = "#0C2340", color = "black", linewidth = 0.6) +
  facet_wrap(~ panel, scales = "free_x") +
  scale_x_discrete(expand = expansion(0), position = "top") +
  scale_y_discrete(expand = expansion(0)) +
  labs(x = "Subpopulation", y = "Condition")
Figures 2.3 and 2.4 (book pp. 63--64): a two-group study in a single subpopulation, and the expanded design that crosses condition with subpopulation.

Figures 2.3 and 2.4 (book pp. 63–64): a two-group study in a single subpopulation, and the expanded design that crosses condition with subpopulation.

2.4 — Overview of Experimental Designs To Be Considered

Section 2.4 introduces the distinctions that structure the rest of the book: how many factors a design has, how the levels of different factors are combined, whether the levels were chosen for their own sake or sampled, and whether each subject experiences one condition or several (pp. 64–68). The schematics in Figures 2.5 through 2.7 are the vocabulary for all of it, drawn here with the book’s own example labels.

Figure 2.5 — one-way and two-way designs

The book’s one-way example is three instructional conditions — classroom instruction, a self-study manual, and a no-treatment control — and its two-way example adds a breakdown of participants by gender, which it notes typically increases power to detect the treatment effect while also allowing a check on whether the effect is consistent across subgroups (p. 66).

HideShow
instruction <- c("Classroom", "Self-Study", "Control")

figure_2_5 <- bind_rows(
  tibble(panel = "One-Way Design", condition = instruction, row = ""),
  expand_grid(
    panel     = "Two-Way Design",
    condition = instruction,
    row       = c("Males", "Females")
  )
) |>
  mutate(
    panel     = factor(panel, levels = c("One-Way Design", "Two-Way Design")),
    condition = factor(condition, levels = instruction),
    row       = factor(row, levels = c("Females", "Males", ""))
  )
figure_2_5
HideShow
# A tibble: 9 × 3
  panel          condition  row      
  <fct>          <fct>      <fct>    
1 One-Way Design Classroom  ""       
2 One-Way Design Self-Study ""       
3 One-Way Design Control    ""       
4 Two-Way Design Classroom  "Males"  
5 Two-Way Design Classroom  "Females"
6 Two-Way Design Self-Study "Males"  
7 Two-Way Design Self-Study "Females"
8 Two-Way Design Control    "Males"  
9 Two-Way Design Control    "Females"
HideShow
#> One row of three cells for the one-way design; Males above Females crossed
#> with the same three columns for the two-way — matches Figure 2.5, p. 66.

ggplot(figure_2_5, aes(x = condition, y = row)) +
  geom_tile(fill = "white", color = "black", linewidth = 0.6) +
  facet_wrap(~ panel, ncol = 1, scales = "free_y") +
  scale_x_discrete(expand = expansion(0), position = "top") +
  scale_y_discrete(expand = expansion(0)) +
  labs(x = NULL, y = NULL)
Figure 2.5 (book p. 66): schematic diagrams of a one-way design (three instructional conditions) and a two-way design (the same three conditions crossed with gender).

Figure 2.5 (book p. 66): schematic diagrams of a one-way design (three instructional conditions) and a two-way design (the same three conditions crossed with gender).

Figure 2.6 — crossed and nested designs

Crossing and nesting differ in which combinations of levels occur. In the book’s example, four therapists deliver Rogerian and Behavior Analytic therapy; if every therapist uses both methods, therapists are crossed with method, and if each is qualified for only one, therapists are nested within method (pp. 66–67). The book marks the absent combinations of the nested design with the word missing, and so do we.

HideShow
methods     <- c("Rogerian", "Behavior Analytic")
therapists  <- paste("Therapist", 1:4)

rogerian_only <- paste("Therapist", 1:2)

figure_2_6 <- bind_rows(
  expand_grid(panel = "Crossed Design", therapist = therapists,
              method = methods) |>
    mutate(present = TRUE),
  expand_grid(panel = "Nested Design", therapist = therapists,
              method = methods) |>
    mutate(present = if_else(
      therapist %in% rogerian_only,
      method == "Rogerian",
      method == "Behavior Analytic"
    ))
) |>
  mutate(
    panel     = factor(panel, levels = c("Crossed Design", "Nested Design")),
    method    = factor(method, levels = methods),
    therapist = factor(therapist, levels = rev(therapists))
  )

figure_2_6 |> filter(panel == "Nested Design")
HideShow
# A tibble: 8 × 4
  panel         therapist   method            present
  <fct>         <fct>       <fct>             <lgl>  
1 Nested Design Therapist 1 Rogerian          TRUE   
2 Nested Design Therapist 1 Behavior Analytic FALSE  
3 Nested Design Therapist 2 Rogerian          TRUE   
4 Nested Design Therapist 2 Behavior Analytic FALSE  
5 Nested Design Therapist 3 Rogerian          FALSE  
6 Nested Design Therapist 3 Behavior Analytic TRUE   
7 Nested Design Therapist 4 Rogerian          FALSE  
8 Nested Design Therapist 4 Behavior Analytic TRUE   
HideShow
#> Therapists 1 and 2 under Rogerian, Therapists 3 and 4 under Behavior
#> Analytic, the other four cells "missing" — matches Figure 2.6, p. 67.

ggplot(figure_2_6, aes(x = method, y = therapist, fill = present)) +
  geom_tile(color = "black", linewidth = 0.6) +
  geom_text(
    data = \(d) filter(d, !present),
    aes(label = "missing"), color = "black", size = 3.4
  ) +
  scale_fill_manual(
    values = c(`TRUE` = "#0C2340", `FALSE` = "white"),
    guide  = "none"
  ) +
  facet_wrap(~ panel, ncol = 1) +
  scale_x_discrete(expand = expansion(0), position = "top") +
  scale_y_discrete(expand = expansion(0)) +
  labs(x = "Therapy method", y = NULL)
Figure 2.6 (book p. 67): diagrams of crossed and nested designs. In the nested design each therapist delivers only one of the two methods.

Figure 2.6 (book p. 67): diagrams of crossed and nested designs. In the nested design each therapist delivers only one of the two methods.

Figure 2.7 — between-subjects and within-subjects designs

The chapter calls this perhaps the most important distinction among designs: whether each subject experiences one condition or several (p. 67). Figure 2.7 shows it with fifteen subjects spread one-to-a-condition across three conditions, against five subjects who each appear in all three.

HideShow
conditions <- paste("Condition", 1:3)

figure_2_7 <- bind_rows(
  expand_grid(panel = "Between-subjects design",
              condition = conditions, slot = 1:5) |>
    mutate(subject = paste0("S", (match(condition, conditions) - 1) * 5 + slot)),
  expand_grid(panel = "Within-subjects design",
              condition = conditions, slot = 1:5) |>
    mutate(subject = paste0("S", slot))
) |>
  mutate(
    panel     = factor(panel, levels = c("Between-subjects design",
                                         "Within-subjects design")),
    condition = factor(condition, levels = conditions),
    slot      = factor(slot, levels = 5:1)
  )

figure_2_7 |> filter(panel == "Between-subjects design")
HideShow
# A tibble: 15 × 4
   panel                   condition   slot  subject
   <fct>                   <fct>       <fct> <chr>  
 1 Between-subjects design Condition 1 1     S1     
 2 Between-subjects design Condition 1 2     S2     
 3 Between-subjects design Condition 1 3     S3     
 4 Between-subjects design Condition 1 4     S4     
 5 Between-subjects design Condition 1 5     S5     
 6 Between-subjects design Condition 2 1     S6     
 7 Between-subjects design Condition 2 2     S7     
 8 Between-subjects design Condition 2 3     S8     
 9 Between-subjects design Condition 2 4     S9     
10 Between-subjects design Condition 2 5     S10    
11 Between-subjects design Condition 3 1     S11    
12 Between-subjects design Condition 3 2     S12    
13 Between-subjects design Condition 3 3     S13    
14 Between-subjects design Condition 3 4     S14    
15 Between-subjects design Condition 3 5     S15    
HideShow
#> S1-S5 in Condition 1, S6-S10 in Condition 2, S11-S15 in Condition 3, against
#> S1-S5 in every condition in the within-subjects panel — matches Figure 2.7,
#> p. 68.

ggplot(figure_2_7, aes(x = condition, y = slot)) +
  geom_tile(fill = "white", color = "black", linewidth = 0.6) +
  geom_text(aes(label = sub("S([0-9]+)", "plain(S)[\\1]", subject)),
            parse = TRUE, color = "#0C2340", size = 3.6) +
  facet_wrap(~ panel, ncol = 1) +
  scale_x_discrete(expand = expansion(0), position = "top") +
  scale_y_discrete(expand = expansion(0), breaks = NULL) +
  labs(x = NULL, y = NULL)
Figure 2.7 (book p. 68): a between-subjects design, in which fifteen subjects each appear in one of three conditions, and a within-subjects design, in which five subjects each appear in all three.

Figure 2.7 (book p. 68): a between-subjects design, in which fifteen subjects each appear in one of three conditions, and a within-subjects design, in which five subjects each appear in all three.

The division of labor these three figures set up is the book’s own table of contents: between-subjects designs in Chapters 3 through 10, within-subjects and correlated-data designs in Chapters 11 through 16 (p. 68).

2.5 — The New Experimental Revolution

Section 2.5 documents how much more common experimentation has become, and devotes its closing pages to online controlled experiments — A/B tests — whose defining feature is an enormous supply of participants and, usually, a very small effect to detect (pp. 68–70). The chapter is explicit that the principles are the same as for traditional experiments while some of the implications differ, and that power analysis and sample-size planning are among the points of emphasis the rest of the book develops (p. 70).

The sample-size arithmetic shows why the scale is what it is. Suppose a conversion rate of 5.0% and a lift worth detecting of one-tenth of a percentage point. Base R’s power.prop.test() and pwr’s pwr.2p.test() — the latter working on the arcsine-transformed difference — give the per-arm requirement.

HideShow
base_arm <- power.prop.test(
  p1          = 0.050,
  p2          = 0.051,
  power       = 0.80,
  sig.level   = 0.05,
  alternative = "two.sided"
)$n

pwr_arm <- pwr.2p.test(
  h           = ES.h(p1 = 0.051, p2 = 0.050),
  power       = 0.80,
  sig.level   = 0.05,
  alternative = "two.sided"
)$n

c(power.prop.test = base_arm, pwr.2p.test = pwr_arm)
HideShow
power.prop.test     pwr.2p.test 
       752702.5        752684.0 
HideShow
#> About 753,000 per arm, 1.5 million in all, for a tenth-of-a-point lift.
#> The two routes differ by 0.002%, well inside any planning tolerance.

A requirement of that size is the reason the book reports, from the online experimentation literature it reviews, that detecting a swing of ten million dollars in annual revenue at 80% power could call for 18.4 billion participants — more than twice the human population (p. 70). We do not reproduce that figure here, because the assumptions behind it belong to the study the chapter is citing rather than to the chapter itself, but the arithmetic is of exactly this kind.

At the other end of the scale the approximation is less comfortable. It is dependable at the sample sizes above, but in a small pilot the discreteness of the binomial starts to matter. For the same two-proportion comparison, power_fisher_exact() in DMAR reports the power Fisher’s exact test actually has at a given pair of group sizes.

HideShow
power_fisher_exact(
  n_1         = 200,
  n_2         = 200,
  p_1         = 0.050,
  p_2         = 0.100,
  alpha_level = 0.05,
  alternative = "two_sided"
)
term value
power 0.472
odds_ratio_alt 0.474
p_1 0.050
p_2 0.100
expected_s 30
n_1 200
n_2 200
alpha_level 0.050
HideShow
power.prop.test(
  n           = 200,
  p1          = 0.050,
  p2          = 0.100,
  sig.level   = 0.05,
  alternative = "two.sided"
)$power
HideShow
[1] 0.4753103
HideShow
#> Exact power 0.472 against the approximation's 0.475: the same conclusion
#> about an underpowered pilot, with the exact test slightly more conservative.

Readers who want the relationship between traditional and online experiments developed at length are pointed by the chapter to its own references on the subject (p. 70).

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 2: Drawing Valid Inferences from Experiments (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