The ToothGrowth data from R datasets will be analyzed in this section. There are total of 60 observations and three varibale; 1.Tooth length (len) 2.Supplement type (sup) 3. Dose in milligrams (dose)
library(datasets)
library(ggplot2)
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
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
qplot(supp,len,data=ToothGrowth, facets=~dose, main="Tooth growth of guinea pigs by supplement type and dosage (mg)",xlab="Supplement type", ylab="Tooth length") + geom_boxplot(aes(fill = supp))+theme_bw()+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
According to the plot, as the dosage increases the tooth growth increases. The tooth growth has a linear relationship with dosage in the specific case of the VC. The OJ supplement generally induces more tooth growth than VC except at higher dosage (2.0 mg).
In order to perform the analysis, certain assumptions are required.The variables must be independent and identically distributed.Variances of tooth growth are different when using different supplement and dosage.Tooth growth follows a normal distribution.Hypothesis for the supplement OJ vs VC. In this case, the null hypothesis indicates no difference in tooth growth when using the supplement OJ and VC.The alternate hypothesis indicates more tooth growth when using supplement OJ than VC.Then, the tooth growth will be obtained by supplement type from the data.
OJ = ToothGrowth$len[ToothGrowth$supp == 'OJ']
VC = ToothGrowth$len[ToothGrowth$supp == 'VC']
t.test(OJ, VC, alternative = "greater", paired = FALSE, var.equal = FALSE, conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: OJ and VC
## t = 1.9153, df = 55.309, p-value = 0.03032
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
## 0.4682687 Inf
## sample estimates:
## mean of x mean of y
## 20.66333 16.96333
The p-value (0.03032) is lower than 0.05, so, the null hypothesis will be rejected. This includes that supplement OJ has a greater effect on the tooth growth than supplement VC.
How about more tooth growth when the dosage increases.
doseHalf = ToothGrowth$len[ToothGrowth$dose == 0.5]
doseOne = ToothGrowth$len[ToothGrowth$dose == 1]
doseTwo = ToothGrowth$len[ToothGrowth$dose == 2]
t.test(doseHalf, doseOne, alternative = "less", paired = FALSE, var.equal = FALSE, conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: doseHalf and doseOne
## t = -6.4766, df = 37.986, p-value = 6.342e-08
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf -6.753323
## sample estimates:
## mean of x mean of y
## 10.605 19.735
t.test(doseOne, doseTwo, alternative = "less", paired = FALSE, var.equal = FALSE, conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: doseOne and doseTwo
## t = -4.9005, df = 37.101, p-value = 9.532e-06
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf -4.17387
## sample estimates:
## mean of x mean of y
## 19.735 26.100
Based on the low p-value in both cases, the conclusion can be drawn that a higher dossage results in higher tooth growth.
According to the boxplot, the tooth growth for supplement OJ and VC is similar at dosage 2.0 mg. Is that there is no difference in tooth growth when using the supplement OJ and VC at dosage 2.0 mg? Let’s try another ttest and figure this one out:
OJ2 = ToothGrowth$len[ToothGrowth$supp == 'OJ' & ToothGrowth$dose == 2]
VC2 = ToothGrowth$len[ToothGrowth$supp == 'VC' & ToothGrowth$dose == 2]
t.test(OJ2, VC2, alternative = "two.sided", paired = FALSE, var.equal = FALSE, conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: OJ2 and VC2
## 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 of x mean of y
## 26.06 26.14
With the high p-value (0.9639), the null hypothesis cannot be rejected. Accordingly there is no difference in tooth growth when using supplement OJ and VC at dosage 2.0 mg.