generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
14.Rmd
277 lines (203 loc) · 4.93 KB
/
14.Rmd
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Comparing several means (one-way ANOVA).
**Learning objectives:**
- What ANOVA is
- Build ANOVA from scratch
- testing available functions
## Introduction
This is an introduction to the widely used statistical tool, the `Analysis of Variance (ANOVA)`. Initially developed by Sir Ronald Fisher in the early 20th century, the term ANOVA is somewhat misleading as it primarily deals with investigating differences in means, not variances.
The chapter focuses on the simplest form of ANOVA, known as `one-way ANOVA`, applicable when dealing with multiple groups of observations to discern variations in an outcome variable.
## Example: Clinical Trial
```{r}
library(lsr)
clin.trial <- readRDS("data/clintrial.rds")
str(clin.trial)
```
```{r}
clin.trial
```
We look at the `effect of drug on mood.gain`
```{r}
library(tidyverse)
clin.trial%>%
group_by(drug)%>%
reframe(avg_mood.gain=mean(mood.gain),
sd_mood.gain=sd(mood.gain))
```
```{r}
gplots::plotmeans(formula = mood.gain ~ drug, # plot mood.gain by drug
data = clin.trial, # the data frame
xlab = "Drug Administered", # x-axis label
ylab = "Mood Gain", # y-axis label
n.label = FALSE )
```
## Use ANOVA
The Null hypothesis is:
$$H_0: \text{it is true that } \mu_P=\mu_A=\mu_J$$
While the alternative is:
$$H_0: \text{it is NOT true that } \mu_P=\mu_A=\mu_J$$
The Sample Variance of Y:
$$Var(Y)=\frac{1}{N}\sum_{k=1}^G\sum_{i=1}^{N_k}(Y_{ik}-\bar{Y})^2$$
### Example 2
```{r}
N<- 5 # number of people
G<- 2 # groups
```
```{r}
data2 <- tibble(name=c("Ann","Ben","Cat","Dan","Egg"),
person_p=seq_along(1:5),
group=c("cool","cool","cool","uncool","uncool"),
group_k=c(1,1,1,2,2),
index_i=c(1,2,3,1,2),
grumpiness_Yp=c(20,55,21,91,22)
)
data2
```
$$Var(Y)=\frac{1}{N}\sum_{p=1}^{N}(Y_{p}-\bar{Y})^2$$
## The sum of squares
$$SS_{tot}=\sum_{k=1}^G\sum_{i=1}^{N_k}(Y_{ik}-\bar{Y})^2$$
### Between-group sum of squares
$$SS_{b}=\sum_{k=1}^G\sum_{i=1}^{N_k}(Y_{k}-\bar{Y})^2$$
$$=\sum_{k=1}^G{N_k}(\bar{Y}_{k}-\bar{Y})^2$$
$$SS_w+SS_b=SS_{tot}$$
## The F-test
$$df_b=G-1$$
$$df_w=N-G$$
$$MS_b=\frac{SS_b}{df_b}$$
$$MS_w=\frac{SS_w}{df_w}$$
$$F=\frac{MS_b}{MS_w}$$
## Example3
```{r}
data3 <- tibble(group_k=c("placebo","placebo","placebo","anxifree","anxifree"),
outcome_Yk=c(0.5,0.3,0.1,0.6,0.4),
group_mean=c(0.45,0.45,0.45,0.72,0.72),
mean_dev=outcome_Yk-group_mean,
sqr_dev=mean_dev^2)
data3
```
```{r}
ss_w <- sum(data3$sqr_dev)
ss_w
```
```{r}
outcome <- clin.trial$mood.gain
group <- clin.trial$drug
tibble(outcome,group)
```
```{r}
# ?tapply
gp.means <- tapply(outcome,group,mean)
```
```{r}
gp.means <- gp.means[group]
```
```{r}
dev.from.gp.means <- outcome - gp.means
squared.devs <- dev.from.gp.means ^2
```
```{r}
Y <- clin.trial%>%
group_by(drug)%>%
reframe(mood.gain,
gp.means=mean(mood.gain),
dev.from.gp.means=mood.gain-gp.means,
squared.devs=(mood.gain-gp.means)^2,
sample_size=rep(6,length(drug)))
Y
```
```{r}
ssw<-sum(Y$squared.devs)
ssw
```
```{r}
grand_mean <- mean(Y$mood.gain)
```
```{r}
data4 <- Y%>%
group_by(drug)%>%
reframe(group_mean=mean(mood.gain))%>%
mutate(grand_mean=grand_mean,
deviation=group_mean-grand_mean,
sqr_dev=deviation^2,
sample_size=c(6,6,6),
w_sqr=sample_size*sqr_dev)
data4
```
```{r}
ssb <- sum(data4$w_sqr)
ssb
```
```{r}
ssw;ssb
```
```{r}
G <- 3
N<- 18
dfb <- G-1
dfw <- N-G
```
```{r}
msb <- ssb/dfb
msw <- ssw/dfw
```
F(2,15) = 18.6
```{r}
f <- msb/msw
f
```
?pf()
```{r}
pf( f, df1 = 2, df2 = 15, lower.tail = FALSE)
```
Reject the NULL hypothesis:
```{r}
pf( f, df1 = 2, df2 = 15, lower.tail = FALSE) < 0.05
```
## Testing available functions
?aov()
```{r}
my.anova <- aov( formula = mood.gain ~ drug,
data = clin.trial )
my.anova
```
```{r}
names(my.anova)
```
```{r}
summary(my.anova)
```
```{r}
posthocPairwiseT( my.anova )
```
## Apply a correction for multiple comparisons
### Bonferroni corrections
The corrected p-value is the result of multiply all your raw p-values by m, where m is the number of separate tests. Such as in the previous case we had to compare placebo vs drug1, placebo vs drug2, drug1 vs drug2; so we had 3 tests.
$${p}'=p*m$$
${p}'< \alpha = 0.05$
```{r}
posthocPairwiseT( my.anova,
p.adjust.method = "bonferroni")
```
### Holm corrections
Another method, pretending the tests are done sequentially.
$${p}'_j=j*p_j$$
```{r}
posthocPairwiseT( my.anova )
```
### Normality, Homogeneity of variance and Independence
#### Welch one-way test
F(2,)
```{r}
oneway.test(mood.gain ~ drug, data = clin.trial)
```
```{r}
oneway.test(mood.gain ~ drug, data = clin.trial, var.equal = TRUE)
```
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/URL")`
<details>
<summary> Meeting chat log </summary>
```
LOG
```
</details>