+ All Categories
Home > Documents > Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the...

Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the...

Date post: 17-May-2018
Category:
Upload: vuongdan
View: 221 times
Download: 1 times
Share this document with a friend
58
Factorial ANOVA Two-factor, crossed, additive models 1
Transcript
Page 1: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Factorial ANOVA

Two-factor, crossed,additive models

1

Page 2: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Applied Stats Algorithm

No Unacceptable

Numerical Categorical

Predictor(s)

CategoricalNumerical Both

1 Factor 2+ Factors

Today

Scientificquestion?

Classify Study

Response Variable Multi-Var

Univariate

Fixed Effects Random Effects

Censored Complete

2

Page 3: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Experiment Description One (numerical) response variable Dependent, Outcome

Two (categorical) independent variables Treatments, Predictors, Explanatory

If the independent variables are both factors and are crossed, called a Factorial Design If there are observations at each treatment

combination, called a complete design There are also incomplete and fractional factorial

designs3

Page 4: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Farming Example(Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels {π‘Žπ‘Ž1, π‘Žπ‘Ž2, π‘Žπ‘Ž3, π‘Žπ‘Ž4} 2 types of crop (Factor B) with levels {𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐,π‘ π‘ π‘π‘π‘ π‘ π‘ π‘ π‘ π‘ π‘Žπ‘Žπ‘π‘}

There are 4 οΏ½ 2 = 8 different treatments, each with 2 replications

This is a 4 x 2 factorial experiment

4

Page 5: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Single-Factor setup Why can’t we just label the treatments as a

single factor and analyze it as before? We can Factor AB with 8 levels:

{π‘Žπ‘Ž1 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐, π‘Žπ‘Ž1 π‘ π‘ π‘π‘π‘ π‘ π‘ π‘ π‘ π‘ π‘Žπ‘Žπ‘π‘, π‘Žπ‘Ž2 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐, … , π‘Žπ‘Ž4 π‘ π‘ π‘π‘π‘ π‘ π‘ π‘ π‘ π‘ π‘Žπ‘Žπ‘π‘}y <- c(65, 64, 56, 60, 55, 58, 62, 65, 66, 69, 72, 76, 60, 64, 68, 70)

a <- factor(rep(c("a1", "a2", "a3", "a4"), each=4))

b <- factor(rep(c("corn", "soybean"), times=8))

farm <- data.frame(yield=y, fertil=a, type=b)

farm <- within(farm, x <- paste(fertil, type))

# farm$x <- paste(farm$fertil, farm$type)with(farm, tapply(yield, x, mean))

boxplot(yield ~ x, data=farm)

anova(lm(yield ~ x, data=farm))5

Page 6: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

R output

6

Page 7: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

R output> with(farm, tapply(yield, x, mean))

a1 corn a1 soybean a2 corn a2 soybean a3 corn a3 soybean a4 corn a4 soybean

60.5 62.0 58.5 61.5 69.0 72.5 64.0 67.0

> anova(lm(yield ~ x, data=farm))Analysis of Variance Table

Response: yield

Df Sum Sq Mean Sq F value Pr(>F)

x 7 315.75 43.857 1.8992 0.194

Residuals 8 190.00 23.750

No significant effects detected, despite the fact that we already know fertilizer is important Effect hidden by non-importance of crop type

7

Page 8: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Two-factor ANOVA Single-factor analysis doesn’t give us the effects

separated by the two factors, which we want It is also impossible to spot interactions with only

one factor Think of an π‘šπ‘š π‘₯π‘₯ 𝑐𝑐 factorial design as a series of 𝑐𝑐 single-factor experiments, each with π‘šπ‘š groups Or a series of π‘šπ‘š experiments with 𝑐𝑐 groups

Define simple effects as the results of one of those 𝑐𝑐 (or π‘šπ‘š) experiments

8

Page 9: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Farming example We have 4 fertilizers, and 2 types of crop For the 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 crops, we have a simple effect of

fertilizer on crop yield For the π‘ π‘ π‘π‘π‘ π‘ π‘ π‘ π‘ π‘ π‘Žπ‘Žπ‘π‘ crops, we have another simple

effect of fertilizer on crop yield These two simple effects, averaged together,

are called the main effect of fertilizer If the simple effects are the same as the main

effect, then there is no interaction present Otherwise, there is an interaction

9

Page 10: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Interactions An interaction means β€œit depends” The effect of Factor A depends on the level of

Factor B The effect of Factor B depends on the level of

Factor A Important to recognize before setting model If present, the statistical model will change

10

Page 11: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Farming Example(Interaction) Do we believe that the effect of fertilizer depends

on what crop type is planted? This is like saying β€œFertilizer π‘Žπ‘Ž1 is best for corn,

but π‘Žπ‘Ž3 is best for soybeans” It’s very possible!

If so, cannot say something like β€œFertilizer π‘Žπ‘Ž1 is better than π‘Žπ‘Ž3” because … it depends!

Just check the group means to find out Such a plot is called an interaction plot

11

Page 12: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Plot the group means

with(farm, interaction.plot(fertil, type, y, col=c("red", "blue"),

main="Interaction Plot", xlab="Fertilizer mean", ylab="Yield"))

12

Page 13: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Another way to do it

with(farm, interaction.plot(type, fertil, y, col=c("red", "blue"),

main="Interaction Plot", xlab="Fertilizer mean", ylab="Yield"))

13

Page 14: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Interactions Other, equivalent definitions of interaction The values of one or more contrasts change at

different levels of the other factor The main effect is not representative of the simple

effects The differences among cell means representing

effect of Factor A at one level of Factor B are not the same as at another level of Factor B

The effects of one factor are conditionally related to the levels of another factor

14

Page 15: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Additive Model It doesn’t look like the effect of Fertilizer

depends on crop type, so fit an additive modelπ‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– = πœ‡πœ‡ + 𝛼𝛼𝑖𝑖 + 𝛽𝛽𝑖𝑖 + πœ€πœ€π‘–π‘–π‘–π‘–π‘–π‘–

π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– – 𝑖𝑖𝑑𝑑𝑑 obs. from 𝑗𝑗𝑑𝑑𝑑 level of Factor A and π‘˜π‘˜π‘‘π‘‘π‘‘ level of Factor Bπœ‡πœ‡ – overall grand mean𝛼𝛼𝑖𝑖 – deviation from mean of 𝑗𝑗𝑑𝑑𝑑 group of Factor A𝛽𝛽𝑖𝑖 – deviation from mean of π‘˜π‘˜π‘‘π‘‘π‘‘ group of Factor Bπœ€πœ€π‘–π‘–π‘–π‘–π‘–π‘– – error/noise/uncertainty We will study models with interactions later

15

Page 16: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Additive Models An additive model means that the β€˜effects’ are

added together There is an effect of being in group 𝑗𝑗 of Factor A If you’re in group 𝑗𝑗, you get 𝛼𝛼𝑖𝑖 added to your

expected outcome There is an effect of being in group π‘˜π‘˜ of Factor B If you’re in group π‘˜π‘˜, you get 𝛽𝛽𝑖𝑖 added to your

expected outcome If you’re in both groups, you get 𝛼𝛼𝑖𝑖 + 𝛽𝛽𝑖𝑖 added to

your expected outcome16

Page 17: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

R output> with(farm, tapply(yield, list(fertil, type), mean))

corn soybean

a1 60.5 62.0

a2 58.5 61.5

a3 69.0 72.5

a4 64.0 67.0

> anova(lm(yield ~ fertil + type, data=farm))Analysis of Variance Table

Response: yield

Df Sum Sq Mean Sq F value Pr(>F)

fertil 3 283.25 94.417 5.4023 0.01571 *

type 1 30.25 30.250 1.7308 0.21507

Residuals 11 192.25 17.477

17

Page 18: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Conclusions It seems like Fertilizer has an effect on crop

Yield, but crop Type does not We did not allow the effect of Fertilizer to depend

on Type, but it doesn’t look like it does anyway According to the interaction plot, at least Should probably do a statistical test for this Coming up!

Of course, we should also follow-up our main effect conclusion with some pairwise tests, contrasts, etc…

18

Page 19: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Factorial ANOVA

Models with interactions

19

Page 20: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Consider Consider 8 hypothetical experiments, each

involving 2 levels of 2 different factors (A and B) Group means:Exp’t 1𝑠𝑠1 𝑠𝑠2 οΏ½π‘Œπ‘Œπ΄π΄

π‘Žπ‘Ž1 5 5 5π‘Žπ‘Ž2 5 5 5οΏ½π‘Œπ‘Œπ΅π΅ 5 5

Exp’t 3𝑠𝑠1 𝑠𝑠2 οΏ½π‘Œπ‘Œπ΄π΄

π‘Žπ‘Ž1 7 3 5π‘Žπ‘Ž2 7 3 5οΏ½π‘Œπ‘Œπ΅π΅ 7 3

Exp’t 2𝑠𝑠1 𝑠𝑠2 οΏ½π‘Œπ‘Œπ΄π΄

π‘Žπ‘Ž1 4 4 4π‘Žπ‘Ž2 6 6 6οΏ½π‘Œπ‘Œπ΅π΅ 5 5

Exp’t 4𝑠𝑠1 𝑠𝑠2 οΏ½π‘Œπ‘Œπ΄π΄

π‘Žπ‘Ž1 6 2 4π‘Žπ‘Ž2 8 4 6οΏ½π‘Œπ‘Œπ΅π΅ 7 3

Exp’t 5𝑠𝑠1 𝑠𝑠2 οΏ½π‘Œπ‘Œπ΄π΄

π‘Žπ‘Ž1 6 4 5π‘Žπ‘Ž2 4 6 5οΏ½π‘Œπ‘Œπ΅π΅ 5 5

Exp’t 7𝑠𝑠1 𝑠𝑠2 οΏ½π‘Œπ‘Œπ΄π΄

π‘Žπ‘Ž1 8 2 5π‘Žπ‘Ž2 6 4 5οΏ½π‘Œπ‘Œπ΅π΅ 7 3

Exp’t 6𝑠𝑠1 𝑠𝑠2 οΏ½π‘Œπ‘Œπ΄π΄

π‘Žπ‘Ž1 5 3 4π‘Žπ‘Ž2 5 7 6οΏ½π‘Œπ‘Œπ΅π΅ 5 5

Exp’t 8𝑠𝑠1 𝑠𝑠2 οΏ½π‘Œπ‘Œπ΄π΄

π‘Žπ‘Ž1 7 1 4π‘Žπ‘Ž2 7 5 6οΏ½π‘Œπ‘Œπ΅π΅ 7 3 20

Page 21: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

No Interactions

21

Page 22: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Interactions

22

Page 23: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Possible Outcomes As you’ve just seen, it is possible to have No interaction, and No main effects Main effect for one factor but not the other Main effects for both factors

Interaction between factors, and No main effects Main effect for one factor but not the other Main effects for both factors

Yikes! The combinations get more numerous as the number of levels (or factors) increases 23

Page 24: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Removable Interactions Sometimes we apply transformations to our data Perhaps to meet ANOVA assumptions Or to speak to Americans

These can dampen out or create interactions:

24

Page 25: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Removable Interactions An interaction is removable if there exists some

transformation on the response that will make the interaction disappear

You can tell from the interaction plot, sort of If the lines cross, it is not removable If the lines would cross if we plotted the axes

reversed, it is not removable Removable interactions are less convincing

since they depend on the way the response variable is collected/measured

25

Page 26: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Practice!

26

Page 27: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Interactive Model(Setup) Two-factor design 2 levels of Factor A; 3 levels of Factor B 4 observations at each treatment combination

Data Table Table of Sumsπ‘Žπ‘Ž1𝑠𝑠1 π‘Žπ‘Ž1𝑠𝑠2 π‘Žπ‘Ž1𝑠𝑠3 π‘Žπ‘Ž2𝑠𝑠1 π‘Žπ‘Ž2𝑠𝑠2 π‘Žπ‘Ž2𝑠𝑠3π‘Œπ‘Œ111 π‘Œπ‘Œ112 π‘Œπ‘Œ113 π‘Œπ‘Œ121 π‘Œπ‘Œ122 π‘Œπ‘Œ123π‘Œπ‘Œ211 π‘Œπ‘Œ212 π‘Œπ‘Œ213 π‘Œπ‘Œ221 π‘Œπ‘Œ222 π‘Œπ‘Œ223π‘Œπ‘Œ311 π‘Œπ‘Œ312 π‘Œπ‘Œ313 π‘Œπ‘Œ321 π‘Œπ‘Œ322 π‘Œπ‘Œ323π‘Œπ‘Œ411 π‘Œπ‘Œ412 π‘Œπ‘Œ413 π‘Œπ‘Œ421 π‘Œπ‘Œ422 π‘Œπ‘Œ423

Sum 𝐴𝐴𝐡𝐡11 𝐴𝐴𝐡𝐡12 𝐴𝐴𝐡𝐡13 𝐴𝐴𝐡𝐡21 𝐴𝐴𝐡𝐡22 𝐴𝐴𝐡𝐡23

𝑠𝑠1 𝑠𝑠2 𝑠𝑠3 π‘†π‘†π‘†π‘†π‘šπ‘šπ‘Žπ‘Ž1 𝐴𝐴𝐡𝐡11 𝐴𝐴𝐡𝐡12 𝐴𝐴𝐡𝐡13 𝐴𝐴1π‘Žπ‘Ž2 𝐴𝐴𝐡𝐡21 𝐴𝐴𝐡𝐡22 𝐴𝐴𝐡𝐡23 𝐴𝐴2π‘†π‘†π‘†π‘†π‘šπ‘š 𝐡𝐡1 𝐡𝐡2 𝐡𝐡3

27

Page 28: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Interactive Model Suppose we want to include an interaction now

π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– = πœ‡πœ‡ + 𝛼𝛼𝑖𝑖 + 𝛽𝛽𝑖𝑖 + 𝛼𝛼𝛽𝛽 𝑖𝑖𝑖𝑖 + πœ€πœ€π‘–π‘–π‘–π‘–π‘–π‘–π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– – 𝑖𝑖𝑑𝑑𝑑 obs. from 𝑗𝑗𝑑𝑑𝑑 level of Factor A and π‘˜π‘˜π‘‘π‘‘π‘‘ level of Factor Bπœ‡πœ‡ – overall grand mean𝛼𝛼𝑖𝑖 – deviation from mean of 𝑗𝑗𝑑𝑑𝑑 group of Factor A𝛽𝛽𝑖𝑖 – deviation from mean of π‘˜π‘˜π‘‘π‘‘π‘‘ group of Factor B𝛼𝛼𝛽𝛽 𝑖𝑖𝑖𝑖 – deviation remaining after main effects are

removedπœ€πœ€π‘–π‘–π‘–π‘–π‘–π‘– – error/noise/uncertainty

28

Page 29: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Deviations of means (Two-factor model) 𝐴𝐴𝑖𝑖 effect = οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ 𝐡𝐡𝑖𝑖 effect = οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ Interaction effect = οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ βˆ’ οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ βˆ’ οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡

= οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ + οΏ½π‘Œπ‘Œπ‘‡π‘‡

We can write the between group deviation as:οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ = οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ + οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ + ( οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ + οΏ½π‘Œπ‘Œπ‘‡π‘‡)οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ = (𝐴𝐴𝑖𝑖 effect) + (𝐡𝐡𝑖𝑖 effect) + (Interaction effect)

29

Page 30: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Deviations of individuals(Two-factor model) That was for group means, not individuals Deviation of an individual from the grand mean Sum of the deviations from the group mean to

grand mean, and individual to group meanπ‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ = οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ + (π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘–)

Replace with formula on last slide gives:

π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ = οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ + οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ + (οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ + οΏ½π‘Œπ‘Œπ‘‡π‘‡) + π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘–

Does this relationship hold for SS as well?30

Page 31: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

SS revisited Need to partition the between-group SS into

three parts instead of just one Factor A, Factor B, Interaction

π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ = οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ + οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡ + οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ + οΏ½π‘Œπ‘Œπ‘‡π‘‡ + π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘–

Ξ£ π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡2 = Ξ£ οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡

2+ Ξ£ οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ βˆ’ οΏ½π‘Œπ‘Œπ‘‡π‘‡

2 + Ξ£ οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ΄π΄π‘—π‘— βˆ’ οΏ½π‘Œπ‘Œπ΅π΅π‘˜π‘˜ + οΏ½π‘Œπ‘Œπ‘‡π‘‡2

+ Ξ£ π‘Œπ‘Œπ‘–π‘–π‘–π‘–π‘–π‘– βˆ’ οΏ½π‘Œπ‘Œπ‘–π‘–π‘–π‘–2

𝑆𝑆𝑆𝑆𝑇𝑇 = 𝑆𝑆𝑆𝑆𝐴𝐴 + 𝑆𝑆𝑆𝑆𝐡𝐡 + 𝑆𝑆𝑆𝑆𝐴𝐴 π‘₯π‘₯ 𝐡𝐡 + 𝑆𝑆𝑆𝑆𝐸𝐸

31

Page 32: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Homework example Consider an experiment where we randomly

assign a group of 16 students to 4 treatments Half will read the lecture notes, half won’t Half will do the practice problems, half won’t

Measure their score on the midterm, out of 100 4 treatment combinations (2 x 2 factorial) Check for interaction

32

Page 33: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Homework example(R code & output)df <- as.data.frame(expand.grid(readLecture = c(rep("no read",2),

rep("read", 2)), pracProbs = c(rep("no practice",2), rep("practice",2)), KEEP.OUT.ATTRS = F))

require(plyr)

df <- arrange(df, pracProbs, readLecture)

df$score <- c(50,45,56,60, 65,58,63,70, 60,70,68,65, 88,89,86,94)

> with(df, tapply(score, list(readLecture, pracProbs), mean))no practice practice

no read 52.75 65.75

read 64.00 89.25

with(df, interaction.plot(readLecture, pracProbs, score, col=c("red", "blue"), main="", type = "b"))

with(df, interaction.plot(pracProbs, readLecture, score, col=c("red", "blue"), main="", type = "b"))

33

Page 34: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Homework example(R output) Interaction plots (both ways, just because) When increasing one level of Factor A also

increases the effect of Factor B, this is called a reinforcement effect

The other way is called an interference effect

34

Page 35: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Homework example(R code & output)> anova(lm(score ~ readLecture*pracProbs, data=df))Analysis of Variance Table

Response: score

Df Sum Sq Mean Sq F value Pr(>F)

readLecture 1 1207.56 1207.56 48.9139 1.447e-05 ***

pracProbs 1 1463.06 1463.06 59.2633 5.561e-06 ***

readLecture:pracProbs 1 150.06 150.06 6.0785 0.02974 *

Residuals 12 296.25 24.69

40

Page 36: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Homework example(Conclusions) When the interaction is not significant, it should

be removed and the main effects tested with an additive model Some statisticians leave it there and test main

effects, but this is less powerful It looks like the two types of studying reinforce

each other Need to be careful how we interpret main effects

in the presence of an interaction, usually Here it is pretty clear – both study methods are

clearly beneficial 41

Page 37: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Farming example(R code & output)> anova(lm(yield ~ fertil + type, data=farm))Analysis of Variance Table

Response: yield

Df Sum Sq Mean Sq F value Pr(>F)

fertil 3 283.25 94.417 5.4023 0.01571 *

type 1 30.25 30.250 1.7308 0.21507

Residuals 11 192.25 17.477

> anova(lm(yield ~ fertil*type, data=farm))Analysis of Variance Table

Response: yield

Df Sum Sq Mean Sq F value Pr(>F)

fertil 3 283.25 94.417 3.9754 0.05262 .

type 1 30.25 30.250 1.2737 0.29178

fertil:type 3 2.25 0.750 0.0316 0.99186

Residuals 8 190.00 23.750

42

Page 38: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Testing Contrasts

Differences between marginal means are definitely contrasts (main effect for fertilizer) 𝐻𝐻0: πœ‡πœ‡11+πœ‡πœ‡21

2= πœ‡πœ‡12+πœ‡πœ‡22

2= πœ‡πœ‡13+πœ‡πœ‡23

2= πœ‡πœ‡14+πœ‡πœ‡24

2

How to form these contrasts in factorial ANOVA?

𝑓𝑓1 𝑓𝑓2 𝑓𝑓3 𝑓𝑓4 π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘

𝑑𝑑1 πœ‡πœ‡11 πœ‡πœ‡12 πœ‡πœ‡13 πœ‡πœ‡14πœ‡πœ‡11 + β‹―+ πœ‡πœ‡14

4

𝑑𝑑2 πœ‡πœ‡21 πœ‡πœ‡22 πœ‡πœ‡23 πœ‡πœ‡24πœ‡πœ‡21 + β‹―+ πœ‡πœ‡24

4

π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘ πœ‡πœ‡11 + πœ‡πœ‡212

πœ‡πœ‡12 + πœ‡πœ‡222

πœ‡πœ‡13 + πœ‡πœ‡232

πœ‡πœ‡14 + πœ‡πœ‡242 πœ‡πœ‡

43

Page 39: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

𝐹𝐹 𝑇𝑇 π’‡π’‡πŸπŸ π’‡π’‡πŸ‘πŸ‘ π’‡π’‡πŸ’πŸ’ π’•π’•πŸπŸ π’‡π’‡πŸπŸπ’•π’•πŸπŸ π’‡π’‡πŸ‘πŸ‘π’•π’•πŸπŸ π’‡π’‡πŸ’πŸ’π’•π’•πŸπŸ 𝑬𝑬(𝒀𝒀|𝑿𝑿 = 𝒙𝒙)1 C 0 0 0 0 0 0 0 𝛽𝛽01 S 0 0 0 1 0 0 0 𝛽𝛽0 + 𝛽𝛽42 C 1 0 0 0 0 0 0 𝛽𝛽0 + 𝛽𝛽12 S 1 0 0 1 1 0 0 𝛽𝛽0 + 𝛽𝛽1 + 𝛽𝛽4 + 𝛽𝛽53 C 0 1 0 0 0 0 0 𝛽𝛽0 + 𝛽𝛽23 S 0 1 0 1 0 1 0 𝛽𝛽0 + 𝛽𝛽2 + 𝛽𝛽4 + 𝛽𝛽64 C 0 0 1 0 0 0 0 𝛽𝛽0 + 𝛽𝛽34 S 0 0 1 1 0 0 1 𝛽𝛽0 + 𝛽𝛽3 + 𝛽𝛽4 + 𝛽𝛽7

Testing Contrasts(Reference Coding) First, write out the model and expected value for

each cell (use reference coding to start)E π‘Œπ‘Œπ‘–π‘–π‘ π‘ π‘Œπ‘Œπ‘Œπ‘Œ = 𝛽𝛽0 + 𝛽𝛽1𝑓𝑓2 + 𝛽𝛽2𝑓𝑓3 + 𝛽𝛽3𝑓𝑓4 + 𝛽𝛽4𝑑𝑑2 +𝛽𝛽5𝑓𝑓2𝑑𝑑2 + 𝛽𝛽6𝑓𝑓3𝑑𝑑2 + 𝛽𝛽7𝑓𝑓4𝑑𝑑2

44

Page 40: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Testing Contrasts(Reference Coding)

To test 𝐻𝐻0: πœ‡πœ‡11+πœ‡πœ‡212

= πœ‡πœ‡12+πœ‡πœ‡222

corresponds to

𝐻𝐻0: 𝛽𝛽0 + 12𝛽𝛽4 βˆ’ 𝛽𝛽0 + 𝛽𝛽1 + 1

2(𝛽𝛽4 + 𝛽𝛽5) = 0

𝐻𝐻0:𝛽𝛽1 = 𝛽𝛽5 = 0 (can you see why?)

𝑓𝑓1 𝑓𝑓2 𝑓𝑓3 𝑓𝑓4 π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘

𝑑𝑑1 𝛽𝛽0 𝛽𝛽0 + 𝛽𝛽1 𝛽𝛽0 + 𝛽𝛽2 𝛽𝛽0 + 𝛽𝛽3 𝛽𝛽0 +14

(𝛽𝛽1 + 𝛽𝛽2 + 𝛽𝛽3)

𝑑𝑑2 𝛽𝛽0 + 𝛽𝛽4𝛽𝛽0 + 𝛽𝛽1

+𝛽𝛽4 + 𝛽𝛽5𝛽𝛽0 + 𝛽𝛽2

+𝛽𝛽4 + 𝛽𝛽6𝛽𝛽0 + 𝛽𝛽3

+𝛽𝛽4 + 𝛽𝛽7

𝛽𝛽0 + 𝛽𝛽4 +14 (𝛽𝛽1 + 𝛽𝛽2 + 𝛽𝛽3)

+14 (𝛽𝛽5 + 𝛽𝛽6 + 𝛽𝛽7)

π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘ 𝛽𝛽0 +12𝛽𝛽4

𝛽𝛽0 + 𝛽𝛽1 +12 (𝛽𝛽4 + 𝛽𝛽5)

𝛽𝛽0 + 𝛽𝛽2 +12 (𝛽𝛽4 + 𝛽𝛽6)

𝛽𝛽0 + 𝛽𝛽3 +12 (𝛽𝛽4 + 𝛽𝛽7)

45

Page 41: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Reference Coding(R code & output)> fit <- lm(yield ~ fertil*type, data=farm)> model.matrix(fit)

> L <- rbind(c(0, 1, 0, 0, 0, 0, 0, 0),c(0, 0, 0, 0, 0, 1, 0, 0))

> glh.test(fit, L)

Test of General Linear Hypothesis

Call:

glh.test(reg = fit, cm = L)

F = 0.0895, df1 = 2, df2 = 8, p-value = 0.9153

46

Page 42: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Testing Contrasts(Reference Coding)

Interactions are also sets of contrasts 𝐻𝐻0: πœ‡πœ‡21 βˆ’ πœ‡πœ‡11 = πœ‡πœ‡22 βˆ’ πœ‡πœ‡12 = πœ‡πœ‡23 βˆ’ πœ‡πœ‡13 = πœ‡πœ‡24 βˆ’ πœ‡πœ‡14 𝐻𝐻0: πœ‡πœ‡12 βˆ’ πœ‡πœ‡11 = πœ‡πœ‡22 βˆ’ πœ‡πœ‡21 and

πœ‡πœ‡13 βˆ’ πœ‡πœ‡12 = πœ‡πœ‡23 βˆ’ πœ‡πœ‡22 and πœ‡πœ‡14 βˆ’ πœ‡πœ‡13 = πœ‡πœ‡24 βˆ’ πœ‡πœ‡23

𝑓𝑓1 𝑓𝑓2 𝑓𝑓3 𝑓𝑓4 π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘

𝑑𝑑1 πœ‡πœ‡11 πœ‡πœ‡12 πœ‡πœ‡13 πœ‡πœ‡14πœ‡πœ‡11 + β‹―+ πœ‡πœ‡14

4

𝑑𝑑2 πœ‡πœ‡21 πœ‡πœ‡22 πœ‡πœ‡23 πœ‡πœ‡24πœ‡πœ‡21 + β‹―+ πœ‡πœ‡24

4

π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘ πœ‡πœ‡11 + πœ‡πœ‡212

πœ‡πœ‡12 + πœ‡πœ‡222

πœ‡πœ‡13 + πœ‡πœ‡232

πœ‡πœ‡14 + πœ‡πœ‡242 πœ‡πœ‡

47

Page 43: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Testing Contrasts(Reference Coding)

𝐻𝐻0: πœ‡πœ‡21 βˆ’ πœ‡πœ‡11 = πœ‡πœ‡22 βˆ’ πœ‡πœ‡12 = πœ‡πœ‡23 βˆ’ πœ‡πœ‡13 = πœ‡πœ‡24 βˆ’ πœ‡πœ‡14 To test the interaction corresponds to𝐻𝐻0:𝛽𝛽4 = 𝛽𝛽4 + 𝛽𝛽5 = 𝛽𝛽4 + 𝛽𝛽6 = 𝛽𝛽4 + 𝛽𝛽7𝐻𝐻0:𝛽𝛽5 = 𝛽𝛽6 = 𝛽𝛽7 = 0

𝑓𝑓1 𝑓𝑓2 𝑓𝑓3 𝑓𝑓4 π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘

𝑑𝑑1 𝛽𝛽0 𝛽𝛽0 + 𝛽𝛽1 𝛽𝛽0 + 𝛽𝛽2 𝛽𝛽0 + 𝛽𝛽3 𝛽𝛽0 +14

(𝛽𝛽1 + 𝛽𝛽2 + 𝛽𝛽3)

𝑑𝑑2 𝛽𝛽0 + 𝛽𝛽4𝛽𝛽0 + 𝛽𝛽1

+𝛽𝛽4 + 𝛽𝛽5𝛽𝛽0 + 𝛽𝛽2

+𝛽𝛽4 + 𝛽𝛽6𝛽𝛽0 + 𝛽𝛽3

+𝛽𝛽4 + 𝛽𝛽7

𝛽𝛽0 + 𝛽𝛽4 +14 (𝛽𝛽1 + 𝛽𝛽2 + 𝛽𝛽3)

+14 (𝛽𝛽5 + 𝛽𝛽6 + 𝛽𝛽7)

π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘ 𝛽𝛽0 +12𝛽𝛽4

𝛽𝛽0 + 𝛽𝛽1 +12 (𝛽𝛽4 + 𝛽𝛽5)

𝛽𝛽0 + 𝛽𝛽2 +12 (𝛽𝛽4 + 𝛽𝛽6)

𝛽𝛽0 + 𝛽𝛽3 +12 (𝛽𝛽4 + 𝛽𝛽7)

48

Page 44: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Reference Coding(R code & output)> L <- rbind(c(0, 0, 0, 0, 0, 1, 0, 0),

c(0, 0, 0, 0, 0, 0, 1, 0),c(0, 0, 0, 0, 0, 0, 0, 1))

> glh.test(fit, L)Test of General Linear Hypothesis

Call:

glh.test(reg = fit, cm = L)

F = 0.0316, df1 = 3, df2 = 8, p-value = 0.9919

> anova(fit)Response: yield

Df Sum Sq Mean Sq F value Pr(>F)

fertil 3 283.25 94.417 3.9754 0.05262 .

type 1 30.25 30.250 1.2737 0.29178

fertil:type 3 2.25 0.750 0.0316 0.99186

Residuals 8 190.00 23.750

49

Page 45: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Testing Contrasts(Reference Coding) Reference coding gets worse the higher the

order of the factorial experiment Cell means is no better

Effect coding gets easier Better learn it now!

50

Page 46: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

𝐹𝐹 𝑇𝑇 π’‡π’‡πŸπŸ π’‡π’‡πŸπŸ π’‡π’‡πŸ‘πŸ‘ π’•π’•πŸπŸ π’‡π’‡πŸπŸπ’•π’•πŸπŸ π’‡π’‡πŸπŸπ’•π’•πŸπŸ π’‡π’‡πŸ‘πŸ‘π’•π’•πŸπŸ 𝑬𝑬(𝒀𝒀|𝑿𝑿 = 𝒙𝒙)1 C 1 0 0 1 1 0 0 𝛽𝛽0 + 𝛽𝛽1 + 𝛽𝛽4 + 𝛽𝛽51 S 1 0 0 -1 -1 0 0 𝛽𝛽0 + 𝛽𝛽1 βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽52 C 0 1 0 1 0 1 0 𝛽𝛽0 + 𝛽𝛽2 + 𝛽𝛽4 + 𝛽𝛽62 S 0 1 0 -1 0 -1 0 𝛽𝛽0 + 𝛽𝛽2 βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽63 C 0 0 1 1 0 0 1 𝛽𝛽0 + 𝛽𝛽3 + 𝛽𝛽4 + 𝛽𝛽73 S 0 0 1 -1 0 0 -1 𝛽𝛽0 + 𝛽𝛽3 βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽7

4 C -1 -1 -1 1 -1 -1 -1 𝛽𝛽0 βˆ’ 𝛽𝛽1 βˆ’ 𝛽𝛽2 βˆ’ 𝛽𝛽3+𝛽𝛽4 βˆ’ 𝛽𝛽5 βˆ’ 𝛽𝛽6 βˆ’ 𝛽𝛽7

4 S -1 -1 -1 -1 1 1 1 𝛽𝛽0 βˆ’ 𝛽𝛽1 βˆ’ 𝛽𝛽2 βˆ’ 𝛽𝛽3βˆ’π›½π›½4 + 𝛽𝛽5 + 𝛽𝛽6 + 𝛽𝛽7

Testing Contrasts(Effect Coding)E π‘Œπ‘Œπ‘–π‘–π‘ π‘ π‘Œπ‘Œπ‘Œπ‘Œ = 𝛽𝛽0 + 𝛽𝛽1𝑓𝑓1 + 𝛽𝛽2𝑓𝑓2 + 𝛽𝛽3𝑓𝑓3 + 𝛽𝛽4𝑑𝑑1 +𝛽𝛽5𝑓𝑓1𝑑𝑑1 + 𝛽𝛽6𝑓𝑓2𝑑𝑑1 + 𝛽𝛽7𝑓𝑓3𝑑𝑑1

51

Page 47: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Testing Contrasts(Effect Coding)

To test 𝐻𝐻0: πœ‡πœ‡11+πœ‡πœ‡212

= πœ‡πœ‡12+πœ‡πœ‡222

corresponds to

𝐻𝐻0: 𝛽𝛽0 + 𝛽𝛽1 βˆ’ 𝛽𝛽0 + 𝛽𝛽2 = 0𝐻𝐻0:𝛽𝛽1 = 𝛽𝛽2 as expected

𝑓𝑓1 𝑓𝑓2 𝑓𝑓3 𝑓𝑓4 π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘

𝑑𝑑1𝛽𝛽0 + 𝛽𝛽1+ 𝛽𝛽4 + 𝛽𝛽5

𝛽𝛽0 + 𝛽𝛽2+ 𝛽𝛽4 + 𝛽𝛽6

𝛽𝛽0 + 𝛽𝛽3+ 𝛽𝛽4 + 𝛽𝛽7

𝛽𝛽0 βˆ’ 𝛽𝛽1 βˆ’ 𝛽𝛽2 βˆ’ 𝛽𝛽3+𝛽𝛽4 βˆ’ 𝛽𝛽5 βˆ’ 𝛽𝛽6 βˆ’ 𝛽𝛽7

𝛽𝛽0 + 𝛽𝛽4

𝑑𝑑2𝛽𝛽0 + 𝛽𝛽1βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽5

𝛽𝛽0 + 𝛽𝛽2βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽6

𝛽𝛽0 + 𝛽𝛽3βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽7

𝛽𝛽0 βˆ’ 𝛽𝛽1 βˆ’ 𝛽𝛽2 βˆ’ 𝛽𝛽3βˆ’π›½π›½4 + 𝛽𝛽5 + 𝛽𝛽6 + 𝛽𝛽7

𝛽𝛽0 βˆ’ 𝛽𝛽4

π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘ 𝛽𝛽0 + 𝛽𝛽1 𝛽𝛽0 + 𝛽𝛽2 𝛽𝛽0 + 𝛽𝛽3 𝛽𝛽0 βˆ’ 𝛽𝛽1 βˆ’ 𝛽𝛽2 βˆ’ 𝛽𝛽3 𝛽𝛽0

52

Page 48: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Testing Contrasts(Effect Coding)

𝐻𝐻0: πœ‡πœ‡21 βˆ’ πœ‡πœ‡11 = πœ‡πœ‡22 βˆ’ πœ‡πœ‡12 = πœ‡πœ‡23 βˆ’ πœ‡πœ‡13 = πœ‡πœ‡24 βˆ’ πœ‡πœ‡14 To test the interaction corresponds to𝐻𝐻0: 2𝛽𝛽4 + 2𝛽𝛽5 = 2𝛽𝛽4 + 2𝛽𝛽6 = 2𝛽𝛽4 + 2𝛽𝛽7 = 2𝛽𝛽4 βˆ’ 2𝛽𝛽5 βˆ’ 2𝛽𝛽6 βˆ’ 2𝛽𝛽7𝐻𝐻0:𝛽𝛽5 = 𝛽𝛽6 = 𝛽𝛽7 = βˆ’π›½π›½5 βˆ’ 𝛽𝛽6 βˆ’ 𝛽𝛽7𝐻𝐻0:𝛽𝛽5 = 𝛽𝛽6 = 𝛽𝛽7 = 0

𝑓𝑓1 𝑓𝑓2 𝑓𝑓3 𝑓𝑓4 π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘

𝑑𝑑1𝛽𝛽0 + 𝛽𝛽1+ 𝛽𝛽4 + 𝛽𝛽5

𝛽𝛽0 + 𝛽𝛽2+ 𝛽𝛽4 + 𝛽𝛽6

𝛽𝛽0 + 𝛽𝛽3+ 𝛽𝛽4 + 𝛽𝛽7

𝛽𝛽0 βˆ’ 𝛽𝛽1 βˆ’ 𝛽𝛽2 βˆ’ 𝛽𝛽3+𝛽𝛽4 βˆ’ 𝛽𝛽5 βˆ’ 𝛽𝛽6 βˆ’ 𝛽𝛽7

𝛽𝛽0 + 𝛽𝛽4

𝑑𝑑2𝛽𝛽0 + 𝛽𝛽1βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽5

𝛽𝛽0 + 𝛽𝛽2βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽6

𝛽𝛽0 + 𝛽𝛽3βˆ’ 𝛽𝛽4 βˆ’ 𝛽𝛽7

𝛽𝛽0 βˆ’ 𝛽𝛽1 βˆ’ 𝛽𝛽2 βˆ’ 𝛽𝛽3βˆ’π›½π›½4 + 𝛽𝛽5 + 𝛽𝛽6 + 𝛽𝛽7

𝛽𝛽0 βˆ’ 𝛽𝛽4

π‘€π‘€π‘ π‘ π‘Žπ‘Žπ‘π‘ 𝛽𝛽0 + 𝛽𝛽1 𝛽𝛽0 + 𝛽𝛽2 𝛽𝛽0 + 𝛽𝛽3 𝛽𝛽0 βˆ’ 𝛽𝛽1 βˆ’ 𝛽𝛽2 βˆ’ 𝛽𝛽3 𝛽𝛽0

53

Page 49: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

A Common Practice We want to predict the Grade in a class from Sex

{Male, Female} and PoST {Stats, Other} A 2x2 factorial design

Main interest is the effect of sex, but recognize that maybe PoST has an effect too, so

Split the data by PoST, and run two-sample t-tests comparing sex within each PoST

If one is significant and the other isn’t, then the effect of sex depends on PoST Good idea? Discuss …

54

Page 50: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Visualize

55

Page 51: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

A Common Practice(R code & output)> with(subset(fact, post == "Stats"), t.test(score[sex == "Male"], score[sex == "Female"]))

Welch Two Sample t-test

t = -2.0979, df = 17.684, p-value = 0.05057

95 percent confidence interval:

-20.42798825 0.02798825

mean of x mean of y

68.8 79.0

> with(subset(fact, post == "Other"), t.test(score[sex == "Male"], score[sex == "Female"]))

Welch Two Sample t-test

t = -2.556, df = 17.62, p-value = 0.02007

95 percent confidence interval:

-18.961573 -1.838427

mean of x mean of y

50.4 60.8

56

Page 52: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

A Common Conclusion At the 5% significance level, Males and females have similar grades in the

Stats program Males and females have different grades in the

Other programs Therefore, the effect of sex depends on program

of study

What do you think?

57

Page 53: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

The correct solution> fit <- lm(score ~ sex*post, data= fact)> anova(fit)Analysis of Variance Table

Response: score

Df Sum Sq Mean Sq F value Pr(>F)

sex 1 1060.9 1060.9 10.557 0.00251 **

post 1 3348.9 3348.9 33.326 1.398e-06 ***

sex:post 1 0.1 0.1 0.001 0.97501

Residuals 36 3617.6 100.5

58

Page 54: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Three Factors: A, B and C Three main effects: one each for A, B and C There are many subsets of simple effects The effect of A, at level 𝑠𝑠𝑖𝑖 and 𝑐𝑐𝑖𝑖, etc…

Three two-factor interactions AxB (averaging over C) AxC (averaging over B) BxC (averaging over A)

One three-factor interaction: AxBxC

59

Page 55: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Three-factor interaction The form of the AxB interaction depends on the

value of C The form of the AxC interaction depends on the

value of B The form of the BxC interaction depends on the

value of A These statements are equivalent Use the one that is easiest to understand

60

Page 56: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Graph a 3-factor interaction Make a 2-factor interaction plot, at each level of

the third factor Maybe pick the factor that has the fewest levels

61

Page 57: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Higher order factorial designs For F factors, There will be one intercept

There will be 𝐹𝐹1 main effects

There will be 𝐹𝐹k k-factor interactions

There is an F test for each one

62

Page 58: Factorial ANOVA - University of Toronto Example (Factorial setup) Suppose we continue with the farming example 16 observations of crop yield (Y) 4 fertilizers (Factor A) with levels

Higher order factorial designs As the number of factors increases The higher-way interactions get harder and harder

to understand All the tests are still tests of sets of contrasts Differences between differences of differences …

It gets harder and harder to write down the contrasts

Effect coding becomes easier

63


Recommended