This study explored the ToothGrowth data in the R datasets package, and performed some basic hypothesis testing.
The necessary libraries are loaded firstly.
library(datasets)
library(ggplot2)
The data is loaded and its basic structure and first servral rows are displayed as the following:
# load data
data(ToothGrowth)
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 ...
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
The data can be summarized as the following: 1) Total Summary, 2) Summary by Group, 3) Unique value of predictors Supplement Type and Dose
# summary
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
by(ToothGrowth$len, INDICES = list(ToothGrowth$dose, ToothGrowth$supp), summary)
## : 0.5
## : OJ
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 8.20 9.70 12.25 13.23 16.18 21.50
## --------------------------------------------------------
## : 1
## : OJ
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 14.50 20.30 23.45 22.70 25.65 27.30
## --------------------------------------------------------
## : 2
## : OJ
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 22.40 24.58 25.95 26.06 27.08 30.90
## --------------------------------------------------------
## : 0.5
## : VC
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.20 5.95 7.15 7.98 10.90 11.50
## --------------------------------------------------------
## : 1
## : VC
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 13.60 15.27 16.50 16.77 17.30 22.50
## --------------------------------------------------------
## : 2
## : VC
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 18.50 23.38 25.95 26.14 28.80 33.90
unique(ToothGrowth$supp)
## [1] VC OJ
## Levels: OJ VC
unique(ToothGrowth$dose)
## [1] 0.5 1.0 2.0
The plots of the numeric summaries are shown as the following:
#boxplot
boxplot <- ggplot(aes(x = factor(dose), y = len), data = ToothGrowth) +
geom_boxplot(aes(fill = factor(dose)))
boxplot <- boxplot +
labs(fill = "Dose of Vatamin C",
x = "Dose of Vitamin C",
y = "Length of Tooth",
title = "Relationship between Vitamin C Dose and Length of Tooth")
print(boxplot)
# plot
plot <- ggplot(aes(x=dose, y = len),
data = ToothGrowth) + geom_point(aes(color = supp))
# connect mean
mean_line <- aggregate(len~., data=ToothGrowth, mean)
plot <- plot +
geom_line(data=mean_line, aes(group=supp, colour=supp))
# labels
plot <- plot +
labs(colour = "Supplement Type",
x = "Dose of Vitamin C",
y = "Length of Tooth",
title = "Relationship between Vitamin C Dose
and Length of Tooth\nBy Supplement Type")
print(plot)
# boxplot 2
boxplot <- ggplot(aes(x = supp, y = len), data = ToothGrowth) +
geom_boxplot(aes(fill = supp))
boxplot <- boxplot +
labs(fill = "Supplement Type",
x = "Supplement Type",
y = "Length of Tooth",
title = "Relationship between Supplement Type
of Vitamin C and Length of Tooth")
print(boxplot)
# boxplot 3
boxplot_by_dose <- ggplot(aes(x = supp, y = len), data = ToothGrowth) +
geom_boxplot(aes(fill = supp)) + facet_wrap(~ dose)
boxplot_by_dose <- boxplot_by_dose +
labs(fill = "Supplement Type",
x = "Supplement Type",
y = "Length of Tooth",
title = "Relationship between Supplement Type of Vitamin C
and Length of Tooth\nBy Does of Vitamin C")
print(boxplot_by_dose)
Hypothesis One: The supplement type of Vitamin C affects the growth of tooth significantly.
Independent T-test is used:
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
According to the result, the null hypothesis can not be rejected. In other words, there is NO evidence that the supplement type of Vitamin C affects the growth of tooth significantly.
Hypothesis Two: The dose of Vitamin C affects the growth of tooth significantly.
ANOVA is used instead of multiple t-tests, so inflated type II error can be avoided.
ToothGrowth$dose <- factor(ToothGrowth$dose)
fit <- aov(len ~ dose, data=ToothGrowth)
summary(fit)
## Df Sum Sq Mean Sq F value Pr(>F)
## dose 2 2426 1213 67.42 9.53e-16 ***
## Residuals 57 1026 18
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The result indicates that the null hypothesis can be rejected. In other words, the dose of Vitamin C affects the growth of tooth significantly.