MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
Last week, we explored RStudio’s built-in dataset, ToothGrowth, which
contained information on the tooth length of two groups of guinea pigs that
had each been fed 2 different kinds of supplements (OJ, VC) at 3 different
dosages (0.5, 1.0, 2.0).
We visualized the data and saw
that OJ outperforms VC in
promoting tooth growth in guinea
pigs at the 0.5 and 1.0 dose, but
not so much at the 2.0 dose.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
This observation led us to the formulation of 3 hypotheses. One was already
mentioned in class. Here are the other two :
1. There is more tooth growth when using the OJ supplement versus VC.
2. There is more tooth growth as dosage increases.
3. There is a difference in tooth growth when using supplement OJ and VC
at the 2.0 dosage.
We’re going to look at the code needed to execute t-tests for these other 2
hypotheses to try and better understand how t-testing works.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
dosage amount
Before we do that, let’s look at
each hypothesis one by one
and see how the box plot
communicates them.
First, let’s explore the different
parts of this box plot to get a
better understanding of how it y-axis is x-axis is this is a
tooth length supplement legend
visualizes the dataset. the value increases it shows the category it shows the color
from bottom to top of supplement, OJ or VC of each supplement
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
dosage amount
HQ1: There is more tooth
growth when using the OJ
supplement versus VC.
✼
Can anyone explain
how this is shown in
the plot?
y-axis is x-axis is this is a
Don’t be afraid of being wrong! tooth length supplement legend
the value increases it shows the category it shows the color
from bottom to top of supplement, OJ or VC of each supplement
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
dosage amount
HQ2: There is more tooth
growth as dosage increases.
✼
Can anyone explain
how this is shown in
the plot?
Don’t be afraid of being wrong!
y-axis is x-axis is this is a
tooth length supplement legend
the value increases it shows the category it shows the color
from bottom to top of supplement, OJ or VC of each supplement
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
dosage amount
HQ3: There is a difference in
tooth growth when using
supplement OJ and VC at the
2.0 dosage.
✼
Can anyone explain
how this is shown in y-axis is x-axis is this is a
the plot? tooth length supplement legend
the value increases it shows the category it shows the color
Don’t be afraid of being wrong! from bottom to top of supplement, OJ or VC of each supplement
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
We performed a t-test to validate the first hypothesis : There is more tooth
growth when using the OJ supplement versus VC.
variables being compared alt. hypothesis
> t.test(OJ, VC, alternative="greater",
paired=FALSE, var.equal=FALSE, conf.level=0.95)
same population? same population? defines alpha value
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
Performing the t-test correctly leads us to a p-value of 0.03032. Since this is
less than 0.05, we consider it statistically significant : OJ is better than VC.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
We can perform similar t-tests for the 2nd hypothesis : There is more
ToothGrowth as dosage increases. We will first do 0.5 vs. 1.0 dose and then
1.0 vs. 2.0 dose. To achieve this, we will need to split the dataset again :
> # Split the dataset pt. 2
> doseHalf = ToothGrowth$len[ToothGrowth$dose == 0.5]
> doseOne = ToothGrowth$len[ToothGrowth$dose == 1]
> doseTwo = ToothGrowth$len[ToothGrowth$dose == 2]
Now we have 3 objects — doseHalf, doseOne, and doseTwo — containing all
the data for both supplements per dosage type.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
With these objects, we can perform the two t-tests needed to validate the 2nd
hypothesis. We first see whether doseHalf is less effective than doseOne,
then we see whether doseOne is less effective than doseTwo.
> t.test(doseHalf, doseOne, alternative = "less", paired = FALSE,
var.equal = FALSE, conf.level = 0.95)
> t.test(doseOne, doseTwo, alternative = "less", paired = FALSE,
var.equal = FALSE, conf.level = 0.95)
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
If you perform the first t-test correctly, you should get a p-value of 6.324e-08.
This is a scientific notation — if you run it through an sf calculator, you will get
the value 0.00000006324. This is very much below 0.05.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
The second t-test also has a significant result of 9.532e-06, which is
equivalent to 0.000009532, which means we can confirm that there is indeed
more tooth growth as dosage increases.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
We have one more hypothesis to validate : There is a difference in tooth
growth when using supplement OJ and VC at the 2.0 dosage. Before we can
perform a t-test for it, we will need to split the dataset a third time.
> # Split the dataset pt. 3
> OJ2 = ToothGrowth$len[ToothGrowth$supp == 'OJ' & ToothGrowth$dose == 2]
> VC2 = ToothGrowth$len[ToothGrowth$supp == 'VC' & ToothGrowth$dose == 2]
Here, we are creating two objects — OJ2 and VC2, which contain the data on
each supplement at the 2.0 dose only.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
With these objects established, we can now perform the t-test to validate the
third hypothesis. Something about this code is different, however :
> t.test(OJ2, VC2, alternative = "two.sided", paired = FALSE,
var.equal = FALSE, conf.level = 0.95)
In the alternative argument, which is meant to contain our alternative
hypothesis, we write two.sided instead of less or greater. Instead of a
one-tailed test, we’re performing a two-tailed test.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
One-tailed versus two-tailed?
In the realm of independent t-tests, one-tailed tests allow for the possibility of
an effect in one direction. Two-tailed tests on the other hand test for the
possibility of an effect in two directions — positive and negative.
‣ In hypothesis 1 and 2, we checked if [OJ > VC] and if [2.0 > 1.0] and [1.0 >
0.5]. The effect goes in one direction.
‣ In hypothesis 3, we were determining if there was a difference between
OJ (2.0mg) and VC (2.0mg). We were open to the possibility of the
influence going in either direction.
MODULE 5 MORE ABOUT T-TESTS
Analysis of ToothGrowth
If done correctly, your two-sided t-test should result in a p-value of 0.9639.
Since this is >0.05, we cannot accept the third alternative hypothesis and
must conclude that there is no difference between OJ and VC at the 2.0 dose.