Start with \(N\)experimental units (EUs), e.g. subjects, mice, etc.
Randomly assign each EU to one of \(a\)treatment groups.
Measure on each EU after treatment a response\(Y\).
Compute the average of the responses in each treatment group
Questions we’d like to answer:
Is the response mean the same in all treatment groups?
If not, then which pairs of means are different?
One-way treatment effects model
Consider the model
\[
Y_{ij} = \mu + \tau_i + \varepsilon_{ij}, \quad j = 1,\dots,n_i, \quad i = 1,\dots,a,
\] where
\(Y_{ij}\) is the response for EU \(j\) in treatment group \(i\).
\(\mu\) represents an overall or baseline mean.
\(\tau_i\) is the treatment effect for treatment \(i\).
The \(\varepsilon_{ij}\) are independent \(\text{Normal}(0,\sigma^2)\) error terms.
The \(n_i\) are the numbers of replicates in the treatment groups.
Of central interest are the hypotheses \[
\text{$H_0$: $\tau_1 = \dots = \tau_a$ \quad versus \quad $H_1$: $\tau_i \neq \tau_{i'}$ for some $i \neq i'$.}
\] If we reject \(H_0\), we may wish to sort/compare the treatments.
Identifiability constraint in the treatment effects model
The model has \(a+1\) parameters to describe \(a\) treatment means.
To identify \(\mu\), \(\tau_1,\dots,\tau_a\) uniquely, we typically set \(\tau_1 = 0\).
Alternative “cell means” setup
An alternate version of the model is
\[
Y_{ij} = \mu_i + \varepsilon_{ij}, \quad j = 1,\dots,n_i, \quad i = 1,\dots,a,
\] where
\(Y_{ij}\) is the response for EU \(j\) in treatment group \(i\).
\(\mu_i\) represents the mean of treatment group \(i\).
The \(\varepsilon_{ij}\) are error terms distributed as \(\text{Normal}(0,\sigma^2)\).
In this version of the model the central hypotheses become \[
\text{$H_0$: $\mu_1 = \dots = \mu_a$ \quad versus \quad $H_1$: $\mu_i \neq \mu_i'$ for some $i \neq i'$}.
\]
Goals in one-way ANOVA
In the one-way treatment effects model \[
Y_{ij} = \mu + \tau_i + \varepsilon_{ij}, \quad j = 1,\dots,n_i, \quad i = 1,\dots,a,
\] where \(\varepsilon_{ij}\overset{\operatorname{ind}}{\sim}\text{Normal}(0,\sigma^2)\), we wish to
Visualize the data.
Estimate the parameters \(\mu,\tau_1,\dots,\tau_a\).
Estimate the error term variance \(\sigma^2\).
Decompose the variation in the \(Y_{ij}\) as signal plus noise.
Test whether there is any difference in treatment group means.
Sort/compare the treatment means if there is any difference.
Check whether the model assumptions are satisfied.
Treatment effect estimation in one-way ANOVA
For each \(i = 1,\dots,a\) define the observed treatment group mean as \[
\bar Y_{i.} = \frac{1}{n_i}\sum_{j=1}^{n_i}Y_{ij}.
\]
Then, setting \(\tau_1 = 0\), estimate \(\mu\) and \(\tau_2,\dots,\tau_a\) as \[
\hat \mu = \bar Y_{1.} \quad \text{ and } \quad \hat \tau_i = \bar Y_{i.} - \bar Y_{1.} \quad \text{ for } i = 2,\dots,a.
\]
So treatment group 1 is regarded as a baseline, where:
The baseline has estimated mean \(\hat \mu\).
The estimates \(\hat \tau_2,\dots,\hat \tau_a\) are deviations from the baseline.
One obtains the fitted values \(\hat Y_{ij} = \hat \mu + \hat \tau_i = \bar Y_{i.}\) for \(i = 1,\dots,a\).
Rust inhibitors example (cont)
Use lm() with as.factor() to fit the one-way ANOVA model.
# use as.factor() to designate brand as a "factor"lm_out <-lm(score ~as.factor(brand), data = rust)lm_out
Estimation of the error term variance \(\sigma^2\)
As in linear regression, define the
fitted values\(\hat Y_{ij}\) as \(\hat Y_{ij} = \bar Y_{i.}\) for \(j = 1,\dots,n_i\), and the
residuals\(\hat \varepsilon_{ij}\) as \(\hat \varepsilon_{ij} = Y_{ij} - \bar Y_{i.}\)
for \(j=1,\dots,n_i\), \(i = 1,\dots,a\).
Then an unbiased estimator of \(\sigma^2\) is given by \[
\hat \sigma^2 = \frac{1}{N - a} \sum_{i=1}^a\sum_{j=1}^{n_i} \hat \varepsilon_{ij}^2 = \frac{1}{N - a} \sum_{i=1}^a\sum_{j=1}^{n_i} (Y_{ij} - \bar {Y_{i.}})^2.
\] Divide by \(N-a\) since the \(N\) residuals depend on \(a\) estimated quantities
The value of \(\hat \sigma\) is printed in the summary() output:
summary(lm_out)
Call:
lm(formula = score ~ as.factor(brand), data = rust)
Residuals:
Min 1Q Median 3Q Max
-4.270 -1.597 0.395 1.275 4.730
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 43.1400 0.7836 55.056 <2e-16 ***
as.factor(brand)2 46.3000 1.1081 41.782 <2e-16 ***
as.factor(brand)3 24.8100 1.1081 22.389 <2e-16 ***
as.factor(brand)4 -2.6700 1.1081 -2.409 0.0212 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 2.478 on 36 degrees of freedom
Multiple R-squared: 0.9863, Adjusted R-squared: 0.9852
F-statistic: 866.1 on 3 and 36 DF, p-value: < 2.2e-16
Sums of squares in the one-way ANOVA model
As in linear regression we decompose the variation in the \(Y_{ij}\) by defining:
Total sum of squares: \(\operatorname{SS}_{\operatorname{Tot}} = \sum_{i=1}^a\sum_{j=1}^{n_i}(Y_{ij} - \bar Y_{..})^2\)
Treatment sum of squares: \(\operatorname{SS}_{\operatorname{Trt}} = \sum_{i=1}^a n_i(\bar Y_{i.} - \bar Y_{..})^2\)
Error sum of squares: \(\operatorname{SS}_{\operatorname{Error}} = \sum_{i=1}^a\sum_{j=1}^{n_i}(Y_{ij} - \bar Y_{i.})^2\)
In the above, \(\bar Y_{..}\) denotes the overall mean, defined as \[
\textstyle \bar Y_{..} = N^{-1}\sum_{i=1}^a\sum_{j=1}^{n_i} Y_{ij}, \quad\text{ where } N = n_1+\dots+n_a.
\]
We have \(\operatorname{SS}_{\operatorname{Tot}} = \operatorname{SS}_{\operatorname{Trt}} + \operatorname{SS}_{\operatorname{Error}}\).
Note that \(\operatorname{SS}_{\operatorname{Trt}}\) is computed just like \(\operatorname{SS}_{\operatorname{Reg}}\) in linear regression.
We again define \(\displaystyle R^2 = \frac{\operatorname{SS}_{\operatorname{Trt}}}{\operatorname{SS}_{\operatorname{Tot}}}\).
Sampling distributions of our sums of squares
The SS, appropriately scaled, follow chi-square distributions:
where \(\phi_{\operatorname{Tot}}\) and \(\phi_{\operatorname{Trt}}\) are noncentrality parameters.
The mean squares in the one-way ANOVA model
Dividing \(\operatorname{SS}_{\operatorname{Trt}}\) and \(\operatorname{SS}_{\operatorname{Error}}\) by their dfs, we define:
Treatment mean square: \(\displaystyle \operatorname{MS}_{\operatorname{Trt}}= \frac{\operatorname{SS}_{\operatorname{Trt}}}{a-1}\)
Error mean square: \(\displaystyle \operatorname{MS}_{\operatorname{Error}} = \frac{\operatorname{SS}_{\operatorname{Error}}}{N-a}\)
The ratio \(\displaystyle F_{\operatorname{test}}= \frac{\operatorname{MS}_{\operatorname{Trt}}}{\operatorname{MS}_{\operatorname{Error}}}\) has an F distribution.
The Analysis of Variance (ANOVA) table
We often present the SS, df, and MS values in a table like this:
Source
Df
SS
MS
F value
p-value
Treatment
\(a-1\)
\(\operatorname{SS}_{\operatorname{Trt}}\)
\(\operatorname{MS}_{\operatorname{Trt}}\)
\(F_{\operatorname{test}}\)
\(P(F > F_{\operatorname{test}})\)
Error
\(N-a\)
\(\operatorname{SS}_{\operatorname{Error}}\)
\(\operatorname{MS}_{\operatorname{Error}}\)
Total
\(N-1\)
\(\operatorname{SS}_{\operatorname{Tot}}\)
In the table \(\displaystyle F_{\operatorname{test}}= \frac{\operatorname{MS}_{\operatorname{Trt}}}{\operatorname{MS}_{\operatorname{Error}}}\).
The p-value is based on \(F \sim F_{a-1,N-a}\).
Rust inhibitors example (cont)
Obtain the ANOVA table “manually”:
Y <- rust$scoreY.. <-mean(Y)Yi. <-aggregate(score ~ brand, mean, data = rust)[,2]n <-aggregate(score ~ brand, length, data = rust)[,2]a <-length(Yi.)N <-sum(n)SStot <-sum((Y - Y..)**2)SStrt <-sum(n*(Yi. - Y..)**2)SSerror <- SStot - SStrtMStrt <- SStrt / (a -1)MSerror <- SSerror / (N - a)Ftest <- MStrt / MSerrorpval <-1-pf(Ftest, a-1, N - a)
Obtain the ANOVA table with the anova() function on the lm() output.
anova(lm_out)
Analysis of Variance Table
Response: score
Df Sum Sq Mean Sq F value Pr(>F)
as.factor(brand) 3 15954 5317.8 866.12 < 2.2e-16 ***
Residuals 36 221 6.1
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Testing whether there is any difference in treatment means
In the one-way treatment effects model we wish to test \[
\text{$H_0$: $\tau_1 = \dots = \tau_a$ \quad versus \quad $H_1$: $\tau_i \neq \tau_{i'}$ for some $i \neq i'$.}
\]
Reject \(H_0\) at \(\alpha\) if \(F_{\operatorname{test}}> F_{a-1,N-a,\alpha}\).
Obtain p-value as \(P(F > F_{\operatorname{test}})\), where \(F \sim F_{a-1,N-a}\).
The value of \(\displaystyle F_{\operatorname{test}}\) and the p-value are printed in the summary() output.
Interpretation of F statistic
Note that \(F_{\operatorname{test}}\) is a ratio of the form \(\displaystyle \frac{\text{Between treatment variation}}{\text{Within treatment variation}}\).
Exercise: For which data set will the F-statistic be largest/smallest?
Exercise: Compute \(F_{\operatorname{test}}\) for the rust data using the summary info:
In the cell-means formulation of the model \[
Y_{ij} = \mu_i + \varepsilon_{ij}, \quad j = 1,\dots,n_i, \quad i = 1,\dots,a,
\] where \(\mu_i = \mu + \tau_i\), we have the following CI formulas:
If we reject \(H_0\): \(\mu_1 = \dots = \mu_a\), then we may wish to compare means.
Call such comparisons post-hoc as we do them after the F-test.
We may wish to compare several pairs of means, which is like testing several hypotheses at once.
When several hypotheses are tested at once, the familywise Type I error rate is the probability that any Type I error is committed.
We discuss two methods for post-hoc comparisons of means which control the familywise Type I error rate.
Comparing all pairs of means
We want to build a CI for \(\mu_i - \mu_{i'}\) for all pairs \(i \neq i'\).
Suppose the design is balanced, i.e. \(n_i = n\) for all \(i = 1,\dots,a\).
If we build for all \(i \neq i'\) the ordinary \((1-\alpha)\times 100\%\) CIs \[
\bar Y_{i.} - \bar Y_{i'.} \pm t_{a(n-1),\alpha/2} \hat \sigma \sqrt{2/n},
\] each one will cover its target with probability \(1-\alpha\).
But now we want simultaneous coverage with probability \(1-\alpha\), i.e. \[
P(\cap_{i \neq i'} \{\text{CI for $\mu_i - \mu_{i'}$ captures target}\}) = 1-\alpha.
\]
Above probability is called the familywise coverage.
The venerable John Tukey
John Tukey, 1915 – 2000
Multiple comparisons of means with Tukey’s HSD
Suppose the design is balanced, i.e. \(n_i = n\) for all \(i = 1,\dots,a\).
Suppose we could find the value \(q_{a,a(n-1),\alpha}\) such that \[
P\left(\max_{i \neq i'} \left\{ \frac{|(\bar Y_{i.} - \bar Y_{i'.}) - (\mu_i - \mu_{i'}) |}{\hat \sigma /\sqrt{n}}\right\} \leq q_{a,a(n-1),\alpha} \right) = 1-\alpha.
\]
Then with probability \(1-\alpha\) the CIs \[
\bar Y_{i.} - \bar Y_{i'.} \pm q_{a,a(n-1),\alpha} \hat \sigma / \sqrt{n}
\] will simultaneously cover the targets \(\mu_i - \mu_{i'}\) for all \(i \neq i'\). Show!
Tukey made tables of the values \(q_{a,a(n-1),\alpha}\).
Can use the simultaneous intervals to sort/compare the means.
Charles Dunnett, 1921 – 2007 (Canadian, served in WWII, photo taken in Belgium)
Dunnett’s method for comparisons with a baseline
Assume \(n_i = n\) for all \(i\) (balanced case).
Given a value \(d_{n,a(n-1),\alpha}\) such that \[
P\left( \max_{2 \leq i \leq a} \Big| \frac{(\bar Y_{i.} - \bar Y_{1.}) - (\mu_i - \mu_1)}{\hat \sigma\sqrt{2/n}} \Big| \leq d_{n,a(n-1),\alpha}\right) = 1 - \alpha,
\] with probability \(1-\alpha\) the CIs \[
\bar Y_{i.} - \bar Y_{1.} \pm d_{n,a(n-1),\alpha} \hat \sigma\sqrt{2/n}
\] will simultaneously cover the targets \(\mu_i - \mu_1\) for all \(i=2,\dots,a\).
Dunnett made tables of the values \(d_{n,a(n-1),\alpha}\).
For the rust data we have \(n = 10\) and \(a = 4\).
At \(\alpha = 0.05\) we have \(d_{a,a(n-1),\alpha} = d_{4,36,0.05}\).
Use value \(2.44\) in the table (should be close).
Treat Brand 1 as the baseline and make comparisons with Dunnett’s.
# just show the comparison of treatment 2 to the baseliney1bar <-mean(rust$score[rust$brand ==1])y2bar <-mean(rust$score[rust$brand ==2])me <-2.44*sqrt(MSE) *sqrt(2/10) # margin of error for Dunnett'slo21 <- y2bar - y1bar - meup21 <- y2bar - y1bar + mec(y2bar - y1bar,lo21,up21)
[1] 46.30000 43.59615 49.00385
Rust inhibitor data (cont)
Use DunnettTest() from R package DescTools.
library(DescTools) # first time run install.packages("DescTools")
Warning: package 'DescTools' was built under R version 4.4.1
Dunnett_out <-DunnettTest(score ~as.factor(brand), data = rust, control ="1")Dunnett_out
Dunnett's test for comparing several treatments with a control :
95% family-wise confidence level
$`1`
diff lwr.ci upr.ci pval
2-1 46.30 43.582516 49.017484 <2e-16 ***
3-1 24.81 22.092516 27.527484 <2e-16 ***
4-1 -2.67 -5.387484 0.047484 0.0549 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(Dunnett_out)
Dunnett’s vs Tukey’s
Tukey’s is for comparisons between all pairs of means.
Dunnett’s is for comparison of means with a baseline.
So Tukey’s must make greater adjustments to control the familywise Type I error.
Therefore Tukey intervals will be wider than Dunnett intervals.
Tukey’s allows you to sort the means, while Dunnett’s does not.
Both methods assume a balanced design, i.e. \(n_i = n\) for all \(i\). Modifications for unbalanced designs exist, but are not straightforward to implement in R.
Bonferroni correction
If building \(B\) CIs you can ALWAYS use the Bonferroni correction:
Build each CI ordinarily, but use \(\alpha/B\) instead of \(\alpha\).
Ensures simultaneous coverage of all CIs with probability \(\geq 1-\alpha\).
True prob of simultaneous coverage may be greater than \(1-\alpha\)
Bonferroni-corrected CIs will be wider than Dunnett’s and wider than Tukey’s if used for making those same comparisons.
Use when we do not know how to adjust for multiple comparisons.
Rust inhibitor data (cont)
Compare Brand 3 to 4 and Brand 1 to 3, using the Bonferroni correction to control the familywise error rate.
Validity of the foregoing analyses depends on these assumptions:
The responses are normally distributed around the treatment means (Check QQ plot of residuals).
The response has the same variance in all treatment groups (Check residuals vs fitted values plot).
The response values are independent of each other (No way to check; must trust experimental design).
Rust inhibitors example (cont)
plot(lm_out,which =2)
Rust inhibitors example (cont)
plot(lm_out,which =1, add.smooth = F) # we don't want the red line
Perception of slope example
Do axis re-scalings affect how we perceive an x-y relationship?
For a single data set with data pairs \((X_i,Y_i)\), with \(X_i \sim \text{Normal}(0,1)\) and \(Y_i = \text{Normal}(X_i,1)\) for \(i = 1,\dots,50\), three scatterplot treatments were constructed:
“Control” used x and y plotting limits given by the range of the data.
“X” extended the x-limits by 1.5 in each direction.
“Y” extended the y-limits by 1.5 in each direction.
Each student in a class was randomly assigned a scatterplot and told to draw with a ruler the best-fitting line through the data. The slope of each student-drawn line was measured and recorded as the response.
Is the response mean the same in the three treatment groups?