-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatisticalInferencePart2.Rmd
More file actions
108 lines (73 loc) · 6.59 KB
/
StatisticalInferencePart2.Rmd
File metadata and controls
108 lines (73 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
---
title: "Statistical Inference Part 2.Rmd"
author: "Samuel Shaw"
date: "9/13/2017"
output: pdf_document
---
## Part II: Inferential Data Analysis
### Summary
In part one I discussed the Law of Large Numbers and the Central Limit Theorem as the basis for statistical inference. That is, because of the normality of sample statistics, we can use the properties of the normal distribution to test relationships between variables. When *n* is relatively small, however, we can still use the assumption that sample statitics are normally distributed, but we need to figure more room for error in our estimates, hence we can use the t distribution (which approximates a normal distribution as n increases). In this report, I will use *t* tests to examine how tooth growth varies by supplement and by the size of its dose.
```{r}
library(datasets)
str(ToothGrowth)
```
Our dataset contains a total of 60 observations; 30 for each supplement (OJ, and VC) and 20 for each dose size (.5, 1, and 2). There are only 10 observations in each dose-supplement category.
### Summary of the Data
```{r}
summary(ToothGrowth$len)
```
In this dataset, tooth length varies widely, from 4.2 to 33.9, but does that variation depend on the supplement given and the size of the dose?
```{r, fig.height=4}
with(ToothGrowth, tapply(len, supp, mean))
with(ToothGrowth, tapply(len, dose, mean))
```
"OJ" is associated with a higher average length than "VC" (mean=20.66 vs. 16.96), but "VC" produces more extreme values on the lower and higher ends of the distribution.
The means tooth length increase for each increase in dose. We can visualize these data using boxplots.
```{r, fig.height=3}
par(mfrow=c(1,2), mar=c(4,4,2,1))
boxplot(ToothGrowth$len~ToothGrowth$supp, col="lightblue",
main="ToothGrowth by Supplement", cex.main=.8, xlab="Supplement", cex.lab=.8)
boxplot(ToothGrowth$len~ToothGrowth$dose, col="lightblue",
main="ToothGrowth by Dose", cex.main=.8, xlab="Dose", cex.lab=.8)
```
### Means Comparisons using T-tests
We can test whether or not the observed differences are due to random chance by determining the probability of those differences using t-tests. The following set of tests make use of the two-sample t.test function in R. We will set alpha = to .05 (the default), so that we will reject the null hypothesis (that the distributions are equal) if p-values are >.05 for two-tailed tests.
#### Supplement
Hypothesis 1 (Ha): Tooth length differs depending on the supplement provided (H0: there is no difference)
```{r}
with(ToothGrowth, t.test(len[supp=="OJ"], len[supp=="VC"]))
```
The output provides several useful statistics. Notably, it tells us that we are testing the difference between the means 20.66 and 16.96, given the number of observations and the variances of each. The test statistic, 1.92 is the standard error under the t-distribution with 55.3 degrees of freedom. Within our 95% confidence interval (setting alpha = 0.05), the difference in means is not enough standard deviations away to safely reject the null hypothesis (it crosses zero). The p-value is 0.06, which tells us that we can expect to observe as much difference or more in our distributions due to random noise alone about six out of 100 times. We must conclude that that the type of supplement is not associated with a difference in tooth length.
#### Dose
Hypothesis 2 (Ha): Tooth length depends on the size of the dose (of either supplement). (H0: There is no difference). Because the our data show us that mean tooth length increases when we increase the dose, we could use a one.tailed test, hypothesizing that increase in dose leads to increase in tooth growth. But I will opt for the two.sided test, which will provide a more conservative estimate.
```{r}
with(ToothGrowth, t.test(len[dose==1], len[dose==.5]))
with(ToothGrowth, t.test(len[dose==2], len[dose==1]))$p.value
```
In the first test, we compare a dose of .5 to a dose of 1. The mean tooth lengths are 10.6 and 19.7 respectively. Notably, we observe a p-value of 0.00000012, which represents the probability that the difference in means is due to random chance if the null hypothesis is actually true. In the second test, we compare tooth length under a dose of 1 (mean = 19.7) to tooth length under a dose of 2 (mean = 26.1). Again, the p-value is a very small number, indicating very little change that the difference could be due to chance. In both cases we can safely reject the null hypothesis and conclude that toothlength is strongly associated with the size of dose (of whatever supplment) that is given.
### 3-way Means Comparisons
Finally, although tooth length does not differ by supplement, we may ask whether or not different doses within supplements produce a difference. The means of supplment-dose categories are tabled below.
```{r}
means<-xtabs(len/10~supp+dose, data=ToothGrowth)
knitr::kable(means, caption="Tooth Length Means for Supplement and Dose")
```
It may be that tooth lenght actually does vary by supplement, until we reach high doses.
Hypothesis 3 (Ha): The non-difference in the effect of supplement (see Hypothesis 1) may be explained by the size of the dose.
First, we will test if there is a tooth length difference by supplement for those given a low dose. The means (OJ = 13.23, VC = 7.98) appear different, but the number of observations is also much smaller (only 10 for each category), so now our test must figure-in more chance of random error. In the following tests, I report only p-values to save space.
```{r}
low<-ToothGrowth[ToothGrowth$dose==.5,]
with(low, t.test(len[supp=="OJ"], len[supp=="VC"]))$p.value
```
Although there are only 15 degrees of freedom (lots of room for error), our test returns a p-value of 0.006, which is less than .05. We can reject the null hypothesis that there is no difference between OJ and VC at low doses.
```{r}
mid<-ToothGrowth[ToothGrowth$dose==1,]
with(mid, t.test(len[supp=="OJ"], len[supp=="VC"]))$p.value
```
The same pattern holds true for medium doses as well.
```{r}
high<-ToothGrowth[ToothGrowth$dose==2,]
with(high, t.test(len[supp=="OJ"], len[supp=="VC"]))$p.value
```
But not for high doses.
### Conclusion
Variation in tooth length is related to the size of given dose of supplement. The greater the dose, the greater the tooth length (Hyp 2). Tooth length is also related to the type of supplement, but not at all dose sizes (Hyp 3). The tests that we conducted made use of properties associated with the central limit theorem and its assumption of normally distributed sample means. Although the *n*s were relatively small in the tests conducted, t.tests (using the t distribution) allow for greater random chance associated with smaller sample sizes.