Rainbow-coloured threads streaming across a dark background
Week: Design of Experiments

Workshop 8A: Difference of Means with t-tests + Permutation Tests

Applying T-tests and ANOVA
Tim Fraser
Tim Fraser, PhD
Assistant Teaching Professor, Systems Engineering
Cornell University
SYSEN 5300: Systems Engineering & Six Sigma
for Design and Operation of Reliable Systems
Learning Outcomes
Target with an arrow in the bullseye
  • Risk Analysis
    • Risk assessment and risk characterization
    • Failure Modes and Effects Analysis
  • Modeling Reliability
    • Component and system reliability
    • Physical acceleration models
  • Quality Control
    • Six sigma and statistical process control
    • Catch performance slipping, in time to act
  • System Improvement
    • Design of experiments
    • Response surfaces

Today's Class

  • Recap: Comparing 2 Groups
  • Comparing 3 Groups
  • Block Randomization with 3 Groups
  • Latin Squares
Rows of laboratory glassware on a bench, one flask in focus

Recap: Comparing 2 Groups


Workshop 8A
Designing Experiments

Example 1

Comparing 2 Unpaired Samples with T-tests in R

An industrial process made varying yields from methods A and B.

Does B have a significantly better yield than A?

library(dplyr)
library(broom)
data = tibble(
  method = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
             "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"),
  yield = c(89.7, 81.4, 84.5, 84.8, 87.3, 79.7, 85.1, 81.7,
            83.7, 84.5, 84.7, 86.1, 83.2, 91.9, 86.3, 79.3,
            82.6, 89.1, 83.7, 88.5))
# T-test assuming equal variance (unpaired)
t.test(formula = yield ~ method, data = data, var.equal = TRUE)
data:  yield by method
t = -0.88158, df = 18, p-value = 0.3896
alternative hypothesis: true difference in means between group A and group B is not equal to 0
# T-test not assuming equal variance
t.test(formula = yield ~ method, data = data, var.equal = FALSE)
data:  yield by method
t = -0.88158, df = 17.129, p-value = 0.3902
alternative hypothesis: true difference in means between group A and group B is not equal to 0
import pandas as pd
from scipy import stats

data = pd.DataFrame({
  'method': ["A"]*10 + ["B"]*10,
  'yield': [89.7, 81.4, 84.5, 84.8, 87.3, 79.7, 85.1, 81.7,
            83.7, 84.5, 84.7, 86.1, 83.2, 91.9, 86.3, 79.3,
            82.6, 89.1, 83.7, 88.5]})
a = data['yield'][data.method == "A"]
b = data['yield'][data.method == "B"]
# T-test assuming equal variance (unpaired)
r = stats.ttest_ind(a, b, equal_var=True)
print(f"t = {r.statistic:.5f}, df = {r.df:.0f}, "
      f"p-value = {r.pvalue:.4f}")
t = -0.88158, df = 18, p-value = 0.3896
# T-test not assuming equal variance
r = stats.ttest_ind(a, b, equal_var=False)
print(f"t = {r.statistic:.5f}, df = {r.df:.3f}, "
      f"p-value = {r.pvalue:.4f}")
t = -0.88158, df = 17.129, p-value = 0.3902

Example 2

Comparing Variance in 2 Groups with F-tests in R

An industrial process made varying yields from methods A and B.

Is the variance in yield for methods A vs. B significantly different?

library(dplyr)
library(broom)

data = tibble(
  method = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
             "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"),
  yield = c(89.7, 81.4, 84.5, 84.8, 87.3, 79.7, 85.1, 81.7,
            83.7, 84.5, 84.7, 86.1, 83.2, 91.9, 86.3, 79.3,
            82.6, 89.1, 83.7, 88.5))

# F test to compare two variances.
var.test(formula = yield ~ method, data = data)

# Are they significantly different? (No --> p = 0.5049)
data:  yield by method
F = 0.63193, num df = 9, denom df = 9, p-value = 0.5049
alternative hypothesis: true ratio of variances is not equal to 1
import pandas as pd
from scipy import stats

data = pd.DataFrame({
  'method': ["A"]*10 + ["B"]*10,
  'yield': [89.7, 81.4, 84.5, 84.8, 87.3, 79.7, 85.1, 81.7,
            83.7, 84.5, 84.7, 86.1, 83.2, 91.9, 86.3, 79.3,
            82.6, 89.1, 83.7, 88.5]})
a = data['yield'][data.method == "A"]
b = data['yield'][data.method == "B"]

from scipy.stats import f

# F test to compare two variances.
stat = a.var(ddof=1) / b.var(ddof=1)
p = 2 * min(f.cdf(stat, len(a)-1, len(b)-1),
            f.sf(stat, len(a)-1, len(b)-1))
print(f"F = {stat:.5f}, num df = {len(a)-1}, "
      f"denom df = {len(b)-1}, p-value = {p:.4f}")

# Are they significantly different? (No --> p = 0.5049)
F = 0.63193, num df = 9, denom df = 9, p-value = 0.5049

Example 3

Comparing 2 Paired Samples with T-tests in R

An industrial process made a series of paired products, each using method A and B.

Do the products made by B have a significantly better yield than A?

library(dplyr)
library(broom)

# When data comes in pairs, like this, we use paired t-tests.
data2 = tibble(
  yield_a = c(89.7, 81.4, 84.5, 84.8, 87.3, 79.7, 85.1, 81.7,
              83.7, 84.5),
  yield_b = c(84.7, 86.1, 83.2, 91.9, 86.3, 79.3, 82.6, 89.1,
              83.7, 88.5)
)

# Basic method for paired t-test
t.test(data2$yield_a, data2$yield_b, paired = TRUE, var.equal = TRUE)
data:  data2$yield_a and data2$yield_b
t = -0.9736, df = 9, p-value = 0.3557
alternative hypothesis: true mean difference is not equal to 0
import pandas as pd
from scipy import stats

# When data comes in pairs, we use paired t-tests.
data2 = pd.DataFrame({
  'yield_a': [89.7, 81.4, 84.5, 84.8, 87.3,
              79.7, 85.1, 81.7, 83.7, 84.5],
  'yield_b': [84.7, 86.1, 83.2, 91.9, 86.3,
              79.3, 82.6, 89.1, 83.7, 88.5]})

# Basic method for paired t-test
r = stats.ttest_rel(data2.yield_a, data2.yield_b)
print(f"t = {r.statistic:.4f}, df = {r.df:.0f}, "
      f"p-value = {r.pvalue:.4f}")
t = -0.9736, df = 9, p-value = 0.3557

Example 4

Permutation Test for Difference of Means

Same yields from methods A and B as in Example 1.

Does B have a significantly better yield than A?

Histogram of 1000 shuffled differences of means centered near zero, with the observed difference of -1.3 marked by a red vertical line
library(dplyr)
library(broom)
data = tibble(
  method = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
             "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"),
  yield = c(89.7, 81.4, 84.5, 84.8, 87.3, 79.7, 85.1, 81.7,
            83.7, 84.5, 84.7, 86.1, 83.2, 91.9, 86.3, 79.3,
            82.6, 89.1, 83.7, 88.5))
set.seed(12345)
perms = tibble(rep = 1:1000) %>%
  group_by(rep) %>%
  reframe(data) %>%
  group_by(rep) %>%
  mutate(yield = sample(yield, size = n(), replace = FALSE)) %>%
  group_by(rep) %>%
  summarize(dbar = mean(yield[method == "A"]) -
                   mean(yield[method == "B"]))
obs = data %>%
  summarize(dbar = mean(yield[method == "A"]) -
                   mean(yield[method == "B"]))
mean( abs(perms$dbar) >= abs(obs$dbar) )
import numpy as np
import pandas as pd

data = pd.DataFrame({
  'method': ["A"]*10 + ["B"]*10,
  'yield': [89.7, 81.4, 84.5, 84.8, 87.3, 79.7, 85.1, 81.7,
            83.7, 84.5, 84.7, 86.1, 83.2, 91.9, 86.3, 79.3,
            82.6, 89.1, 83.7, 88.5]})

# Shuffling is random -- fix the seed so your numbers match.
rng = np.random.default_rng(12345)
y = data['yield'].to_numpy()
is_a = (data.method == "A").to_numpy()
dbar = []
for rep in range(1000):
  z = rng.permutation(y)
  dbar.append(z[is_a].mean() - z[~is_a].mean())
perms = np.array(dbar)
obs = y[is_a].mean() - y[~is_a].mean()
np.mean(np.abs(perms) >= np.abs(obs))
Example 4
Activity — Practice It (Permutation Tests)

Shuffle the labels yourself: run a permutation test on a new pair of samples.

Open the activity →

Comparing 3 Groups


Workshop 8A
Designing Experiments
Comparing 3 Groups
Case Study: Blood Coagulation Times
  • 24 animals receiving four different diets A, B, C, D.
  • Diets randomly assigned to animals
  • Testing done in random order.

Is there real difference between the mean coagulation times for the 4 diets?

  • Analysis of Variance (ANOVA) table
ABCD
62(20)63(12)68(16)56(23)
60(2)67(9)66(7)62(3)
63(11)71(15)71(1)60(6)
59(10)64(14)67(17)61(18)
63(5)65(4)68(13)63(22)
59(24)66(8)68(21)64(19)
Treatment average61666861
Grand average64646464
Difference−3+2+4−3
Dot plot of the 24 coagulation times, one row per diet with D on top and A at the bottom, each diet's dots on a soft blob in its own colour: A tan, B purple, C blue, D rose, on an axis from 56 to 71
Comparing 3 Groups
ANOVA: Analysis of Variance
The same dot plot with a dark vertical line at the grand mean of 64 and a coloured tick at each diet's group mean: D 61 red, C 68 light blue, B 66 purple, A 61 yellow
  • Key Question: How much better are the groups at explaining variation than the grand mean?
yti = η + τt + εti
yti: i-th observation, diet tη: grand meanτt: treatment effectεti: IID error
ABCD
62(20)63(12)68(16)56(23)
60(2)67(9)66(7)62(3)
63(11)71(15)71(1)60(6)
59(10)64(14)67(17)61(18)
63(5)65(4)68(13)63(22)
59(24)66(8)68(21)64(19)
Treatment average61666861
Grand average64646464
Difference−3+2+4−3
E(mT) = Σ τt2 + σ2    E(mR) = σ2 No difference in the four treatments, i.e. H0: τ1 = τ2 = τ3 = τ4 = 0 Then both mT and mR estimate σ2
Comparing 3 Groups
Calculating Deviations in ANOVA
Source of VariationSum of SquaresDegrees of FreedomMean Square
Between treatmentsST = 228νT = 3mT = 76.0F3,20 = 13.6
Within treatmentsSR = 112νR = 20mR = 5.6
Total about the grand averageSD = 340νD = 23
Observations yti
62636856
60676662
63717160
59646761
63656863
59666864
Y
 
 
:
Deviations from grand average of 64
−2−14−8
−432−2
−177−4
−503−3
−114−1
−5240
D = Y − 64
340
23
=
Treatment deviations
−324−3
−324−3
−324−3
−324−3
−324−3
−324−3
T
228
3
+
Residuals within-treatment deviations
1−30−5
−11−21
253−1
−2−2−10
2−102
−2003
R
112
20
ABCD
62(20)63(12)68(16)56(23)
60(2)67(9)66(7)62(3)
63(11)71(15)71(1)60(6)
59(10)64(14)67(17)61(18)
63(5)65(4)68(13)63(22)
59(24)66(8)68(21)64(19)
Treatment average61666861
Grand average64646464
Difference−3+2+4−3
Plain dot plot of the 24 coagulation times, one row per diet, D on top, on an axis from 56 to 71
Comparing 3 Groups
Quantities of Interest in ANOVA
Source of VariationSum of SquaresDegrees of FreedomMean Square
Between treatmentsST = 228νT = 3mT = 76.0F3,20 = 13.6
Within treatmentsSR = 112νR = 20mR = 5.6
Total about the grand averageSD = 340νD = 23
  • Total Sum of Squares measures the total variation in your data, using the grand mean as benchmark.
    TSS = Σ (x − x̄grand)2
  • Residual Sum of Squares is the unexplained error left within each treatment group.
    RSS = Σ (x − x̄group)2
  • Explained Sum of Squares is the deviation explained by your group means.
    ESS = TSS − RSS
  • n = sample size (24); k = # of groups (4)
  • Mean Squares Explained: average variation explained, divided by k − 1 groups.
    MSE = (TSS − RSS) / (k − 1)
  • Mean Squared Error: average variation unexplained, divided by n − k.
    MSR = RSS / (n − k)
  • F-statistic: ratio of variation explained versus unexplained.
    F = MSE / MSR
Comparing 3 Groups
F-statistic in ANOVA
  • If the error is normally distributed (independence of observation assumption of regression)
  • Then MSE and MSR are independently distributed.
  • So, ratio F = MSE / MSR follows an F distribution.
  • F-statistic: How much better is your model than an intercept model alone? (grand mean)
Under H0 that Στ2 = 0,
F = mT / mR ~ F3,20
The F distribution with 3 and 20 degrees of freedom: a sharp peak near zero and a long right tail, with arrows at the 5% point (3.10), the 1% point (4.94) and the 0.1% point (8.10), and the observed ratio 13.6 far out in the tail

Example 5

Comparing 3+ group means with F-test in R

Craig, Kim, and Melanie work for a bakery, but Craig’s donuts seem kinda weird.

Does the weight of these donuts vary significantly by the baker?

Calculate an F-statistic and evaluate its significance.

library(dplyr); library(readr); library(broom)
url = "https://raw.githubusercontent.com/timothyfraser/sysen/main/"
donuts = read_csv(paste0(url, "workshops/donuts.csv"))
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
url = "https://raw.githubusercontent.com/timothyfraser/sysen/main/"
donuts = pd.read_csv(url + "workshops/donuts.csv")
Violin plots of donut weight for three bakers: Craig at the bottom with a long right tail up to about 39, Kim in the middle tightly around 28.5, Melanie at the top with a gap before a few heavy donuts; a large blue dot marks each baker's mean and a vertical blue line marks the grand mean of 30
# use lm()
donuts %>%
  summarize(lm(weight ~ baker) %>% glance()) %>%
  select(sigma, statistic, p.value, df)
sigmastatisticp.valuedf
2.946.240.003952
# or use aov()
donuts %>%
  reframe(aov(weight ~ baker) %>% tidy())
termdfsumsqmeansqstatisticp.value
baker2108.54.06.240.00395
Residuals47407.8.66NANA
# use lm()
m = smf.ols('weight ~ baker', data=donuts).fit()
# sigma, statistic, p.value, df
m.mse_resid ** 0.5, m.fvalue, m.f_pvalue, m.df_model
(2.9424, 6.2398, 0.003951, 2.0)
# or use aov()
sm.stats.anova_lm(m, typ=2)
          sum_sq    df       F    PR(>F)
baker      108.05   2.0  6.2398  0.003951
Residual   406.91  47.0     NaN       NaN

Example 6

Comparing unequal group variances with K2 test in R

Craig, Kim, and Melanie work for a bakery, but Craig’s donuts seem kinda weird.

  1. Does the variance in weight vary significantly by baker? Calculate a K2 statistic.
  2. If so, do NOT assume equal variance in ANOVA. Calculate your F statistic.
library(dplyr); library(readr); library(broom)
url = "https://raw.githubusercontent.com/timothyfraser/sysen/main/"
donuts = read_csv(paste0(url, "workshops/donuts.csv"))
# Are the variances of my 3+ groups significantly different?
# Homogeneity of Variance - Bartlett's test for K^2
bartlett.test(weight ~ baker, data = donuts)
# If K-squared is significant, the differences
# in variance are significant.
data:  weight by baker
Bartlett's K-squared = 10.64, df = 2, p-value = 0.004892
donuts %>%
  summarize(
    oneway.test(weight ~ baker,
                data = donuts, var.equal = FALSE) %>% tidy())
num.dfden.dfstatisticp.value
228.46.140.00607
import numpy as np
from scipy import stats
# donuts: the same data as Example 5
g = [d['weight'].to_numpy()
     for _, d in donuts.groupby('baker')]
# Bartlett's test for K^2
stats.bartlett(*g)
BartlettResult(statistic=10.64, pvalue=0.004892)
# Welch's ANOVA -- no equal-variance assumption
n = np.array([len(x) for x in g])
mu = np.array([x.mean() for x in g])
w = n / np.array([x.var(ddof=1) for x in g])
mw = (w * mu).sum() / w.sum()
k = len(g)
lam = ((1 - w / w.sum())**2 / (n - 1)).sum()
num = (w * (mu - mw)**2).sum() / (k - 1)
stat = num / (1 + 2 * (k - 2) / (k**2 - 1) * lam)
df2 = 1 / (3 / (k**2 - 1) * lam)
k - 1, df2, stat, stats.f.sf(stat, k - 1, df2)
(2, 28.4, 6.14, 0.00607)

Randomized Block Design with 3+ groups


Workshop 8A
Designing Experiments
Comparing 3 Groups
Randomized Block Design
  • To thoroughly control for alternative explanations, we can randomly assign observations to treatments within blocks.
  • Example: Penicillin
  • Estimate effect of treatments accounting for blends.
BlockTreatmentBlock averagesBlock deviations
ABCD
Blend 189(1)88(3)97(2)94(4)92+6
Blend 284(4)77(2)92(3)79(1)83−3
Blend 381(2)87(1)87(4)85(3)85−1
Blend 487(1)92(3)89(2)84(4)88+2
Blend 579(3)81(4)80(1)88(2)82−4
Treatment averages84858986Grand average:
Treatment deviations−2−1+3086
It is important to understand that in a randomized block experiment the treatments are randomized within the blocks.
Twenty animals split into five blends of four, each blend receiving treatments A, B, C and D N = 20 Animals N1 = 4 N1A N1B N1C N1D N2 = 4 N2A N2B N2C N2D N3 = 4 N3A N3B N3C N3D N4 = 4 N4A N4B N4C N4D N5 = 4 N5A N5B N5C N5D
Comparing 3 Groups
Randomized Block Design (2)
Source of variationSum of squaresDegrees of freedomMean squareF ratio
Between blocks (blends)SB = 264νB = (n − 1) = 4mB = 66.0F4,12 = 3.51
Between treatmentsST = 70νT = (k − 1) = 3mT = 23.3F3,12 = 1.24
ResidualsSR = 226νR = (n − 1)(k − 1) = 12mR = 18.8
Deviations from grand averageSD = 560nk − 1 = 19
Observations
ybt
89889794
84779279
81878785
87928984
79818088
Y
Analysis of
observations
:
Deviations from Grand Average
ybt − ȳ
32118
−2−96−7
−511−1
163−2
−7−5−62
D
SD = 560
νD = 19
=
Block Deviations
ȳb − ȳ
6666
−3−3−3−3
−1−1−1−1
2222
−4−4−4−4
B
SB = 264
νB = 4
+
Treatment Deviations
ȳt − ȳ
−2−130
−2−130
−2−130
−2−130
−2−130
T
ST = 70
νT = 3
+
Residuals
ybt − ȳb − ȳt + ȳ
−1−322
3−56−4
−23−10
15−2−4
−10−56
R
SR = 226
νR = 12
Comparing 3 Groups
Randomized Block Design (3)
Why Use Randomized Blocking?
  • Of the total sum of squares not associated with treatments or with the mean, almost 50% is just block-to-block variation.
  • If experiment arranged on a completely randomized basis with no blocks, the error variance would have been much larger.
  • With randomized block, error much less: of the total of SD = 560, SB = 264 removed by blocks.
  • Randomized block design greatly increased sensitivity of experiment; now able to detect smaller treatment differences.
Formulation
yti = η + βi + τt + εti
ηti = η + βi + τt
Additive: τ3 = 6 and block 4 adds 5, so both together add 6 + 5 = 11
If not additive: a block × treatment interaction
Comparing 3 Groups
Randomized Block Design (4)
Diagnostic Tests
Dot plots of the 20 penicillin residuals on an axis from minus 6 to 6, first all together and then split by blend 1 to 5 and by treatment 1 to 4
Figure 4.9 (a) Dot plots of residuals. (b) Residuals identified by block and treatment.
Scatter plot of the penicillin residuals against the predicted values from 80 to 95, scattered evenly around zero
Figure 4.10 Residuals plotted against the predicted values: penicillin experiment. data · code

Latin Squares


Workshop 8A
Designing Experiments
Latin Squares
Using 2 Types of Blocks: Air Pollution

How much does adding Chemical A, B, C, or D to gasoline reduce air pollution?

  • 4 treatments tested
  • 4 different drivers
  • 4 different cars
  • 2 block factors: drivers & cars
  • Latin Squares Design helps eliminate differences in treatment comparisons that are just due to drivers or cars.
  • Each treatment (A, B, C, or D) randomly appears 1 per row (driver) and 1 per column (car).
Hypothesized relationship: chemical type affects air pollution; driver differences and car differences are confounding variables that act on both Hypothesized Relationship Chemical Type Air Pollution Driver Differences Car Differences Confounding Variables
Driver1234
IA 19B 24D 23C 26
IID 23C 24A 19B 30
IIIB 15D 14C 15A 16
IVC 19A 18B 19D 16
CarsDriversAdditives
1: 19I: 23A: 18
2: 20II: 24B: 22
3: 19III: 15C: 21
4: 22IV: 18D: 19
Cars 1–4 across, drivers I–IV down. Grand average: 20
Latin Squares
Using 2 Types of Blocks (2): Air Pollution
  • No convincing evidence for differences between the treatments
  • Latin square design effective in eliminating variation due to drivers
Source of VariationSum of SquaresdfMean SquareRatio of Mean SquaresP
Cars (columns)SC = 243mC = 8.00F3,6 = mC/mR = 1.50.31
Drivers (rows)SD = 2163mD = 72.00F3,6 = mD/mR = 13.5<0.01
Treatments (additives)ST = 403mT = 13.33F3,6 = mT/mR = 2.50.16
ResidualsSR = 326mR = 5.33
TotalSV = 31215
Observations (cars 1–4)
A 19B 24D 23C 26
D 23C 24A 19B 30
B 15D 14C 15A 16
C 19A 18B 19D 16
Y
 
 
:
Deviations from grand average (20)
−1436
34−110
−5−6−5−4
−1−2−1−4
V
312
15
=
Columns (cars)
−10−12
−10−12
−10−12
−10−12
C
24
3
+
Rows (drivers)
3333
4444
−5−5−5−5
−2−2−2−2
D
216
3
+
Treatments (additives)
−22−11
−11−22
2−11−2
1−22−1
T
40
3
+
Residuals
−1−120
1−1−22
−1001
120−3
R
32
6
Latin Squares
Using 3 Types of Blocks: Graeco-Latin Squares
  • A Graeco-Latin square is a k by k pattern
  • Allows study of k treatment simultaneously, with 3 blocking variables, each at k levels
  • Example: 1 extra blocking variable in car emissions
  • Eg. day of test
DriverCar 1Car 2Car 3Car 4
IAαBβCγDδ
IIBδAγDβCα
IIICβDαAδBγ
IVDγCδBαAβ
Additives: A, B, C, D  ·  Days: α, β, γ, δ
Latin Squares
Using 3+ Types of Blocks: Hyper-Graeco Squares
  • Machine used to test wearing quality of types of cloth. Records weight loss from test piece in 1 machine cycle (rubbed against a std. grade of emery paper)
  • Treatments: 4 types of cloth A, B, C, D
  • Block Type 1: Mounted in 4 specimen holder 1, 2, 3, 4.
  • Block Type 2: Each holder can be in 1 of 4 positions P1, P2, P3, P4.
  • Block Type 3: Each emery paper sheet alpha, beta, gamma, delta was cut into 4 quarters.
  • Objective: (1) Make accurate comparison of the treatments; (2) Understand variability caused by various factors-holders, positions, emery papers, and cycles
A smiling corgi looking out of the round door of a stainless-steel washing machine
Latin Squares
Martindale Wear Tester: the design
CyclesP1P2P3P4Legend
C1αA1 / 320βB2 / 297γC3 / 299δD4 / 313Replicate I
Cycles: C1–C4
Treatments: A, B, C, D
Holders: 1, 2, 3, 4
Emery paper sheets: α, β, γ, δ
C2βC4 / 266αD3 / 227δA2 / 260γB1 / 240
C3γD2 / 221δC1 / 240αB4 / 267βA3 / 252
C4δB3 / 301γA4 / 238βD1 / 243αC2 / 290
Table 4.12. Cell = emery paper, treatment, holder / wear.
CyclesP1P2P3P4Legend
C5εA1 / 285ξB2 / 280θC3 / 331κD4 / 311Replicate II
Cycles: C5–C8
Treatments: A, B, C, D
Holders: 1, 2, 3, 4
Emery paper sheets: ε, ξ, θ, κ
C6ξC4 / 268εD3 / 233κA2 / 291θB1 / 280
C7θD2 / 265κC1 / 273εB4 / 234ξA3 / 243
C8κB3 / 306θA4 / 271ξD1 / 270εC2 / 272
  • The design was effective both in removing sources of extraneous variation and in indicating their relative importance.
  • Because of the elimination of these disturbances, the residual variance was reduced by a factor of 8
  • We could detect much smaller differences in treatment
Latin Squares
Martindale Wear Tester: the analysis
TreatmentsA: 270.0B: 275.6C: 279.9D: 260.4
Holders1: 268.92: 272.03: 274.04: 271.0
PositionsP1: 279.0P2: 257.4P3: 274.4P4: 275.1
Emery papersα: 276.0β: 264.5γ: 249.5δ: 278.5ε: 256.0ξ: 265.3θ: 286.8κ: 295.3
CyclesC1: 307.3C2: 248.3C3: 245.0C4: 268.0C5: 301.8C6: 268.0C7: 253.8C8: 279.8
ReplicatesI: 267.1II: 275.8Grand average = 271.5
SourcedfSum of SquaresMean SquaresRatio of Mean Sq.
Σ(y − ȳ)23126,463.97
Replications1603.78mRep = 603.78mRep/mR = 5.73
Cycles614,770.44mC = 2,461.74mC/mR = 23.35
Positions32,217.34mP = 739.11mP/mR = 7.01
Emery papers66,108.94mE = 1,018.16mE/mR = 9.66
Holders3109.09mH = 36.36mH/mR = 0.34
Treatments31,705.34mT = 568.45mT/mR = 5.39
Residuals9949.03mR = 105.45
  • The F-stat=5.39 with 3, 9 DOF, significant at 2% level.
  • By using a design which makes it possible to remove the effects of many larger disturbing factors, differences between treatment were made detectable.
Latin Squares
Martindale Wear Tester: graphical analysis
Graphical ANOVA: dots for the replicates, cycles, emery papers, positions, holders, treatments and residuals of the Martindale wear example; position P2 sits far to the left
Graphical ANOVA for the Martindale wear example (Box, Hunter & Hunter, Figure 4.14). data · code
  • In graphical analysis, position P2 gives much less wear than others, indicating a need of improvement
  • The analysis identified the large contributions to the total variation due to cycles and to emery papers.
  • This suggested improvements, which later led to changes in the design of the machine.
Latin Squares
Balanced Incomplete Block Design
  • Suppose the Martindale wear tester allowed only three samples to be included in each cycle, but you had 4 treatment A, B, C, and D to compare
  • You have 4 treatments but a block size of 3, too small to accommodate all the treatments simultaneously → balanced incomplete block design.
  • Property: every pair of treatment occurs together in a block the same number of times.
Table 4.14. A Balanced Incomplete Block Design, t = 4 Treatments in b = 4 Blocks of Size k = 3
Block (cycle) of 1000 revolutionsTreatments
1ABC
2ABD
3ACD
4BCD
Block (cycle) of 1000 revolutionsABCD
1xxx
2xxx
3xxx
4xxx
Latin Squares
Doubly Balanced Designs: Youden Squares
  • Compare 7 treatment in seven blocks of size 4 (e.g., test 7 types of cloth A, B, C, D, E, F, and G)
  • But only 4 test pieces could be compared simultaneously in a single machine cycle)
  • Also had the opportunity to eliminate a second source of block variation, machine positions
Block (cycle)ABCDEFG
1α 627β 248γ 563δ 252
2α 344β 233δ 442γ 226
3α 251γ 211δ 160β 297
4β 337δ 537γ 195α 300
5γ 520δ 278β 199α 595
6γ 369δ 196α 185β 606
7δ 396β 602γ 240α 273
Table 4.15. Youden Square, t = 7 Treatments, b = 7 Blocks, Block Size k = 4. Within blocks, every pair of treatments appears twice.
Your Turn: R playground

Compare the three bakers in R — group means of donut weight, then lm() and anova().

Your Turn: Python playground

The same ANOVA in Python — smf.ols() and anova_lm().

Latin Squares
Factorial Design
  • How do we handle these wacky designs?
  • Learn more next week in Factorial Design!
Closing illustration pointing ahead to factorial design

Appendix


Workshop 8A
Original slides (not taught)
Appendix: original — Coagulation dot plot
Dot plot of coagulation times for diets A, B, C and D on an axis from 56 to 71
Appendix: original — Coagulation data table
Table of 24 coagulation times by diet A to D with treatment averages 61, 66, 68, 61, grand average 64 and differences -3, +2, +4, -3
Appendix: original — One-way ANOVA model
The one-way ANOVA model as an equation: y sub t i equals eta plus tau sub t plus epsilon sub t i, where y sub t i is the i-th observation in diet group t, eta is the overall (grand) mean, tau sub t is the treatment effect (the deviation produced by treatment t), and epsilon sub t i is the associated error, assumed IID
Appendix: original — Expected mean squares
Expected mean squares as equations: E of m sub T equals the sum of tau sub t squared plus sigma squared, and E of m sub R equals sigma squared; if there were no difference in the four treatments, that is, null hypothesis tau 1 equals tau 2 equals tau 3 equals tau 4 equals 0, then both m sub T and m sub R estimate sigma squared
Appendix: original — F ratio formula
F ratio as mean squares explained over mean squared error
Appendix: original — Block model formulation
Randomized block model y = eta + beta_i + tau_t + epsilon, its additive expected response, and a worked additive example with tau_3 = 6 and block 4 adding 5
Appendix: original — Latin square data
Table 4.8, the 4 by 4 Latin square of automobile emissions data by drivers I to IV and cars 1 to 4, with averages
Appendix: original — Latin square decomposition
Table 4.9, decomposition of the Latin square into grand average, columns, rows, treatments and residuals with sums of squares 312, 24, 216, 40, 32
Appendix: original — Latin square ANOVA table
Table 4.10, analysis of variance for the Latin square example
Appendix: original — Example 1 equal-variance t-test
R console output of a two-sample t-test assuming equal variance: t = -0.88158, df = 18, p-value = 0.3896
Appendix: original — Example 1 Welch t-test
R console output of a Welch two-sample t-test: t = -0.88158, df = 17.129, p-value = 0.3902
Appendix: original — Example 2 F-test
R console output of var.test comparing the variance of yield between methods A and B: F = 0.63193, p-value = 0.5049
Appendix: original — Example 3 paired t-test
R console output of a paired t-test comparing yield_a and yield_b: t = -0.9736, df = 9, p-value = 0.3557
Appendix: original — Example 4 permutation histogram
Histogram of 1000 permuted differences of means with the observed difference marked
Appendix: original — ANOVA table
The analysis of variance table for the coagulation data: between treatments S_T 228 on 3 degrees of freedom, mean square 76.0, F 13.6; within treatments S_R 112 on 20, mean square 5.6; total S_D 340 on 23
Appendix: original — Deviations in ANOVA
Box, Hunter and Hunter's decomposition of the 24 coagulation observations Y into deviations from the grand average D = Y - 64, treatment deviations T and residuals R, with sums of squares 340 = 228 + 112 and degrees of freedom 23 = 3 + 20
Appendix: original — F distribution
Skewed F density with arrows at the 5 percent, 1 percent and 0.1 percent points and the observed ratio of 13.6 far in the tail
Appendix: original — lm glance output
R console output: sigma 2.94, statistic 6.24, p.value 0.00395, df 2
Appendix: original — aov tidy output
R console output: baker 2 degrees of freedom, sum of squares 108, mean square 54.0, statistic 6.24, p-value 0.00395; Residuals 47, 407, 8.66
Appendix: original — Bartlett test output
R console output of Bartlett's test: K-squared 10.64, df 2, p-value 0.004892
Appendix: original — Welch ANOVA output
R console output of the one-way test without equal variances: num.df 2, den.df 28.4, statistic 6.14, p.value 0.00607
Appendix: original — donut weights by baker
Violin plots of donut weight for Craig, Kim and Melanie with each baker's mean marked and the grand mean as a vertical line
Appendix: original — Penicillin table
Penicillin yields for five blends and four treatments with run order, block and treatment averages and deviations
Appendix: original — Block ANOVA table
Analysis of variance table for the randomized block penicillin experiment
Appendix: original — Block decomposition
Penicillin observations decomposed into block, treatment and residual matrices
Appendix: original — Residual dot plots
Dot plots of penicillin residuals overall, by block and by treatment
Appendix: original — Residuals vs predicted
Penicillin residuals plotted against predicted values
Appendix: original — Graeco-Latin square
Table 4.11, the 4 by 4 Graeco-Latin square of additives A to D and days alpha to delta by drivers I to IV and cars 1 to 4
Appendix: original — Hyper-Graeco design
Table 4.12, the Martindale wear tester design for Replicates I and II: cycles by positions, each cell holding emery paper, treatment, holder and wear
Appendix: original — Hyper-Graeco averages and ANOVA
Averages table and analysis of variance table for the Martindale wear example with total 26,463.97 on 31 degrees of freedom
Appendix: original — Hyper-Graeco effects
Dot plot of the effects of cycles, emery papers, positions, holders and treatments in the Martindale example
Appendix: original — position plot
Graphical ANOVA dot plot of the Martindale wear example by factor, showing position P2 far to the left
Appendix: original — balanced incomplete block design
Table 4.14, a balanced incomplete block design of four treatments in four blocks of size three, shown as letters and as an incidence table
Appendix: original — Youden square
Table 4.15, a Youden square of seven treatments in seven blocks of size four with positions alpha to delta and wear values