library(janitor)
library(car)
library(emmeans)
library(lmtest)
library(tidyverse)
options(contrasts = c("contr.sum", "contr.poly"))Homework 7 — ANCOVA
Covers ANCOVA
📥 Ipomopsis data 📥 Answer sheet (.docx)
Book source: Crawley, M.J. (2007). The R Book. Wiley. Chapter 12, Analysis of Covariance (ipomopsis.txt). It is Crawley’s teaching dataset, not a published field record. Not from W&S or Gotelli, so there is no assigned chapter to look up.
The real argument behind it. Scarlet gilia (Ipomopsis aggregata) is browsed by deer and elk. One study claimed browsed plants come back with more fruit than unbrowsed ones, so being eaten is good for them. A second study, Crawley’s own, found the opposite. Read the abstracts after you finish Part 5:
- Paige, K.N. & Whitham, T.G. (1987). Overcompensation in response to mammalian herbivory: the advantage of being eaten. American Naturalist 129: 407–416. doi:10.1086/284645
- Bergelson, J. & Crawley, M.J. (1992). Herbivory and Ipomopsis aggregata: the disadvantages of being eaten. American Naturalist 139: 870–882. doi:10.1086/285362
What we changed: nothing. The columns are Crawley’s: Root, Fruit, Grazing.
The data
40 plants, 20 grazed and 20 ungrazed.
| Column | What it is | Role |
|---|---|---|
Fruit |
fruit dry mass at the end of the season (mg) | response |
Grazing |
Grazed or Ungrazed |
factor |
Root |
diameter of the rootstock at the top (mm), measured before grazing | covariate |
The question: does grazing change how much fruit a plant makes?
How this homework works
You answer the question twice: first with the ANOVA you would have run before this week, then with an ANCOVA. The point is to see what changes between the two, and why. Use THE ANCOVA RECIPE in the 17_ancova.R skeleton.
- Start a new script,
scripts/hw7_ipomopsis.R, and put the data indata/. - Work through the Parts in order.
- Answer the ten ✏️ questions on the answer sheet. Q9 is your final figure.
Part 1 · Read the data
Read data/ipomopsis.csv, run clean_names(), and make grazing a factor with Ungrazed first. Then show the number of plants and the mean fruit in each group.
Part 2 · The ANOVA you would have run last month
Plot fruit by grazing (a boxplot with the points on top). Then fit lm(fruit ~ grazing) and run Anova().
✏️ Q1. Which group made more fruit, and by how much? Is the difference significant? Write the conclusion you would have handed in last month, in one sentence.
Part 3 · Now look at the covariate
Show the mean root diameter in each group. Then plot fruit against root, colored by grazing, with a straight line per group.
✏️ Q2. Which group started out with bigger plants? At the same root diameter, which group sits higher on the scatterplot? Root diameter was measured before grazing. Why does that matter for using it as a covariate?
Part 4 · Test the slopes
Fit lm(fruit ~ root * grazing) and run Anova(type = 3).
✏️ Q3. Report F and p for root:grazing. Are you allowed to fit the additive model?
Part 5 · The ANCOVA
Fit lm(fruit ~ root + grazing) and run Anova(type = 2). Then get the adjusted means with emmeans() and compare them with pairs().
✏️ Q4. Fill in this table:
raw mean fruit adjusted mean fruit
Ungrazed
Grazed
difference (U - G)
✏️ Q5. The ANOVA and the ANCOVA disagree about the direction of the effect. Explain in two or three sentences, using root size, how both can come from the same 40 plants. Which one answers the question “does grazing change fruit production?” Read the two abstracts in the source box: which paper do your results agree with?
✏️ Q6. Compare the residual sum of squares of the ANOVA (Part 2) with the ANCOVA’s. What fraction did root remove?
✏️ Q7. Would you end here? What else could you do to explore the story in more detail and if so does it change the interpretation any?
Part 6 · Check the assumptions
Check the assumptions on the ANCOVA from Part 5: plot() in a 2 × 2 panel, shapiro.test() on the residuals, and bptest(). Then show the root-diameter range in each group.
✏️ Q8. Are the assumptions met? The adjusted means are set at the mean root diameter. Do both groups have plants of that size, or is one group’s mean an extrapolation?
Part 7 · The final figure
One publication-quality figure, with axis labels, a theme and readable sizing:
- the raw points, colored by grazing
- the two parallel fitted lines from the ANCOVA (use
predict()on a grid of root values, orgeom_line()onfitted()) - a dashed vertical line at the mean root diameter
- the two adjusted means on that line, with their confidence intervals
✏️ Q9. Paste your final figure into the answer sheet.
Part 8 · Write it up
✏️ Q10. A results paragraph of 3–4 sentences: the slopes test, then the ANCOVA (F, df, p for root and grazing), then the adjusted means ± SE, saying what root diameter they are adjusted to.
What to turn in
- The answer sheet (Word) with Q1–Q10 answered and your figure pasted in at Q9.
- Your script,
hw7_ipomopsis.R. It must run top to bottom. It is not graded line by line, but an answer your script can’t reproduce earns no credit.
Points: 30, 3 per question. 3 great: correct and fully explained. 2 good: the right idea with a piece missing or a small error. 1 ok: attempted, but mostly wrong or unexplained. 0: missing.