Doris Chen
2023-01-26
This is the Statistical Inference Course Project Part 2. In this project, we are going to do some basic inferential data analysis with the ToothGrowth data in R datasets package.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.1 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(datasets)
data("ToothGrowth")
First, we check the structure, first few rows and summary of the data.
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
Then, we check the count of subjects taking different dose of different supplements.
ToothGrowth %>%
ggplot(aes(x=dose))+
geom_histogram(aes(fill=supp), binwidth = 0.1)+
facet_wrap(.~ supp)
As can be seen from the plot, the count of subjects taking different doses of different supplements are the same (10 for each).
Last, we check the tooth growth of subjects taking different dose of different supplements.
ToothGrowth %>%
ggplot(aes(x=dose, y=len))+
geom_boxplot(aes(group=dose, fill=supp))+
facet_wrap(.~ supp)
The plot shows that, as the dose increases, the tooth growth increases too.
Basic Summary:
It’s a data frame with 60 observations on 3 variables:
1. Comparing tooth growth by supp
We will do hypothesis test to compare tooth growth with same doses of different supplements.
First, we subset six groups of data by supp and dose for the pairwise comparison.
oj_0.5 <- ToothGrowth[which(ToothGrowth$supp=="OJ" & ToothGrowth$dose==0.5),1]
oj_1 <- ToothGrowth[which(ToothGrowth$supp=="OJ" & ToothGrowth$dose==1),1]
oj_2 <- ToothGrowth[which(ToothGrowth$supp=="OJ" & ToothGrowth$dose==2),1]
vc_0.5 <- ToothGrowth[which(ToothGrowth$supp=="VC" & ToothGrowth$dose==0.5),1]
vc_1 <- ToothGrowth[which(ToothGrowth$supp=="VC" & ToothGrowth$dose==1),1]
vc_2 <- ToothGrowth[which(ToothGrowth$supp=="VC" & ToothGrowth$dose==2),1]
Then we proceed the following hypothesis test:
(1) 0.5 dose of OJ versus 0.5 dose of VC
t.test(oj_0.5, vc_0.5, paired = FALSE, var.equal = FALSE)
##
## Welch Two Sample t-test
##
## data: oj_0.5 and vc_0.5
## 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 of x mean of y
## 13.23 7.98
The p-value=0.006, which is less than 0.05, we reject H0. The 95% confidence interval is 1.72 to 8.78.
(2) 1 dose of OJ versus 1 dose of VC
t.test(oj_1, vc_1, paired = FALSE, var.equal = FALSE)
##
## Welch Two Sample t-test
##
## data: oj_1 and vc_1
## 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 of x mean of y
## 22.70 16.77
The p-value=0.001, which is much less than 0.05, we reject H0. The 95% confidence interval is 2.8 to 9.06.
(3) 2 doses of OJ versus 2 doses of VC
t.test(oj_2, vc_2, paired = FALSE, var.equal = FALSE)
##
## Welch Two Sample t-test
##
## data: oj_2 and vc_2
## 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
The p-value=0.96, which is greater than 0.05, we fail to reject H0. The 95% confidence interval is -3.8 to 3.64. Also, the mean value of the two groups are very close, with the number of 26.06 and 26.14 respectively.
2. Comparing tooth growth by dose
Now we want to analysis the efficacy of dosage independent from the supplement.
First, we subset three groups with different doses of VC/OJ.
d_0.5 <- ToothGrowth[which(ToothGrowth$dose==0.5), 1]
d_1 <- ToothGrowth[which(ToothGrowth$dose==1), 1]
d_2 <- ToothGrowth[which(ToothGrowth$dose==2), 1]
Then we proceed the following hypothesis test:
(1) 0.5 dose versus 1 dose
H0: Meand_0.5 = Meand_1
H1: Meand_0.5 != Meand_1
We assume their variance are not equal.
t.test(d_0.5, d_1, paired=FALSE, var.equal=FALSE)
##
## Welch Two Sample t-test
##
## data: d_0.5 and d_1
## 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 of x mean of y
## 10.605 19.735
The p-value=1.268e-07, which is much smaller than 0.05, we reject the H0. The 95% confidence interval is -11.98 to -6.28.
(2) 0.5 dose versus 2 doses
t.test(d_0.5, d_2, paired=FALSE, var.equal=FALSE)
##
## Welch Two Sample t-test
##
## data: d_0.5 and d_2
## t = -11.799, df = 36.883, p-value = 4.398e-14
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -18.15617 -12.83383
## sample estimates:
## mean of x mean of y
## 10.605 26.100
The p-value=4.398e-14, which is much smaller than 0.05, we reject the H0. The 95% confidence interval is -18.16 to -12.83.
(3) 1 dose versus 2 dose
t.test(d_1, d_2, paired=FALSE, var.equal=FALSE)
##
## Welch Two Sample t-test
##
## data: d_1 and d_2
## 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 of x mean of y
## 19.735 26.100
The p-value=1.906e-05, which is much smaller than 0.05, we reject the H0. The 95% confidence interval is -9 to -3.73.
Based on our assumptions that the groups are independent and the variance are equal, we draw the conclusions from the test as following:
1. Comparing by supplements
2. Comparing by dose