-
Notifications
You must be signed in to change notification settings - Fork 5
/
07-VariationsinDesign.Rmd
391 lines (333 loc) · 12.4 KB
/
07-VariationsinDesign.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# Effect of Varying Designs on Power
```{r echo=FALSE}
load("data/variation_data.RData")
```
Researchers might consider what the effects on the statistical power of their design is, when they add participants. Participants can be added to an additional condition, or to the existing design.
In a one-way ANOVA adding a condition means, for example, going from a 1x2 to a 1x3 design. For example, in addition to a control and intensive training condition, we add a light training condition.
```{r variations_in_design}
string <- "2b"
n <- 50
mu <- c(80, 86)
sd <- 10
labelnames <- c("Condition", "control", "intensive_training")
design_result <- ANOVA_design(design = string,
n = n,
mu = mu,
sd = sd,
labelnames = labelnames)
# Power for the given N in the design_result
power_oneway_between(design_result)$power
power_oneway_between(design_result)$Cohen_f
power_oneway_between(design_result)$eta_p_2
```
```{r eval=FALSE}
simulation_result <- ANOVA_power(design_result,
alpha_level = alpha_level,
nsims = nsims,
verbose = FALSE)
```
```{r echo = FALSE}
knitr::kable(simulation_result_7.1$main_results,
caption = "Simulated ANOVA Result")%>%
kable_styling(latex_options = "hold_position")
```
```{r}
exact_result <- ANOVA_exact(design_result,
alpha_level = alpha_level,
verbose = FALSE)
```
```{r echo = FALSE}
knitr::kable(exact_result$main_results,
caption = "Exact ANOVA Result")%>%
kable_styling(latex_options = "hold_position")
```
We now addd a condition. Let's assume the 'light training' condition falls in between the other two means.
And we can see power across sample sizes
```{r}
# Plot power curve (from 5 to 100)
plot_power(design_result, max_n = 100)
```
```{r}
string <- "3b"
n <- 50
mu <- c(80, 83, 86)
sd <- 10
labelnames <- c("Condition", "control", "light_training", "intensive_training")
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
labelnames = labelnames
)
# Power for the given N in the design_result
power_oneway_between(design_result)$power
power_oneway_between(design_result)$Cohen_f
power_oneway_between(design_result)$eta_p_2
exact_result <- ANOVA_exact(design_result,
alpha_level = alpha_level,
verbose = FALSE)
```
```{r echo = FALSE}
knitr::kable(exact_result$main_results,
caption = "Exact ANOVA Result") %>%
kable_styling(latex_options = "hold_position")
```
We see that adding a condition that falls between the other two means reduces our power. Let's instead assume that the 'light training' condition is not different from the control condition. In other words, the mean we add is as extreme as one of the existing means.
```{r}
# Plot power curve (from 5 to 100)
plot_power(design_result, max_n = 100)
```
```{r}
string <- "3b"
n <- 50
mu <- c(80, 80, 86)
sd <- 10
labelnames <- c("Condition", "control", "light_training", "intensive_training")
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
labelnames = labelnames
)
# Power for the given N in the design_result
power_oneway_between(design_result)$power
power_oneway_between(design_result)$Cohen_f
power_oneway_between(design_result)$eta_p_2
```
Now power has increased. This is not always true. The power is a function of many factors in the design, incuding the effect size (Cohen's *f*) and the total sample size (and the degrees of freedom and number of groups). But as we will see below, as we keep adding conditions, the power will reduce, even if initially, the power might increase.
```{r}
# Plot power curve (from 5 to 100)
plot_power(design_result,max_n = 100)
```
It helps to think of these different designs in terms of either partial eta-squared, or Cohen's *f* (the one can easily be converted into the other).
```{r}
#Two groups
mu <- c(80, 86)
sd = 10
n <- 50 #sample size per condition
mean_mat <- t(matrix(mu, nrow = 2, ncol = 1)) #Create a mean matrix
# Using the sweep function to remove rowmeans from the matrix
mean_mat_res <- sweep(mean_mat, 2, rowMeans(mean_mat))
mean_mat_res
MS_a <- n * (sum(mean_mat_res ^ 2) / (2 - 1))
MS_a
SS_A <- n * sum(mean_mat_res ^ 2)
SS_A
MS_error <- sd ^ 2
MS_error
SS_error <- MS_error * (n * 2)
SS_error
eta_p_2 <- SS_A / (SS_A + SS_error)
eta_p_2
f_2 <- eta_p_2 / (1 - eta_p_2)
f_2
Cohen_f <- sqrt(f_2)
Cohen_f
#Three groups
mu <- c(80, 83, 86)
sd = 10
n <- 50
mean_mat <- t(matrix(mu, nrow = 3, ncol = 1)) #Create a mean matrix
# Using the sweep function to remove rowmeans from the matrix
mean_mat_res <- sweep(mean_mat, 2, rowMeans(mean_mat))
mean_mat_res
MS_a <- n * (sum(mean_mat_res ^ 2) / (3 - 1))
MS_a
SS_A <- n * sum(mean_mat_res ^ 2)
SS_A
MS_error <- sd ^ 2
MS_error
SS_error <- MS_error * (n * 3)
SS_error
eta_p_2 <- SS_A / (SS_A + SS_error)
eta_p_2
f_2 <- eta_p_2 / (1 - eta_p_2)
f_2
Cohen_f <- sqrt(f_2)
Cohen_f
```
The `SS_A` or the sum of squares for the main effect, is 900 for two groups, and the `SS_error` for the error term is 10000. When we add a group, `SS_A` is 900, and the `SS_error` is 15000. Because the added condition falls exactly on the grand mean (83), the sum of squared for this extra group is 0. In other words, it does nothing to increase the signal that there is a difference between groups. However, the sum of squares for the error, which is a function of the total sample size, is increased, which reduces the effect size. So, adding a condition that falls on the grand mean reduces the power for the main effect of the ANOVA. Obviously, adding such a group has other benefits, such as being able to compare the two means to a new third condition.
We already saw that adding a condition that has a mean as extreme as one of the existing groups increases the power. Let's again do the calculations step by step when the extra group has a mean as extreme as one of the two original conditions.
```{r}
#Three groups
mu <- c(80, 80, 86)
sd = 10
n <- 50
mean_mat <- t(matrix(mu, nrow = 3, ncol = 1)) #Create a mean matrix
# Using the sweep function to remove rowmeans from the matrix
mean_mat_res <- sweep(mean_mat, 2, rowMeans(mean_mat))
mean_mat_res
MS_a <- n * (sum(mean_mat_res ^ 2) / (3 - 1))
MS_a
SS_A <- n * sum(mean_mat_res ^ 2)
SS_A
MS_error <- sd ^ 2
MS_error
SS_error <- MS_error * (n * 3)
SS_error
eta_p_2 <- SS_A / (SS_A + SS_error)
eta_p_2
f_2 <- eta_p_2 / (1 - eta_p_2)
f_2
Cohen_f <- sqrt(f_2)
Cohen_f
```
We see the sum of squares of the error stays the same - 15000 - because it is only determined by the standard error and the sample size, but not by the differences in the means. This is an increase of 5000 compared to the 2 group design. The sum of squares (the second component that determines the size of partial eta-squared) increases, which increases Cohen's *f*.
## Within Designs
Now imagine our design described above was a within design. The means and sd remain the same. We collect 50 participants (instead of 100, or 50 per group, for the between design). Let's first assume the two samples are completely uncorrelated.
```{r}
string <- "2w"
n <- 50
mu <- c(80, 86)
sd <- 10
labelnames <- c("Condition", "control", "intensive_training") #
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
labelnames = labelnames
)
power_oneway_within(design_result)$power
```
```{r}
exact_result <- ANOVA_exact(design_result,
alpha_level = alpha_level,
verbose = FALSE)
```
```{r echo = FALSE}
knitr::kable(exact_result$main_results,
caption = "Exact ANOVA Result")%>%
kable_styling(latex_options = "hold_position")
```
We see power is ever so slightly less than for the between subject design. This is due to the loss in degrees of freedom, which is $2(n-1)$ for between designs, and $n-1$ for within designs. But as the correlation increases, the power advantage of within designs becomes stronger.
```{r}
string <- "3w"
n <- 50
mu <- c(80, 83, 86)
sd <- 10
labelnames <- c("Condition", "control", "light_training", "intensive_training")
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
labelnames = labelnames
)
power_oneway_within(design_result)$power
```
```{r}
exact_result <- ANOVA_exact(design_result,
alpha_level = alpha_level,
verbose = FALSE)
```
```{r echo=FALSE}
knitr::kable(exact_result$main_results,
caption = "Exact ANOVA Result")%>%
kable_styling(latex_options = "hold_position")
```
When we add a a condition in a within design where we expect the mean to be identical to the grand mean, we again see that the power decreases. This similarly shows that adding a condition that equals the grand mean to a within subject design does not come for free, but has a power cost.
```{r}
n <- 30
sd <- 10
r <- 0.5
string <- "2w"
mu <- c(0, 5)
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
r = r
)
power_oneway_within(design_result)$power
power_oneway_within(design_result)$Cohen_f
power_oneway_within(design_result)$Cohen_f_SPSS
power_oneway_within(design_result)$lambda
power_oneway_within(design_result)$F_critical
string <- "3w"
mu <- c(0, 0, 5)
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
r = r
)
power_oneway_within(design_result)$power
power_oneway_within(design_result)$Cohen_f
power_oneway_within(design_result)$Cohen_f_SPSS
power_oneway_within(design_result)$lambda
power_oneway_within(design_result)$F_critical
string <- "4w"
mu <- c(0, 0, 0, 5)
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
r = r
)
power_oneway_within(design_result)$power
power_oneway_within(design_result)$Cohen_f
power_oneway_within(design_result)$Cohen_f_SPSS
power_oneway_within(design_result)$lambda
power_oneway_within(design_result)$F_critical
string <- "5w"
mu <- c(0, 0, 0, 0, 5)
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
r = r
)
power_oneway_within(design_result)$power
power_oneway_within(design_result)$Cohen_f
power_oneway_within(design_result)$Cohen_f_SPSS
power_oneway_within(design_result)$lambda
power_oneway_within(design_result)$F_critical
string <- "6w"
mu <- c(0, 0, 0, 0, 0, 5)
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
r = r
)
power_oneway_within(design_result)$power
power_oneway_within(design_result)$Cohen_f
power_oneway_within(design_result)$Cohen_f_SPSS
power_oneway_within(design_result)$lambda
power_oneway_within(design_result)$F_critical
string <- "7w"
mu <- c(0, 0, 0, 0, 0, 0, 5)
design_result <- ANOVA_design(
design = string,
n = n,
mu = mu,
sd = sd,
r = r
)
print(paste0("power: ", power_oneway_within(design_result)$power, "\nCohen's F: ", power_oneway_within(design_result)$Cohen_f,
"\nCohen's F (SPSS): ", power_oneway_within(design_result)$Cohen_f_SPSS,
"\nlambda: ", power_oneway_within(design_result)$lambda,
"\nCritical F: ",
power_oneway_within(design_result)$F_critical
))
# power_oneway_within(design_result)$power
# power_oneway_within(design_result)$Cohen_f
# power_oneway_within(design_result)$Cohen_f_SPSS
# power_oneway_within(design_result)$lambda
# power_oneway_within(design_result)$F_critical
```
This set of designs where we increase the number of conditions demonstrates a common pattern where the power initially increases, but then starts to decrease.
Again, the exact pattern (and when the power starts to decrease) depends on the effect size and sample size.
Note also that the effect size (Cohen's f) decreases as we add conditions, but the increased sample size compensates for this when calculating power.
When using power analysis software such as g\*power [@faul2007g], this is important to realize.
You can't just power for a medium effect size, and then keep adding conditions under the assumption that the increased power you see in the program will become a reality.
Increasing the number of conditions will reduce the effect size, and therefore, adding conditions will not automatically increase power (and might even decrease it).
Overall, the effect of adding conditions with an effect close to the grand mean reduces power quite strongly, and adding conditions with means close to the extreme of the current conditions will either slightly increase of decrease power.