Lesson 11—Two-Sample t tests
By Olivia Mah
First published on April 20, 2019
Perform a two-sample t hypothesis test
To conduct a two-sample t test with a two-sided alternative hypothesis. The following example to test if there is a difference between heights of plants grown with and without fertilizers (see p111 in ).
Here are the null and alternative hypotheses in this example:
\(H_0: \mu_1 = \mu_2 \)
\(H_a: \mu_1 \neq \mu_2 \)
\( \mu_1 \) = population mean height of plants grown without fertilizers
\( \mu_2 \)= population mean height of plants grown with fertilizers
Here are the steps:
Step 1: Enter the data
We create a vector called cont to store heights of plants grown without fertilizers.
cont = c(64.7, 86.6, 67.1, 62.6, 75.1, 83.8, 71.7, 83.4, 90.3, 82.7)
We then create another vector called fert to store heights of plants grown with fertilizers.
fert = c(110.3, 130.4, 114.0, 135.7, 129.9, 98.2, 109.4, 131.4, 127.9, 125.7)
Step 3: Draw boxplots to check data
We draw two boxplots to check if the data are roughly symmetric and without too many extreme outliers:
boxplot(cont, fert, names =c("Control", "Fertilizer"), xlab = "Treatment", ylab = "Plant Height (cm)", main = "Plants with(out) Fertilizer", cex.lab =1.5)
The argument cex.lab magnifies the labels (default value is 1).
Step 4: Run the two-sample t test via the R function t.test
t.test(cont, fert, mu = 0, conf.level = 0.99)
Whenever R runs a hypothesis test, R automatically calculates the corresponding confidence interval —the range of values which the population mean is estimated to lie within.
Given a set of data, the corresponding hypothesis test result and the confidence interval are closely related. Therefore if we want the significance level \(\alpha \) to be 0.01, then we set the argument conf.level = 0.99 because conf.level = 1 – \(\alpha \) .
By default, R automatically sets mu=0 and conf.level = 0.95 even if you don’t explicitly type these arguments. So you can skip typing these arguments into the t.test function if you are testing a two-sided alternative hypothesis with \(\alpha =0.05\).
 Hartvigsen, G. 2014. A Premier in Biological Data Analysis and Visualization Using R. Columbia University Press.
« Lesson 10—Matched-Pairs t tests | COURSE | Lesson 12—The Chi-Square Test for Goodness of Fit »