| ← Previous Lecture | Home | Next Lecture → |
In Lecture 05, we investigated whether enzyme activity depends on the dose of a treatment.
The experiment included three treatment groups:
Although there are three treatment groups, they represent three levels of the same experimental factor: treatment dose.
A factor is an experimental variable that may influence the measured outcome. The different values or categories of that factor are called levels.
| Component | In our experiment |
|---|---|
| Factor | Treatment dose |
| Levels | Control, Low dose, High dose |
| Number of factors | 1 |
| Number of levels | 3 |
| Measured response | Enzyme activity |
Since we vary only one factor (treatment dose), this is called one-way ANOVA, regardless of how many levels that factor has.
The scientific question is:
Does mean enzyme activity differ among the three treatment doses?
One-way ANOVA compares the means of the three independent groups to test this question.
Lecture 05 used \(X_{ij}\) for an individual measurement, where \(i\) (row index) is the observation number and \(j\) (column index) is the treatment-group number.
Recall the enzyme-activity measurements from Lecture 05:
| Observation \(i\) | Control (\(j=1\)) | Low dose (\(j=2\)) | High dose (\(j=3\)) |
|---|---|---|---|
| 1 | 10.2 | 11.0 | 13.1 |
| 2 | 11.4 | 12.6 | 14.3 |
| 3 | 10.9 | 11.8 | 13.8 |
| 4 | 12.0 | 12.1 | 14.9 |
| 5 | 11.3 | 13.2 | 12.9 |
| 6 | 10.7 | 12.4 | 14.1 |
| Group mean | 11.0833 | 12.1833 | 13.8500 |
Here \(X_{ij}\) means the measurement in row \(i\), column \(j\): Thus \(X_{32}=11.8\) is the third observation in the second treatment group.
Each group has its own mean, written \(\bar X_j\). For example, \(\bar X_1=11.0833\) is the Control mean.
The grand mean over all 18 measurements is \(\bar X=12.3722\).
A one-way ANOVA model describes each measurement as
\[ \boxed{X_{ij}=\mu+\alpha_j+\varepsilon_{ij}} \]
Recall that ANOVA separates variation into between-group and within-group components.
For the first Control measurement, \(X_{11}=10.2\), we can obtain this decomposition by adding and subtracting the Control group mean (\(11.0833\)) and the grand mean (\(12.3722\)):
\[ \begin{aligned} X_{11} &=10.2\\ &=10.2-11.0833+11.0833-12.3722+12.3722\\ &=\underbrace{12.3722}_{\text{grand mean}} +\underbrace{(11.0833-12.3722)}_{\text{between-group deviation}}\\ &\quad+\underbrace{(10.2-11.0833)}_{\text{within-group deviation}}\\ &=12.3722-1.2889-0.8833. \end{aligned} \]
Thus, every observation can be expressed as
\[ \boxed{ X_{ij}=\bar X+(\bar X_j-\bar X)+(X_{ij}-\bar X_j) } \]
where:
Thus, the sample-data decomposition corresponds to the statistical model
\[ X_{ij}=\mu+\alpha_j+\varepsilon_{ij}. \]
The sample means and residuals provide estimates of the corresponding population quantities and random errors.
This is an exact mathematical identity. Squaring and summing these deviations gives the familiar one-way ANOVA decomposition:
\[ SS_{\mathrm{total}}=SS_{\mathrm{between}}+SS_{\mathrm{within}}. \]
The corresponding statistical model is
\[ X_{ij}=\mu+\alpha_j+\varepsilon_{ij}, \]
where \(\mu+\alpha_j=\mu_j\) is the population mean of group \(j\).
For classical one-way ANOVA, we assume that the random errors are independent and follow
\[ \boxed{ \varepsilon_{ij}\sim N(0,\sigma^2) } \]
This means that observations fluctuate normally around their respective population group means, with the same variance \(\sigma^2\). Normality is assumed for the errors within groups, not for differences between group means.
The decomposition into deviations is an algebraic identity; the normality assumption is an additional condition used to justify the classical ANOVA F-test.
The null hypothesis remains exactly the one used in Lecture 05:
\[ H_0:\mu_1=\mu_2=\cdots=\mu_k. \]
Equivalently, with the usual constraint that the group effects sum to zero, \(H_0\) says that all \(\alpha_j=0\).
The statistic is
\[ F=\frac{MS_{\mathrm{between}}}{MS_{\mathrm{within}}}. \]
A large \(F\) indicates that the differences among group means are large relative to ordinary variation within groups. A significant overall ANOVA does not identify which group means differ; appropriately adjusted comparisons, such as Tukey’s HSD, address that question.
# 01_RecallOneWay.R — standalone, base R
control <- c(10.2, 11.4, 10.9, 12.0, 11.3, 10.7)
low <- c(11.0, 12.6, 11.8, 12.1, 13.2, 12.4)
high <- c(13.1, 14.3, 13.8, 14.9, 12.9, 14.1)
df <- data.frame(
activity = c(control, low, high),
treatment = factor(rep(c("Control", "Low", "High"), each = 6))
)
fit <- aov(activity ~ treatment, data = df)
print(summary(fit))
print(TukeyHSD(fit))Question: What if enzyme activity also depends on another factor such as genotype? Would the treatment have the same effect in every genotype? One-way ANOVA cannot directly answer that second question.
We now add a second factor. A factor is an explanatory variable, such as treatment, genotype, catalyst, or mixing method; its categories are called levels.
Suppose a chemical synthesis uses two factors:
| Factor | Levels | Number of levels |
|---|---|---|
| Catalyst | Platinum, Palladium, Iridium, Palladium/Iridium, Rhodium | 5 |
| Mixing | Stirring, Ultrasonication | 2 |
If every catalyst is tested under both mixing conditions, there are
\[ 5\times2=10 \]
factor combinations (also called cells). Testing all combinations is a full factorial design. With five independent syntheses per cell, the total number of observations is
\[ N=5\times2\times5=50. \]
The scientific questions are now:
The third question concerns an interaction.
# 02_FactorialDesign.R — standalone, base R
catalyst <- c("Platinum", "Palladium", "Iridium", "PdIr", "Rhodium")
mixing <- c("Stirred", "Ultrasonic")
design <- expand.grid(catalyst = catalyst, mixing = mixing)
print(design) # 5 x 2 = 10 combinations
print(nrow(design))
# Add five replicate rows per combination
design_50 <- design[rep(seq_len(nrow(design)), each = 5), ]
row.names(design_50) <- NULL
print(table(design_50$catalyst, design_50$mixing))[!NOTE] For demonstration, we use
expand.grid()andrep()to quickly create a table with five replicates for each factor combination. In a real experiment, replicates must represent independent experimental units.
For two categorical factors \(A\) and \(B\), we add one more group index while continuing to put the observation/replicate index first:
\[ \boxed{X_{ijk}=\mu+\alpha_j+\beta_k+(\alpha\beta)_{jk}+\varepsilon_{ijk}} \]
| Index | Meaning | Example |
|---|---|---|
| \(i\) | Replicate within one factor combination | \(i=1\): first synthesis |
| \(j\) | Level of factor \(A\) | Catalyst \(j=1\): Platinum |
| \(k\) | Level of factor \(B\) | Mixing \(k=1\): Stirred |
For example, when \(X\) represents yield (%) in chemical synthesis:
The terms of the model are:
In plain language:
Observed yield = overall mean + catalyst effect + mixing effect + interaction + replicate-to-replicate error.
An interaction means that the effect of changing the mixing method depends on which catalyst is used. Without an interaction, effects are additive.
With the usual constraints on the effects, ANOVA decomposes variation as
\[ SS_{\mathrm{total}}=SS_A+SS_B+SS_{A:B}+SS_{\mathrm{error}}. \]
For a balanced \(a\times b\) design with \(r\) replicates per combination:
| Source | Degrees of freedom | Mean square | F-statistic |
|---|---|---|---|
| Factor A | \(a-1\) | \(MS_A=SS_A/(a-1)\) | \(MS_A/MS_E\) |
| Factor B | \(b-1\) | \(MS_B=SS_B/(b-1)\) | \(MS_B/MS_E\) |
| Interaction \(A:B\) | \((a-1)(b-1)\) | \(MS_{A:B}=SS_{A:B}/[(a-1)(b-1)]\) | \(MS_{A:B}/MS_E\) |
| Residual/error | \(ab(r-1)\) | \(MS_E=SS_E/[ab(r-1)]\) | — |
| Total | \(abr-1\) | — | — |
As in one-way ANOVA, the classical two-way ANOVA assumes that the random errors \(\varepsilon_{ijk}\) are independent, normally distributed, and have the same variance across all factor combinations:
\[ \boxed{\varepsilon_{ijk}\sim N(0,\sigma^2)} \]
This means that observations are normally distributed around their respective factor-combination (cell) means, with a common variance \(\sigma^2\).
Normality is assumed for the random errors within each cell, not for the combined observations across all cells. These assumptions allow us to use the F-distribution for hypothesis testing in the standard fixed-effects ANOVA model.
# Main effects only (additive model; assumes no interaction)
yield ~ catalyst + mixing
# Interaction term alone (not usually a complete ANOVA model)
yield ~ catalyst:mixing
# Main effects and interaction (full factorial model)
yield ~ catalyst * mixing
# Exactly equivalent expanded formula
yield ~ catalyst + mixing + catalyst:mixingMeaning of *: In a model formula,
A * B means A + B + A:B; it is not arithmetic
multiplication of the measured data.
For the full model, we consider:
If interaction is important, the average main effect may conceal the scientifically relevant differences. Interpret the interaction before making broad claims about the main effects.
Source: Rowe, Chapter 14.
Scientific question: Does drug-synthesis yield (%) depend on the catalyst, mixing method, or their interaction?
Five separate syntheses were reported for each catalyst–mixing combination. All 50 published measurements follow.
| Mixing | Replicate | Platinum | Palladium | Iridium | Pd/Ir alloy | Rhodium |
|---|---|---|---|---|---|---|
| Stirred | 1 | 11.1 | 15.9 | 10.9 | 14.1 | 13.0 |
| Stirred | 2 | 11.7 | 18.9 | 13.4 | 13.9 | 12.3 |
| Stirred | 3 | 9.8 | 18.9 | 10.5 | 14.2 | 11.0 |
| Stirred | 4 | 12.1 | 15.5 | 12.2 | 13.5 | 9.9 |
| Stirred | 5 | 9.3 | 17.2 | 10.9 | 14.0 | 10.6 |
| Ultrasonic | 1 | 12.8 | 20.0 | 14.8 | 16.3 | 13.1 |
| Ultrasonic | 2 | 13.8 | 19.9 | 11.9 | 17.2 | 13.4 |
| Ultrasonic | 3 | 12.3 | 19.5 | 15.3 | 16.0 | 13.8 |
| Ultrasonic | 4 | 12.4 | 20.5 | 14.3 | 15.1 | 13.6 |
| Ultrasonic | 5 | 12.0 | 18.3 | 15.2 | 17.0 | 12.8 |
| Mixing | Platinum | Palladium | Iridium | Pd/Ir alloy | Rhodium |
|---|---|---|---|---|---|
| Stirred | 10.80 | 17.28 | 11.58 | 13.94 | 11.36 |
| Ultrasonic | 12.66 | 19.64 | 14.30 | 16.32 | 13.34 |
| Ultrasonic − stirred | 1.86 | 2.36 | 2.72 | 2.38 | 1.98 |
Ultrasonication produces an observed yield increase of roughly 2–3 percentage points for every catalyst. Because these increases are similar, the pattern suggests an approximately additive effect (little catalyst × mixing interaction). Their average is 2.26 percentage points; the interaction test below assesses whether the differences among these increases exceed what random variation could explain.
aov()# 03_Catalyst_aov.R — standalone, base R
alpha <- 0.05
catalyst <- c("Platinum", "Palladium", "Iridium", "PdIr", "Rhodium")
stir <- list(
c(11.1, 11.7, 9.8, 12.1, 9.3),
c(15.9, 18.9, 18.9, 15.5, 17.2),
c(10.9, 13.4, 10.5, 12.2, 10.9),
c(14.1, 13.9, 14.2, 13.5, 14.0),
c(13.0, 12.3, 11.0, 9.9, 10.6)
)
ultra <- list(
c(12.8, 13.8, 12.3, 12.4, 12.0),
c(20.0, 19.9, 19.5, 20.5, 18.3),
c(14.8, 11.9, 15.3, 14.3, 15.2),
c(16.3, 17.2, 16.0, 15.1, 17.0),
c(13.1, 13.4, 13.8, 13.6, 12.8)
)
make_rows <- function(values, mixing_label) {
data.frame(
catalyst = rep(catalyst, each = 5),
mixing = mixing_label,
yield = unlist(values, use.names = FALSE)
)
}
df <- rbind(make_rows(stir, "Stirred"),
make_rows(ultra, "Ultrasonic"))
df$catalyst <- factor(df$catalyst, levels = catalyst)
df$mixing <- factor(df$mixing, levels = c("Stirred", "Ultrasonic"))
print(table(df$catalyst, df$mixing))
print(aggregate(yield ~ catalyst + mixing, data = df, FUN = mean))
fit <- aov(yield ~ catalyst * mixing, data = df)
result <- summary(fit)
print(result)
p <- result[[1]][1:3, "Pr(>F)"]
for (i in seq_along(p)) {
cat(names(p)[i], ":", if (p[i] < alpha) "Reject H0" else "Do not reject H0", "\n")
}Run this after 03_Catalyst_aov.R, while its
df is available:
# Mean yield for each catalyst and mixing method
group_means <- aggregate(
yield ~ catalyst + mixing,
data = df,
FUN = mean
)
print(group_means)
# Overall mean yield for each mixing method
mixing_means <- aggregate(
yield ~ mixing,
data = df,
FUN = mean
)
print(mixing_means)
# Average increase due to ultrasonication
increase <- mixing_means$yield[
mixing_means$mixing == "Ultrasonic"
] - mixing_means$yield[
mixing_means$mixing == "Stirred"
]
cat("Mean yield increase =", increase,
"percentage points\n")In summary(fit), Df gives the degrees
of freedom, Sum Sq the sum of squares, Mean
Sq the mean square, F value the test
statistic, and Pr(>F) the p-value. Each F-statistic
compares the mean square for that term with the residual mean square. We
report it as \(F_{df_1,df_2}\), where
\(df_1\) is the term’s degrees of
freedom and \(df_2\) is the residual
degrees of freedom.
| Source | Df | F value | p-value |
|---|---|---|---|
| Catalyst | 4 | 67.41 | \(<0.001\) |
| Mixing | 1 | 57.20 | \(<0.001\) |
| Catalyst × Mixing | 4 | 0.27 | 0.898 |
| Residuals | 40 | — | — |
At \(\alpha=0.05\), catalyst and mixing both have significant main effects, while the interaction is not significant. Thus, mean yield differs among catalysts and between mixing methods, but there is insufficient evidence that the effect of mixing differs among catalysts. The observed average improvement with ultrasonication is 2.26 percentage points; the lack of significant interaction does not establish that its effect is exactly equal for all catalysts.
Example of statistical reporting:
Two-way ANOVA indicated effects of catalyst (\(F_{4,40}=67.41\), \(p<0.001\)) and mixing method (\(F_{1,40}=57.20\), \(p<0.001\)), with no detected catalyst × mixing interaction (\(F_{4,40}=0.27\), \(p=0.898\)). Ultrasonication increased mean yield by approximately 2.26 percentage points across the five catalysts studied.
The F-statistics, degrees of freedom, and p-values come from
summary(fit); the average yield difference comes from the
group means calculated with aggregate().
A main effect describes how the mean response changes with one factor, averaged across the levels of the other factor. An interaction occurs when the effect of one factor depends on the level of the other.
In Example 1, ultrasonication increases mean yield by 1.86, 2.36, 2.72, 2.38, and 1.98 percentage points for the five catalysts. These increases are similar, with an average of 2.26 percentage points. This suggests an approximately additive effect: ultrasonication improves yield by roughly the same amount for each catalyst. The interaction F-test finds no statistically significant evidence that these increases differ.
An interaction plot helps visualize this pattern. It connects the two mixing-method means for each catalyst:
The plot describes the observed data; the ANOVA interaction test evaluates whether the differences provide statistical evidence of interaction.
# 04_Catalyst_interaction.R — standalone, base R
catalyst <- c("Platinum", "Palladium", "Iridium", "PdIr", "Rhodium")
stir <- c(10.80, 17.28, 11.58, 13.94, 11.36)
ultra <- c(12.66, 19.64, 14.30, 16.32, 13.34)
# Plot each catalyst's line from stirred to ultrasonic mixing
plot(c(1, 2), range(c(stir, ultra)), type = "n", xaxt = "n",
xlab = "Mixing method", ylab = "Mean yield (%)",
main = "Catalyst x mixing: approximately additive effects")
axis(1, at = 1:2, labels = c("Stirred", "Ultrasonic"))
for (i in seq_along(catalyst)) {
lines(1:2, c(stir[i], ultra[i]), type = "b", pch = i, lty = i)
}
legend("topleft", legend = catalyst, pch = seq_along(catalyst),
lty = seq_along(catalyst), cex = 0.8, bty = "n")
increase <- ultra - stir
print(data.frame(catalyst, increase))
cat("Average improvement =", mean(increase), "percentage points\n")Source: Rowe, Chapter 14.
Scientific question: How do punch design (A, B, C) and compression force (low, high) affect the tensile strength (MPa) of tablets?
There are \(3\times2=6\) combinations, six replicates in each, and \(N=36\) observations.
| Compression force | Replicate | Punch A | Punch B | Punch C |
|---|---|---|---|---|
| Low | 1 | 4.00 | 6.49 | 7.92 |
| Low | 2 | 4.79 | 8.35 | 8.15 |
| Low | 3 | 5.50 | 6.40 | 7.60 |
| Low | 4 | 5.15 | 6.37 | 9.64 |
| Low | 5 | 5.26 | 6.86 | 9.98 |
| Low | 6 | 6.55 | 8.08 | 9.23 |
| High | 1 | 4.90 | 9.63 | 14.60 |
| High | 2 | 4.90 | 9.67 | 13.81 |
| High | 3 | 5.04 | 8.72 | 13.20 |
| High | 4 | 6.93 | 8.08 | 14.62 |
| High | 5 | 7.13 | 10.25 | 13.77 |
| High | 6 | 5.82 | 8.10 | 13.18 |
| Punch | Low force mean | High force mean | High − low (MPa) |
|---|---|---|---|
| A | 5.208 | 5.787 | 0.578 |
| B | 7.092 | 9.075 | 1.983 |
| C | 8.753 | 13.863 | 5.110 |
The observed benefit of higher compression force differs markedly by punch design: 0.58 MPa for A versus 5.11 MPa for C. We use the interaction F-test to assess whether this pattern provides evidence of a genuine difference in force effects.
aov()# 05_Tablet_aov.R — standalone, base R
alpha <- 0.05
low <- list(
A = c(4.00, 4.79, 5.50, 5.15, 5.26, 6.55),
B = c(6.49, 8.35, 6.40, 6.37, 6.86, 8.08),
C = c(7.92, 8.15, 7.60, 9.64, 9.98, 9.23)
)
high <- list(
A = c(4.90, 4.90, 5.04, 6.93, 7.13, 5.82),
B = c(9.63, 9.67, 8.72, 8.08, 10.25, 8.10),
C = c(14.60, 13.81, 13.20, 14.62, 13.77, 13.18)
)
make_rows <- function(group, force_label) {
data.frame(punch = rep(names(group), each = 6),
force = force_label,
strength = unlist(group, use.names = FALSE))
}
df <- rbind(make_rows(low, "Low"), make_rows(high, "High"))
df$punch <- factor(df$punch, levels = c("A", "B", "C"))
df$force <- factor(df$force, levels = c("Low", "High"))
print(aggregate(strength ~ punch + force, data = df, mean))
fit <- aov(strength ~ punch * force, data = df)
result <- summary(fit)
print(result)
p <- result[[1]][1:3, "Pr(>F)"]
for (i in seq_along(p)) {
cat(names(p)[i], ":", if (p[i] < alpha) "Reject H0" else "Do not reject H0", "\n")
}
interaction.plot(df$force, df$punch, df$strength,
xlab = "Compression force", ylab = "Mean strength (MPa)",
trace.label = "Punch", main = "Tablet strength: interaction")Run this after 05_Tablet_aov.R, while its
df is available:
# Mean tablet strength for each combination
group_means <- aggregate(
strength ~ punch + force,
data = df,
FUN = mean
)
print(group_means)
# Calculate High - Low for each punch
low <- subset(
group_means,
force == "Low"
)
high <- subset(
group_means,
force == "High"
)
# Match the punch designs
low <- low[order(low$punch), ]
high <- high[order(high$punch), ]
# Difference in mean strength
increase <- high$strength - low$strength
results <- data.frame(
Punch = low$punch,
Low = low$strength,
High = high$strength,
Increase = increase
)
print(transform(
results,
Low = round(Low, 3),
High = round(High, 3),
Increase = round(Increase, 3)
))The design has \(a=3\) punch
designs, \(b=2\) force levels, and
\(r=6\) independent replicates per
combination. The ANOVA results from summary(fit) are:
| Source | Df | F value | p-value |
|---|---|---|---|
| Punch design | 2 | 128.23 | \(<0.001\) |
| Compression force | 1 | 74.21 | \(<0.001\) |
| Punch × Force | 2 | 20.35 | \(<0.001\) |
| Residuals | 30 | — | — |
At \(\alpha=0.05\), both punch design and compression force have significant main effects. More importantly, the punch × force interaction is significant, indicating that the effect of increasing compression force depends on punch design.
The observed high-minus-low mean strength differences are 0.58 MPa for Punch A, 1.98 MPa for Punch B, and 5.11 MPa for Punch C. Because the interaction is significant, these punch-specific effects are more informative than a single average effect of force.
Example of statistical reporting:
Two-way ANOVA detected a punch × force interaction (\(F_{2,30}=20.35\), \(p<0.001\)), indicating that the effect of greater compression force differed among punch designs. The observed high-minus-low mean strength difference was approximately 0.58 MPa for A, 1.98 MPa for B, and 5.11 MPa for C.
As in Example 1, the test statistics and p-values come from
summary(fit); the strength differences come from the means
calculated with aggregate().
Compare Examples 1 and 2: Both show strong main effects, but only Example 2 provides strong evidence that the effect of one factor differs across the levels of the other.
Nothing prevents us from adding a third factor, such as genotype, sex, temperature category, or treatment duration. A three-way factorial ANOVA can test three main effects, three two-factor interactions, and one three-factor interaction.
For example, suppose gene expression depends on three factors:
There are \(2\times2\times2=8\) factor combinations (cells). The full model includes the three main effects, all three two-factor interactions, and the three-factor interaction.
In R:
expression ~ treatment * genotype * sexThe three-way interaction asks whether the treatment × genotype interaction differs by sex. It is not just another main effect.
# 06_ThreeWay.R — standalone, base R (simulated example)
set.seed(206)
df <- expand.grid(
treatment = c("Control", "Drug"),
genotype = c("WT", "Mutant"),
sex = c("Female", "Male"),
replicate = 1:8
)
df$treatment <- factor(df$treatment)
df$genotype <- factor(df$genotype)
df$sex <- factor(df$sex)
drug <- as.integer(df$treatment == "Drug")
mutant <- as.integer(df$genotype == "Mutant")
male <- as.integer(df$sex == "Male")
df$expression <- 10 + 2*drug + mutant + 0.5*male +
2*drug*mutant + 1.5*drug*mutant*male +
rnorm(nrow(df), sd = 1.5)
fit <- aov(expression ~ treatment * genotype * sex, data = df)
print(summary(fit))
print(aggregate(expression ~ treatment + genotype + sex, df, mean))As the number of factors and their levels grow, the number of combinations rises rapidly. Chapter 14 (Section 14.5) introduces fractional factorial designs, which study a carefully selected subset of combinations, typically with assumptions about which interactions can be estimated. Such designs must be chosen before data collection.
For the standard fixed-effects ANOVA models in this lecture, check:
If ANOVA assumptions are seriously violated, alternative statistical methods may be needed. Welch’s ANOVA, introduced in Lecture 05, handles unequal variances in one-way ANOVA but is not directly applicable to factorial ANOVA with two or more factors. Such designs require other methods that account for unequal variances.
In two-way ANOVA, each observation belongs to a particular combination of factor levels (a cell).
For example, Punch A at low compression force has six observed tablet strengths:
\[ 4.00,\ 4.79,\ 5.50,\ 5.15,\ 5.26,\ 6.55. \]
Their mean is
\[ \bar X=\frac{4.00+4.79+5.50+5.15+5.26+6.55}{6} =5.208\text{ MPa}. \]
In our two-way ANOVA model with interaction, the fitted value for each observation is the mean of its corresponding cell.
The residual is the difference between an observed value and its fitted value:
\[ \boxed{\text{Residual}=\text{Observed value}-\text{Fitted value}} \]
For the first observation (4.00 MPa):
\[ \text{Residual}=4.00-5.208=-1.208\text{ MPa}. \]
Thus, the fitted value represents the mean strength for that experimental condition, while the residual represents the variation of an individual observation around that mean.
In R:
# Fitted values (cell means)
fitted(fit)
# Residuals (observed - fitted)
resid(fit)We can now examine these residuals to check whether the ANOVA assumptions are reasonable.
We use three diagnostic plots to check whether the assumptions of two-way ANOVA are reasonable.
First, prepare the data and fit the ANOVA model. Then run each diagnostic plot separately.
# 07_Diagnostics.R — base R
low <- list(
A = c(4.00, 4.79, 5.50, 5.15, 5.26, 6.55),
B = c(6.49, 8.35, 6.40, 6.37, 6.86, 8.08),
C = c(7.92, 8.15, 7.60, 9.64, 9.98, 9.23)
)
high <- list(
A = c(4.90, 4.90, 5.04, 6.93, 7.13, 5.82),
B = c(9.63, 9.67, 8.72, 8.08, 10.25, 8.10),
C = c(14.60, 13.81, 13.20, 14.62, 13.77, 13.18)
)
make_rows <- function(group, force_label) {
data.frame(
punch = rep(names(group), each = 6),
force = force_label,
strength = unlist(group, use.names = FALSE)
)
}
df <- rbind(
make_rows(low, "Low"),
make_rows(high, "High")
)
df$punch <- factor(df$punch)
df$force <- factor(df$force)
fit <- aov(strength ~ punch * force, data = df)Purpose: Examine whether residual variability is approximately constant across fitted values.
plot(
fitted(fit), resid(fit),
pch = 16,
xlab = "Fitted strength (MPa)",
ylab = "Residual (MPa)",
main = "Residuals vs Fitted Values"
)
abline(h = 0, lty = 2)In this two-way ANOVA model, fitted(fit) gives the mean of each punch × force combination, while resid(fit) gives each observation minus its corresponding cell mean. The plot compares residual variability across all six experimental conditions.
Interpretation: Residuals should be scattered around zero with approximately constant vertical spread. A funnel-shaped pattern may indicate unequal variances.
Purpose: Examine whether residuals are approximately normally distributed.
qqnorm(
resid(fit),
main = "Normal Q-Q Plot"
)
qqline(resid(fit))Interpretation: Points should approximately follow the straight reference line. Systematic departures from this line may indicate non-normality. In this example, the Q-Q plot shows a mildly S-shaped pattern, suggesting some departure from normality. However, most points remain reasonably close to the reference line. This mild deviation does not necessarily invalidate the ANOVA results.
Purpose: Compare variability among the six punch × force combinations.
df$cell <- interaction(
df$punch,
df$force,
sep = "/"
)
boxplot(
strength ~ cell,
data = df,
xlab = "Punch / Force",
ylab = "Strength (MPa)",
main = "Tablet Strength by Experimental Condition"
)Interpretation: The six groups may have different mean strengths, but their within-group variability should be reasonably similar. Large differences in box or whisker lengths may indicate unequal variances.
Together, these plots help identify possible violations of the normality and equal-variance assumptions of two-way ANOVA.
The independence assumption cannot be established from these plots; it must be assessed from how the experiment was designed and the measurements were collected.
Since there are only six observations per factor combination, these graphical checks are approximate rather than definitive.
| Experimental question | Appropriate starting point | R formula/function |
|---|---|---|
| One factor, 3+ independent groups | One-way ANOVA | aov(activity ~ A) |
| Two fixed factors, independent observations | Two-way ANOVA | aov(activity ~ A * B) |
| Two fixed factors, additive effects specified in advance | Additive two-way model | aov(activity ~ A + B) |
| Three fixed factors | Three-way ANOVA | aov(activity ~ A * B * C) |