In this analysis we will explore a data set containing “the effect of vitamin C on tooth growth in guinea pigs” and we’ll use hypothesis tests to compare tooth length by supplement type and dose in milligrams.
The R datasets library data set ToothGrowth contains 60 observations on 3 variables:
As a first step in our analysis, we’ll initiate this data set and review it’s structure and some basic statistics.
# Load the datasets library
library(datasets)
# Load the ToothGrowth data set
data("ToothGrowth")
# Review the first 6 observations
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
# View the structure of the data set
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 ...
# Summary statistics
summary(ToothGrowth)
## len supp dose
## Min. : 4.20 OJ:30 Min. :0.500
## 1st Qu.:13.07 VC:30 1st Qu.:0.500
## Median :19.25 Median :1.000
## Mean :18.81 Mean :1.167
## 3rd Qu.:25.27 3rd Qu.:2.000
## Max. :33.90 Max. :2.000
After loading in the data set we see that supp is a factor variable, with half of the observations with the value of “OJ” and the other with “VC”. Additionally we see that our response variable len ranges from 4.20 to 33.90, we aren’t told unit of measurement used for the length but we can assume it’s probably something like millimeters since we’re referring to tooth length, and we know that the dose was recorded using the metric system (e.g. milligrams.) The overall mean for the tooth length is 18.8133333 with a standard deviation of 7.6493152. Looking at the dose attribute we see that it ranges from 0.5 to 2, with a median of 1. Since their are 60 total observations with ten distinct guinea pigs and two distinct supplements, and since this data set was created as part of a well designed experiment, we can infer from the summary statistics that there were only three distinct dose values used as well: {0.5, 1, 2}, let’s double check to see that this is the case.
unique(ToothGrowth$dose)
## [1] 0.5 1.0 2.0
Given that there are only three unique dose levels, we’ll treat the dose level as a factor variable in our analysis. Let’s plot the len by dose and supp and see if there are any differences in tooth length that we might be able to observe.
# load the ggplot2 library
library(ggplot2)
g <- ggplot(data = ToothGrowth, aes(x = factor(dose), y = len, fill = supp))
g <- g + geom_violin()
g <- g + labs(title = "Violin Plot of Tooth Length by Dose and Supplement"
,x = "Dose"
,y = "Tooth Length"
,fill = "Supplement")
g <- g + geom_boxplot(width = 0.1, position = position_dodge(0.9))
g
Our violin plot is similar to a box plot in that it shows the range of values in our numeric data by group, but it also allows us to see the probability density of the numeric values in the same way that a histogram would allow. We’ve superimposed a box plot on top of the violin plot so show that we can review median, inter-quartile range and clearly see if there are any outliers in our data. We can see that there appears to be a positive relationship between tooth length and the dose in milligrams for vitamin C such that as the amount of vitamin C increases, so does the tooth length. Looking at the type of supplement it appears as though “OJ” might be more effective in increasing tooth length at lower does of vitamin C relative to “VC”, but at higher doses of vitamin C the effect of the supplement is no longer present. This may be evidence of an interaction between the dose and the type of supplement. Additionally from the violin plot we see that the shapes of the probability densities look different across the doses and types of supplement combinations, therefore we’ll assume that their variances are different as well (without doing any more rigorous statistical tests) when we perform our hypothesis tests.
From our exploratory analysis we saw that there appeared to be a relationship between tooth length and the amount of vitamin C dose in milligrams. For our first few hypothesis tests we’ll determine if there are statistical differences between the different dose levels for tooth length. Note that even though our data set represents an experiment on the same 10 guinea pigs for each of the dose and supplement combinations, we don’t actually have a variable that tells us which guinea pig the tooth lengths were recorded for, therefore we won’t be able to do a paired t-test. Additionally we observed from the probability density portion of our violin plot that the distributions appear to be different, so we’ll also assume that the variances are not equal.
Hypothesis Test 1:
\(H_0: \mu_{0.5} = \mu_{1}\)
\(H_1: \mu_{0.5} \ne \mu_{1}\)
where \(\mu_{i}\) is the mean tooth length for dose \(i \in \left( 0.5, 1, 2 \right)\)
t.test.1 <- t.test(len ~ factor(dose), paired = FALSE, var.equal = FALSE, conf.level = 0.95, alternative = c("two.sided"), data = subset(ToothGrowth, factor(dose) %in% c("0.5","1")))
t.test.1
##
## Welch Two Sample t-test
##
## data: len by factor(dose)
## t = -6.4766, df = 37.986, p-value = 1.268e-07
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -11.983781 -6.276219
## sample estimates:
## mean in group 0.5 mean in group 1
## 10.605 19.735
We see from this first test that the p-value is 1.268300710^{-7} which is less than 0.05 so we reject the null hypothesis that the mean tooth length is the same for a dose of 0.5 milligrams and a dose of 1.0 milligrams. Based on this and the positive relationship that was found in our violin plot, we can say that a dose of 1.0 milligrams of vitamin C results in statistically significantly higher tooth length than a dose of 0.5 milligrams. Now let’s check on what happens if we compare a dose of 1.0 milligrams with a dose of 2.0 milligrams.
Hypothesis Test 2:
\(H_0: \mu_{1} = \mu_{2}\)
\(H_1: \mu_{1} \ne \mu_{2}\)
where \(\mu_{i}\) is the mean tooth length for dose \(i \in \left( 0.5, 1, 2 \right)\)
t.test.2 <- t.test(len ~ factor(dose), paired = FALSE, var.equal = FALSE, conf.level = 0.95, alternative = c("two.sided"), data = subset(ToothGrowth, factor(dose) %in% c("1","2")))
t.test.2
##
## Welch Two Sample t-test
##
## data: len by factor(dose)
## t = -4.9005, df = 37.101, p-value = 1.906e-05
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -8.996481 -3.733519
## sample estimates:
## mean in group 1 mean in group 2
## 19.735 26.100
Again we see that p-value of our hypothesis test is less than 0.05 and we reject the null hypothesis that the means are equal. Based on this hypothesis test and our observations from the violin plot, we may say that the mean tooth length is statistically significantly greater with a vitamin C dose of 2.0 milligrams than 1.0 milligrams (and transitively that 2.0 milligrams is much greater than 0.5 milligrams). Therefore we may infer that tooth length is positively related to the amount of vitamin C given.
We may now turn our attention to the type of vitamin C supplement. Our next hypothesis test will compare the tooth length mean for the “VC” supplement and the “OJ” supplement, with a null hypothesis that they are equivalent.
Hypothesis Test 3:
\(H_0: \mu_{VC} = \mu_{OJ}\)
\(H_1: \mu_{VC} \ne \mu_{OJ}\)
where \(\mu_{i}\) is the mean tooth length for supplement \(i \in \left( 'VC', 'OJ' \right)\)
t.test.3 <- t.test(len ~ supp, paired = FALSE, var.equal = FALSE, conf.level = 0.95, alternative = c("two.sided"), data = ToothGrowth)
t.test.3
##
## 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
Here the p-value for our hypothesis test is 0.0606345 which is not less than 0.05. Therefore we can not reject the null hypothesis based on this hypothesis test. This implies that overall there is not a statistical difference in the means of tooth length between the two types of vitamin C supplement. However our violin plot seemed to show that there might be some interaction between dose and supp that might indicate that at lower doses, the type of vitamin C supplement might be important. Let’s check to see if there is a statistically significant difference in the mean tooth length if we hold the dose constant at 0.5 milligrams first.
Hypothesis Test 4:
\(H_0: \mu_{VC, 0.5} = \mu_{OJ, 0.5}\)
\(H_1: \mu_{VC, 0.5} \ne \mu_{OJ, 0.5}\)
where \(\mu_{i,j}\) is the mean tooth length for supplement \(i \in \left( 'VC', 'OJ' \right)\) and dose \(j \in \left( 0.5, 1, 2 \right)\)
t.test.4 <- t.test(len ~ supp, paired = FALSE, var.equal = FALSE, conf.level = 0.95, alternative = c("two.sided"), data = subset(ToothGrowth, dose == 0.5))
t.test.4
##
## 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
Holding the vitamin C dose constant at 0.5 milligrams provides a p-value of 0.0063586 which is less than 0.05! Looking at the 95 percent confidence interval from the result we can see that when the dose is held constant of 0.5 the mean tooth length is statistically significantly higher when the type of supplement is “OJ” relative to when it’s “VC”, indicating that our observation of a possible interaction between the type of supplement and the dose of vitamin C. Let’s see if this relationship still holds when we hold the dose constant at 1.0 milligrams now as well.
Hypothesis Test 5:
\(H_0: \mu_{VC, 1} = \mu_{OJ, 1}\)
\(H_1: \mu_{VC, 1} \ne \mu_{OJ, 1}\)
where \(\mu_{i,j}\) is the mean tooth length for supplement \(i \in \left( 'VC', 'OJ' \right)\) and dose \(j \in \left( 0.5, 1, 2 \right)\)
t.test.5 <- t.test(len ~ supp, paired = FALSE, var.equal = FALSE, conf.level = 0.95, alternative = c("two.sided"), data = subset(ToothGrowth, dose == 1))
t.test.5
##
## 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
Again we see that when the dose is held constant at 1.0 milligrams that we must reject the null hypothesis that the tooth length means are equivalent for the two supplements, and again we see that the “OJ” supplement provides a statistically significant greater tooth length relative to “VC” when the dose is held constant at 1.0 milligrams. So what about when the dose is held constant at 2 milligrams? We would expect that we would uphold the null hypothesis in that case based on on our original violin plot which appeared to show that there was no difference between the two supplements at a dose of 2.0, and of course there must be some reason why we couldn’t reject the null hypothesis in our 3rd hypothesis test that compared the tooth length means when the dose was allowed to fluctuate. We’ll perform this final hypothesis test now with the dose held constant at 2.0 milligrams.
Hypothesis Test 6:
\(H_0: \mu_{VC, 2} = \mu_{OJ, 2}\)
\(H_1: \mu_{VC, 2} \ne \mu_{OJ, 2}\)
where \(\mu_{i,j}\) is the mean tooth length for supplement \(i \in \left( 'VC', 'OJ' \right)\) and dose \(j \in \left( 0.5, 1, 2 \right)\)
t.test.6 <- t.test(len ~ supp, paired = FALSE, var.equal = FALSE, conf.level = 0.95, alternative = c("two.sided"), data = subset(ToothGrowth, dose == 2))
t.test.6
##
## 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
Looking at the means alone, we can see that at a dose of 2.0 milligrams there is virtually no difference between the supplements, just as we expected based on our violin plot, and with a p-value of 0.9638516 it is very clear that we cannot reject the null hypothesis and that the difference between the means is not statistically significantly different than zero. The relationship between the tooth length and the type of supplement is so nonexistent at a dose of 2.0 milligrams of vitamin C that it was the reason why we couldn’t reject the null hypothesis earlier when we looked at tooth length and type of supplement alone.
In conclusion we can say that there is a positive relationship between tooth length and dose of vitamin C regardless of the supplement, such that when the level of vitamin C increases, so does the tooth length in guinea pigs. Overall there is not a statistically significant difference in tooth length based on the supplement; however, at lower levels of vitamin C dosage, the supplement “OJ” is more effective at increasing tooth growth than the supplement “VC”, but at higher levels of vitamin C dosage there is not a statistically significant difference in the tooth length.
M.Giglia 11/22/2015 for Johns Hopkins University Bloomberg School of Public Health @ Coursera’s “Statistical Inference” course as part of the Data Science Specialization.