-
Notifications
You must be signed in to change notification settings - Fork 5
/
10-PowerCurves.Rmd
460 lines (360 loc) · 16.1 KB
/
10-PowerCurves.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# Power Curve
```{r include = FALSE}
load("data/powercurve_data.RData")
```
Power is calculated for a specific value of an effect size, alpha level, and sample size. Because you often do not know the true effect size, it often makes more sense to think of the power curve as a function of the size of the effect. Although power curves could be constructed from Monte Carlo simulations (`ANOVA_power`) the `plot_power` function utilizes the `ANOVA_exact` function within its code because these "exact" simulations are much faster. The basic approach is to calculate power for a specific pattern of means, a specific effect size, a given alpha level, and a specific pattern of correlations. This is one example:
```{r}
#2x2 design
design_result <- ANOVA_design(design = "2w*2w",
n = 20,
mu = c(0,0,0,0.5),
sd = 1,
r = 0.5)
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")
```
\pagebreak
We can make these calculations for a range of sample sizes, to get a power curve. We created a simple function that performs these calculations across a range of sample sizes (from n = 3 to max_, a variable you can specify in the function).
```{r}
p_a <- plot_power(design_result,
max_n = 50,
plot = TRUE,
verbose = TRUE)
```
If we run many of these `plot_power` functions across small changes in the ANOVA_design we can compile a number of power curves that can be combined into a single plot. We do this below. The code to reproduce these plots can be found on the [GitHub repository for this book](https://github.com/arcaldwell49/SuperpowerBook/tree/master/data).
\pagebreak
## Explore increase in effect size for moderated interactions.
The design has means 0, 0, 0, 0, with one cell increasing by 0.1, up to 0, 0, 0, 0.5. The standard deviation is set to 1. The correlation between all variables is 0.5.
```{r, fig.height = 7, fig.width = 7, echo = FALSE}
plot1
```
\pagebreak
## Explore increase in effect size for cross-over interactions.
The design has means 0, 0, 0, 0, with two cells increasing by 0.1, up to 0.5, 0, 0, 0.5. The standard deviation is set to 1. The correlation between all variables is 0.5.
```{r, fig.height = 7, fig.width = 7, echo = FALSE}
plot2
```
\pagebreak
## Explore increase in correlation in moderated interactions.
The design has means 0, 0, 0, 0.3. The standard deviation is set to 1. The correlation between all variables increases from 0 to 0.9.
```{r, fig.height = 7, fig.width = 7, echo = FALSE}
plot3
```
\pagebreak
## Increasing correlation in on factor decreases power in second factor
As @potvin2000statistical write:
>The more important finding with respect to the effect of *r* on power relates to the effect of the correlations associated with one factor on the power of the test of the main effect of the other factor. Specifically, if the correlations among the levels of B are larger than those within the AB matrix (i.e., *r*(B) - *r*(AB) > 0.0), there is a reduction in the power for the test of the A effect (and the test on B is similarly affected by the A correlations).
We see this in the plots below. As the correlation of the A factor increases from 0.4 to 0.9, we see the power for the main effect decreases.
```{r, fig.height = 7, fig.width = 7, echo = FALSE}
plot4
```
\pagebreak
## Code to Reproduce Power Curve Figures
```{r eval=FALSE}
nsims = 10000
library(Superpower)
library(pwr)
library(tidyverse)
library(viridis)
string <- "2w*2w"
labelnames = c("A", "a1", "a2", "B", "b1", "b2")
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.0),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_a <- plot_power(design_result,
max_n = 100)
p_a$power_df$effect <- 0
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.1),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_b <- plot_power(design_result,
max_n = 100)
p_b$power_df$effect <- 0.1
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.2),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_c <- plot_power(design_result,
max_n = 100)
p_c$power_df$effect <- 0.2
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_d <- plot_power(design_result,
max_n = 100)
p_d$power_df$effect <- 0.3
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.4),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_e <- plot_power(design_result,
max_n = 100)
p_e$power_df$effect <- 0.4
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.5),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_f <- plot_power(design_result,
max_n = 100)
p_f$power_df$effect <- 0.5
plot_data <- rbind(p_a$power_df, p_b$power_df, p_c$power_df,
p_d$power_df, p_e$power_df, p_f$power_df)%>%
mutate(AxB = `A:B`) %>% select(n,effect,A,B,AxB)
long_data = plot_data %>%
gather("A", "B", "AxB", key = factor, value = power)
plot1 <- ggplot(long_data, aes(x = n, y = power,
color = as.factor(effect))) +
geom_line(size = 1.5) +
scale_y_continuous(breaks = seq(0,100,10)) +
labs(color = "Effect Size",
x = "Sample Size per Group",
y = "Power (%)") +
scale_color_viridis_d() + facet_wrap( ~ factor, ncol=2)
####
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.0),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_a <- plot_power(design_result,
max_n = 100)
p_a$power_df$effect <- 0
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0.1,0,0,0.1),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_b <- plot_power(design_result,
max_n = 100)
p_b$power_df$effect <- 0.1
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0.2,0,0,0.2),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_c <- plot_power(design_result,
max_n = 100)
p_c$power_df$effect <- 0.2
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0.3,0,0,0.3),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_d <- plot_power(design_result,
max_n = 100)
p_d$power_df$effect <- 0.3
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0.4,0,0,0.4),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_e <- plot_power(design_result,
max_n = 100)
p_e$power_df$effect <- 0.4
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0.5,0,0,0.5),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_f <- plot_power(design_result,
max_n = 100)
p_f$power_df$effect <- 0.5
plot_data <- rbind(p_a$power_df, p_b$power_df, p_c$power_df,
p_d$power_df, p_e$power_df, p_f$power_df)%>%
mutate(AxB = `A:B`) %>% select(n,effect,A,B,AxB)
long_data = plot_data %>%
gather("A", "B", "AxB", key = factor, value = power)
plot2 <- ggplot(long_data, aes(x = n, y = power,
color = as.factor(effect))) +
geom_line(size = 1.5) +
scale_y_continuous(breaks = seq(0,100,10)) +
labs(color = "Effect Size",
x = "Sample Size per Group",
y = "Power (%)") +
scale_color_viridis_d() + facet_wrap( ~ factor, ncol=2)
##
string <- "2w*2w"
labelnames = c("A", "a1", "a2", "B", "b1", "b2")
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r = 0.0,
labelnames = labelnames)
p_a <- plot_power(design_result,
max_n = 100)
p_a$power_df$correlation <- 0.0
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r = 0.1,
labelnames = labelnames)
p_b <- plot_power(design_result,
max_n = 100)
p_b$power_df$correlation <- 0.1
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r = 0.3,
labelnames = labelnames)
p_c <- plot_power(design_result,
max_n = 100)
p_c$power_df$correlation <- 0.3
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r = 0.5,
labelnames = labelnames)
p_d <- plot_power(design_result,
max_n = 100)
p_d$power_df$correlation <- 0.5
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r = 0.7,
labelnames = labelnames)
p_e <- plot_power(design_result,
max_n = 100)
p_e$power_df$correlation <- 0.7
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r = 0.9,
labelnames = labelnames)
p_f <- plot_power(design_result,
max_n = 100)
p_f$power_df$correlation <- 0.9
plot_data <- rbind(p_a$power_df, p_b$power_df,
p_c$power_df, p_d$power_df, p_e$power_df, p_f$power_df)%>%
mutate(AxB = `A:B`) %>% select(n,correlation,A,B,AxB)
long_data = plot_data %>%
gather("A", "B", "AxB", key = factor, value = power)
plot3 <- ggplot(long_data, aes(x = n, y = power,
color = as.factor(correlation))) +
geom_line(size = 1.5) +
scale_y_continuous(breaks = seq(0,100,10)) +
labs(color = "Correlation",
x = "Sample Size per Group",
y = "Power (%)") +
scale_color_viridis_d() + facet_wrap( ~ factor, ncol=2)
##############
string <- "2w*2w"
labelnames = c("A", "a1", "a2", "B", "b1", "b2")
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r <- c(
0.4, 0.4, 0.4,
0.4, 0.4,
0.4),
labelnames = labelnames)
p_a <- plot_power(design_result,
max_n = 100)
p_a$power_df$corr_diff <- 0
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r <- c(
0.5, 0.4, 0.4,
0.4, 0.4,
0.5),
labelnames = labelnames)
p_b <- plot_power(design_result,
max_n = 100)
p_b$power_df$corr_diff <- 0.1
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r <- c(
0.6, 0.4, 0.4,
0.4, 0.4,
0.6),
labelnames = labelnames)
p_c <- plot_power(design_result,
max_n = 100)
p_c$power_df$corr_diff <- 0.2
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r <- c(
0.7, 0.4, 0.4,
0.4, 0.4,
0.7),
labelnames = labelnames)
p_d <- plot_power(design_result,
max_n = 100)
p_d$power_df$corr_diff <- 0.3
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r <- c(
0.8, 0.4, 0.4,
0.4, 0.4,
0.8),
labelnames = labelnames)
p_e <- plot_power(design_result,
max_n = 100)
p_e$power_df$corr_diff <- 0.4
design_result <- ANOVA_design(design = string,
n = 20,
mu = c(0,0,0,0.3),
sd = 1,
r <- c(
0.9, 0.4, 0.4,
0.4, 0.4,
0.9),
labelnames = labelnames)
p_f <- plot_power(design_result,
max_n = 100)
p_f$power_df$corr_diff <- 0.5
plot_data <- rbind(p_a$power_df, p_b$power_df, p_c$power_df,
p_d$power_df, p_e$power_df, p_f$power_df)%>%
mutate(AxB = `A:B`) %>% select(n,corr_diff,A,B,AxB)
long_data = plot_data %>%
gather("A", "B", "AxB", key = factor, value = power)
plot4 <- ggplot(long_data, aes(x = n, y = power,
color = as.factor(corr_diff))) +
geom_line(size = 1.5) +
scale_y_continuous(breaks = seq(0,100,10)) +
labs(color = "Difference in Correlation",
x = "Sample Size per Group",
y = "Power (%)") +
scale_color_viridis_d() + facet_wrap( ~ factor, ncol=2)
```