Testing homogeneity of slopes, fitting an ANCOVA, and comparing covariate-adjusted means with emmeans, using the Partridge & Farquhar fruit fly longevity data from lecture.
ANCOVA compares group means (like ANOVA) while adjusting for a continuous covariate (like regression). The covariate soaks up variation that would otherwise sit in the error term, so group differences are easier to detect, and groups are compared at the same covariate value.
The key extra assumption: homogeneity of slopes. The covariate must have the same slope in every group (parallel lines). Test this first with a group × covariate interaction.
Data: Partridge & Farquhar (1981). Male fruit fly longevity (days) under five mating treatments, with thorax length (mm) as the covariate. Bigger flies live longer, so we adjust for size.
Look only at the THORAX:treatment row. p > 0.05 means the slopes do not differ, so we can drop the interaction and fit the ANCOVA. (If it were significant, the treatment effect would depend on size; see the ANCOVA lecture for emtrends().)
Step 4: Fit the ANCOVA
ancova_model <-lm(LONGEV ~ THORAX + treatment, data = partridge)Anova(ancova_model, type ="III")
The residual sum of squares is much larger without thorax length. The covariate removes size-related noise, so the F for treatment is larger in the ANCOVA.
Step 7: Adjusted means and pairwise comparisons
emmeans() gives each treatment’s mean at the average thorax length.
treatment emmean SE df lower.CL upper.CL .group
Eight virgin females daily 41.6 2.12 119 36.0 47.1 a
One virgin female daily 54.5 2.11 119 49.0 60.0 b
No females 61.5 2.11 119 56.0 67.0 bc
One inseminated female daily 64.2 2.10 119 58.7 69.7 c
Eight inseminated females daily 65.4 2.11 119 59.9 71.0 c
Confidence level used: 0.95
Conf-level adjustment: sidak method for 5 estimates
P value adjustment: sidak method for 10 tests
significance level used: alpha = 0.05
NOTE: If two or more means share the same grouping symbol,
then we cannot show them to be different.
But we also did not show them to be the same.
# put R's default contrasts backoptions(contrasts =c("contr.treatment", "contr.poly"))
Step 8: Write it up
anc_aov <-Anova(ancova_model, type ="III")slope_aov <-Anova(homo_slopes_model, type ="III")
After adjusting for thorax length (F1,119 = 119.2, p < 0.001), longevity differed among mating treatments (ANCOVA: F4,119 = 21.8, p < 0.001). Slopes were homogeneous (thorax × treatment: p = 0.98). Males housed with eight virgin females had the shortest adjusted lifespan.
Report the slopes test, both F-tests from the ANCOVA, and the adjusted means (not the raw means).