-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path09-AnalyticPowerFunctions.Rmd
More file actions
155 lines (118 loc) · 4.38 KB
/
09-AnalyticPowerFunctions.Rmd
File metadata and controls
155 lines (118 loc) · 4.38 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
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
# Analytic Power Functions
For some designs it is possible to calculate power analytically, using closed functions. Within the `Superpower` package we have included a number of these closed functions. As you will see below, each analytic function only serves a very narrow scenario while the simulations functions are much more flexible. In addition, we will compare these functions to other packages/software. Please note, that the analytic power functions are designed to reject designs that are not appropriate for the functions (i.e., a `3w` design will be rejected by the `power_oneway_between` function).
## One-Way Between Subjects ANOVA
First, we can setup a one-way design with four levels, and perform a exact simulation power analysis with the `ANOVA_exact` function.
```{r}
string <- "4b"
n <- 60
mu <- c(80, 82, 82, 86)
# Enter means in the order that matches the labels below.
sd <- 10
design_result <- ANOVA_design(design = string,
n = n,
mu = mu,
sd = sd)
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 can also calculate power analytically with a `Superpower` function.
```{r}
#using default alpha level of .05
power_oneway_between(design_result)$power
```
This is a generalized function for one-way ANOVA's for any number of groups. It is in part based on code from the `pwr2ppl` [@R-pwr2ppl] package (but Aberson's code allows for different n per condition, and different sd per condition).
```{r}
pwr2ppl::anova1f_4(m1 = 80, m2 = 82, m3 = 82, m4 = 86,
s1 = 10, s2 = 10, s3 = 10, s4 = 10,
n1 = 60, n2 = 60, n3 = 60, n4 = 60,
alpha = .05)
```
We can also use the function in the `pwr` package [@R-pwr]. Note that we need to calculate *f* to use this function, which is based on the means and sd, as illustrated in the formulas above.
```{r}
pwr::pwr.anova.test(n = 60,
k = 4,
f = 0.2179449,
sig.level = 0.05)
```
Finally, g\*Power [@faul2007g] provides the option to calculate `f` from the means, sd and n for the cells. It can then be used to calculate power.

## Two-way Between Subject Interaction
Now, we will setup a 2x2 between-subject ANOVA.
```{r}
string <- "2b*2b"
n <- 20
mu <- c(20, 20, 20, 25)
# Enter means in the order that matches the labels below.
sd <- 5
design_result <- ANOVA_design(design = string,
n = n,
mu = mu,
sd = sd)
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")
```
Now, let's use the analytic function `power_twoway_between`.
```{r}
#using default alpha level of .05
power_res <- power_twoway_between(design_result)
power_res$power_A
power_res$power_B
power_res$power_AB
```
We can compare these results to [@R-pwr2ppl], as well.
```{r}
pwr2ppl::anova2x2(m1.1 = 20,
m1.2 = 20,
m2.1 = 20,
m2.2 = 25,
s1.1 = 5,
s1.2 = 5,
s2.1 = 5,
s2.2 = 5,
n1.1 = 20,
n1.2 = 20,
n2.1 = 20,
n2.2 = 20,
alpha = .05,
all = "OFF")
```
## 3x3 Between Subject ANOVA
We can extend this function to a two-way design with 3 levels.
```{r}
string <- "3b*3b"
n <- 20
mu <- c(20, 20, 20, 20, 20, 20, 20, 20, 25)
# Enter means in the order that matches the labels below.
sd <- 5
design_result <- ANOVA_design(design = string,
n = n,
mu = mu,
sd = sd)
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")
```
```{r}
#using default alpha level of .05
power_res <- power_twoway_between(design_result)
power_res$power_A
power_res$power_B
power_res$power_AB
```