-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_analysis.Rmd
69 lines (56 loc) · 2.03 KB
/
power_analysis.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
---
title: "Power analysis"
output: html_notebook
---
```{r}
library(tidyverse)
library(WebPower)
```
### Minimum effect size detectable with full sample
```{r}
wp.correlation(n = 640, r = NULL, power = .8, alpha = 0.0125, p = 4)
```
### Minimum effect size detectable with a partial sample (25% missing)
```{r}
wp.correlation(n = 480, r = NULL, power = .8, alpha = 0.0125, p = 4)
```
In both cases, a small effect size is detectable (r 0.13 - 0.15)
### Power curve
```{r}
get_power <- function(eff_size){
wp_obj <- wp.correlation(n = seq(440, 640, 20), r = eff_size, power = NULL, alpha = 0.0125, p = 4)
bind_cols(n = wp_obj$n, power = wp_obj$power)
}
power_df <-
tibble(r = seq(0.1, 0.2, 0.01)) %>%
mutate(power = map(r, get_power)) %>%
unnest(power) %>%
mutate(power_dicht = case_when(power >= 0.8 ~ "> 80%",
power >= 0.7 & power < 0.8 ~ "> 70%",
TRUE ~ "< 70%"))
power_df %>%
ggplot(aes(x = r, y = n, fill = power)) +
geom_tile() +
geom_tile(data = power_df %>% filter(power >= 0.8),
color = "midnightblue") +
geom_tile(data = power_df %>% filter(power >= 0.7 & power < 0.8),
color = "gray") +
labs(x = "Pearson's (partial) r", y = "Sample size", fill = "Power\n",
title = "Power curve for detecting Pearon's r with four confounders",
subtitle = "Blue outlines indicate points with 80% power, gray with 70% power") +
scale_x_continuous(limits = c(.09, .21),
expand = c(0, 0),
breaks = seq(.1, .2, by = 0.02)
) +
scale_y_continuous(limits = c(420, 660),
expand = c(0, 0),
breaks = seq(440, 640, by = 40)) +
scale_fill_continuous(breaks = seq(0.2, 1, 0.2),
limits = c(0.2, 1),
labels = scales::percent) +
theme_light() +
theme(panel.grid = element_blank(),
aspect.ratio = .75,
panel.border = element_blank())
ggsave("plots/power_analysis.png", scale = 2)
```