forked from psych252/psych252book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-power.Rmd
893 lines (684 loc) · 27.6 KB
/
06-power.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# Power
## Learning goals
- Making decisions based on statistical inference.
- The concept of statistical power.
- Calculating power.
- Common effect size measures.
- Determining sample size via simulation.
- Understanding `map()` and its children.
- Understanding `nest()` and `unnest()`.
## Load packages and set plotting theme
```{r, message=FALSE}
library("knitr") # for knitting RMarkdown
library("kableExtra") # for making nice tables
library("broom") # for tidying up model fits
library("magrittr") # for going all in with the pipe
library("effectsize") # for computing effect size measures
library("pwr") # for power calculations
library("tidyverse") # for wrangling, plotting, etc.
```
```{r}
theme_set(theme_classic() + #set the theme
theme(text = element_text(size = 20))) #set the default text size
# markdown settings for rendered code chunks
opts_chunk$set(comment = "",
fig.show = "hold")
# suppress summary warnings
options(dplyr.summarise.inform = F)
```
## Load data sets
```{r, warning=F, message=F}
df.poker = read_csv("data/poker.csv")
```
## Decision-making
Figures to illustrate power:
```{r}
mu0 = 10
mu1 = 18
# mu0 = 8
# mu1 = 20
# sd0 = 3
# sd1 = 3
sd0 = 2
sd1 = 2
alpha = 0.05
# alpha = 0.01
ggplot(data = tibble(x = c(0, 30)),
mapping = aes(x = x)) +
stat_function(fun = "dnorm",
size = 1,
color = "blue",
args = list(mean = mu0,
sd = sd0)) +
stat_function(fun = "dnorm",
size = 1,
color = "red",
args = list(mean = mu1,
sd = sd1)) +
stat_function(fun = "dnorm",
geom = "area",
size = 1,
fill = "blue",
alpha = 0.5,
args = list(mean = mu0,
sd = sd0),
xlim = c(qnorm(1-alpha, mean = mu0, sd = sd0), 20)) +
stat_function(fun = "dnorm",
geom = "area",
size = 1,
fill = "red",
alpha = 0.5,
args = list(mean = mu1,
sd = sd1),
xlim = c(0, c(qnorm(1-alpha, mean = mu0, sd = sd0)))) +
geom_vline(xintercept = qnorm(1-alpha, mean = mu0, sd = sd0),
size = 1) +
coord_cartesian(expand = F)
```
## Effect sizes
### Cohen's d
Cohen's $d$ is defined as:
$$
d = \frac{\overline y_1 - \overline y_2}{s_p}
$$
where
$$
s_p = \sqrt\frac{(n_1-1)s_1^2 + (n_2-1)s_2^2}{n_1 + n_2 - 2}
$$
```{r}
# using the effectsize package
cohens_d(x = balance ~ skill,
data = df.poker)
# compute by hand
df.cohen = df.poker %>%
group_by(skill) %>%
summarize(mean = mean(balance),
var = var(balance),
n = n()) %>%
ungroup() %>%
pivot_wider(names_from = skill,
values_from = c(mean, var, n),
names_sep = "") %>%
mutate(sp = sqrt(((n1 - 1) * var1 + (n2 - 1) * var2) / (n1 + n2 - 2)),
d = abs(mean1 - mean2) / sp)
print(df.cohen)
```
## Determining sample size
One way to determine sample size is by using the `pwr` package. While this packages is very convenient, we cannot compute power for all the hypotheses that we might be interested in testing.
### `pwr` package
The `pwr` package has a number of functions that we can use do determine the desired sample size for different experimental designs. Check out this vignette [here](https://cran.r-project.org/web/packages/pwr/vignettes/pwr-vignette.html) for more information.
#### Binomial test
We can determine what sample size we need for a Binomial test that compares two different proportions like so:
```{r}
pwr.p.test(h = ES.h(p1 = 0.75, p2 = 0.50),
sig.level = 0.05,
power = 0.80,
alternative = "greater")
```
The `pwr.p.test()` function wants the effect size h as an argument which we can compute via the `ES.h()` function that takes two proportions as arguments. I then further defined the desired significance level, power, and whether the test is one-sided or two-sided.
To have an 80% chance of detecting a difference between a proportion of `p1 = 0.75` and `p2 = 0.50`, we would need to run a study with 23 participants.
We can use the `plot()` function to illustrate what power we would get for different sample sizes.
```{r}
pwr.p.test(h = ES.h(p1 = 0.75, p2 = 0.50),
sig.level = 0.05,
power = 0.80,
alternative = "greater") %>%
plot()
```
Notice that this is a ggplot object, so we could tweak it further if we'd like to, like so:
```{r}
pwr.p.test(h = ES.h(p1 = 0.75, p2 = 0.50),
sig.level = 0.05,
power = 0.80,
alternative = "greater") %>%
plot() +
geom_hline(yintercept = 0.5,
linetype = 2,
color = "blue") +
theme(plot.title = element_text(size = 20))
```
#### t-test (two independent samples)
Here is how we could calculate the desired sample size for a t-test with two independent samples.
```{r}
pwr.t.test(d = 0.3,
power = 0.8,
type = "two.sample",
alternative = "two.sided")
```
So, to achieve a power of 0.8 for an effect size of d = 0.3, we would need to have 176 participants in each condition!
### Power analysis
While the `pwr` package works great for relatively simple designs, it quickly reaches its limits. For example, you may be interested in the sample size required to achieve adequate power for detecting an interaction, or for a specific linear contrast.
Luckily, there is a very flexible approach to determining sample size: via simulation!
#### The general recipe
- assume: significance level, n, effect size
- simulate a large number of data sets of size n with the specified effect size
- for each data set, run a statistical test to calculate the p-value
- determine the probability of rejecting the H0 (given that H1 is true)
#### Using `map()` and list columns
##### Understanding `map()`
`map()` is a powerful family of functions that's part of the `purrr` package (which is included in the `tidyverse`, so we don't have to load it separately). Using `map()` allows us to avoid nasty for-loops!
Let's take a look at the help function.
```{r, eval=F}
help(map)
```
As the help function says, `map()` allows us to apply a function to each element of a vector. Here is a simple example:
```{r}
map(.x = 1:3, .f = ~ .x^2)
```
I've passed the vector of numbers `.x = 1:3` to `map()`, and then applied the anonymous function `.f = ~ .x^2` to each of the elements in that vector.
> Always make sure to use the `~` for defining anonymous functions!
As with many R functions, you can achieve the same outcome in many different ways. Here are a number of ways to do the same thing:
```{r, echo=F}
# omitting arguments, and using . instead of .x
tmp1 = map(1:3, ~ .^2)
# longer form function definition
tmp2 = map(1:3, function(x) x^2)
# explicit function definition
squared = function(x){
return(x^2)
}
tmp3 = map(1:3, squared)
str_c("Is tmp1 identical to tmp2? ", identical(tmp1, tmp2))
str_c("Is tmp2 identical to tmp3? ", identical(tmp2, tmp3))
```
###### Understanding `map()`'s children
Notice that the output of `map()` is a list.
```{r}
map(.x = 1:3, .f = ~ .x^2)
```
The `map()` function has many children that differ in what they output. For example, `map_dbl()` outputs a numeric vector instead of a list.
```{r}
map_dbl(.x = 1:3, .f = ~ .x^2)
```
And `map_lgl()` returns logical values.
```{r}
map_lgl(.x = 1:3, .f = ~ .x == 1)
```
We can also return data frames by using either `map_dfr()` which binds data frames by row, or `map_dfc()` which binds data frames by column. Here is an example:
```{r}
set.seed(1)
# function to create a data frame
fun_make_df = function(x){
tibble(number = x,
group = sample(c("A", "B"),
size = 3,
replace = T),
value = rnorm(n = 3))
}
# bind data frames by row
map_dfr(.x = 1:3,
.f = ~ fun_make_df(.))
```
A nice use-case of the `map_dfr()` function can be to read in a number of csv files from individual participants into one larger data frame. Let's simulate some data first, and save the data of each participant as a separate csv file (using `map()` of course).
```{r}
set.seed(1)
fun_simulate_csv = function(x){
n_observations = sample(3:6, size = 1)
df = tibble(age = sample(18:99, size = n_observations),
responses = rnorm(n = n_observations, mean = 100, sd = 10))
write_csv(df, file = str_c("data/participant", x, ".csv"))
}
map(.x = 1:3,
.f = ~ fun_simulate_csv(.))
```
Now, let's read in the data from the three participants and combine it into a single data frame.
```{r, warning=F, message=F}
map_dfr(.x = 1:3,
.f = ~ read_csv(str_c("data/participant", .x, ".csv")),
.id = "participant")
```
Notice how I used the `.id = ` argument of the function to add a `participant` column to my data frame in this case.
###### Use `map2()` for functions with more than one input
If you have a function with more than one input, `map2()` is your friend. Here is a silly example:
```{r}
map2_dbl(.x = c(1.23, 2.13, 5.32),
.y = c(0, 1, 2),
.f = ~ round(.x, digits = .y))
```
Here, I took the vector of numbers `.x` and rounded it to a different number of digits according to what I've specified it `.y`.
The same works with data frames, too, like so:
```{r}
tibble(x = c(1.23, 2.13, 5.32),
n = c(0, 1, 2)) %>%
mutate(rounded = map2_dbl(.x = x,
.y = n,
.f = ~ round(.x, digits = .y)))
```
###### Use pmap() to go all out (i.e. for functions with more than two arguments)
`pmap()` is your friend for functions that have more than two arguments. Here is an example:
```{r}
tibble(x = c(1, 2, 3),
y = c(23, 12, 1),
z = c(4, 5, 4)) %>%
mutate(result = pmap_dbl(.l = .,
.f = ~ ..1 * ..2 + ..3))
```
Notice than when using more than two arguments, we refer to each function argument with ..1, ..2, ..3, etc.
###### Practice 1 -- Having fun with `map()`
Use the `map2_dbl()` function to create a new variable in this data frame that's the maximum of each row across columns `a` and `b`.
```{r}
df.practice = tibble(a = c(12, 14, 52, 23, 23),
b = c(29, 12, 4, 48, 37))
# write your code here
```
For the fast ones: For each row in the data frame, write a function that calculates the mean of columns `a` and `b`, and the rounds to the number of digits specified in column `d`.
```{r}
df.practice = tibble(a = c(12.123, 53.234, 23.324, 54.232, 12.454),
b = c(12.456, 23.234, 6.736, 3.346, 7.232),
d = c(1, 2, 2, 3, 1))
# write your code here
```
##### List columns
`map()` becomes particularly powerful when combined with list columns. List columns allow you to put data frames into a column of your data frame. For example, you can do something like this:
```{r}
df.data = tibble(participant = 1,
age = 23,
data = list(tibble(trial = c(1, 2, 3),
response = c(23, 95, 37))))
print(df.data)
```
We could access what's in this data column like so
```{r}
df.data %>%
pluck("data", 1)
```
###### `unnest()`
For getting data frames out of list columns, we can use the `unnest()` function.
```{r}
df.data %>%
unnest(cols = data)
```
###### `nest()`
We can use the `nest()` function to create list columns. `nest()` works particularly well in combination with `group_by()`. For example, here I'm created three separate data sets where the size of each data set is determined by the `x` column.
```{r}
# original data frame
df.data = tibble(participant = c(1, 1, 1, 2, 2, 3),
response1 = 1:6,
response2 = 6:1)
print(df.data)
# nested data frame
df.data = df.data %>%
group_by(participant) %>%
nest() %>%
ungroup()
print(df.data)
# and back to the original data frame
df.data = df.data %>%
unnest(cols = data)
print(df.data)
```
And, of course, I can use unnest() to get back to my original data frame.
##### Combining `nest()` and `map()`
`nest()` and `map()` unfold their power together when it comes to fitting models. For example, consider that you want to fit a separate linear to subests of your data. Here is how you can do that using `nest()` and `map()`.
I'll demonstrate via the infamous `mtcars` data set that comes with R.
```{r}
mtcars %>%
head(10)
```
What I want to do is to fit separate regression models predicting `mpg` (miles per gallon) as a function of `wt` (the car's weight) for cars with different numbers of cylinders. Here is how that works.
```{r}
df.data = mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(fit = map(.x = data,
.f = ~ lm(formula = mpg ~ 1 + wt,
data = .)))
```
I first grouped by the `cyl` (the number of cylinders), used `nest()` to put the rest of the data into a list column, and then used `mutate()` to run a separate linear model on each data set and saved the modle result into the `fit` column.
With some more wrangling, I could, for example, extract the coefficients of each model like so:
```{r}
mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(fit = map(.x = data,
.f = ~ lm(mpg ~ 1 + wt, data = .)),
coef = map(.x = fit,
.f = ~ tidy(.))) %>%
unnest(cols = coef) %>%
select(-c(data, fit))
```
Voila! A data frame that contains the coefficients for each of the three models.
Base R has a number of functions like `apply()`, `sapply()`, `lapply()`, etc. that do similar things to `map()`. However, the `map()` family of functions works very well with the rest of the `tidyverse`, that's why we'll use it.
#### Power analysis via simulation
So, after this long detour via `map()`, list columns, `nest()`, and `unnest()`, we can finally start doing some power analysis via simulation, yay!
##### Simulating the Binomial test
Let's start with the Binomial test that we played around with above. We want to use simulation to determine the sample size we need to have an 80% of detecting a difference between two proportions p = 0.75 and p = 0.5.
I'll first do it step by step, and then afterwards put all the code in one place together
###### Step by step
Here is how we would go about this. First, I'll set up a simulation grid.
```{r}
# number of simulations
n_simulations = 10
# set up simulation grid
df.power = expand_grid(n = seq(10, 40, 2),
simulation = 1:n_simulations,
p = 0.75) %>%
mutate(index = 1:n(),
.before = n) # add an index column
df.power
```
The `expand_grid()` function creates a data frame that contains all the combinations of the variables.
Now, let's generate data according to our hypothesis.
```{r}
set.seed(1)
df.power = df.power %>%
group_by(index, n, simulation) %>%
mutate(response = rbinom(n = 1,
size = n,
prob = p))
df.power
```
The `response` variable now contains samples from the sample size `n` according to the probability specified in `p`. Now it's time for `group_by()` and `nest()` because we want to calculate the p-value for observing this response if in fact the null hypothesis was true (i.e. p = 0.5).
```{r}
df.power = df.power %>% # generate random data
mutate(fit = map2(.x = response,
.y = n,
.f = ~ binom.test(x = .x, # define formula
n = .y,
p = 0.5,
alternative = "two.sided")))
df.power
```
Now that we've fitted a bunch of binomial models, we only need to get at the p-values. Again, we can use the `tidy()` function from the `broom` package for help, like so:
```{r}
df.power = df.power %>%
mutate(coef = map(.x = fit,
.f = ~ tidy(.))) %>%
select(simulation, p, index, coef) %>%
unnest(cols = coef) %>%
select(index, n, simulation, p.value)
df.power
```
Finally, all that's left is to calculate power by looking at the proportion of times in which we rejected the null hypothesis.
```{r}
df.power %>%
group_by(n) %>%
summarize(power = sum(p.value < 0.05) / n())
```
Notice here that the power values fluctuate quite a bit. This is because we only ran 10 simulations for each sample size. To have more robust results, we need to increase the number of simulations.
But first, let's make a plot that visualizes what we found:
```{r, message=FALSE}
df.plot = df.power %>%
group_by(n) %>%
summarize(power = sum(p.value < 0.05) / n())
ggplot(data = df.plot,
mapping = aes(x = n, y = power)) +
geom_hline(yintercept = seq(0, 1, 0.1),
linetype = 2,
color = "gray50",
size = 0.1) +
geom_smooth(method = "loess",
formula = "y ~ x") +
geom_point()
```
As expected, the power increases with the sample size n.
###### All in one
Here is a slightly different way to run the same simulation we just did before in one go:
```{r}
# make reproducible
set.seed(1)
# number of simulations
n_simulations = 5
# run simulation
expand_grid(n = seq(10, 40, 2),
simulation = 1:n_simulations,
p = 0.75) %>%
mutate(index = 1:n(),
.before = n) %>%
group_by(index, n, p, simulation) %>%
mutate(response = rbinom(n = 1,
size = n,
prob = p),
p.value = binom.test(x = response,
n = n,
p = 0.5,
alternative = "two.sided")$p.value) %>%
group_by(n, p) %>%
summarize(power = sum(p.value < 0.05) / n()) %>%
ungroup()
```
And another time with a larger sample size, and also for two different alternative hypotheses: p1 = 0.75, and p2 = 0.9.
```{r, message=FALSE}
# make reproducible
set.seed(1)
# number of simulations
n_simulations = 100
# run simulation
df.power = expand_grid(n = seq(10, 40, 2),
simulation = 1:n_simulations,
p = c(0.75, 0.9)) %>% # added another hypothesis here
mutate(index = 1:n(),
.before = n) %>%
group_by(index, n, simulation) %>%
mutate(response = rbinom(n = 1,
size = n,
prob = p)) %>%
ungroup() %>%
mutate(fit = map2(.x = response,
.y = n,
.f = ~ binom.test(x = .x,
n = .y,
p = 0.5,
alternative = "two.sided"))) %>%
mutate(coef = map(.x = fit,
.f = ~ tidy(.))) %>%
unnest(cols = coef) %>%
select(index, n, p, p.value) %>%
group_by(n, p) %>%
summarize(power = sum(p.value < 0.05) / n()) %>%
ungroup()
# visualize results
ggplot(data = df.power,
mapping = aes(x = n,
y = power,
fill = as.factor(p),
group = p)) +
geom_hline(yintercept = seq(0, 1, 0.1),
linetype = 2,
color = "gray50",
size = 0.1) +
geom_smooth(method = "loess",
formula = "y ~ x",
color = "black") +
geom_point(shape = 21) +
labs(fill = "alternative") +
guides(fill = guide_legend(reverse = T))
```
##### Simulating an independent samples t-test
Let's simulate data for an independent samples t-test. To do so, we need to make some assumptions about what we expect the distribution of the data to look like. Here, I assume that we get normally distributed data with some mean and standard deviation.
The procedure will be very similar to the Binomial test above. The only thing that changes really is how we generate the data (and then some small wrangling differences).
Let's say that we collected the following pilot data set:
```{r}
set.seed(1)
# parameters
n = 10
mean1 = 10
sd1 = 2
mean2 = 11
sd2 = 3
df.ttest = tibble(group1 = rnorm(n = n,
mean = mean1,
sd = sd1),
group2 = rnorm(n = n,
mean = mean2,
sd = sd2)) %>%
pivot_longer(cols = everything()) %>%
arrange(name)
```
The two groups in our sample don't differ significantly from each other.
```{r, message=FALSE}
# visualize the data
ggplot(data = df.ttest,
mapping = aes(x = name,
y = value)) +
stat_summary(fun.data = "mean_cl_boot")
# compute a t-test
t.test(formula = value ~ name,
data = df.ttest)
```
Let's calculate what the effect size was in our sample. Remember that Cohen's d is defined as
$$d = \frac{\lvert\overline y_1 - \overline y_2\rvert}{s_p}$$
where
$$s_p = \sqrt\frac{(n_1-1)s_1^2 + (n_2-1)s_2^2}{n_1 + n_2 - 2}$$
In our sample, the effect size Cohen's d was:
```{r}
df.sample = df.ttest %>%
group_by(name) %>%
summarize(mean = mean(value),
sd = sd(value),
n = n()) %>%
ungroup()
# compute the pooled standard deviation
sp = sqrt(((df.sample$n[1] - 1) * df.sample$sd[1]^2 + (df.sample$n[2] - 1) *
df.sample$sd[2]^2) / (df.sample$n[1] + df.sample$n[2] - 2))
d = abs(df.sample$mean[1] - df.sample$mean[2]) / sp
d
```
Let's double check that we got it right:
```{r}
cohens_d(x = value ~ name,
data = df.ttest)
```
We did!
So let's now calculate the means and standard deviations based on our pilot data and run a power analysis to determine how many participants we would need, to have an 80% chance of rejecting the null hypothesis for the estimated effect size.
```{r, message=FALSE}
# make reproducible
set.seed(1)
# parameters
mean1 = df.sample$mean[1]
mean2 = df.sample$mean[2]
sd1 = df.sample$sd[1]
sd2 = df.sample$sd[2]
# number of simulations
n_simulations = 10
# n_simulations = 100
# run simulation
df.power2 = expand_grid(n = seq(from = 10, to = 60, by = 5),
simulation = 1:n_simulations) %>%
mutate(index = 1:n(),
.before = n) %>%
group_by(index, n, simulation) %>%
mutate(data = list(tibble(group1 = rnorm(n = n,
mean = mean1,
sd = sd1),
group2 = rnorm(n = n,
mean = mean2,
sd = sd2)) %>%
pivot_longer(cols = everything()))) %>%
mutate(fit = map(.x = data,
.f = ~ lm(formula = value ~ 1 + name,
data = .)),
parameters = map(.x = fit,
.f = ~ tidy(.))) %>%
select(index, n, simulation, parameters) %>%
unnest(cols = parameters) %>%
filter(term != "(Intercept)") %>%
select(index, n, simulation, p.value) %>%
group_by(n) %>%
summarize(power = sum(p.value < 0.05) / n()) %>%
ungroup()
# visualize results
ggplot(data = df.power2,
mapping = aes(x = n,
y = power)) +
geom_hline(yintercept = seq(0, 1, 0.1),
linetype = 2,
color = "gray50",
size = 0.1) +
geom_smooth(method = "loess",
color = "black",
formula = "y ~ x") +
geom_point(shape = 21) +
scale_x_continuous(breaks = seq(10, 60, 10),
labels = seq(10, 60, 10)) +
scale_y_continuous(breaks = seq(0, 1, 0.1),
labels = seq(0, 1, 0.1))
```
Let's compare to what we'd get from the `pwr` package.
```{r}
pwr.t.test(d = 0.5874251,
power = 0.8,
type = "two.sample",
alternative = "two.sided")
```
Looks pretty similar!
##### Practice 2 -- Simulation of an interaction effect
Try to run a simulation to determine how many participants you would need to have an 80% chance of rejecting the null hypothesis that there is no interaction based on the following pilot data set:
```{r}
set.seed(1)
# population parameters
b0 = 1
b1 = 2
b2 = 3
b1_2 = -2
sd = 2
n = 10
df.linear = tibble(x = runif(n = n),
y = rep(c(0, 1), each = n/2),
z = b0 + b1 * x + b2 * y + b1_2 * x * y + rnorm(n = n, sd = sd))
```
Let's visualize the pilot data first:
```{r, message=FALSE}
ggplot(data = df.linear,
mapping = aes(x = x,
y = z,
group = y,
fill = as.factor(y),
color = as.factor(y))) +
geom_smooth(method = "lm",
se = F,
show.legend = F) +
geom_point(shape = 21,
color = "black",
show.legend = F)
```
Let's estimate the parameters based on our sample:
```{r}
# parameter estimates for the coefficients based on the sample
b = lm(formula = z ~ x * y,
data = df.linear) %>%
tidy() %>%
select(term, estimate, p.value)
# parameter estimate of the residual standard deviation
sigma = lm(formula = z ~ x * y,
data = df.linear) %>%
glance() %>%
pull(sigma)
```
Run a power analysis to see how many participants you would need to have an 80% of rejecting the null hypothesis that there is no interaction. Use the parameter estimates (the beta coefficients and the standard deviation of the residuals `sigma`) based on your pilot data to simulate new data.
Here is the strategy:
- Try to closely emulate what we've been doing for the independent samples t-test above.
- However, this time, we have a different way of generating the data (namely by using the regression equation: $z \sim b_0 + b_1 \cdot x + b_2 \cdot y + b_{1\_2} \cdot x \cdot y + e)$, where $e \sim N(0, \sigma)$.
- Fit the model first to extract the estimates for the beta coefficients, and the standard deviation of the residuals `sigma`.
- Then use these parameters to generate new data assuming that `x` is a continuous predictor between 0 and 1 (`x = runif(n = n)`) and `y` is a binary, dummy-coded variable (`y = rep(c(0, 1), each = n/2)`).
- Extract the coefficients of each model fit, and check whether the interaction is significant.
- Make a plot that shows how power changes with the sample size n.
```{r}
set.seed(1)
# write your code here
```
Run the same power analysis this time assuming the ground truth parameters from the population (rather than the parameters that we've estimated from the sample).
```{r}
set.seed(1)
# write your code here
```
## Additional resources
### Datacamp
- [Functional programming with purrr](https://www.datacamp.com/courses/foundations-of-functional-programming-with-purrr)
### Cheatsheets
- [purrr](figures/purrr.pdf)
### Misc
- [Sample size justification](https://lakens.github.io/statistical_inferences/08-samplesizejustification.html)
- [Guide to reporting effect sizes and confidence intervals](https://matthewbjane.quarto.pub/)
- [Getting started with `pwr`](https://cran.r-project.org/web/packages/pwr/vignettes/pwr-vignette.html)
- [Visualize power](https://rpsychologist.com/d3/NHST/)
- [Calculating and reporting effect sizes to facilitate cumulative science: a practical primer for t-tests and ANOVAs](https://www.frontiersin.org/articles/10.3389/fpsyg.2013.00863/full)
- [purrr tutorial](https://emoriebeck.github.io/R-tutorials/purrr/)
- [simr](https://cran.r-project.org/web/packages/simr/index.html): R Package for running power analysis for generalized linear mixed effects models.
- [simglm](https://cran.r-project.org/web/packages/simglm/index.html): Alternative R package for running power analysis via simulation.
- [cautionary tale about using pilot studies for power calculations](https://nccih.nih.gov/grants/whatnccihfunds/pilot_studies)
## Session info
Information about this R session including which version of R was used, and what packages were loaded.
```{r}
sessionInfo()
```