I recommend getting and reading Discovering Statistics using R by Field. He has a nice section on ANCOVA.
To run ANCOVA in R load the following packages:
car
compute.es
effects
ggplot2
multcomp
pastecs
WRS
If you are using lm
or aov
(I use aov
) make sure that you set the contrasts using the "contrasts" function before doing either aov
or lm
. R uses non-orthogonal contrasts by default which can mess everything up in an ANCOVA. If you want to set orthogonal contrasts use:
contrasts(dataname$factorvariable)=contr.poly(# of levels, i.e. 3)
then run your model as
model.1=aov(dv~covariate+factorvariable, data=dataname)
To view the model use:
Anova(model.1, type="III")
Make sure you use capital "A" Anova
here and not anova
. This will give results using type III SS.
summary.lm(model.1)
will give another summary and includes the R-sq. output.
posth=glht(model.1, linfct=mcp(factorvariable="Tukey")) ##gives the post-hoc Tukey analysis
summary(posth) ##shows the output in a nice format.
If you want to test for homogeneity of regression slopes you can also include an interaction term for the IV and covariate. That would be:
model=aov(dv~covariate+IV+covariate:IV, data=dataname)
If the interaction term is significant then you do not have homogeneity.