handling categorical predictors:...

17
1/33 Handling Categorical Predictors: ANOVA 2/33 I Hate Lines! I When we think of experiments, we think of manipulating categories I Control, Treatment 1, Treatment 2 I Models with Categorical Predictors still reflect an underlying data generating process and error generating process I In many ways, it’s like having many processed generating data I Linear (or Generalized Linear) Models still underlie ANOVA or other analyses with categorical predictors

Upload: others

Post on 08-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

1/33

Handling Categorical Predictors:ANOVA

2/33

I Hate Lines!

I When we think of experiments, we think of manipulatingcategories

I Control, Treatment 1, Treatment 2

I Models with Categorical Predictors still reflect an underlyingdata generating process and error generating process

I In many ways, it’s like having many processed generating data

I Linear (or Generalized Linear) Models still underlie ANOVA orother analyses with categorical predictors

Page 2: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

3/33

Categorical Predictors: Gene Expression and MentalDisorders

4/33

Categorical Predictors

control bipolar schizo

−0.

4−

0.2

0.0

0.2

0.4

How do we determine the importance of categorical predictors?

Page 3: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

5/33

Aside: Reordering FactorsbrainGene$group <- factor(brainGene$group,

levels=c("control", "bipolar", "schizo"))

control bipolar schizo

−0.

4−

0.2

0.0

0.2

0.4

How do we determine the importance of categorical predictors?

6/33

Categorical Predictors Ubiquitous

I Treatments in an ExperimentI Spatial groups - plots, Sites, States, etc.I Individual sampling unitsI Temporal groups - years, seasons, months

Page 4: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

7/33

Modeling Categorical Predictors

8/33

Traditional Way to Think About Categories

−0.2

−0.1

0.0

control bipolar schizogroup

mea

n.E

xpre

ssio

n

What is the variance between groups v. within groups?

Page 5: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

9/33

But How is the Model Fit?

−0.25

0.00

0.25

0.50

control bipolargroup

PLP

1.ex

pres

sion

10/33

But How is the Model Fit?

−0.25

0.00

0.25

0.50

control bipolargroup

PLP

1.ex

pres

sion

Underlying linear model with control = intercept, dummy variablefor bipolar

Page 6: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

11/33

But How is the Model Fit?

−0.25

0.00

0.25

0.50

0 1group

PLP

1.ex

pres

sion

Underlying linear model with control = intercept, dummy variablefor bipolar

12/33

But How is the Model Fit?

−0.25

0.00

0.25

0.50

control schizogroup

PLP

1.ex

pres

sion

Underlying linear model with control = intercept, dummy variablefor schizo

Page 7: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

13/33

Different Ways to Write a Categorical Model

yij = y + (yi − y) + (yij − yi)

yij = µ+ αi + εij , εij ∼ N(0, σ2)

yj = β0 +∑

βixi + εj , xi = 0, 1

xi inidicates presence/abscence of a category

Traditional ANOVA special case where all xi are orthogonal

Often one category set to β0 for ease of fitting

14/33

This is a Linear Model

bg.sub.lm <- lm(PLP1.expression ˜ group, data=brainGene)

Page 8: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

15/33

Evaluating Categorical Predictors

16/33

Hypothesis Testing with a Categorical Model: ANOVA

H0 = µ1 = µ2 = µ3 = ...

OR

β0 = µ, βi = 0

Page 9: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

17/33

F-Test to Compare

Regression: SST otal = SSModel + SSError

SST otal = SSBetween + SSW ithin

SSBetween =∑

i

∑j(Yi − Y )2, df=k-1

SSW ithin =∑

i

∑j(Yij − Yi)2, df=n-k

To compare them, we need to correct for different DF. This is theMean Square.

MS = SS/DF, e.g, MSW = SSWn−k

18/33

F-Test to Compare

F = MSB

MSWwith DF=k-1,n-k

(note similarities to SSR and SSE notation of regression)

Page 10: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

19/33

ANOVA

anova(bg.sub.lm)

# Analysis of Variance Table## Response: PLP1.expression# Df Sum Sq Mean Sq F value Pr(>F)# group 2 0.54025 0.270127 7.8231 0.001294# Residuals 42 1.45023 0.034529

20/33

Is using ANOVA valid?

Page 11: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

21/33

Assumptions of Ordinary Least Squares Regression

I Independence of data pointsI Normality within groupsI Homoscedasticity (homogeneity of variance)

22/33

Inspecting Assumptionspar(mfrow=c(2,3))plot(bg.sub.lm, which=1:5 )par(mfrow=c(1,1))

−0.25 −0.10

−0.

20.

20.

4

Fitted values

Res

idua

ls

Residuals vs Fitted7

21

43

−2 0 1 2

−1

01

23

Theoretical Quantiles

Sta

ndar

dize

d re

sidu

als

Normal Q−Q721

43

−0.25 −0.10

0.0

0.5

1.0

1.5

Fitted values

Sta

ndar

dize

d re

sidu

als

Scale−Location7

21

43

0 20 40

0.00

0.05

0.10

0.15

Obs. number

Coo

k's

dist

ance

Cook's distance7

21

43

−2

−1

01

23

Factor Level Combinations

Sta

ndar

dize

d re

sidu

als

control schizogroup :

Constant Leverage: Residuals vs Factor Levels

721

43

Page 12: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

23/33

Levene’s Test of Homogeneity of Variance

library(car)leveneTest(PLP1.expression ˜ group, data=brainGene)

# Levene's Test for Homogeneity of Variance (center = median)# Df F value Pr(>F)# group 2 1.0067 0.3741# 42

Levene’s test robust to departures from normality

24/33

What do I do if I Violate Assumptions?

I Nonparametric Kruskal-Wallace (uses ranks)I Transform?I GLM with ANODEV

Page 13: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

25/33

Kruskal Wallace Test

kruskal.test(PLP1.expression ˜ group, data=brainGene)

## Kruskal-Wallis rank sum test## data: PLP1.expression by group# Kruskal-Wallis chi-squared = 13.1985, df = 2, p-value# = 0.001361

26/33

Exercise: Daphnia Resistance

I Evaluate whether the data isappropriate for ANOVA

I Fit an ANOVA and checkdiagnostics

I Evaluate results & compare toKruskal-Wallace and a glm with aGamma distribution

Page 14: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

27/33

Daphnia Data

0.6

0.7

0.8

0.9

low med highcyandensity

resi

stan

ce

28/33

Daphnia Means

0.65

0.70

0.75

0.80

low med highcyandensity

mea

n_re

sist

ance

Page 15: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

29/33

How about HOV?

leveneTest(resistance ˜ cyandensity, data=daphnia)

# Levene's Test for Homogeneity of Variance (center = median)# Df F value Pr(>F)# group 2 2.0019 0.1533# 29

30/33

ANOVA shows an Effect

daphniaLM <- lm(resistance ˜ cyandensity, data=daphnia)anova(daphniaLM)

# Analysis of Variance Table## Response: resistance# Df Sum Sq Mean Sq F value Pr(>F)# cyandensity 2 0.089195 0.044598 6.6916 0.004078# Residuals 29 0.193277 0.006665

Page 16: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

31/33

KW shows an Effect

## Kruskal-Wallis rank sum test## data: resistance by cyandensity# Kruskal-Wallis chi-squared = 8.1996, df = 2, p-value# = 0.01658

32/33

Bad GLM Does Not

# Analysis of Deviance Table## Model: Gamma, link: identity## Response: resistance## Terms added sequentially (first to last)### Df Deviance Resid. Df Resid. Dev# NULL 31 0.52908# cyandensity 2 0.16216 29 0.36692

Page 17: Handling Categorical Predictors: ANOVAbyrneslab.net/classes/biol607/lectures/lecture_20_anova1... · 2014. 11. 6. · 25/33 Kruskal Wallace Test kruskal.test(PLP1.expression˜group,data=brainGene)

33/33

Diagnostics Also Good

0.68 0.74 0.80

−0.

10.

00.

10.

2

Fitted values

Res

idua

ls

Residuals vs Fitted3231

1

−2 0 1 2

−2

−1

01

2

Theoretical Quantiles

Sta

ndar

dize

d re

sidu

als

Normal Q−Q3231

1

0.68 0.74 0.80

0.0

0.5

1.0

1.5

Fitted values

Sta

ndar

dize

d re

sidu

als

Scale−Location3231

1

0 10 20 30

0.00

0.05

0.10

0.15

Obs. number

Coo

k's

dist

ance

Cook's distance32

1 31

0.00 0.04 0.08

−2

−1

01

2

Leverage

Sta

ndar

dize

d re

sidu

als

Cook's distance

Residuals vs Leverage32

1

31