



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




22s:152 Applied Linear Regression
Chapter 8: ANOVA
the populations are classified by two fac-
tors (or categorical variables)
dicting salary
rate’ one-way ANOVAs
∗ more efficient to study two factors si-
multaneously rather than separately
∗ can reduce residual variation in a model
by including a second factor believed to
affect the response (more predictors)
∗ can study interaction between factors
1
Example: Poison data
The data give the survival times (in 10 hour units) in
a 3 x 4 factorial experiment, the factors being
(a) three poisons and
(b) four treatments.
Each combination of the two factors is used for four
animals, the allocation to animals being completely
randomized.
The variables in the dataset are:
Time : Survival time (in 10 hour units)
Poison : Factor with levels 1,2,
Treatment : Factor with levels 1,2,3,
2
‘Cells’ in a two-way ANOVA:
level j of B
Cell means model:
ijk
= μ ij
ijk
with! ijk
iid
∼ N (0, σ
2
)
level j of B (there are ab distinct cell means)
structure.
i of A and level j of B
Effects model:
ijk
= μ + α i
β j
γ ij
ijk
is the main effect parameter for level i of A
is the main effect parameter for level j of B
is the interaction parameter for level i of A and
level j of B
mean structure, this is an over-parameterization.
ters estimable.
there are only main effects present (no in-
teraction) because it’s a much simpler model
to interpret (more on this later).
is kth observation from population with level
i of A and level j of B
5
Time : Survival time (in 10 hour units)
Poison : Factor with levels 1,2,
Treatment : Factor with levels 1,2,3,
poison.data=read.delim("poison.data)
head(poison.data)
Poison Treatment Time
1 1 1 0.
2 1 1 0.
3 1 1 0.
4 1 1 0.
5 1 2 0.
6 1 2 1.
levels(Treatment)
NULL
Treatment=as.factor(Treatment)
is.factor(Treatment)
[1] TRUE
levels(Treatment)
[1] "1" "2" "3" "4"
6
levels(Poison)
NULL
##Seen as numeric, redefine as categorical:
Poison=as.factor(Poison)
is.factor(Poison)
[1] TRUE
levels(Poison)
[1] "1" "2" "3"
Get the 12 cell means:
means=tapply(Time,list(Poison,Treatment),mean)
means
1 2 3 4
1 0.4125 0.880 0.5675 0.
2 0.3200 0.815 0.3750 0.
3 0.2100 0.335 0.2350 0.
Get the number of observations in each cell:
table(Poison,Treatment)
Treatment
Poison 1 2 3 4
1 4 4 4 4
2 4 4 4 4
3 4 4 4 4
This is a balanced 2-way ANOVA.
See how R is creating the dummy regressors:
contrasts(Poison)
2 3
1 0 0
2 1 0
3 0 1
contrasts(Treatment)
2 3 4
1 0 0 0
2 1 0 0
3 0 1 0
4 0 0 1
⇒ Poison level 1 and Treatment level 1
is the baseline.
In models that include interaction (as this one
does), a common first step is to test for the sig-
nificance of the interaction.
If there is no interaction, we perceive the true
profile plot of the means to be parallel.
Poison
mean of Time
1 2 3
Treatment
2
4
3
1
The interaction is NOT significant in this model.
Sum Sq Df F value Pr(>F)
Poison:Treatment 0.25014 6 1.8743 0.
So, these lines (connecting the cell means) are
not significantly different than ‘parallel’.
13
A model like this, with no significant interac-
tion, is said to have only ‘main effects’.
We can plot the ‘main effects’ fitted model, which
shows parallel lines:
Main effects model (Survival Time vs. Poison by Treatment)
Poisson
Survival Time
1 2 3
1
1 1 1
1
1
1
1 1 1 1
1
2
2
2
2
2
2
2
2
2
2 2
2
3 3
3
3
3
3
3
3
3
3 3 3
4
4
4
4
4
4
4
4
4
4
4
4
TRT 1
TRT 2
TRT 3
TRT 4
Since there is no significant interaction, we can
say “The Treatment effect is the same for all
levels of Poison”.
Or, “at each Poison level, the Treatment effect
is the same.”
14
When the interaction is not significant, we can
go back and fit the reduced model (without in-
teraction), and use this reduced model for the
rest of the analysis.
Anova(lm.no.interaction,type="III")
Anova Table (Type III tests)
Response: Time
Sum Sq Df F value Pr(>F)
(Intercept) 1.63654 1 65.408 4.224e-10 ***
Poison 1.03301 2 20.643 5.704e-07 ***
Treatment 0.92121 3 12.273 6.697e-06 ***
Residuals 1.05086 42
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05. 0.1 1
Effects model with only Main Effects:
ijk
= μ + α i
ijk
Both main effects are significant here.
We can get the parameter estimates like usual:
lm.no.interaction$coefficients
(Intercept) Poison2 Poison3 Treatment2 Treatment3 Treatment
0.45229167 -0.07312500 -0.34125000 0.36250000 0.07833333 0.
You can also get the fitted values using the lm()
output:
cbind(Poison,Treatment,lm.no.interaction$fitted)
Poison Treatment
1 1 1 0.
2 1 1 0.
3 1 1 0.
4 1 1 0.
5 1 2 0.
6 1 2 0.
7 1 2 0.
8 1 2 0.
9 1 3 0.
10 1 3 0.
.
.
.
There will be 12 fitted values in the 2-way ANOVA.
To plot the main effects fitted model, we can
plug-in the estimated parameters to the main
effects model...
ijk
= μˆ + αˆ i
β j
to get the 12 fitted means, and then connect the
relevant fitted values.
coeff=lm.no.interaction$coefficients
coeff
(Intercept) Poison2 Poison3 Treatment2 Treatment3 Treatment
0.45229167 -0.07312500 -0.34125000 0.36250000 0.07833333 0.
Sum the relevant parameter estiamates to get the 12 fitted values:
mu.11=coeff[1] ## Baseline group.
mu.21=sum(coeff[c(1,2)])
mu.31=sum(coeff[c(1,3)])
mu.12=sum(coeff[c(1,4)])
mu.22=sum(coeff[c(1,2,4)])
mu.32=sum(coeff[c(1,3,4)])
mu.13=sum(coeff[c(1,5)])
mu.23=sum(coeff[c(1,2,5)])
mu.33=sum(coeff[c(1,3,5)])
mu.14=sum(coeff[c(1,6)])
mu.24=sum(coeff[c(1,2,6)])
mu.34=sum(coeff[c(1,3,6)])
TRT.1.means=c(mu.11,mu.21,mu.31)
TRT.2.means=c(mu.12,mu.22,mu.32)
TRT.3.means=c(mu.13,mu.23,mu.33)
TRT.4.means=c(mu.14,mu.24,mu.34)
17
plot(c(0.5,3.5),range(Time),type="n",xlab="Poisson",
ylab="Survival Time",axes=F,main="Main effects model
(Survival Time vs. Poison by Treatment)")
axis(2)
axis(1,at=1:3,c("1","2","3"))
box()
poison.num=as.numeric(Poison)
points(jitter(poison.num[Treatment=="1"],factor=0.5),
Time[Treatment=="1"],pch="1",col=1)
points(jitter(poison.num[Treatment=="2"],factor=0.5),
Time[Treatment=="2"],pch="2",col=2)
points(jitter(poison.num[Treatment=="3"],factor=0.5),
Time[Treatment=="3"],pch="3",col=3)
points(jitter(poison.num[Treatment=="4"],factor=0.5),
Time[Treatment=="4"],pch="4",col=4)
lines(c(1,2,3),TRT.1,col=1)
lines(c(1,2,3),TRT.2,col=2)
lines(c(1,2,3),TRT.3,col=3)
lines(c(1,2,3),TRT.4,col=4)
legend(3,1.2,c("TRT 1","TRT 2","TRT 3","TRT 4"),col=c(1,2,3,4),lty=1)
Main effects model (Survival Time vs. Poison by Treatment)
Poisson
Survival Time
1 2 3
1
1 1 1
1
1
1
1 1 1 1
1
2
2
2
2
2
2
2
2
2
2 2
2
3
3
3
3
3
3 3
3
3 3 3 3
4
4
4 4
4
4
4
4
4
4
4
4
TRT 1
TRT 2
TRT 3
TRT 4
18
If there IS significant interaction, we shouldn’t
consider the tests for main effects (i.e. the tests
for Poison or Treatment in the Anova() output).
Instead, we should consider the Treatment ef-
fect separately at each Poison level (because the
Treatment effect depends on the level of Poison,
and there isn’t a common ‘global’ Treatment ef-
fect to be testing all at once).
Poison
mean of Time
1 2 3
Treatment
2
4
3
1
Another option for testing for interaction is to
use a Partial F -test.
lm.no.interaction=lm(Time ~ Poison + Treatment)
anova(lm.no.interaction,lm.out)
Analysis of Variance Table
Model 1: Time ~ Poison + Treatment
Model 2: Time ~ Poison + Treatment + Poison:Treatment
Res.Df RSS Df Sum of Sq F Pr(>F)
1 42 1.
2 36 0.80073 6 0.25014 1.8743 0.
This p-value is the same as the one seen in the
Anova(lm.out, type = ”III”)
output.