Skip to main content

Homework 4 — Two-Way ANOVA

Covers Analysis of Variance and both Two-Way ANOVA sessions

Do shrubs and trees differ in leaf area? A one-way ANOVA says no. A two-way ANOVA says it depends on where you are, and the answer flips sign between locations.
Author

Bill Perry

📥 Leaf area data 📥 Answer sheet (.docx)

Note📚 Where these data come from

Book source: Quinn, G.P. & Keough, M.J. (2002), Experimental Design and Data Analysis for Biologists, Ch. 9, Box 9-2 (reich.csv).

Original study: Reich, P.B., Walters, M.B., Ellsworth, D.S., et al. (1998). Oecologia 114: 471–482.

What we changed: nothing. The columns are the book’s LOCATION, FUNCTION, LEAFAREA.

The data

58 woody plants from four locations with very different climates. Each plant is a shrub or a tree.

Column What it is Role
LOCATION Newmex, Scarolin, Venezuel, Wiscons factor
FUNCTION Shrub or Tree factor
LEAFAREA leaf area (cm²) response

The question: do shrubs and trees differ in leaf area, and is the difference the same everywhere?

Warning⚠️ Two things about this file
  1. A . marks one missing value, so LEAFAREA reads in as text. Use read_csv(..., na = c("", "NA", ".")).
  2. FUNCTION is a reserved word in R. After clean_names(), rename it: rename(func = \function`)`.

How this homework works

The code is from the Analysis of Variance and Two-Way ANOVA skeletons. Reuse it.

  • Start a new script, scripts/hw4_leaf_area.R, and put the data in data/.
  • Work through the Parts in order.
  • Answer the ten ✏️ questions on the answer sheet. Q9 is your final figure.
library(janitor)
library(car)          # Anova(), leveneTest()
library(emmeans)
library(effectsize)   # eta_squared()
library(tidyverse)

options(contrasts = c("contr.sum", "contr.poly"))   # for Anova(type = 3)

Part 1 · Read the data

Read the file without the na = fix first and check class(leaf_df$LEAFAREA). Then read it again with the fix, clean_names(), rename function to func, drop the missing row, and make location and func factors. Show the number of plants in each cell with count(location, func).

✏️ Q1. What class was LEAFAREA before the fix, and why? How many plants are left? Which cell has the fewest plants and which the most?


Part 2 · Look first

Make a boxplot of leafarea by location, filled by func. Then make an interaction plot: the mean leaf area of each cell, location on the x-axis, one colored line per func (stat_summary(fun = mean, geom = "line") with aes(group = func)).

✏️ Q2. Are the lines in the interaction plot parallel? In which location is the shrub–tree difference biggest, and which way does it go?


Part 3 · Check the assumptions, and fix them

Fit lm(leafarea ~ location * func). Check the residuals with plot() (2 × 2), shapiro.test(residuals()) and leveneTest(). Then refit with log(leafarea) as the response and check again.

✏️ Q3. Which assumption fails on the raw scale? Does the log fix it? Give the Shapiro p for both. Use the log model from here on.


Part 4 · The one-way ANOVA first

Ignore location. Fit lm(log(leafarea) ~ func) and run Anova().

✏️ Q4. Report F, df and p. What would you conclude about shrubs and trees from this alone?


Part 5 · The two-way ANOVA

Run Anova(type = 3) on the log model from Part 3. Then run Anova(type = 2) and plain anova() (Type I) on the same model.

✏️ Q5. Report F, df and p for location, func and location:func from the Type III table. Which terms are significant?

✏️ Q6. Compare the func and location rows across Types I, II and III. Do they change? Why can they change in this design, when they would not in a balanced one?


Part 6 · Where do shrubs and trees differ?

Get the shrub–tree contrast within each location with pairs(emmeans(model, ~ func | location)). Then get partial η² with eta_squared().

✏️ Q7. In which locations do shrubs and trees differ, and in which direction? Use this to explain why func is not significant as a main effect, and why the one-way ANOVA in Part 4 missed the story.

✏️ Q8. Which term has the largest partial η²? The Venezuela result drives the interaction. How many shrubs is that result based on, and how much would you trust it?


Part 7 · The final figure

One publication-quality figure of the model’s means: emmeans(model, ~ func | location, type = "response") gives back-transformed means and 95% CIs. Plot them with location on the x-axis, colored by func, with points and error bars (position_dodge()), axis labels with units, and a theme. Save it with ggsave().

✏️ Q9. Paste your final figure into the answer sheet.

✏️ Q10. Write a 3–4 sentence results paragraph: the transformation and why, the interaction test (F, df, p), and the shrub–tree difference in the locations where it was significant, with the back-transformed means.


What to turn in

  1. The answer sheet (Word) with Q1–Q10 answered and your figure pasted in at Q9.
  2. Your script, hw4_leaf_area.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.