The response is the length of odontoblasts (teeth) in each of 10 guinea pigs at each of three dose levels of Vitamin C (0.5, 1, and 2 mg) with each of two delivery methods (orange juice or ascorbic acid).
The null hypothesis is that there is no difference in tooth growth between the two supplement types, OJ and VC. Further, we will test that there is no difference in tooth growth between the two supplement types controlling for the dosage level.
Assumptions: The test subjects are assumed to be independent and the data are assumed to be normally distributed.
#set the global options, set the seed and load packages so that the data analysis is reproducible
library(ggplot2)
library(knitr)
opts_chunk$set(echo=TRUE)
set.seed(42)
#Look at the data
data("ToothGrowth")
head(ToothGrowth)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
str(ToothGrowth)
## 'data.frame': 60 obs. of 3 variables:
## $ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
## $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
## $ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth,aes(x=supp, y=len)) + geom_boxplot(aes(fill=dose)) +
xlab( "Supplement (OJ and VC)") +
ylab("Length of Tooth") +
ggtitle("Comparison of Tooth Growth based on Supplement Type and Dosage") +
scale_fill_discrete(name="Dose in \nmg")
The boxplot shows a positive correlation between tooth growth and dosage of supplements.
ggplot(ToothGrowth, aes(x = len, fill = supp, group = dose)) +
geom_bar(color = "green", fill = "blue", binwidth = 1) +
facet_grid(dose ~ supp) +
ggtitle("Growth by dose and for each supplement type") +
xlab("Length of Tooth") +
ylab("Dosage in mg")
There also seems to be a larger difference in tooth growth for the lower dosages of both supplement types.
To compare tooth growth by supplement type, we use the Welch two sample t-test on OJ and VC, with the null hypothesis stating that there is no difference in tooth growth when using OJ or VC.
ggplot(ToothGrowth,aes(x=supp, y=len)) + geom_boxplot(fill="green")
The graph visually implies that there is a difference in tooth growth between OJ and VC, but because there is a large overlapping area in their results the difference is not significant enough to reject the null hypothesis.
#T test including all dosages
t.test(len~supp, data = ToothGrowth)
##
## Welch Two Sample t-test
##
## data: len by supp
## t = 1.9153, df = 55.309, p-value = 0.06063
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.1710156 7.5710156
## sample estimates:
## mean in group OJ mean in group VC
## 20.66333 16.96333
qt(.975, 55.309)
## [1] 2.003793
The t statistic is smaller than critical value of 2.003 and the p-value is greater than \(α\) = 0.05 and therefore we do not reject the null hypothesis that there is no difference in the two supplements when we do not control for supplement dosage amount.
Now we will perform hypothesis tests by supplement types controlling for dosage amount for each of the three dosage levels: 0.5mg, 1.0mg and 2.0mg.
The first test is for dosage level of 0.5mg:
#Subset data by dosage for T tests
toothdose0.5 <- ToothGrowth[ToothGrowth$dose == 0.5,]
t.test(len~supp, data = toothdose0.5)
##
## Welch Two Sample t-test
##
## data: len by supp
## t = 3.1697, df = 14.969, p-value = 0.006359
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 1.719057 8.780943
## sample estimates:
## mean in group OJ mean in group VC
## 13.23 7.98
qt(0.975, 14.969)
## [1] 2.131834
At the 0.5mg dosage level, the t statistic is greater than the critical value of 2.132 and is considered to be significant. The p-value is less than \(α\) = 0.05 and therefore the null hypothesis that there is no difference in growth amounts between the two supplement types at the 0.5mg dosage level is rejected.
The second test is for dosage level of 1mg:
toothdose1 <- ToothGrowth[ToothGrowth$dose == 1,]
t.test(len~supp, data = toothdose1)
##
## Welch Two Sample t-test
##
## data: len by supp
## t = 4.0328, df = 15.358, p-value = 0.001038
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 2.802148 9.057852
## sample estimates:
## mean in group OJ mean in group VC
## 22.70 16.77
qt(0.975, 15.358)
## [1] 2.12713
The t statistic is again greater than the critical value of 2.13 and is considered significant. The p-value is less than \(α\) = 0.05 and therefore the null hypothesis that there is no difference in growth amounts between the two supplement types at the 1.0mg dosage level is rejected.
The final test is for dosage level of 2mg:
toothdose2 <- ToothGrowth[ToothGrowth$dose == 2,]
t.test(len~supp, data = toothdose2)
##
## Welch Two Sample t-test
##
## data: len by supp
## t = -0.046136, df = 14.04, p-value = 0.9639
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -3.79807 3.63807
## sample estimates:
## mean in group OJ mean in group VC
## 26.06 26.14
qt(0.975, 14.04)
## [1] 2.144214
The t statistic is much smaller than the critical value of 2.144 and the p-value is also much higher than \(α\) = 0.05 and therefore the null hypothesis that there is no difference in growth amounts between the two supplement types at the 2.0mg dosage level is not rejected.
pvalue <- t.test(len~supp, data = ToothGrowth)$p.value
conf.interval <- t.test(len~supp, data = ToothGrowth)$conf.int
pvalue0.5 <- t.test(len~supp, data = toothdose0.5)$p.value
conf.interval0.5 <- t.test(len~supp, data = toothdose0.5)$conf.int
pvalue1.0 <- t.test(len~supp, data = toothdose1)$p.value
conf.interval1.0 <- t.test(len~supp, data = toothdose1)$conf.int
pvalue2.0 <- t.test(len~supp, data = toothdose2)$p.value
conf.interval2.0 <- t.test(len~supp, data = toothdose2)$conf.int
| Hypothesis Test | P-value | Confidence Interval |
|---|---|---|
| Overall | 0.0606345 | -0.1710156, 7.5710156 |
| Dose 0.5 mg | 0.0063586 | 1.7190573, 8.7809427 |
| Dose 1.0mg | 0.0010384 | 2.8021482, 9.0578518 |
| Dose 2.0mg | 0.9638516 | -3.7980705, 3.6380705 |
We cannot reject the null hypothesis that there is no difference in tooth growth when given the two different supplements. When we control for dosage, we can reject the null hypothesis that there is no difference between OJ and VC at the 0.5mg and 1.0 mg. Tooth growth is affected by dosage rather than by suppplement type.