Docsity

Notes on ANOVA - Applied Linear Regression | 22S 152, Study notes of Statistics

Toggle sidebar
bg1
22s:152 Applied Linear Regression
Chapter 8: ANOVA
————————————————————
•Two-way ANOVA
–Used to compare populations means when
the populations are classified by two fac-
tors (or categorical variables)
–For example sex and occupation for pre-
dicting salary
–advantages compared to doing two ‘sepa-
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,3
Treatment : Factor with levels 1,2,3,4
2
‘Cells’ in a two-way ANOVA:
3
•Notation
–first categorical variable: factor Awith alevels
–second categorical variable: factor Bwith blevels
–nij is the sample size for level iof Aand
level jof B
–Nis total observations
Cell means model:
Yijk =µij +!ijk with !ijk
iid
∼N(0,σ2)
–µij is population mean for level iof Aand
level jof B(there are ab distinct cell means)
–Allows for ‘freely fit’ mean in each cell
–It will take ab parameters to describe the mean
structure.
–Yijk is kth observation from population with level
iof Aand level jof B
4
pf3
pf4
pf5

Partial preview of the text

Download Notes on ANOVA - Applied Linear Regression | 22S 152 and more Study notes Statistics in PDF only on Docsity!

22s:152 Applied Linear Regression

Chapter 8: ANOVA

  • Two-way ANOVA
    • Used to compare populations means when

the populations are classified by two fac-

tors (or categorical variables)

  • For example sex and occupation for pre-

dicting salary

  • advantages compared to doing two ‘sepa-

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:

  • Notation
    • first categorical variable: factor A with a levels
    • second categorical variable: factor B with b levels
    • n ij is the sample size for level i of A and

level j of B

  • N is total observations

Cell means model:

Y

ijk

= μ ij

ijk

with! ijk

iid

∼ N (0, σ

2

)

  • μ ij is population mean for level i of A and

level j of B (there are ab distinct cell means)

  • Allows for ‘freely fit’ mean in each cell
  • It will take ab parameters to describe the mean

structure.

  • Y ijk is kth observation from population with level

i of A and level j of B

Effects model:

Y

ijk

= μ + α i

  • β j

  • γ ij

ijk

  • α i

is the main effect parameter for level i of A

  • β j

is the main effect parameter for level j of B

  • γ ij

is the interaction parameter for level i of A and

level j of B

  • There are (1+a+b+ab) parameters to describe the

mean structure, this is an over-parameterization.

  • We’ll need to use constraints to make the parame-

ters estimable.

  • But, the effects model can be useful when

there are only main effects present (no in-

teraction) because it’s a much simpler model

to interpret (more on this later).

  • Y ijk

is kth observation from population with level

i of A and level j of B

5

  • Example: Poison data

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

R sees it as numeric, redefine as categorical:

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:

Y

ijk

= μ + α i

  • β j

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...

Y

ijk

= μˆ + αˆ i

β j

to get the 12 fitted means, and then connect the

relevant fitted values.

Main effects plot (connecting fitted means within same treatment):

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)])

Create vector of means for each treatment:

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.