library(readr)
library(dplyr)
library(ggplot2)
leaves <- read_csv("data/leaf-mass.csv", show_col_types = FALSE)
leaf_plot <- ggplot(leaves, aes(x = leaf_area_cm2, y = dry_mass_mg)) +
geom_point(size = 2) +
labs(x = "Leaf area (cm²)", y = "Leaf dry mass (mg)") +
theme_minimal()
leaf_plot
range(leaves$leaf_area_cm2)Day 20 — A biological association
Lecture slides · Quiz · Weekly revision
Today’s question
Do larger leaves have more dry mass, and how much more per cm² of leaf? Leaf area is easy to measure from a photo, while dry mass means drying and weighing the leaf, so a relation between the two is useful in plant ecology. You fit a line with the same lm() as on day 19, but this time neither variable was set by us: both were measured on leaves as they were found.
data/leaf-mass.csv contains one leaf from each of 18 plants, with its area (leaf_area_cm2) and dry mass (dry_mass_mg); see data/README.md. The data are simulated.
Save today: association-report.Rmd and its HTML, output/day20-leaf-line.png, and transfer-association.Rmd with its HTML. Create association-report.Rmd at the project root and put the code below into it.
Plotting leaf mass against area
Larger leaves tend to be heavier, but leaves of similar area still differ in mass by several mg. The areas range from 2.96 to 14.68 cm².
How much mass per cm²?
Use lm() to find out how much dry mass goes with each extra cm² of leaf area:
leaf_fit <- lm(dry_mass_mg ~ leaf_area_cm2, data = leaves)
coef(leaf_fit)
leaf_plot <- leaf_plot +
geom_abline(intercept = coef(leaf_fit)[1], slope = coef(leaf_fit)[2],
colour = "steelblue")
leaf_plot
ggsave("output/day20-leaf-line.png", leaf_plot, width = 6, height = 4)
The slope is 4.11 mg per cm²: in these plants, a leaf with 1 cm² more area had on average about 4.11 mg more dry mass. The intercept, 9.48 mg, is where the line would cross zero area. No leaf has zero area and the smallest one measured is 2.96 cm², so the intercept only positions the line.
Because leaf area was not assigned to the plants, the line describes how area and mass go together in this sample. Older or better-lit plants might have both larger and heavier leaves, so the line does not show that making a leaf larger would add 4.11 mg.
What mass does the line predict for an 8 cm² leaf?
Use the two coefficients to find out the fitted mass at 8 cm²:
coef(leaf_fit)[1] + coef(leaf_fit)[2] * 8The answer is 42.36 mg: the average mass the line expects for leaves of that size. Single 8 cm² leaves will be somewhat lighter or heavier. A leaf of 80 cm² is far outside the measured areas; the formula would give 338 mg, but nothing tells us that the line continues that far.
Which leaves lie far from the line?
Use residuals() to find out how far each leaf lies above or below the line:
leaves <- leaves |>
mutate(fitted_mass_mg = fitted(leaf_fit),
residual_mg = residuals(leaf_fit))
leaf_residual_plot <- ggplot(leaves, aes(x = leaf_area_cm2, y = residual_mg)) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_point(size = 2) +
labs(x = "Leaf area (cm²)", y = "Observed − fitted mass (mg)") +
theme_minimal()
leaf_residual_plot
Check one by hand: A01 has area 2.96 cm² and mass 21.19 mg, the line predicts 9.48 + 4.11 × 2.96 = 21.64 mg, so its residual is −0.45 mg, just below the line. The largest residuals are A17 (8.42 mg below) and A14 (6.31 mg above). The points lie on both sides of zero without a curve, so a straight line is a fair summary.
Does one leaf decide the slope?
A18 is the largest leaf, at the end of the range, where one point can tilt a line the most. Use a second fit without A18 to find out how much the slope depends on it:
without_a18 <- leaves |> filter(plant_id != "A18")
sensitivity_fit <- lm(dry_mass_mg ~ leaf_area_cm2, data = without_a18)
coef(leaf_fit)
coef(sensitivity_fit)Without A18 the slope is 4.065 instead of 4.110 mg per cm², a small change, so the conclusion does not hinge on one leaf. Nothing in the records says A18 was measured wrongly, so it stays in the main result; the second fit is only a check, and you label it as one.
The association report
In association-report.Rmd, describe the study (one leaf from each of 18 plants, area and dry mass measured, nothing assigned), show the plot with the line and the residual plot, and answer three questions in the conclusion:
- What association did you see?
- What does the slope mean, in numbers with units?
- What would this study need in order to show that leaf area causes mass?
Add one sentence about the A18 check, restart R and render.
“Within the measured range of 2.96 to 14.68 cm², each additional 1 cm² of leaf area was associated with about 4.11 mg more dry mass on average.” Avoid “making the leaf larger causes …”, because area was measured, not changed.
Does the slope change if area is in mm²?
1 cm² is 100 mm². Use mutate(leaf_area_mm2 = 100 * leaf_area_cm2) and a new fit, unit_fit <- lm(dry_mass_mg ~ leaf_area_mm2, data = leaves), to find out whether changing the unit changes the line.
The slope becomes 0.0411 mg per mm², 100 times smaller, while the intercept and every fitted mass stay the same: max(abs(fitted(leaf_fit) - fitted(unit_fit))) is essentially zero. The number changes with the unit; the relation between the leaves does not. Always write the unit next to a slope.
Did larger seedlings stay larger?
In the second seedling study, data/seedlings-transfer.csv, each of 18 plants was measured at the start and seven days later. Create transfer-association.Rmd and use lm(final_mm ~ initial_mm, data = transfer) to find out how strongly a seedling’s final length goes with its initial length. Colour the points by treatment, add the combined line, and make a residual plot.
Initial lengths range from 20.0 to 28.8 mm. The slope is 0.51: a seedling 1 mm longer at the start was on average about 0.5 mm longer at the end. A seedling starting at 24 mm has a fitted final length of 40.0 mm. The colours show that the treated plants lie mostly above the line, so the treatment adds to the pattern. This is a different question from day 17’s, which compared mean growth between the randomly allocated groups; that comparison remains the way to judge the treatment.
Week 4 in one table
At the end of association-report.Rmd, add a table comparing the four analyses of this week, for use in later lab courses:
| Question | Design | Day | What R gives |
|---|---|---|---|
| Did treated plants grow more? | Two independent groups | 16–17 | Mean difference, Welch CI and p-value |
| Did each plant’s rate change? | Same plants measured twice | 18 | Mean change, paired CI and p-value |
| What concentration is in my sample? | Prepared standards | 19 | Calibration line, estimate within range |
| Do two measurements go together? | Two variables measured on each unit | 20 | Slope with units, residuals |
Check your understanding
- Is the slope measured in mg, cm², or mg/cm²? mg/cm².
- Does a strong relationship show that one variable causes the other? No; here area was measured, not changed.
- Would 18 leaves from one plant give 18 independent units? No; leaves of the same plant share its conditions.
Leave with: association-report.Rmd, its HTML, output/day20-leaf-line.png, and transfer-association.Rmd with its HTML.
Next: Day 21 — How a computer works, and how big is your data?.