SlideShare a Scribd company logo
1 of 32
Download to read offline
Lab 11 – ANCOVA
October 29 & 30, 2018
FANR 6750
Richard Chandler and Bob Cooper
ANCOVA overview
Scenario
• We are interested in doing a one-way ANOVA
• However, we need to account for variation associated with a
continuous predictor variable
Regression One-way ANOVA ANCOVA 2 / 19
ANCOVA overview
Scenario
• We are interested in doing a one-way ANOVA
• However, we need to account for variation associated with a
continuous predictor variable
Additive model
yij = µ + αi + β(xij − ¯x) + εij
Regression One-way ANOVA ANCOVA 2 / 19
ANCOVA overview
Scenario
• We are interested in doing a one-way ANOVA
• However, we need to account for variation associated with a
continuous predictor variable
Additive model
yij = µ + αi + β(xij − ¯x) + εij
ANCOVA can be thought of as a hybrid between ANOVA
and regression
Regression One-way ANOVA ANCOVA 2 / 19
ANCOVA overview
Scenario
• We are interested in doing a one-way ANOVA
• However, we need to account for variation associated with a
continuous predictor variable
Additive model
yij = µ + αi + β(xij − ¯x) + εij
ANCOVA can be thought of as a hybrid between ANOVA
and regression
ANOVA, regression, and ANCOVA are linear models
Regression One-way ANOVA ANCOVA 2 / 19
The Diet Data
Import the data and view the levels of the factor
dietData <- read.csv("dietData.csv")
levels(dietData$diet)
## [1] "Control" "High" "Low" "Med"
Regression One-way ANOVA ANCOVA 3 / 19
The Diet Data
Import the data and view the levels of the factor
dietData <- read.csv("dietData.csv")
levels(dietData$diet)
## [1] "Control" "High" "Low" "Med"
Reorder the levels of the factor, just for convenience
levels(dietData$diet) <- list(Control="Control", Low="Low",
Med="Med", High="High")
levels(dietData$diet)
## [1] "Control" "Low" "Med" "High"
Regression One-way ANOVA ANCOVA 3 / 19
The diet data
plot(weight ~ age, dietData)
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q q
q
q
q
q
q
q
q
q
q
q
q
q
6 8 10 12 14 16 18
202530
age
weight
boxplot(weight ~ diet, dietData, ylab="Weight")
Control Low Med High
202530
Weight
Regression One-way ANOVA ANCOVA 4 / 19
Simple linear regression using lm
fm1 <- lm(weight ~ age, dietData)
summary(fm1)
##
## Call:
## lm(formula = weight ~ age, data = dietData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.6906 -1.2625 0.0522 1.0233 6.1680
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 21.32523 0.80685 26.430 < 2e-16 ***
## age 0.51807 0.06742 7.685 2.07e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.171 on 58 degrees of freedom
## Multiple R-squared: 0.5045, Adjusted R-squared: 0.496
## F-statistic: 59.05 on 1 and 58 DF, p-value: 2.072e-10
Regression One-way ANOVA ANCOVA 5 / 19
Simple linear regression using lm
fm1 <- lm(weight ~ age, dietData)
summary(fm1)
##
## Call:
## lm(formula = weight ~ age, data = dietData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.6906 -1.2625 0.0522 1.0233 6.1680
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 21.32523 0.80685 26.430 < 2e-16 ***
## age 0.51807 0.06742 7.685 2.07e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.171 on 58 degrees of freedom
## Multiple R-squared: 0.5045, Adjusted R-squared: 0.496
## F-statistic: 59.05 on 1 and 58 DF, p-value: 2.072e-10
The two estimates correspond to the intercept and slope parameters
Regression One-way ANOVA ANCOVA 5 / 19
Regression line and confidence interval
Regression lines and CIs can be created using predict
(1) Create a new data.frame containing a sequence of values of
the predictor variable age
(2) Predict weight using these values of age
(3) Put predictions and data together for easy plotting
Regression One-way ANOVA ANCOVA 6 / 19
Regression line and confidence interval
Regression lines and CIs can be created using predict
(1) Create a new data.frame containing a sequence of values of
the predictor variable age
(2) Predict weight using these values of age
(3) Put predictions and data together for easy plotting
age <- dietData$age
predData1 <- data.frame(age=seq(min(age), max(age), length=50))
pred1 <- predict(fm1, newdata=predData1, se.fit=TRUE,
interval="confidence")
predictions1 <- data.frame(pred1$fit, predData1)
Regression One-way ANOVA ANCOVA 6 / 19
Regression line and confidence interval
plot(weight ~ age, data=dietData) # raw data
lines(fit ~ age, data=predictions1, lwd=2) # fitted line
lines(lwr ~ age, data=predictions1, lwd=2, lty=3) # lower CI
lines(upr ~ age, data=predictions1, lwd=2, lty=3) # upper CI
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q q
q
q
q
q
q
q
q
q
q
q
q
q
6 8 10 12 14 16 18
202530
age
weight
Regression One-way ANOVA ANCOVA 7 / 19
One-way ANOVA using lm
Change the contrasts option so that the estimates will
correspond to the additive model, and then fit the ANOVA
options(contrasts=c("contr.sum", "contr.poly"))
fm2 <- lm(weight ~ diet, dietData)
summary.aov(fm2)
## Df Sum Sq Mean Sq F value Pr(>F)
## diet 3 54.6 18.216 2.053 0.117
## Residuals 56 496.9 8.873
Regression One-way ANOVA ANCOVA 8 / 19
One-way ANOVA using lm
Change the contrasts option so that the estimates will
correspond to the additive model, and then fit the ANOVA
options(contrasts=c("contr.sum", "contr.poly"))
fm2 <- lm(weight ~ diet, dietData)
summary.aov(fm2)
## Df Sum Sq Mean Sq F value Pr(>F)
## diet 3 54.6 18.216 2.053 0.117
## Residuals 56 496.9 8.873
The aov function gives identical results
summary(aov(weight ~ diet, dietData))
## Df Sum Sq Mean Sq F value Pr(>F)
## diet 3 54.6 18.216 2.053 0.117
## Residuals 56 496.9 8.873
Regression One-way ANOVA ANCOVA 8 / 19
An alternative summary
summary(fm2)
##
## Call:
## lm(formula = weight ~ diet, data = dietData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.6371 -1.9253 -0.0366 1.9770 5.4576
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.13962 0.38456 70.573 <2e-16 ***
## diet1 -1.02179 0.66608 -1.534 0.131
## diet2 -0.56593 0.66608 -0.850 0.399
## diet3 0.08027 0.66608 0.121 0.905
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.979 on 56 degrees of freedom
## Multiple R-squared: 0.09908, Adjusted R-squared: 0.05082
## F-statistic: 2.053 on 3 and 56 DF, p-value: 0.1169
Regression One-way ANOVA ANCOVA 9 / 19
An alternative summary
summary(fm2)
##
## Call:
## lm(formula = weight ~ diet, data = dietData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.6371 -1.9253 -0.0366 1.9770 5.4576
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.13962 0.38456 70.573 <2e-16 ***
## diet1 -1.02179 0.66608 -1.534 0.131
## diet2 -0.56593 0.66608 -0.850 0.399
## diet3 0.08027 0.66608 0.121 0.905
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.979 on 56 degrees of freedom
## Multiple R-squared: 0.09908, Adjusted R-squared: 0.05082
## F-statistic: 2.053 on 3 and 56 DF, p-value: 0.1169
Because we changed the contrast option to contr.sum, the intercept is the grand
mean (µ) and the other estimates are the effect sizes (αi)
Regression One-way ANOVA ANCOVA 9 / 19
One-way ANOVA
The predict function can also be used to obtain group means, SEs,
and CIs from a one-way ANOVA
predData2 <- data.frame(diet=levels(dietData$diet))
pred2 <- predict(fm2, newdata=predData2,
se.fit=TRUE, interval="confidence")
Regression One-way ANOVA ANCOVA 10 / 19
One-way ANOVA
The predict function can also be used to obtain group means, SEs,
and CIs from a one-way ANOVA
predData2 <- data.frame(diet=levels(dietData$diet))
pred2 <- predict(fm2, newdata=predData2,
se.fit=TRUE, interval="confidence")
predictions2 <- data.frame(pred2$fit, SE=pred2$se, predData2)
predictions2
## fit lwr upr SE diet
## 1 26.11783 24.57710 27.65856 0.7691199 Control
## 2 26.57368 25.03295 28.11442 0.7691199 Low
## 3 27.21988 25.67915 28.76062 0.7691199 Med
## 4 28.64707 27.10634 30.18780 0.7691199 High
Regression One-way ANOVA ANCOVA 10 / 19
One-way ANOVA
Control Low Med High
Diet
Weight
202224262830
Regression One-way ANOVA ANCOVA 11 / 19
ANCOVA preliminaries
Additive model
yij = µ + αi + β(xij − ¯x) + εij
Regression One-way ANOVA ANCOVA 12 / 19
ANCOVA preliminaries
Additive model
yij = µ + αi + β(xij − ¯x) + εij
Make sure the contrasts are set as before
options(contrasts=c("contr.sum", "contr.poly"))
Regression One-way ANOVA ANCOVA 12 / 19
ANCOVA preliminaries
Additive model
yij = µ + αi + β(xij − ¯x) + εij
Make sure the contrasts are set as before
options(contrasts=c("contr.sum", "contr.poly"))
Centering the covariate isn’t required, but doing so allow the intercept to
be interpretted as the grand mean
dietData$ageCentered <- dietData$age - mean(dietData$age)
Regression One-way ANOVA ANCOVA 12 / 19
ANCOVA
Put the covariate before the treatment variable in the formula.
fm3 <- lm(weight ~ ageCentered + diet, dietData)
Regression One-way ANOVA ANCOVA 13 / 19
ANCOVA
Put the covariate before the treatment variable in the formula.
fm3 <- lm(weight ~ ageCentered + diet, dietData)
summary(fm3)
##
## Call:
## lm(formula = weight ~ ageCentered + diet, data = dietData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.8214 -1.2213 -0.2519 1.2161 4.9185
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.1396 0.2406 112.787 < 2e-16 ***
## ageCentered 0.5573 0.0594 9.382 5.2e-13 ***
## diet1 -1.7446 0.4238 -4.116 0.00013 ***
## diet2 -0.3758 0.4173 -0.901 0.37171
## diet3 0.7819 0.4234 1.847 0.07020 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.864 on 55 degrees of freedom
## Multiple R-squared: 0.6536, Adjusted R-squared: 0.6284
## F-statistic: 25.94 on 4 and 55 DF, p-value: 4.147e-12
Regression One-way ANOVA ANCOVA 13 / 19
The ANOVA table
The null hypothesis of no diet effect is rejected, even though it was
not rejected before.
summary.aov(fm3)
## Df Sum Sq Mean Sq F value Pr(>F)
## ageCentered 1 278.25 278.25 80.095 2.54e-12 ***
## diet 3 82.22 27.41 7.889 0.000182 ***
## Residuals 55 191.07 3.47
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' '
Regression One-way ANOVA ANCOVA 14 / 19
Predict weight
Create predictions of weight over a sequences of ages, for every
level of diet
ageC <- dietData$ageCentered
predData3 <- data.frame(
diet=rep(c("Control", "Low", "Med", "High"), each=20),
ageCentered=rep(seq(min(ageC), max(ageC),
length=20),
times=4))
pred3 <- predict(fm3, newdata=predData3, se.fit=TRUE,
interval="confidence")
predictions3 <- data.frame(pred3$fit, predData3)
Regression One-way ANOVA ANCOVA 15 / 19
Plot the regression lines
colrs <- c("black", "royalblue", "orange", "darkcyan")
plot(weight ~ ageCentered, dietData, pch=16, cex=1.2,
col=rep(colrs, each=15))
lines(fit ~ ageCentered, predictions3, subset=diet=="Control",
col=colrs[1], lwd=2)
lines(fit ~ ageCentered, predictions3, subset=diet=="Low", lty=1,
col=colrs[2], lwd=2)
lines(fit ~ ageCentered, predictions3, subset=diet=="Med", lty=1,
col=colrs[3], lwd=2)
lines(fit ~ ageCentered, predictions3, subset=diet=="High", lty=1,
col=colrs[4], lwd=2)
legend(4, 23, c("High", "Med", "Low", "Control"), pch=16,
title="Diet", lwd=2, col=rev(colrs))
Regression One-way ANOVA ANCOVA 16 / 19
Plot the regression lines
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q q
q
q
q
q
q
q
q
q
q
q
q
q
−6 −4 −2 0 2 4 6 8
202530
ageCentered
weight
Regression One-way ANOVA ANCOVA 17 / 19
Plot the regression lines
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q q
q
q
q
q
q
q
q
q
q
q
q
q
−6 −4 −2 0 2 4 6 8
202530
ageCentered
weight
q
q
q
q
Diet
High
Med
Low
Control
Regression One-way ANOVA ANCOVA 17 / 19
Multiple comparisons
## install.packages("multcomp")
library(multcomp)
summary(glht(fm3, linfct=mcp(diet="Tukey")))
##
## Simultaneous Tests for General Linear Hypotheses
##
## Multiple Comparisons of Means: Tukey Contrasts
##
##
## Fit: lm(formula = weight ~ ageCentered + diet, data = dietData)
##
## Linear Hypotheses:
## Estimate Std. Error t value Pr(>|t|)
## Low - Control == 0 1.3688 0.6875 1.991 0.20389
## Med - Control == 0 2.5265 0.6973 3.623 0.00336 **
## High - Control == 0 3.0830 0.6832 4.513 < 0.001 ***
## Med - Low == 0 1.1577 0.6828 1.696 0.33583
## High - Low == 0 1.7143 0.6817 2.515 0.06861 .
## High - Med == 0 0.5566 0.6869 0.810 0.84931
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- single-step method)
Regression One-way ANOVA ANCOVA 18 / 19
Assignment
Complete the following and upload your R script to
ELC before lab next week
(1) Fit an ANCOVA model to the data in treeData.csv, which
represent the height of trees following a fertilizer experiment.
The covariate is pH.
(2) Use: options(contrasts=c("contr.sum",
"contr.poly")) so that your estimates correspond to the
additive model from the lecture notes
(3) Interpret each of the estimates from lm. What is the null
hypothesis associated with each p-value?
(4) Plot the data and the regression lines. Use different colors or
symbols to distinguish the treatment groups.
(5) Which fertilizer treatments are significantly different?
Regression One-way ANOVA ANCOVA 19 / 19

More Related Content

Similar to ANCOVA in R

Logistic Regression in Case-Control Study
Logistic Regression in Case-Control StudyLogistic Regression in Case-Control Study
Logistic Regression in Case-Control StudySatish Gupta
 
Multiple regression in spss
Multiple regression in spssMultiple regression in spss
Multiple regression in spssDr. Ravneet Kaur
 
Memorization of Various Calculator shortcuts
Memorization of Various Calculator shortcutsMemorization of Various Calculator shortcuts
Memorization of Various Calculator shortcutsPrincessNorberte
 
2.0.statistical methods and determination of sample size
2.0.statistical methods and determination of sample size2.0.statistical methods and determination of sample size
2.0.statistical methods and determination of sample sizesalummkata1
 
Workshop 4
Workshop 4Workshop 4
Workshop 4eeetq
 
Stats Ch 3 worksheet
Stats Ch 3 worksheetStats Ch 3 worksheet
Stats Ch 3 worksheetopilipets
 
Ge 105 lecture 1 (LEAST SQUARES ADJUSTMENT) by: Broddett B. Abatayo
Ge 105 lecture 1 (LEAST SQUARES ADJUSTMENT) by: Broddett B. AbatayoGe 105 lecture 1 (LEAST SQUARES ADJUSTMENT) by: Broddett B. Abatayo
Ge 105 lecture 1 (LEAST SQUARES ADJUSTMENT) by: Broddett B. AbatayoBPA ABATAYO Land Surveying Services
 
Simple linear regression
Simple linear regressionSimple linear regression
Simple linear regressionMaria Theresa
 
Role of regression in statistics (2)
Role of regression in statistics (2)Role of regression in statistics (2)
Role of regression in statistics (2)Nadeem Uddin
 
L1 updated introduction.pptx
L1 updated introduction.pptxL1 updated introduction.pptx
L1 updated introduction.pptxMesfinTadesse8
 
Correlation and regression
Correlation and regressionCorrelation and regression
Correlation and regressionKhalid Aziz
 
Data science classica_hypos
Data science classica_hyposData science classica_hypos
Data science classica_hyposNeeraj Sinha
 

Similar to ANCOVA in R (20)

One-way ANOVA
One-way ANOVAOne-way ANOVA
One-way ANOVA
 
Measures of Variation.pdf
Measures of Variation.pdfMeasures of Variation.pdf
Measures of Variation.pdf
 
Logistic Regression in Case-Control Study
Logistic Regression in Case-Control StudyLogistic Regression in Case-Control Study
Logistic Regression in Case-Control Study
 
Multiple regression in spss
Multiple regression in spssMultiple regression in spss
Multiple regression in spss
 
Hmisiri nonparametrics book
Hmisiri nonparametrics bookHmisiri nonparametrics book
Hmisiri nonparametrics book
 
Statistics Assignment Help
Statistics Assignment HelpStatistics Assignment Help
Statistics Assignment Help
 
Assumptions of ANOVA
Assumptions of ANOVAAssumptions of ANOVA
Assumptions of ANOVA
 
Regression analysis
Regression analysisRegression analysis
Regression analysis
 
Memorization of Various Calculator shortcuts
Memorization of Various Calculator shortcutsMemorization of Various Calculator shortcuts
Memorization of Various Calculator shortcuts
 
2.0.statistical methods and determination of sample size
2.0.statistical methods and determination of sample size2.0.statistical methods and determination of sample size
2.0.statistical methods and determination of sample size
 
Workshop 4
Workshop 4Workshop 4
Workshop 4
 
Stats Ch 3 worksheet
Stats Ch 3 worksheetStats Ch 3 worksheet
Stats Ch 3 worksheet
 
Regression for class teaching
Regression for class teachingRegression for class teaching
Regression for class teaching
 
Ge 105 lecture 1 (LEAST SQUARES ADJUSTMENT) by: Broddett B. Abatayo
Ge 105 lecture 1 (LEAST SQUARES ADJUSTMENT) by: Broddett B. AbatayoGe 105 lecture 1 (LEAST SQUARES ADJUSTMENT) by: Broddett B. Abatayo
Ge 105 lecture 1 (LEAST SQUARES ADJUSTMENT) by: Broddett B. Abatayo
 
Simple linear regression
Simple linear regressionSimple linear regression
Simple linear regression
 
Role of regression in statistics (2)
Role of regression in statistics (2)Role of regression in statistics (2)
Role of regression in statistics (2)
 
L1 updated introduction.pptx
L1 updated introduction.pptxL1 updated introduction.pptx
L1 updated introduction.pptx
 
Correlation and regression
Correlation and regressionCorrelation and regression
Correlation and regression
 
Statistics Project
Statistics ProjectStatistics Project
Statistics Project
 
Data science classica_hypos
Data science classica_hyposData science classica_hypos
Data science classica_hypos
 

More from richardchandler

Introduction to Generalized Linear Models
Introduction to Generalized Linear ModelsIntroduction to Generalized Linear Models
Introduction to Generalized Linear Modelsrichardchandler
 
Introduction to statistical modeling in R
Introduction to statistical modeling in RIntroduction to statistical modeling in R
Introduction to statistical modeling in Rrichardchandler
 
t-tests in R - Lab slides for UGA course FANR 6750
t-tests in R - Lab slides for UGA course FANR 6750t-tests in R - Lab slides for UGA course FANR 6750
t-tests in R - Lab slides for UGA course FANR 6750richardchandler
 
Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750richardchandler
 
Hierarchichal species distributions model and Maxent
Hierarchichal species distributions model and MaxentHierarchichal species distributions model and Maxent
Hierarchichal species distributions model and Maxentrichardchandler
 
The role of spatial models in applied ecological research
The role of spatial models in applied ecological researchThe role of spatial models in applied ecological research
The role of spatial models in applied ecological researchrichardchandler
 

More from richardchandler (8)

Introduction to Generalized Linear Models
Introduction to Generalized Linear ModelsIntroduction to Generalized Linear Models
Introduction to Generalized Linear Models
 
Introduction to statistical modeling in R
Introduction to statistical modeling in RIntroduction to statistical modeling in R
Introduction to statistical modeling in R
 
t-tests in R - Lab slides for UGA course FANR 6750
t-tests in R - Lab slides for UGA course FANR 6750t-tests in R - Lab slides for UGA course FANR 6750
t-tests in R - Lab slides for UGA course FANR 6750
 
Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750Introduction to R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750
 
Hierarchichal species distributions model and Maxent
Hierarchichal species distributions model and MaxentHierarchichal species distributions model and Maxent
Hierarchichal species distributions model and Maxent
 
Slides from ESA 2015
Slides from ESA 2015Slides from ESA 2015
Slides from ESA 2015
 
The role of spatial models in applied ecological research
The role of spatial models in applied ecological researchThe role of spatial models in applied ecological research
The role of spatial models in applied ecological research
 
2014 ISEC slides
2014 ISEC slides2014 ISEC slides
2014 ISEC slides
 

Recently uploaded

SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...Sérgio Sacani
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCEPRINCE C P
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)DHURKADEVIBASKAR
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzohaibmir069
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxAArockiyaNisha
 
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfAnalytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfSwapnil Therkar
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Recombination DNA Technology (Microinjection)
Recombination DNA Technology (Microinjection)Recombination DNA Technology (Microinjection)
Recombination DNA Technology (Microinjection)Jshifa
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Luciferase in rDNA technology (biotechnology).pptx
Luciferase in rDNA technology (biotechnology).pptxLuciferase in rDNA technology (biotechnology).pptx
Luciferase in rDNA technology (biotechnology).pptxAleenaTreesaSaji
 

Recently uploaded (20)

SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistan
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
 
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfAnalytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Recombination DNA Technology (Microinjection)
Recombination DNA Technology (Microinjection)Recombination DNA Technology (Microinjection)
Recombination DNA Technology (Microinjection)
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Luciferase in rDNA technology (biotechnology).pptx
Luciferase in rDNA technology (biotechnology).pptxLuciferase in rDNA technology (biotechnology).pptx
Luciferase in rDNA technology (biotechnology).pptx
 

ANCOVA in R

  • 1. Lab 11 – ANCOVA October 29 & 30, 2018 FANR 6750 Richard Chandler and Bob Cooper
  • 2. ANCOVA overview Scenario • We are interested in doing a one-way ANOVA • However, we need to account for variation associated with a continuous predictor variable Regression One-way ANOVA ANCOVA 2 / 19
  • 3. ANCOVA overview Scenario • We are interested in doing a one-way ANOVA • However, we need to account for variation associated with a continuous predictor variable Additive model yij = µ + αi + β(xij − ¯x) + εij Regression One-way ANOVA ANCOVA 2 / 19
  • 4. ANCOVA overview Scenario • We are interested in doing a one-way ANOVA • However, we need to account for variation associated with a continuous predictor variable Additive model yij = µ + αi + β(xij − ¯x) + εij ANCOVA can be thought of as a hybrid between ANOVA and regression Regression One-way ANOVA ANCOVA 2 / 19
  • 5. ANCOVA overview Scenario • We are interested in doing a one-way ANOVA • However, we need to account for variation associated with a continuous predictor variable Additive model yij = µ + αi + β(xij − ¯x) + εij ANCOVA can be thought of as a hybrid between ANOVA and regression ANOVA, regression, and ANCOVA are linear models Regression One-way ANOVA ANCOVA 2 / 19
  • 6. The Diet Data Import the data and view the levels of the factor dietData <- read.csv("dietData.csv") levels(dietData$diet) ## [1] "Control" "High" "Low" "Med" Regression One-way ANOVA ANCOVA 3 / 19
  • 7. The Diet Data Import the data and view the levels of the factor dietData <- read.csv("dietData.csv") levels(dietData$diet) ## [1] "Control" "High" "Low" "Med" Reorder the levels of the factor, just for convenience levels(dietData$diet) <- list(Control="Control", Low="Low", Med="Med", High="High") levels(dietData$diet) ## [1] "Control" "Low" "Med" "High" Regression One-way ANOVA ANCOVA 3 / 19
  • 8. The diet data plot(weight ~ age, dietData) q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q 6 8 10 12 14 16 18 202530 age weight boxplot(weight ~ diet, dietData, ylab="Weight") Control Low Med High 202530 Weight Regression One-way ANOVA ANCOVA 4 / 19
  • 9. Simple linear regression using lm fm1 <- lm(weight ~ age, dietData) summary(fm1) ## ## Call: ## lm(formula = weight ~ age, data = dietData) ## ## Residuals: ## Min 1Q Median 3Q Max ## -5.6906 -1.2625 0.0522 1.0233 6.1680 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 21.32523 0.80685 26.430 < 2e-16 *** ## age 0.51807 0.06742 7.685 2.07e-10 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 2.171 on 58 degrees of freedom ## Multiple R-squared: 0.5045, Adjusted R-squared: 0.496 ## F-statistic: 59.05 on 1 and 58 DF, p-value: 2.072e-10 Regression One-way ANOVA ANCOVA 5 / 19
  • 10. Simple linear regression using lm fm1 <- lm(weight ~ age, dietData) summary(fm1) ## ## Call: ## lm(formula = weight ~ age, data = dietData) ## ## Residuals: ## Min 1Q Median 3Q Max ## -5.6906 -1.2625 0.0522 1.0233 6.1680 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 21.32523 0.80685 26.430 < 2e-16 *** ## age 0.51807 0.06742 7.685 2.07e-10 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 2.171 on 58 degrees of freedom ## Multiple R-squared: 0.5045, Adjusted R-squared: 0.496 ## F-statistic: 59.05 on 1 and 58 DF, p-value: 2.072e-10 The two estimates correspond to the intercept and slope parameters Regression One-way ANOVA ANCOVA 5 / 19
  • 11. Regression line and confidence interval Regression lines and CIs can be created using predict (1) Create a new data.frame containing a sequence of values of the predictor variable age (2) Predict weight using these values of age (3) Put predictions and data together for easy plotting Regression One-way ANOVA ANCOVA 6 / 19
  • 12. Regression line and confidence interval Regression lines and CIs can be created using predict (1) Create a new data.frame containing a sequence of values of the predictor variable age (2) Predict weight using these values of age (3) Put predictions and data together for easy plotting age <- dietData$age predData1 <- data.frame(age=seq(min(age), max(age), length=50)) pred1 <- predict(fm1, newdata=predData1, se.fit=TRUE, interval="confidence") predictions1 <- data.frame(pred1$fit, predData1) Regression One-way ANOVA ANCOVA 6 / 19
  • 13. Regression line and confidence interval plot(weight ~ age, data=dietData) # raw data lines(fit ~ age, data=predictions1, lwd=2) # fitted line lines(lwr ~ age, data=predictions1, lwd=2, lty=3) # lower CI lines(upr ~ age, data=predictions1, lwd=2, lty=3) # upper CI q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q 6 8 10 12 14 16 18 202530 age weight Regression One-way ANOVA ANCOVA 7 / 19
  • 14. One-way ANOVA using lm Change the contrasts option so that the estimates will correspond to the additive model, and then fit the ANOVA options(contrasts=c("contr.sum", "contr.poly")) fm2 <- lm(weight ~ diet, dietData) summary.aov(fm2) ## Df Sum Sq Mean Sq F value Pr(>F) ## diet 3 54.6 18.216 2.053 0.117 ## Residuals 56 496.9 8.873 Regression One-way ANOVA ANCOVA 8 / 19
  • 15. One-way ANOVA using lm Change the contrasts option so that the estimates will correspond to the additive model, and then fit the ANOVA options(contrasts=c("contr.sum", "contr.poly")) fm2 <- lm(weight ~ diet, dietData) summary.aov(fm2) ## Df Sum Sq Mean Sq F value Pr(>F) ## diet 3 54.6 18.216 2.053 0.117 ## Residuals 56 496.9 8.873 The aov function gives identical results summary(aov(weight ~ diet, dietData)) ## Df Sum Sq Mean Sq F value Pr(>F) ## diet 3 54.6 18.216 2.053 0.117 ## Residuals 56 496.9 8.873 Regression One-way ANOVA ANCOVA 8 / 19
  • 16. An alternative summary summary(fm2) ## ## Call: ## lm(formula = weight ~ diet, data = dietData) ## ## Residuals: ## Min 1Q Median 3Q Max ## -7.6371 -1.9253 -0.0366 1.9770 5.4576 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 27.13962 0.38456 70.573 <2e-16 *** ## diet1 -1.02179 0.66608 -1.534 0.131 ## diet2 -0.56593 0.66608 -0.850 0.399 ## diet3 0.08027 0.66608 0.121 0.905 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 2.979 on 56 degrees of freedom ## Multiple R-squared: 0.09908, Adjusted R-squared: 0.05082 ## F-statistic: 2.053 on 3 and 56 DF, p-value: 0.1169 Regression One-way ANOVA ANCOVA 9 / 19
  • 17. An alternative summary summary(fm2) ## ## Call: ## lm(formula = weight ~ diet, data = dietData) ## ## Residuals: ## Min 1Q Median 3Q Max ## -7.6371 -1.9253 -0.0366 1.9770 5.4576 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 27.13962 0.38456 70.573 <2e-16 *** ## diet1 -1.02179 0.66608 -1.534 0.131 ## diet2 -0.56593 0.66608 -0.850 0.399 ## diet3 0.08027 0.66608 0.121 0.905 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 2.979 on 56 degrees of freedom ## Multiple R-squared: 0.09908, Adjusted R-squared: 0.05082 ## F-statistic: 2.053 on 3 and 56 DF, p-value: 0.1169 Because we changed the contrast option to contr.sum, the intercept is the grand mean (µ) and the other estimates are the effect sizes (αi) Regression One-way ANOVA ANCOVA 9 / 19
  • 18. One-way ANOVA The predict function can also be used to obtain group means, SEs, and CIs from a one-way ANOVA predData2 <- data.frame(diet=levels(dietData$diet)) pred2 <- predict(fm2, newdata=predData2, se.fit=TRUE, interval="confidence") Regression One-way ANOVA ANCOVA 10 / 19
  • 19. One-way ANOVA The predict function can also be used to obtain group means, SEs, and CIs from a one-way ANOVA predData2 <- data.frame(diet=levels(dietData$diet)) pred2 <- predict(fm2, newdata=predData2, se.fit=TRUE, interval="confidence") predictions2 <- data.frame(pred2$fit, SE=pred2$se, predData2) predictions2 ## fit lwr upr SE diet ## 1 26.11783 24.57710 27.65856 0.7691199 Control ## 2 26.57368 25.03295 28.11442 0.7691199 Low ## 3 27.21988 25.67915 28.76062 0.7691199 Med ## 4 28.64707 27.10634 30.18780 0.7691199 High Regression One-way ANOVA ANCOVA 10 / 19
  • 20. One-way ANOVA Control Low Med High Diet Weight 202224262830 Regression One-way ANOVA ANCOVA 11 / 19
  • 21. ANCOVA preliminaries Additive model yij = µ + αi + β(xij − ¯x) + εij Regression One-way ANOVA ANCOVA 12 / 19
  • 22. ANCOVA preliminaries Additive model yij = µ + αi + β(xij − ¯x) + εij Make sure the contrasts are set as before options(contrasts=c("contr.sum", "contr.poly")) Regression One-way ANOVA ANCOVA 12 / 19
  • 23. ANCOVA preliminaries Additive model yij = µ + αi + β(xij − ¯x) + εij Make sure the contrasts are set as before options(contrasts=c("contr.sum", "contr.poly")) Centering the covariate isn’t required, but doing so allow the intercept to be interpretted as the grand mean dietData$ageCentered <- dietData$age - mean(dietData$age) Regression One-way ANOVA ANCOVA 12 / 19
  • 24. ANCOVA Put the covariate before the treatment variable in the formula. fm3 <- lm(weight ~ ageCentered + diet, dietData) Regression One-way ANOVA ANCOVA 13 / 19
  • 25. ANCOVA Put the covariate before the treatment variable in the formula. fm3 <- lm(weight ~ ageCentered + diet, dietData) summary(fm3) ## ## Call: ## lm(formula = weight ~ ageCentered + diet, data = dietData) ## ## Residuals: ## Min 1Q Median 3Q Max ## -3.8214 -1.2213 -0.2519 1.2161 4.9185 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 27.1396 0.2406 112.787 < 2e-16 *** ## ageCentered 0.5573 0.0594 9.382 5.2e-13 *** ## diet1 -1.7446 0.4238 -4.116 0.00013 *** ## diet2 -0.3758 0.4173 -0.901 0.37171 ## diet3 0.7819 0.4234 1.847 0.07020 . ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 1.864 on 55 degrees of freedom ## Multiple R-squared: 0.6536, Adjusted R-squared: 0.6284 ## F-statistic: 25.94 on 4 and 55 DF, p-value: 4.147e-12 Regression One-way ANOVA ANCOVA 13 / 19
  • 26. The ANOVA table The null hypothesis of no diet effect is rejected, even though it was not rejected before. summary.aov(fm3) ## Df Sum Sq Mean Sq F value Pr(>F) ## ageCentered 1 278.25 278.25 80.095 2.54e-12 *** ## diet 3 82.22 27.41 7.889 0.000182 *** ## Residuals 55 191.07 3.47 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' Regression One-way ANOVA ANCOVA 14 / 19
  • 27. Predict weight Create predictions of weight over a sequences of ages, for every level of diet ageC <- dietData$ageCentered predData3 <- data.frame( diet=rep(c("Control", "Low", "Med", "High"), each=20), ageCentered=rep(seq(min(ageC), max(ageC), length=20), times=4)) pred3 <- predict(fm3, newdata=predData3, se.fit=TRUE, interval="confidence") predictions3 <- data.frame(pred3$fit, predData3) Regression One-way ANOVA ANCOVA 15 / 19
  • 28. Plot the regression lines colrs <- c("black", "royalblue", "orange", "darkcyan") plot(weight ~ ageCentered, dietData, pch=16, cex=1.2, col=rep(colrs, each=15)) lines(fit ~ ageCentered, predictions3, subset=diet=="Control", col=colrs[1], lwd=2) lines(fit ~ ageCentered, predictions3, subset=diet=="Low", lty=1, col=colrs[2], lwd=2) lines(fit ~ ageCentered, predictions3, subset=diet=="Med", lty=1, col=colrs[3], lwd=2) lines(fit ~ ageCentered, predictions3, subset=diet=="High", lty=1, col=colrs[4], lwd=2) legend(4, 23, c("High", "Med", "Low", "Control"), pch=16, title="Diet", lwd=2, col=rev(colrs)) Regression One-way ANOVA ANCOVA 16 / 19
  • 29. Plot the regression lines q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q −6 −4 −2 0 2 4 6 8 202530 ageCentered weight Regression One-way ANOVA ANCOVA 17 / 19
  • 30. Plot the regression lines q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q −6 −4 −2 0 2 4 6 8 202530 ageCentered weight q q q q Diet High Med Low Control Regression One-way ANOVA ANCOVA 17 / 19
  • 31. Multiple comparisons ## install.packages("multcomp") library(multcomp) summary(glht(fm3, linfct=mcp(diet="Tukey"))) ## ## Simultaneous Tests for General Linear Hypotheses ## ## Multiple Comparisons of Means: Tukey Contrasts ## ## ## Fit: lm(formula = weight ~ ageCentered + diet, data = dietData) ## ## Linear Hypotheses: ## Estimate Std. Error t value Pr(>|t|) ## Low - Control == 0 1.3688 0.6875 1.991 0.20389 ## Med - Control == 0 2.5265 0.6973 3.623 0.00336 ** ## High - Control == 0 3.0830 0.6832 4.513 < 0.001 *** ## Med - Low == 0 1.1577 0.6828 1.696 0.33583 ## High - Low == 0 1.7143 0.6817 2.515 0.06861 . ## High - Med == 0 0.5566 0.6869 0.810 0.84931 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## (Adjusted p values reported -- single-step method) Regression One-way ANOVA ANCOVA 18 / 19
  • 32. Assignment Complete the following and upload your R script to ELC before lab next week (1) Fit an ANCOVA model to the data in treeData.csv, which represent the height of trees following a fertilizer experiment. The covariate is pH. (2) Use: options(contrasts=c("contr.sum", "contr.poly")) so that your estimates correspond to the additive model from the lecture notes (3) Interpret each of the estimates from lm. What is the null hypothesis associated with each p-value? (4) Plot the data and the regression lines. Use different colors or symbols to distinguish the treatment groups. (5) Which fertilizer treatments are significantly different? Regression One-way ANOVA ANCOVA 19 / 19