comparison <- t.test(treated, control, var.equal = FALSE,
alternative = "two.sided", conf.level = 0.95)
comparisonDay 17 — A guided Welch t-test
Lecture slides · Quiz · Weekly revision
Today’s question
On day 16 the treated seedlings grew on average 6.22 mm more than the controls. A second experiment with 24 new seedlings would give a different number, so how much should we trust 6.22 mm? Today you add a Welch two-sample t-test to comparison.Rmd. It gives the two numbers that every results section reports next to an estimate: a 95% confidence interval and a p-value.
Open comparison.Rmd and run its chunks, so that seedlings, treated and control exist. The study design is described in data/README.md.
Save today: comparison.Rmd and its HTML, day17-checks.R, and an updated transfer-comparison.Rmd with its HTML.
Does the Welch test fit this study?
Each value in treated is the growth of one plant, and each value in control is the growth of another plant. The plants were randomly allocated to the two groups, so the groups are independent, which is what Welch’s test needs. The treated plants vary more (SD 4.87 mm) than the controls (SD 3.19 mm); that is fine, because Welch’s test does not assume equal spread. Look at the day 16 plot once more for a single plant far away from all others. There is none here, so the test can go ahead.
Running the test
t.test() subtracts the second vector from the first, so the result describes treated minus control, the direction we chose on day 16. var.equal = FALSE selects Welch’s method (it is also R’s default; writing it shows the reader which test you used), alternative = "two.sided" counts a difference in either direction, and conf.level = 0.95 asks for a 95% interval. The R documentation for t.test lists all arguments.
You should see, among other lines:
t = 3.7012, df = 18.986, p-value = 0.001517
95 percent confidence interval:
2.700984 9.732349
sample estimates:
mean of x mean of y
21.49167 15.27500
Reading the output
| R prints | Value | Units | Meaning |
|---|---|---|---|
mean of x, mean of y |
21.49, 15.28 | mm | Mean growth of treated and control plants |
| (subtract them) | 6.22 | mm | The estimate: treated − control |
95 percent confidence interval |
2.70 to 9.73 | mm | Uncertainty in the mean difference |
t, df |
3.70, 19.0 | none | Used to calculate the p-value |
p-value |
0.0015 | none | How surprising the data would be if the means were equal |
Read the result in this order. The estimate says that treated plants grew 6.22 mm more on average in our sample. The confidence interval says that population mean differences from about 2.70 to 9.73 mm fit the data; because the whole interval lies above zero, the data point to more growth with the treatment. The interval is about the mean difference, not about single plants: several control plants grew more than several treated plants. If the whole experiment were repeated many times, about 95% of such intervals would contain the true difference.
The p-value answers a narrower question: if treated and control plants had the same mean growth, how often would a difference at least this large appear by chance? About 0.15% of the time, so rarely. This is not the probability that the treatment has no effect, and it says nothing about whether 6 mm matters for a seedling of about 20 mm. That judgement needs the estimate and biology.
Writing the result in the report
Use $ to take numbers out of the result instead of retyping them, so the report stays correct if the data change:
mean(treated) - mean(control)
comparison$conf.int
comparison$p.valueIn comparison.Rmd, add a Methods sentence (two independent groups, growth = final − initial, treated minus control, Welch two-sample t-test) and a Results paragraph such as:
Treated seedlings grew on average 6.22 mm more than controls (21.49 mm versus 15.28 mm, 12 plants per group; 95% confidence interval 2.70 to 9.73 mm; Welch two-sample t-test, p = 0.0015).
End with one sentence on the biological size of the difference and one on the limits of this small simulated study. Keep the individual-point plot. Restart R and render.
Zero means “no difference in mean growth”. An interval from, say, −1 to 5 mm contains both small negative and moderate positive differences, so the study cannot tell the direction. Report it as inconclusive, not as “no effect”.
Does the order of the groups matter?
In a new script, day17-checks.R, use t.test(control, treated) to find out whether swapping the two groups changes the conclusion.
The interval becomes −9.73 to −2.70 mm: the same numbers with the opposite sign, because it now describes control minus treated. The p-value stays 0.0015. The data have not changed, only the direction in which we describe them, so keep the treated-minus-control version in the report.
How sure is “95% sure”?
In day17-checks.R, use t.test(treated, control, conf.level = 0.90) and conf.level = 0.99 to find out how the range of plausible differences changes when you ask for more or less confidence.
90%: 3.31 to 9.12 mm. 95%: 2.70 to 9.73 mm. 99%: 1.41 to 11.02 mm. The more confident you want to be that the interval contains the true difference, the wider it must be. The means do not change. Keep 95% in the report: choose the level before you look, not after.
Correcting wrong sentences
Each of these sentences, all common in student reports, contains one error. Write a corrected version of each in day17-checks.R as a comment.
- “There is a 0.15% probability that the treatment has no effect.”
- “95% of treated plants grew between 2.70 and 9.73 mm more.”
- “Another study found a 2 mm difference with a 95% interval of −1 to 5 mm, so the treatment does nothing.”
- The p-value is calculated assuming equal means: if the means were equal, a difference this large would appear in about 0.15% of experiments.
- The interval is for the mean difference, not for individual plants.
- The interval includes zero but also differences of up to 5 mm, so the study could not determine the direction. It is inconclusive, not evidence of no effect.
A second class’s data
Another class repeated the experiment with nine plants per group (data/seedlings-transfer.csv; you summarised it on day 16 in transfer-comparison.Rmd). Use t.test(new_treated, new_control) to find out whether treated plants also grew more in this study, and how precisely the difference is estimated. Create the two vectors from transfer the same way you created treated and control, then add the test and a results sentence to transfer-comparison.Rmd and render it.
Mean growth was 17.87 mm (treated) and 13.66 mm (control): a difference of 4.21 mm, 95% CI 1.95 to 6.47 mm, Welch p = 0.0012. This interval is narrower than the first study’s (7.0 mm wide against 4.5 mm), even though each group has three plants fewer, because these plants vary much less (SD 1.94 and 2.53 mm against 4.87 and 3.19 mm). Precision depends on both sample size and spread.
Check your understanding
- Which numbers have units of mm: the difference, the interval, or the p-value? The difference and the interval; a p-value has no unit.
- Does the SD of a group describe the uncertainty in the difference? No; SD describes how much single plants vary.
- If all treated plants had grown in one shared pot and all controls in another, would this test still fit? No; the plants would no longer be independent units.
Leave with: comparison.Rmd and its HTML reporting the estimate, interval, p-value and sample sizes; day17-checks.R; and transfer-comparison.Rmd with its HTML.