data <- scan(what = list(type=0,hrskey=0,hrspain=0), multi.line=F) 1 60 85 1 72 95 1 61 69 1 50 58 2 54 41 2 68 74 2 66 71 2 59 52 3 56 41 3 56 34 3 55 50 3 51 40 data keyboard <- data.frame(data) rm(data) keyboard$type <- as.factor(keyboard$type) # looking only at the response and factor plot(keyboard$type,keyboard$hrspain,ylab="Pain",xlab="Type",main="Keyboarding by type") # look at the data plot(keyboard$hrskey,keyboard$hrspain,pch=as.numeric(keyboard$type), col=as.numeric(keyboard$type),ylab="Pain",xlab="Keyboarding hours") legend("bottomright",legend=c("Type 1","Type 2","Type 3"),levels(keyboard$type),pch=1:3,col=1:3) title("Keyboarding Study") # standard ANCOVA assuming equal slopes keyboard.lm1 <- lm(hrspain ~ hrskey +type , data=keyboard) anova(keyboard.lm1) summary(keyboard.lm1) # another plot of the data with fitted parallel lines plot(keyboard$hrskey,keyboard$hrspain,pch=as.numeric(keyboard$type), col=as.numeric(keyboard$type),ylab="Pain",xlab="Keyboarding hours") legend("bottomright",legend=c("Type 1","Type 2","Type 3"),levels(keyboard$type),pch=1:3,col=1:3) title("Keyboarding Study with fitted parallel lines") abline(-33.81, 1.82, lty=1, lwd=2, col=1) abline(-33.81 -19.07, 1.82, lty=1, lwd=2, col=2) abline(-33.81 -24.13, 1.82, lty=1, lwd=2, col=3) # calculate adjustedMeans and standard errors from the effects package library(effects) adjustedMeans <- effect("type",keyboard.lm1) adjustedMeans adjustedMeans$se # multiple comparisons using the multcomp package library(multcomp) summary(glht(keyboard.lm1,linfct=mcp(type="Tukey"))) # check assumptions windows() par(mfrow=c(2,2)) plot(keyboard.lm1) par(mfrow=c(1,1)) # check equal slopes assumption keyboard.lm2 <- lm(hrspain ~ hrskey +type +hrskey:type , data=keyboard) anova(keyboard.lm2) # ------------------------------------------------------------------------------ # one way to think about ANCOVA - not for routine use keyboard.lm0 <- lm(hrspain ~ hrskey , data=keyboard) windows() plot(keyboard$type,keyboard.lm0$residuals,ylab="Residuals",xlab="Type",main="Res plot") anova(lm(keyboard.lm0$residuals ~ keyboard$type)) # ------------------------------------------------------------------------------