Homework 7 — One-Way ANOVA
A dose series R puts in the wrong order
Worksheet: A Dose Series R Puts in the Wrong Order
📥 Daphnia data 📥 Same data (.xlsx) 📥 Tools script (.R) 📥 Script skeleton (.R)
Book source: Whitlock & Schluter, The Analysis of Biological Data — Chapter 15 (Comparing means of more than two groups), end-of-chapter problem 17. The book’s own file is chap15q17DaphniaResistance.csv.
Original study: Daphnia raised across a gradient of toxic cyanobacteria density, scored for resistance to the toxin.
What we changed: the book’s cyandensity column is renamed cyanobacteria_density; values are the book’s. The group labels (low, med, high) are exactly as the book has them — which is the whole point of this homework.
The study. Daphnia were raised at three densities of toxic cyanobacteria — low, med and high — and their resistance to the toxin was measured. Does exposure density change resistance?
How this relates to class. You are running the crab worksheet again, in the same order. Two things are different, and both will bite if you do not notice them:
- the design is unbalanced, so the shortcuts that worked on 21-per-group do not apply
- the three group labels sort into an order that is biologically meaningless
Columns: cyanobacteria_density and resistance.
Before you start
There is no hidden right answer to find. You are graded on whether your conclusions follow from your output, and on whether you notice what R did to your factor before you interpret anything.
Part 1 · Setup and the data
▶ Run this:
# ── Part 1.1 · Setup and the data ───────────────────────────────────────
library(tidyverse)
library(car)
source("themes_functions/r_themes_and_functions.R")
daphnia_df <- read_csv("data/daphnia_resistance.csv") %>%
mutate(cyanobacteria_density = factor(cyanobacteria_density))
count(daphnia_df, cyanobacteria_density)
levels(daphnia_df$cyanobacteria_density)✏️ Your turn: Record the design, then the order R chose.
n per group: low = ______ med = ______ high = ______
Balanced? ______
Levels, in R's order: _______________________________________
Is that the order the experiment was designed in? ______
Part 2 · Fix the order first
In class the reference level was a control. Here there is no control — but there is a natural low-to-high sequence, and R has not used it.
▶ Your turn to write it: put the levels in dose order, lowest first, then check.
# ── Part 2.1 · Fix the order first ──────────────────────────────────────
# Use fct_relevel() to order the levels low, med, high.
# Then print levels() again to confirm.✏️ Your turn: Why does the order matter here even though it will not change the F-test?
____________________________________________________________
____________________________________________________________
Part 3 · Look, then fit
▶ Your turn to write it: a boxplot of resistance by density with the raw points jittered on top, then the ANOVA.
# ── Part 3.1 · Look, then fit ───────────────────────────────────────────
# Plot the data, then fit daphnia_model with lm() and print anova().✏️ Your turn: Report the test.
F = __________ df = ______ and ______ p = __________
R squared = __________
✏️ Your turn: In class, df for the error term was 4(21 − 1) = 80. Why can you not use that shortcut here, and what is the general formula?
____________________________________________________________
Part 4 · Check the assumptions
▶ Your turn to write it: the four diagnostic plots, the Shapiro test on the residuals, and Levene’s test.
# ── Part 4.1 · Check the assumptions ────────────────────────────────────✏️ Your turn: Fill in, and say whether you may proceed with the parametric test.
Shapiro p = __________ Levene p = __________
Proceed with ANOVA? ______ Because: __________________________
Part 5 · Read the coefficients
▶ Your turn to write it: print the coefficient table of your model.
# ── Part 5.1 · Read the coefficients ────────────────────────────────────✏️ Your turn: Write each coefficient out as a sentence in the units of the data.
Intercept = __________ , which is the mean resistance of the ______ group
med = __________ , meaning ________________________________
high = __________ , meaning ________________________________
✏️ Your turn: Refit the model without your relevel — using R’s alphabetical order — and look at the coefficient table again. Do not change anything else.
Which level is the reference now? ______
Write out the "low" coefficient as a sentence:
____________________________________________________________
Which of the two tables would a reader understand faster, and why?
____________________________________________________________
Part 6 · Which densities differ?
▶ Your turn to write it: Tukey’s HSD on the releveled model.
# ── Part 6.1 · Which densities differ? ──────────────────────────────────✏️ Your turn: Fill in all three adjusted p-values.
med - low = __________
high - low = __________
high - med = __________
✏️ Your turn: Describe the pattern in one sentence. Is resistance a steady climb across the three densities, or something else?
____________________________________________________________
Part 7 · The question the ANOVA cannot answer
low, med and high are not three unrelated categories. They are three points on a dose scale, and ANOVA has thrown that ordering away — it would give exactly the same F if you shuffled the labels.
✏️ Your turn: What would you gain by coding density as a number and running a regression instead? What would you lose?
Gain: ______________________________________________________
Lose: ______________________________________________________
✏️ Your turn: Using your Part 6 answer, which of the two approaches better describes what these data actually show? Defend it in two sentences.
____________________________________________________________
____________________________________________________________
Part 8 · Report it, and hand in a figure
✏️ Your turn: Write the results sentence — test, F with both df, p, R², post-hoc method, and the pattern.
____________________________________________________________
____________________________________________________________
▶ Your turn to write it: one publication-quality figure, saved as a PDF.
# ── Part 8.1 · Report it, and hand in a figure ──────────────────────────
# Group means with standard-error bars, in dose order. Title, subtitle,
# axis labels with units, theme_regular(). Save to
# figures/daphnia_anova.pdf with ggsave() at 6 x 4 inches.✏️ Your turn: Check the bars run low → med → high in the PDF. If they do not, what did you forget?
____________________________________________________________
Review and checkpoint
At this point you can:
📤 What to turn in
12_one_way_anova_homework.Rfigures/daphnia_anova.pdf- This worksheet, with your written answers