Lecture 7. Analysis of Covariance (ANCOVA)

34
PSYC 3031 INTERMEDIATE STATISTICS LABORATORY J. Elder LAST UPDATED: November 22, 2012 ANALYSIS OF COVARIANCE (ANCOVA)

Transcript of Lecture 7. Analysis of Covariance (ANCOVA)

Page 1: Lecture 7. Analysis of Covariance (ANCOVA)

PSYC 3031 INTERMEDIATE STATISTICS LABORATORY J. Elder

LAST UPDATED: November 22, 2012

ANALYSIS OF COVARIANCE (ANCOVA)

Page 2: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

2

Acknowledgements

¨  Some of these slides have been sourced or modified from slides created by A. Field for Discovering Statistics using R.

Page 3: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

3

Aims

¨  When and why do we use ANCOVA? ¨  Partitioning variance ¨  Carrying out ANCOVA using R

¨  Interpretation ¤ Main effects ¤ Covariates

Page 4: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

4

When and Why

¨  To test for differences between group means when we know that an extraneous continuous variable affects the continuous outcome variable

Page 5: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

5

Advantages of ANCOVA

¨  Reduces error variance ¤ By explaining some of the unexplained variance (SSR)

the error variance in the model can be reduced.

¨  Greater experimental control ¤ By controlling known extraneous variables, we gain

greater insight into the effect of the predictor variable(s).

Page 6: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

6

Variance

Page 7: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

7

An Example

¨  We will use the Viagra example (from the book). ¤  There are several possible confounding variables – e.g.

partner’s libido, medication.

¨  We can conduct the same study but measure partner’s libido over the same time period following the dose of Viagra. ¤ Outcome (or DV) = participant’s libido ¤  Predictor (or IV) = dose of Viagra (placebo, low, high) ¤ Covariate = partner’s libido

Page 8: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

8

Types of Sums of Squares*

¨  The statistical significance and effect size of each predictor is determined in part by the model sum of squares attributed to that predictor.

¨  However, there are different ways of calculating the SSM for each predictor.

¨  Here we will consider only Type I and Type III sums of squares.

*This is a level (3) topic, but is required for this course.

Page 9: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

9

Type I Sum of Squares

¨  Consider the model

¨  Each SSM is calculated relative to the preceding model. ¤  SSM1 is calculated relative to Model 0: y0 = b0:

¤  SSM2 is calculated relative to Model 1: y1 = b0 + b1x1:

¤  …

¨  The total of the sums of squares for each predictor equals the total sum of squares for the model as a whole:

y = b0 + b1x1 ++ bnxn

SSM1 = y1i − y0 i( )2

i=1

n

SSM = SSM1 ++SSMn

SSM 2 = y2i − y1i( )2

i=1

n

Page 10: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

10

Type III Sum of Squares

¨  Consider the model

¨  Each SSM is calculated relative to the entire model but excluding that predictor. ¤  SSM1 is calculated relative to

¤  SSM2 is calculated relative to

¤ …

¨  Note that the individual SSMs no longer sum to the total SSM: SSM ≠ SSM1 ++SSMn

y = b0 + b1x1 ++ bnxn

y = b0 + b2x2 ++ bnxn

y = b0 + b1x1 + b3x3 ++ bnxn

Page 11: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

11

Type I vs Type III Sums of Squares

¨  Type I Sums of Squares can be appropriate for purely nested models.

¨  For the Viagra example, Type III is best.

Page 12: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

12

Additional Assumptions of ANCOVA

¨  Independence of covariate and treatment ¨  Homogeneity of regression slopes

Page 13: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

13

Relationships between the IV and Covariate

Page 14: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

14

Independence of the Covariate and Treatment Effect

¨  In our Viagra example, it is plausible that some variance is shared between the treatment (Viagra) and the covariate (partner Libido). ¤ e.g., Viagra à increased arousal à increased partner

libido ¨  Because this is an experiment, we know that this

shared variance originates with the treatment, not the covariate.

¨  But introducing the covariate could reduce statistical power, as some of the treatment effect may be loaded onto the covariate.

Page 15: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

15

Homogeneity of Regression Slopes

¨  Adding the covariate term makes sense only if the effect of the covariate is independent of the treatment.

¨  This is the same assumption underlying multiple regression: the predictors are assumed to act independently on the outcome.

¨  For ANCOVA, this independence corresponds to homogeneity of the regression slopes.

Page 16: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

16

Homogeneity of Regression Slopes

Page 17: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

17

Procedure for ANCOVA

¨  Explore the data & check ANOVA assumptions ¨  Check for homogeneity ¨  Check for independence between treatment and

covariate ¨  Run the ANCOVA ¨  Run contrasts/comparisons ¨  Check for homogeneity of regression slopes

Page 18: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

18

ANCOVA using R

¨  Required Packages: ¤  car

n  Levene’s test n  Type III sums of squares

¤  compute.es n  Effect sizes

¤  Effects n  Adjusted means

¤  ggplot2 n  Graphs

¤  multcomp n  Post hoc tests

¤  pastecs n  Descriptive statistics

Page 19: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

19

Example: ViagraCovariate.dat

Page 20: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

20

 Boxplots  of  the  Viagra  data  

Explore Data

Page 21: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

21

Homogeneity of Variance

¨  leveneTest(viagraData$libido, viagraData$dose, center = median)

Levene's Test for Homogeneity of Variance (center = median) Df F value Pr(>F) group 2 0.3256 0.7249 27

Page 22: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

22

Are the Predictor Variable and Covariate Independent?

¨  We can test this by running an ANOVA with partnerLibido as the outcome and dose as the predictor.

Page 23: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

23

Fitting an ANCOVA model

¨  To create an ANCOVA model we can use the aov() function.

¨  Remember that to add a predictor, we simply write ‘+ variableName’ into the model. In Chapter 10 our ANOVA model was: viagraModel<-aov(libido ~ dose, data = viagraData)

¨  To add the predictor partnerLibido, we could simply change the model to this: viagraModel<-aov(libido ~ dose + partnerLibido, data = viagraData)

Page 24: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

24

Fitting an ANCOVA Model

¨  However, note that aov() will use Type I Sum of Squares by default. So our result here will depend upon the order in which we entered the predictors.

¨  Better to use Type III Sum of Squares, available using the Anova() function of the car package:

¨  NB: The calculation of Type III sums of squares is only correct if orthogonal contrasts have been specified.

viagraModel<-aov(libido ~ dose + partnerLibido, data = viagraData) Anova(viagraModel, type = “III”)

Page 25: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

25

Contrasts ¨  Contrast 1: Compare the placebo group to all doses of Viagra. ¨  Contrast 2: Compare the high and low doses:

contrasts(viagraData$dose)<-cbind(c(-2,1,1), c(0,-1,1))

¨  To run the ANCOVA (with Type III sums of squares) we would execute:

contrasts(viagraData$dose)<-cbind(c(-2,1,1), c(0,-1,1)) viagraModel<-aov(libido ~ partnerLibido + dose, data = viagraData) Anova(viagraModel, type="III")

¨  The first line sets the contrasts for dose, the second line creates the ANCOVA model, and the third line prints the model summary with Type III sums of squares.

Page 26: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

26

The Main ANCOVA Model

Page 27: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

27

Planned Contrasts in ANCOVA

¨  The overall ANCOVA does not tell us which means differ, so to break down the overall effect of dose we need to look at the contrasts that we specified before we created the ANCOVA model: summary.lm(viagraModel)

Page 28: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

28

Interpreting Contrasts

Contrast Group Code

1 Placebo -2

1 Low +1

1 High +1

2 Placebo 0

2 Low -1

2 High +1

libido = b0 + b1contrast1 + b2contrast2

Page 29: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

29

Adjusted Means

¨  To interpret the significant effect of Viagra on libido, we want to analyze mean libido as a function of dose.

¨  However, what we are really interested in is mean libido when the covariate (partner’s libido) is held constant (at its mean value).

¨  These adjusted means can be computed using the effect() function of the effects package: ¤  adjustedMeans <- effect(“dose”, viagraModel)

Unadjusted Means (SDs)

Page 30: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

30

Adjusted Means

dose effect dose Placebo Low Dose High Dose 2.926370 4.712050 5.151251 Lower 95 Percent Confidence Limits dose Placebo Low Dose High Dose 1.700854 3.435984 4.118076 Upper 95 Percent Confidence Limits dose Placebo Low Dose High Dose 4.151886 5.988117 6.184427

Page 31: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

31

Post hoc tests in ANCOVA

¨  Because we want to test differences between the adjusted means, we can use only the glht() function.

¨  As such, we are limited to using Tukey or Dunnett’s post hoc tests: postHocs<-glht(viagraModel, linfct = mcp(dose = "Tukey")) summary(postHocs) confint(postHocs)

Page 32: Lecture 7. Analysis of Covariance (ANCOVA)
Page 33: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

33

Effect Size

¨  Recall that for Type III sums of squares, the model SSs do not sum to equal the total SS:

¨  Thus is no longer a great measure of effect size.

¨  Instead, we can compute the partial , where SSR is the residual SS for the whole model.

¨  The partial represents the proportion of variance unexplained by the remainder of the model that is explained by the predictor.

SSMi

i=1

k

∑ ≠ SST .

η2 =

SSMi

SST

ηi

2 =SSMi

SSMi +SSR

η2

Page 34: Lecture 7. Analysis of Covariance (ANCOVA)

Analysis of Covariance (ANCOVA)

J. Elder PSYC 3031 INTERMEDIATE STATISTICS LABORATORY

34

Effect Size

¨  Results:

¨  We will not cover other methods for measuring effect size (please skip Pages 492 – 494).

partial ηDose2 = .24

partial ηPartner Libido2 = .16