Day 23 — plan and analyse a small biological study

Lecture slides · Quiz · Weekly revision

Today’s question

Duckweed is a small aquatic plant that floats as tiny leaf-like fronds. In our simulated study, twenty flasks started with equal amounts of duckweed; ten were randomly given extra nutrients and ten were controls. After seven days the dry biomass in each flask was weighed. How much more biomass did the nutrient flasks produce, and how certain is that difference?

Today you answer this on your own, from the raw file to a rendered report, using what you learned in weeks 2–4. The flask is the independent unit: the fronds in one flask share its water and nutrients. One nutrient flask, F17, was spilled before weighing, so its biomass is missing. The full design is in data/README.md.

If your teacher chooses the organic-laboratory study instead, skip to The organic alternative and do that one only.

Save today: duckweed-report.Rmd and its HTML, reports/study-decisions.txt, algae-transfer.R and reports/algae-transfer.txt.

A plan before the code

Before writing code, write five lines in reports/study-decisions.txt: the question, the independent unit, the outcome and its units, what you will do with the missing F17, and the direction of the comparison (nutrient minus control). These decisions shape every later step, and writing them first keeps the code and the text in agreement.

Is the file what the design says?

Use Save As on the root report.Rmd to create duckweed-report.Rmd, replace the title and the seedling text, and load the data:

library(readr)
library(dplyr)
library(ggplot2)
duckweed <- read_csv("data/duckweed.csv", show_col_types = FALSE)
glimpse(duckweed)

Use count(duckweed, treatment) to find out how many flasks are in each group, anyDuplicated(duckweed$flask_id) to find out whether any flask appears twice, and sum(is.na(duckweed$biomass_mg)) to find out how many flasks have no biomass. Write each result in the report as a sentence, for example “20 flasks, 10 per group, no duplicates; F17 has no biomass because it was spilled.”

20 rows, 10 control and 10 nutrient, anyDuplicated() returns 0, and one missing biomass value (F17). The biomass values run from 19.8 to 46.2 mg.

How much biomass is in each group?

Use group_by() and summarise() to find out the number of flasks with a measurement, the mean and the SD in each group. Then plot every flask:

available <- duckweed |>
  filter(!is.na(biomass_mg))

available holds the 19 flasks with a measurement, while duckweed keeps F17’s row. Make an individual-point plot of available with treatment on the x-axis and biomass_mg on the y-axis, with the label “Dry biomass after seven days (mg)”, and state in the caption that F17 is missing.

In summarise(), use n_available = sum(!is.na(biomass_mg)), mean_mg = mean(biomass_mg, na.rm = TRUE) and sd_mg = sd(biomass_mg, na.rm = TRUE). For the plot, reuse the week 3 point plot with horizontal jitter only.

Control: 10 flasks, mean 32.28 mg, SD 5.70 mg. Nutrient: 9 flasks, mean 39.63 mg, SD 4.79 mg. The groups overlap: nutrient flask F11 (31.7 mg) is below most controls, and control F08 (19.8 mg) is low but has no recorded error.

How large is the nutrient effect?

The flasks are independent and were randomly allocated, so the Welch test from day 17 fits. Use t.test() to find out the nutrient-minus-control difference and its 95% confidence interval:

nutrient <- available |>
  filter(treatment == "nutrient")
control <- available |>
  filter(treatment == "control")
comparison <- t.test(nutrient$biomass_mg, control$biomass_mg)
comparison

Nutrient comes first, so the result is nutrient minus control.

Difference 7.35 mg (39.63 − 32.28), 95% CI 2.27 to 12.44 mg, p = 0.0072. The whole interval lies above zero, so the nutrient flasks produced more biomass on average.

Writing the result

Write a paragraph with the direction and size of the difference in mg, the number of flasks in each group, the confidence interval and p-value, and one limitation, such as the missing F17 or the small size of the study. Use an inline result, for example `r round(comparison$conf.int[1], 2)`, for at least one number, so the text updates itself when the data change.

A classmate wrote this draft. Find what is wrong with it before you write yours:

“There were 20 biomass results. The nutrient plants all grew more, and the group SD is the error in the treatment effect. The p-value tells us the probability that nutrients work.”

There are 20 flasks but 19 measurements, because F17 is missing. The groups overlap, so only the mean is higher, not every flask. The SD describes how much single flasks vary; the uncertainty of the difference is the confidence interval. The p-value is calculated assuming no difference: it says how surprising the data would be if nutrients had no effect, not how likely it is that they work.

Save the report, restart R and render. Before you finish, check:

  • Can a reader find the question, the flask as the unit, and why there are 19 values?
  • Do the plot, the table and the text give the same numbers?
  • Does the sign of the difference agree with the order in t.test()?
  • Does the report run without anything typed in the Console first?

The organic alternative

Do this study instead of the duckweed report if your teacher selects it. Twelve synthesis runs, procedures that make a chemical product, were randomly assigned a standard or a modified collection protocol, six each. For each run, the file gives the maximum possible product mass (theoretical_mass_g) and the dry product actually collected (isolated_mass_g). Runs started at different scales, so grams cannot be compared directly, but the percentage yield can:

yield (%) = 100 × isolated mass (g) / theoretical mass (g)

Question: which protocol collected a larger percentage of the possible product? Save the report as organic-study.Rmd; if you made organic-report.Rmd on day 15, keep it and use Save As.

library(readr)
library(dplyr)
library(ggplot2)
organic <- read_csv("data/organic-recovery.csv", show_col_types = FALSE)
organic <- organic |>
  mutate(yield_percent = 100 * isolated_mass_g / theoretical_mass_g)

Check Y01 by hand: 0.62 g of 1.00 g possible is 62%. Then use group_by() and summarise() to find out the mean yield and SD in each protocol, and plot every run. Each run is one independent unit; weighing a product three times would still be one run. Save a summary CSV and the plot in output/ with names starting organic-. A descriptive comparison is all this study needs: there is no test today. Write a result paragraph and render.

Six runs per protocol, no missing values. Standard: mean 68.67%, SD 4.32 percentage points. Modified: mean 77.00%, SD 3.74. The modified protocol collected 8.33 percentage points more on average. That is a difference of percentage points, not an 8.33% relative increase. Purity was assumed, not measured, so a higher yield says nothing about how pure the product is.

A new dataset: algae cultures

Can you apply the same steps to data you have not seen before? In data/algae.csv, 24 algae cultures were grown separately and randomly given a reference or a supplement treatment, twelve each, and harvested after seven days. Culture A19 was spilled before weighing. The outcome is dry biomass in mg.

Write a short script, algae-transfer.R, that imports the file and uses the same functions as above to find out:

  1. how many cultures have a measurement in each group;
  2. the mean and SD of biomass in each group, and the supplement-minus-reference difference;
  3. whether every supplement culture produced more than every reference culture (make an individual-point plot).

Save the summary and plot in output/ with names starting algae-transfer, and write a short result paragraph in reports/algae-transfer.txt. Restart R and run the script from the top. Day 24 will turn it into a report that someone else can reproduce.

24 unique cultures; 12 reference and 11 supplement measurements, with A19 kept as NA. Reference: mean 28.20 mg, SD 7.26 mg. Supplement: mean 33.92 mg, SD 3.37 mg. Supplement minus reference: +5.72 mg. Not every supplement culture is higher: the largest single value, 41.7 mg, is reference culture A11. Putting 0 for A19 would wrongly lower the supplement mean to 31.09 mg.

Leave with: duckweed-report.Rmd (or organic-study.Rmd) and its HTML, reports/study-decisions.txt, algae-transfer.R, and reports/algae-transfer.txt. Note anything that failed.

Next: Day 24 — Reproduce and share your report.