-
Notifications
You must be signed in to change notification settings - Fork 260
/
chapter_14_old.html
1029 lines (795 loc) · 26.2 KB
/
chapter_14_old.html
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>chapter_14.knit</title>
<meta charset="utf-8" />
<meta name="author" content="" />
<script src="libs/header-attrs-2.21/header-attrs.js"></script>
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link rel="stylesheet" href="css/Font_Style.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle
<span style="font-size: 60px;">第十四章</span> <br>
<span style="font-size: 50px;">统计检验力分析 <br>
<br>
<br>
<span style="font-size: 30px;">胡传鹏</span> <br>
<span style="font-size: 30px;">2023/06/05</span> <br>
---
# <h1 lang="zh-CN">什么是统计检验力</h1>
**统计检验力(统计功效, statistical power)**: 假设检验中正确拒绝虚无假设的概率。<br>
<br>
`\(1 - \beta = P(sig \ | \ H_1)\)`
<br>
.center[<img src="./picture/chp14/beta.png" width="60%" height="50%">]
---
统计检验力主要由三个因素决定:<br>
- 效应量(effect size)
- 一类错误率(Type 1 error, `\(\alpha\)`)
- 样本量(sample size)
效应量、一类错误率、效应量越大,统计功检验力越高;相反则更低。
---
# <h1 lang="zh-CN">其他统计量与统计检验力的关系——以单样本*t*检验为例</h1>
---
## <h2 lang="zh-CN">效应量与统计检验力的关系——以单样本*t*检验为例</h2>
在本次模拟中,样本量固定为$ n = 20$,一类错误率固定为$\alpha = 0.05$
```r
rm(list = ls())
if (!requireNamespace("pacman", quietly = TRUE)) {
install.packages("pacman") } # 检查是否已安装 pacman, 如果未安装,则安装包
pacman::p_load("tidyverse", "faux", "pwr", "MASS", "afex") # 使用p_load来载入需要的包
```
```r
effect_size <- seq(0.1, 1, 0.1) # 生成10个效应量,从0.1到1,间隔为0.1
len_power <- length(effect_size) # 获取生成效应量的长度,即10
idx <- 0 # 设置一个idx变量作为标识符,后续用于定位
power <- numeric(len_power) # 生成与len_power长度一致的0,便于后续修改
for (i in effect_size) { # 遍历所有的效应量
p <- numeric(5000) # 生成5000个长度的列表,用于储存p值
idx <- idx + 1 # 每运行一次for循环,idx就加1
for (j in 1:5000) {
# 在遍历效应量的基础上运行5000次计算p值,用于后续计算power
# 生成20个服从均值为某一效应量,标准差为1的正态分布的样本
x <- rnorm(n = 20, mean = i, sd = 1)
p[j] <- t.test(x)$p.value # 储存p值
}
power[idx] <- sum(p < 0.05) / 5000 # 计算p值小于0.05的个数并除5000计算power
}
df_sim1 <- data.frame(effect_size = effect_size, power = power)
```
---
```r
df_sim1 %>%
ggplot2::ggplot(aes(x = effect_size, y = power)) +
ggplot2::geom_point(size = 5) +
ggplot2::geom_line() +
ggplot2::labs(title = "Fixed sample size and alpha value",
x = "Effect size (Cohen's d)",
y = "Statistical power") +
papaja::theme_apa()
```
![](chapter_14_files/figure-html/plot effect size and power-1.png)<!-- -->
---
有许多R包可用于计算统计检验力,如`pwr`
```r
pwr.t.test(
n = 20, d = 0.2, sig.level = 0.05,
type = "one.sample", alternative = "two.sided"
)[["power"]]
```
```
## [1] 0.1359563
```
---
可以使用map函数结合`pwr`来计算不同效应量下的统计检验力
```r
map_dbl(effect_size, ~ pwr.t.test(
n = 20, d = .x,
sig.level = 0.05,
type = "one.sample",
alternative = "two.sided"
)[["power"]])
```
```
## [1] 0.07094116 0.13595629 0.24708909 0.39699383 0.56450442 0.72100510
## [7] 0.84350588 0.92389876 0.96815164 0.98859129
```
---
## <h2 lang="zh-CN"> `\(\alpha\)`与统计检验力的关系 </h2>
在本次模拟中,样本量固定为20,效应量固定为0.5
```r
alpha <- c(0.001, 0.01, 0.05, 0.1) # 生成5个alpha值
len_alpha <- length(alpha) # 获取生成alpha向量的长度,即5
idx <- 0 # 设置一个idx变量作为标识符,后续用于定位
power <- numeric(len_alpha) # 生成与len_alpha长度一致的0,便于后续修改
for (i in alpha) { # 遍历所有的效应量
p <- numeric(5000) # 生成5000个长度的列表,用于储存p值
idx <- idx + 1 # 每运行一次for循环,idx就加1
for (j in 1:5000) {
# 在遍历效应量的基础上运行5000次计算p值,用于后续计算power
x <- rnorm(n = 20, mean = 0.5, sd = 1)
# 生成20个服从均值为某一效应量,标准差为1的正态分布的样本
p[j] <- t.test(x)$p.value # 储存p值
}
power[idx] <- sum(p < i) / 5000 # 计算p值小于alpha的个数并除5000计算power
}
df_sim2 <- data.frame(alpha = alpha, power = power)
```
---
```r
df_sim2 %>%
ggplot2::ggplot(aes(x = alpha, y = power)) +
ggplot2::geom_point(size = 5) +
ggplot2::geom_line() +
ggplot2::labs(title = "Fixed sample size and effect size",
x = "alpha",
y = "Statistical power") +
papaja::theme_apa()
```
![](chapter_14_files/figure-html/plot alpha and power-1.png)<!-- -->
---
```r
map_dbl(alpha, ~ pwr.t.test(
n = 20, d = 0.5,
sig.level = .x,
type = "one.sample",
alternative = "two.sided"
)[["power"]])
```
```
## [1] 0.08739216 0.29734608 0.56450442 0.69522018
```
---
## <h2 lang="zh-CN">样本量与统计检验力的关系</h2>
```r
sample_size <- seq(10, 100, 10)
# 生成10个效应量,从0.1到1,间隔为0.1
len_n <- length(sample_size) # 获取生成效应量的长度,即10
idx <- 0 # 设置一个idx变量作为标识符,后续用于定位
power <- numeric(len_n) # 生成与len_n长度一致的0,便于后续修改
for (i in sample_size) { # 遍历所有的效应量
p <- numeric(5000) # 生成5000个长度的列表,用于储存p值
idx <- idx + 1 # 每运行一次for循环,idx就加1
# 在遍历效应量的基础上运行5000次计算p值,用于后续计算power
for (j in 1:5000) {
x <- rnorm(n = i, mean = 0.5, sd = 1)
# 生成20个服从均值为某一效应量,标准差为1的正态分布的样本
p[j] <- t.test(x)$p.value # 储存p值
}
# 计算p值小于0.05的个数并除5000计算power
power[idx] <- sum(p < 0.05) / 5000
}
df_sim3 <- data.frame(sample_size = sample_size, power = power)
```
---
```r
df_sim3 %>%
ggplot2::ggplot(aes(x = sample_size, y = power)) +
ggplot2::geom_point(size = 5) +
ggplot2::geom_line() +
ggplot2::labs(title = "Fixed effect size and alpha",
x = "Sample size",
y = "Statistical power") +
papaja::theme_apa()
```
![](chapter_14_files/figure-html/plot sample size and power-1.png)<!-- -->
---
```r
map_dbl(sample_size, ~ pwr.t.test(
n = .x, d = 0.5,
sig.level = 0.05,
type = "one.sample",
alternative = "two.sided"
)[["power"]])
```
```
## [1] 0.2931756 0.5645044 0.7539647 0.8693981 0.9338976 0.9677886 0.9847848
## [8] 0.9929987 0.9968496 0.9986097
```
---
# <h1 lang="zh-CN">G*Power中的结果是如何计算的</h1>
在使用GPower时,我们通常需要在软件中填写两组之间的差异Cohen's *d*(如 Cohen's *d* = 0.5)。
假设我们有两组被试分别进行了某些测验,每组共10名被试,两组被试的均值分别为0.5和0,标准差为1。通过之前课程学习到的计算*d*的公式:
`$$Cohen's \ d_s = \frac{X_1 - X_2}{\sqrt{SD_{pool}}} = \frac{X_1 - X_2}{\sqrt{\frac{(n_1 -1)SD_1^2 + (n_2-1)SD_2^2)}{n_1+n2-2}}}$$`
我们可以计算出*d*为:
`$$Cohen's \ d_s = \frac{0.5 - 0}{\sqrt{\frac{(10 -1)1^2 + (10-1)1^2)}{10+10-2}}}=0.5$$`
---
代码实现:
```r
n1 <- 10
n2 <- 10
m1 <- 0.5
m2 <- 0
sd1 <- 1
sd2 <- 1
group1 <- rnorm(n1, m1, sd1) # 生成n1个,以m1为均值,sd1位标准差服从正态分布的样本
group2 <- rnorm(n2, m2, sd2)
print(group1)
```
```
## [1] 0.90831662 1.84201988 2.57461825 -0.41290862 0.09906172 0.29442386
## [7] 1.20431297 0.75675988 -0.00117836 1.42199792
```
```r
print(group2)
```
```
## [1] 1.4233688 0.2913802 1.5057658 0.9337539 -1.6776908 0.8896208
## [7] 0.5889603 0.2755393 0.9973405 1.3861335
```
---
*t*检验结果显示,未达到统计学意义上的显著性水平
```r
t.test(group1, group2)
```
```
##
## Welch Two Sample t-test
##
## data: group1 and group2
## t = 0.50068, df = 17.997, p-value = 0.6227
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.6626481 1.0772985
## sample estimates:
## mean of x mean of y
## 0.8687424 0.6614172
```
---
为了进行统计检验力分析,我们需要模拟很多次并观察是否每次在给定效应量和一类错误率时的显著性水平。我们以传统上80%的统计检验力为标准,对比当前模拟的结果。
```r
sim <- 5000 # 进行5000次模拟
n1 <- 10
n2 <- 10
m1 <- 0.5
m2 <- 0
sd1 <- 1
sd2 <- 1
pvals <- c()
cohen_ds <- c()
for (i in 1:sim) {
group1 <- rnorm(n1, m1, sd1)
group2 <- rnorm(n2, m2, sd2)
pvals[i] <- t.test(group1, group2)$p.value
cohen_ds[i] <- (mean(group1)-mean(group2))/
(sqrt(((n1-1)*sd(group1)^2 + (n2-1) * sd(group2)^2) /
(n1+n2-2)))
}
```
---
可以看到,这里我们的统计检验力只有:
```r
sum(pvals < 0.05) / sim
```
```
## [1] 0.1876
```
```r
mean(cohen_ds)
```
```
## [1] 0.5235306
```
---
与pwr包对比
```r
pwr.t.test(n = 10, d = 0.5, sig.level = 0.05, type = "two.sample")
```
```
##
## Two-sample t test power calculation
##
## n = 10
## d = 0.5
## sig.level = 0.05
## power = 0.1850957
## alternative = two.sided
##
## NOTE: n is number in *each* group
```
---
与G*Power对比
.center[<img src="./picture/chp14/gpower2sample.png" width="60%">]
---
可以看到模拟的结果与其他两个工具包/软件的结果很相似。
---
接下来我们可以改进一下代码,在给定效应量,和一类错误水平的情况下,被试数量到达多少时,统计检验力达到80%。
**sample size planning?**
```r
sim <- 5000 # 模拟次数为5000
power <- c() # 储存power值
d_mean <- c() # 储存效应量
n1 <- 10 # 第一组被试初识数量为10名
n2 <- 10
m1 <- 0.5
m2 <- 0
sd1 <- 1
sd2 <- 1
increase_num <- 0 # 从0开始计算power,即从每组10人开始计算
```
---
```r
for (i in 1:5000000) { # for循环给定一个极大的数进行遍历
pvals <- c() # 储存后续每一次模拟的p值
cohen_ds <- c() # 储存每一次模拟的d值
for (j in 1:sim) { # 开始对每一个组数量进行模拟
group1 <- rnorm(n1, m1, sd1)
group2 <- rnorm(n2, m2, sd2)
pvals[j] <- t.test(group1, group2)$p.value
cohen_ds[j] <- (mean(group1) - mean(group2)) / (sqrt(((n1 - 1) * sd(group1)^2 + (n2 - 1) * sd(group2)^2) / (n1 + n2 - 2)))
}
power[increase_num + 1] <- sum(pvals < 0.05) / sim # 储存power值,由于无法添加索引0的值,所以先加1
d_mean[increase_num + 1] <- mean(cohen_ds)
n1 <- n1 + 1 # 每次计算完power后,对每组加1
n2 <- n2 + 1
if (power[increase_num + 1] >= 0.8) {
break # 当power达到0.8时停止运行
}
increase_num <- increase_num + 1 # 增加的被试加1
}
```
---
统计检验力随着样本量增加的变化
```r
plot(10:(increase_num + 10), power,
xlab = "Number of subjects each group",
ylab = "power", ylim = c(0, 1.0))
abline(h = 0.8, col = "red")
```
![](chapter_14_files/figure-html/unnamed-chunk-12-1.png)<!-- -->
---
效应量随着样本量增加的变化(?)
```r
plot(10:(increase_num + 10), d_mean,
xlab = "Number of subjects each group",
ylab = "Cohen's d", ylim = c(0.2, 0.8))
abline(h = 0.5, col = "red")
```
![](chapter_14_files/figure-html/unnamed-chunk-13-1.png)<!-- -->
---
模拟达到80%统计检验力所需要的样本数量为每组:
```r
print(10 + increase_num)
```
```
## [1] 64
```
与G*Power对比:
.center[<img src="./picture/chp14/gpowersamplesize.png" width="50%">]
---
## <h2 lang="zh-CN">生成前后测数据——配对样本*t*检验</h2>
前后测数据通常之间存在一定程度的相关
为了生成前后测数据,我们可以通过`MASS`包中的`mvrnorm()`函数生成。
首先我们设定被试数量为100,前测的均值为0,后测均值为1,标准差均为1,两次测量结果的相关为0.3。
```r
n <- 100
test1_mean <- 0
test2_mean <- 1
test1_sd <- 1
test2_sd <- 1
rho <- 0.3
```
---
然后,我们根据公式生成一个方差协方差矩阵
.center[<img src="./picture/chp14/cov.png" width="50%">]
- https://en.wikipedia.org/wiki/Multivariate_normal_distribution
---
代码实现:
```r
sigma <- matrix(c(test1_sd^2, test1_sd * test2_sd * rho,
test1_sd * test2_sd * rho, test2_sd^2),
ncol = 2)
df <- data.frame(mvrnorm(
n = n, mu = c(test1_mean, test2_mean),
Sigma = sigma
))
head(df)
```
```
## X1 X2
## 1 0.42562489 -0.12468088
## 2 -3.08432581 0.81261842
## 3 -0.03832954 1.04359116
## 4 -0.03956911 1.66984808
## 5 -0.36318167 -0.06728387
## 6 -0.49988273 1.33472709
```
---
我们可以简单检查一下模拟结果:
```r
cor(df$X1, df$X2)
```
```
## [1] 0.4036649
```
```r
mean(df$X1)
```
```
## [1] -0.08156958
```
```r
mean(df$X2)
```
```
## [1] 1.038246
```
---
最后,我们可以根据计算cohen'd的公式确定一下配对数据的效应量,然后进行统计检验力分析,这里与G*Power一致采用:
`$$Cohen's d_{z} = \frac{M_{diff}}{SD_{diff}}$$`
```r
library(MASS)
sim <- 1000 # 模拟次数为5000
power <- c() # 储存power值
d_mean <- c()
cor_mean <- c()
n <- 10 # 第一组被试初识数量为10名
m1 <- 10
m2 <- 10.5
sd1 <- 1
sd2 <- 2
rho <- 0.3
increase_num <- 0 # 从0开始计算power,即从每组10人开始计算
```
---
```r
for (i in 1:500000) { # for循环给定一个极大的数进行遍历
pvals <- c() # 储存后续每一次模拟的p值
ds <- c()
cors <- c()
for (j in 1:sim) { # 开始对每一个组数量进行模拟
sigma <- matrix(c(sd1^2, sd1 * sd2 * rho,
sd1 * sd2 * rho, sd2^2),
ncol = 2)
df <- data.frame(mvrnorm(
n = n, mu = c(m1, m2),
Sigma = sigma
))
pvals[j] <- t.test(df$X1, df$X2, paired = TRUE)$p.value
ds <- (mean(df$X1) - mean(df$X2)) / sd(df$X1 - df$X2)
cors[j] <- cor(df$X1, df$X2)
}
power[increase_num + 1] <- sum(pvals < 0.05) / sim
d_mean[increase_num + 1] <- mean(ds)
cor_mean[increase_num + 1] <- mean(cors)
n <- n + 1 # 每次计算完power后,对每组加1
if (power[increase_num + 1] >= 0.8) {
break # 当power达到0.8时停止运行
}
increase_num <- increase_num + 1 # 增加的被试加1
}
```
---
```r
plot(10:(increase_num + 10), power,
xlab = "Number of subjects",
ylab = "power", ylim = c(0, 1.0))
abline(h = 0.8, col = "red")
```
![](chapter_14_files/figure-html/unnamed-chunk-20-1.png)<!-- -->
---
```r
plot(x = 10:(increase_num + 10),
y = cor_mean, ylim = c(0, 0.6),
ylab = "correlation",
xlab = "Number of subjects")
```
![](chapter_14_files/figure-html/unnamed-chunk-21-1.png)<!-- -->
---
总共需要样本量为:
```r
print(10 + increase_num)
```
```
## [1] 116
```
---
与G*Power对比:
- 首先先生成一个拥有相同参数的很大样本量的数据,用于计算总体水平的Cohen's dz,并将该值输入到G*Power中
```r
set.seed(124)
n <- 500000 # 第一组被试初识数量为10名
m1 <- 10
m2 <- 10.5
sd1 <- 1
sd2 <- 2
rho <- 0.3
sigma <- matrix(c(sd1^2, sd1 * sd2 * rho,
sd1 * sd2 * rho, sd2^2),
ncol = 2)
df <- data.frame(mvrnorm(
n = n, mu = c(m1, m2),
Sigma = sigma
))
dz <- (mean(df$X1) - mean(df$X2)) / sd(df$X1 - df$X2)
print(dz)
```
```
## [1] -0.2578324
```
---
.center[<img src="./picture/chp14/paired.png" width="50%">]
---
# <h1 lang="zh-CN">如何计划分析方法:以ANOVA为例</h1>
---
## <h2 lang="zh-CN">生成假数据</h2>
在规划样本量时通常使用G\*Power进行先验统计检验力分析 (*a-priori* power analysis)。使用使用工具进行先验统计检验力分析非常方便,但是灵活性不高。在进行更复杂的检验时,我们可以使用前面使用过模拟的方式进行。
假设我们要进行一个2\*2被试内设计的实验,根据前人的研究我们设定groupA和groupB的四个水平的均值分别为A1B1 = 100, A1B2 = 80, A2B1 = 200, A2B2 = 250。
并且,我们主要关注和检验groupA和groupB的交互作用
---
### <h3 lang="zh-CN">代码实现</h3>
使用`faux`包生成2*2被试内的模拟数据
---
```r
df_aov <- sim_design(
n = 50, # 生成50名被试
within = list(groupA = c("A1", "A2"), groupB = c("B1", "B2")), # 生成被试内因子的列表
mu = c(100, 80, 200, 250), # 设置变量的均值
sd = c(A1 = 20, A2 = 10, B1 = 50, B2 = 90), # 设置变量的标准差
long = TRUE, # 输出长数据形式
dv = "score", # 因变量命名为score
plot = TRUE # 显示图
)
```
![](chapter_14_files/figure-html/unnamed-chunk-24-1.png)<!-- -->
```r
head(df_aov)
```
```
## id groupA groupB score
## 1 S01 A1 B1 148.03321
## 2 S02 A1 B1 103.66462
## 3 S03 A1 B1 124.13556
## 4 S04 A1 B1 113.45001
## 5 S05 A1 B1 57.60605
## 6 S06 A1 B1 71.01715
```
---
### <h3 lang="zh-CN">检查数据</h3>
我们检查一下模拟生成的数据
```r
df_aov %>%
group_by(groupA, groupB) %>%
summarise(
m = mean(score),
sd = sd(score)
)
```
```
## `summarise()` has grouped output by 'groupA'. You can override using the
## `.groups` argument.
```
```
## # A tibble: 4 × 4
## # Groups: groupA [2]
## groupA groupB m sd
## <fct> <fct> <dbl> <dbl>
## 1 A1 B1 99.1 21.9
## 2 A1 B2 80.8 9.46
## 3 A2 B1 202. 44.4
## 4 A2 B2 246. 75.6
```
---
### <h3 lang="zh-CN">计算交互项p值</h3>
使用`afex`包对数据进行重复测量方差分析,并输出交互项的*p*值
```r
aov <- aov_ez(id = "id", dv = "score", within = c("groupA", "groupB"), data = df_aov)
aov
```
```
## Anova Table (Type 3 tests)
##
## Response: score
## Effect df MSE F ges p.value
## 1 groupA 1, 49 2016.88 446.18 *** .690 <.001
## 2 groupB 1, 49 2078.12 3.76 + .019 .058
## 3 groupA:groupB 1, 49 2259.62 21.02 *** .105 <.001
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
```
```r
aov[["anova_table"]][["Pr(>F)"]][3]
```
```
## [1] 3.155556e-05
```
```r
aov[["anova_table"]][["Pr(>F)"]][3]
```
```
## [1] 3.155556e-05
```
---
## <h2 lang="zh-CN">基于模拟的统计检验力分析</h2>
进行统计检验力分析
```r
sim <- 1000 # 模拟次数为1000
power <- c() # 储存power值
n <- 5 # 第一组被试初识数量为10名
increase_num <- 0 # 从0开始计算power,即从每组10人开始计算
```
---
```r
for (i in 1:5000000) { # for循环给定一个极大的数进行遍历
pvals <- c() # 储存后续每一次模拟的p值
for (j in 1:sim) { # 开始对每一个组数量进行模拟
df_aov <- sim_design(
n = n,
within = list(groupA = c("A1", "A2"), groupB = c("B1", "B2")),
mu = c(100, 80, 200, 250),
sd = c(A1 = 20, A2 = 10, B1 = 50, B2 = 90),
long = TRUE,
dv = "score",
plot = FALSE
)
aov <- aov_ez(id = "id", dv = "score", within = c("groupA", "groupB"), data = df_aov)
pvals[j] <- aov[["anova_table"]][["Pr(>F)"]][3]
}
power[increase_num + 1] <- sum(pvals < 0.05) / sim
n <- n + 1 # 每次计算完power后,对每组加1
if (power[increase_num + 1] >= 0.8) {
break # 当power达到0.8时停止运行
}
increase_num <- increase_num + 1 # 增加的被试加1
}
```
---
```r
plot(5:(increase_num + 5), power,
xlab = "Number of subjects",
ylab = "power", ylim = c(0, 1.0))
abline(h = 0.8, col = "red")
```
![](chapter_14_files/figure-html/unnamed-chunk-30-1.png)<!-- -->
---
我们一共需要的样本量为:
```r
print(5 + increase_num)
```
```
## [1] 20
```
---
# <h1 lang="zh-CN">统计检验力分析小结</h1>
- 效应量、一类错误率、样本量在控制其他因素的情况下越大,统计检验力越高
- 可以通过模拟进行样本量规划
- 明确感兴趣的效应是进行统计检验力分析的重要步骤
---
# <h1 lang="zh-CN">推荐阅读</h1>
- Daniël Lakens; Sample Size Justification. *Collabra: Psychology* 5 January 2022; 8 (1): 33267. doi: https://doi.org/10.1525/collabra.33267
- DeBruine LM, Barr DJ. Understanding Mixed-Effects Models Through Data Simulation. *Advances in Methods and Practices in Psychological Science*. 2021;4(1). doi:10.1177/2515245920965119
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
// add `data-at-shortcutkeys` attribute to <body> to resolve conflicts with JAWS
// screen reader (see PR #262)
(function(d) {
let res = {};
d.querySelectorAll('.remark-help-content table tr').forEach(tr => {
const t = tr.querySelector('td:nth-child(2)').innerText;
tr.querySelectorAll('td:first-child .key').forEach(key => {
const k = key.innerText;
if (/^[a-z]$/.test(k)) res[k] = t; // must be a single letter (key)
});
});
d.body.setAttribute('data-at-shortcutkeys', JSON.stringify(res));
})(document);
(function() {
"use strict"
// Replace <script> tags in slides area to make them executable
var scripts = document.querySelectorAll(
'.remark-slides-area .remark-slide-container script'
);
if (!scripts.length) return;
for (var i = 0; i < scripts.length; i++) {
var s = document.createElement('script');
var code = document.createTextNode(scripts[i].textContent);
s.appendChild(code);
var scriptAttrs = scripts[i].attributes;
for (var j = 0; j < scriptAttrs.length; j++) {
s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
}
scripts[i].parentElement.replaceChild(s, scripts[i]);
}
})();
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};
for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');