forked from hcp4715/R4Psy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_7.html
873 lines (766 loc) · 53.5 KB
/
chapter_7.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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>chapter_7.knit</title>
<meta charset="utf-8" />
<meta name="author" content="" />
<script src="libs/header-attrs-2.17/header-attrs.js"></script>
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<script src="libs/htmlwidgets-1.5.4/htmlwidgets.js"></script>
<link href="libs/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="libs/datatables-binding-0.27/datatables.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<link href="libs/dt-core-1.12.1/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="libs/dt-core-1.12.1/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="libs/dt-core-1.12.1/js/jquery.dataTables.min.js"></script>
<link href="libs/crosstalk-1.2.0/css/crosstalk.min.css" rel="stylesheet" />
<script src="libs/crosstalk-1.2.0/js/crosstalk.min.js"></script>
<link rel="stylesheet" href="css/Custumed_Style.css" type="text/css" />
<link rel="stylesheet" href="css/zh-CN.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle
<span style="font-size: 60px;">第七章</span> <br>
<span style="font-size: 50px;">如何进行基本的数据分析 (一) </span> <br>
<span style="font-size: 50px;">*t*-test和ANOVA</span> <br>
<br>
<br>
<span style="font-size: 30px;">胡传鹏</span> <br>
<span style="font-size: 30px;">2023/04/10</span> <br>
---
class: center, middle
<span style="font-size: 60px;">7.1 *t*-test</span> <br>
---
# <h1 lang="zh-CN">回顾*t*检验基础知识</h1>
<br>
<center>
## One sample *t*-test:单样本*t*检验
</center>
<br>
<br>
### **Compare the sample mean against a fixed value.**
<br>
## The hypotheses:
* `\(H_0\)`: The current sample is sampled from a population with μ of the current value.
* `\(H_1\)`: The current sample is sampled from a population with μ that different from the current value.
* The significance level: `\(\alpha\)` = 0.05
---
# <h1 lang="zh-CN">回顾*t*检验基础知识 </h1>
<br>
## Typical research questions are:与某一特定值比较的三种情况(大于/小于或等于)
<br>
* whether the mean (m) of the sample is equal to the theoretical mean, `\(\mu\)`?
* whether the mean (m) of the sample is less than the theoretical mean, `\(\mu\)`?
* whether the mean (m) of the sample is greater than the theoretical mean, `\(\mu\)`?
---
# <h1 lang="zh-CN">回顾*t*检验基础知识</h1>
<br>
<h2 lang="en">In statistics, we can define the corresponding null hypothesis ($H_0$) as follow:</h2>
<br>
* `\(H_0\)`: m = μ
* `\(H_0\)`: m <= μ
* `\(H_0\)`: m => μ
<br>
<br>
<h2 lang="en">The corresponding alternative hypotheses (H1) are as follow:</h2>
<br>
<h3 lang="en">H1:m ≠ μ (not equal to)</h3>
<h3 lang="en">H1:m > μ (greater than)</h3>
<h3 lang="en">H1:m < μ (less than)</h3>
---
# <h1 lang="zh-CN">回顾*t*检验基础知识</h1>
<br>
<center>
<h2 lang="en">Two-sample *t*-test :双样本*t*检验</h2>
</center>
<br>
<h2 lang="en">Dependent(paried) *t*-test</h2>
<h3 lang="en">Population of difference scores has a normal distribution [population normality]</h3>
<h3 lang="en">Correlation between pairs of scores [dependent data]</h3>
<br>
<br>
<h2 lang="en">Independent *t*-test</h2>
<br>
<h3 lang="en">Population of difference scores has a normal distribution [population normality]</h3>
<h3 lang="en">The variances in two populations are similar [homogeneity of variance]</h3>
---
# <h1 lang="zh-CN">回顾*t*检验基础知识</h1>
<br>
<h2 lang="en">Dependent (Paired) *t*-test:配对样本*t*检验</h2>
<h3 lang="en">The paired *t*-test is used to compare the means between two related groups of samples. In this case, you have two values (i.e., pair of values) for the same samples.</h3>
<br>
<br>
<br>
<h2 lang="en">Independent *t*-test:独立样本*t*检验</h2>
<h2 lang="en">The independent samples *t*-test is used to compare the mean of two independent groups.</h2>
<br>
<h2 lang="en">Typical research questions are:</h2>
<h2 lang="en">whether the mean of group A (μ) is equal to the mean of group B (μ)?</h2>
<h2 lang="en">whether the mean of group A (μ) is less than the mean of group B (μ)?</h2>
<h2 lang="en">whether the mean of group A (μ) is greater than the mean of group B (μ)?</h2>
---
class: center, middle
<span style="font-size: 60px;">7.2 *t*-test in R</span> <br>
---
# <h1 lang="zh-CN">几个假想的问题</h1>
<br>
<br>
<h3 lang="zh-CN">1. “在penguin数据中,参与者对于家的依恋水平是否小于/等于/大于均值水平(假设在总体水平上,人们对家庭的依恋水平(HOME)均值为3.5)?”</h3>
<br>
<h3 lang="zh-CN">2. “在penguin数据中,男女生在亲密关系经验(ECR)中的得分是否存在显著差异?”</h3>
<br>
<h3 lang="zh-CN">3. “在penguin数据中,参与者在不同时间的温度差异是否具有显著性?”</h3>
---
# <h1 lang="zh-CN">载入包 - 设置工作路径 - 导入原始数据</h1>
```r
# 检查是否已安装 pacman
if (!requireNamespace("pacman", quietly = TRUE)) {
install.packages("pacman") } # 如果未安装,则安装包
# 使用p_load来载入需要的包
pacman::p_load("tidyverse", "bruceR")
```
```
##
## bruceR (version 0.8.9)
## BRoadly Useful Convenient and Efficient R functions
##
## Packages also loaded:
## √ dplyr √ emmeans √ ggplot2
## √ tidyr √ effectsize √ ggtext
## √ stringr √ performance √ cowplot
## √ forcats √ lmerTest √ see
## √ data.table
##
## Main functions of `bruceR`:
## cc() Describe() TTEST()
## add() Freq() MANOVA()
## .mean() Corr() EMMEANS()
## set.wd() Alpha() PROCESS()
## import() EFA() model_summary()
## print_table() CFA() lavaan_summary()
##
## https://psychbruce.github.io/bruceR/
##
## NEWS: A new version of bruceR (0.8.10) is available on 2023-03-03!
## ***** Please Update *****
## install.packages("bruceR", dep=TRUE)
##
## These R packages are dependencies of `bruceR` but not installed:
## lmtest, vars, phia, GGally, GPArotation
## ***** Please Install All Dependencies *****
## install.packages("bruceR", dep=TRUE)
```
---
# <h1 lang="zh-CN">载入包 - 设置工作路径 - 导入原始数据</h1>
```r
# WD <- here::here()
df.pg.raw <- read.csv('./data/penguin/penguin_rawdata.csv',
header = T, sep=",", stringsAsFactors = FALSE)
```
<div id="htmlwidget-6dfbbe551d46ce48a332" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-6dfbbe551d46ce48a332">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5","6","7","8","9","10"],[1975,1995,1995,1988,1991,1995,1996,1973,1996,1996],[1,2,4,2,2,2,1,3,2,3],[1,2,1,3,1,3,1,3,3,3],[1,2,1,4,1,1,1,2,1,1],[2,2,2,5,5,3,1,3,3,2],[2,2,4,3,2,2,4,4,2,4],[1,2,1,4,2,1,1,4,2,1],[2,2,4,2,3,1,2,4,2,2],[2,2,1,4,4,2,1,4,2,4],[4,4,1,4,1,1,4,4,2,4],[1,2,2,3,1,2,2,5,2,1],[1,2,1,2,2,1,1,3,1,4],[2,2,1,2,1,2,2,2,2,1],[4,2,2,4,2,3,4,2,2,2],[3,2,1,2,4,4,3,3,4,3],[4,3,1,4,4,5,5,4,3,5],[2,2,1,3,2,4,5,1,2,4],[3.72222222222222,4.05555555555556,1.44444444444444,4.72222222222222,2.11111111111111,2,2.55555555555556,3.05555555555556,3.55555555555556,4.38888888888889],[6,0,0,2,0,0,null,0,0,0],[3.11111111111111,4.88888888888889,1.33333333333333,3.88888888888889,4.66666666666667,3.33333333333333,3.11111111111111,3.55555555555556,3.33333333333333,3.22222222222222],[3.44444444444444,2,2,3,1.77777777777778,2.44444444444444,3.77777777777778,3.33333333333333,1.88888888888889,4.22222222222222],[89,89,89,89,89,89,89,89,89,89],[3.27777777777778,3,1.61111111111111,3.94444444444444,4.94444444444444,3.77777777777778,6.44444444444444,3.22222222222222,4,5.27777777777778],[15,null,null,null,null,null,null,10,null,1],[1.63636363636364,2.18181818181818,2,3.27272727272727,2.18181818181818,1.72727272727273,1.72727272727273,3.54545454545455,2,2.63636363636364],[1,1,1,2,1,1,1,1,1,1],[5,6,1,3,1,1,2,3,4,5],[5,4,1,6,1,1,1,2,4,6],[3,2,1,6,1,1,4,3,4,6],[5,5,1,5,1,2,1,2,4,5],[5,5,1,4,1,2,4,3,4,4],[3,2,1,4,5,1,1,3,1,3],[1,3,1,3,2,1,2,3,4,5],[6,5,1,3,1,2,7,3,4,7],[5,2,1,4,7,6,1,3,4,3],[2,3,1,4,1,2,4,1,4,3],[6,7,1,6,6,2,2,5,2,6],[3,4,1,5,1,2,1,2,4,4],[3,3,1,6,2,4,2,5,4,4],[2,3,7,5,1,1,1,4,1,2],[3,4,1,5,2,2,1,5,4,1],[3,5,3,5,1,1,2,3,4,4],[6,5,1,6,2,3,6,3,4,7],[1,5,1,5,2,2,4,2,4,4],[2,3,2,5,1,5,7,3,4,5],[2,4,2,5,7,5,1,2,4,7],[5,3,2,5,2,4,6,4,4,5],[3,2,2,4,5,4,7,3,4,5],[3,2,2,3,2,3,7,3,4,6],[2,3,1,4,4,4,7,3,4,5],[5,3,1,6,2,3,7,4,4,6],[5,4,1,3,6,3,7,4,4,7],[3,5,1,3,7,1,7,4,4,7],[2,3,1,4,6,3,6,3,4,4],[4,2,2,5,6,6,6,3,4,4],[2,2,2,3,6,5,7,2,4,5],[3,4,1,4,7,4,7,2,4,3],[3,2,2,5,2,3,7,3,4,6],[4,3,3,3,7,4,6,3,4,5],[5,3,1,2,6,5,7,4,4,6],[4,3,1,3,6,3,7,6,4,5],[2,3,2,4,7,3,7,2,4,4],["9:23:38","9:23:26","8:57:08","8:55:14","8:54:35","8:39:12","8:32:08","8:28:57","8:28:03","8:25:57"],[3,2.2,1.2,3,2.6,3.6,3.8,2.4,2.6,3],[2,2,2,2,2,2,2,2,2,2],[0,0,12,0,5,0,null,1,0,2],[4,4,4,3,4,4,4,1,3,3],[4,5,1,5,5,4,5,3,3,4],[3,5,1,4,5,4,2,3,3,5],[4,5,4,5,5,4,4,2,4,3],[3,5,1,5,5,4,5,4,5,3],[3,5,1,4,4,4,2,3,4,4],[2,4,1,2,5,2,1,4,3,2],[4,5,1,2,4,2,5,5,2,2],[3,5,1,4,4,3,1,4,3,3],[2,5,1,4,5,3,3,4,3,3],[4,1,5,5,1,3,2,3,3,4],[4,2,6,4,1,3,2,3,3,5],[5,6,5,3,3,3,2,2,2,1],[4,2,4,1,2,2,2,2,1,3],[4,3,3,2,2,2,2,2,3,2],[1,2,1,2,2,4,2,3,3,3],[2,2,1,2,1,4,2,3,1,2],[17,28,24,19,29,33,53,17,17,19],[4.33333333333333,6.5,6.83333333333333,3.5,1.5,2,5.83333333333333,6.33333333333333,5.66666666666667,5.16666666666667],[2.27272727272727,2.63636363636364,2.18181818181818,2,1.90909090909091,2.45454545454545,2.81818181818182,2.54545454545455,1.81818181818182,3.90909090909091],[2,3,3,1,3,4,4,2,2,5],[2,2,4,1,2,4,4,3,3,4],[2,3,2,2,1,2,2,3,2,5],[4,3,4,3,4,4,2,2,2,4],[2,3,1,2,1,3,3,3,2,3],[2,2,1,2,1,1,2,2,1,3],[2,2,1,2,1,2,2,4,2,4],[2,3,3,1,1,1,2,3,2,3],[3,3,3,2,2,2,4,2,1,3],[2,3,1,4,2,1,2,1,1,4],[2,2,1,2,3,3,4,3,2,5],[4,2,1,2,2,3,3,3,3,4],[4,2,4,4,1,3,4,4,2,4],[2,2,1,3,1,2,5,3,1,2],[5,2,4,2,1,3,4,3,4,4],[2,2,1,3,1,2,1,2,3,5],[2,2,1,2,3,2,3,4,1,4],[4,2,1,3,2,2,5,3,1,5],[4,2,1,4,1,3,5,4,1,5],[4,2,4,4,4,2,4,4,1,5],[2,2,1,1,2,2,2,2,2,2],[2,4,2,4,3,5,2,1,3,3],[2,4,3,2,5,5,4,4,3,4],[2,4,3,1,5,5,2,5,5,5],[1,4,2,1,3,2,2,2,5,1],[1,4,4,2,5,3,1,2,5,2],[4,3,2,1,5,4,3,3,3,1],[2,4,4,2,5,3,2,3,4,2],[2,2,1,3,4,3,2,2,5,4],[5,4,3,4,4,5,4,4,3,3],[4,3,5,3,3,5,5,3,4,2],[3,4,4,2,4,5,3,2,2,5],[5,5,4,2,4,4,1,3,4,3],[4,4,4,2,4,3,2,2,2,2],[2.84615384615385,3.76923076923077,3.15384615384615,2.23076923076923,4.15384615384615,4,2.53846153846154,2.76923076923077,3.69230769230769,2.84615384615385],[1,1,1,2,1,1,2,1,1,2],[1,2,2,2,2,2,2,1,2,1],[2,1,1,1,2,2,2,4,2,2],[1,1,1,1,1,1,1,1,1,1],[null,null,null,null,null,null,null,null,null,null],[4,4,4,4,2,4,4,4,4,4],[4,4,4,4,2,4,4,2,4,4],[5,1,4,5,5,5,5,5,5,5],[null,null,4,null,null,null,null,null,null,null],[1,8,2,1,8,2,3,8,2,6],[null,6,2,null,5,1,2,3,2,3],[6,4,5,5,7,5,8,4,3,8],[3,1,5,1,3,5,8,4,2,8],[1,1,1,1,2,1,1,1,1,1],[null,null,null,null,8,null,null,null,null,null],[1,2,2,1,1,2,2,2,2,2],[null,8,8,null,null,8,8,8,8,8],[3,3,2,3,3,1,1,1,1,1],[2,8,1,8,1,null,null,null,null,null],[6,5,6,4,8,null,null,null,null,null],[1,3,1,1,1,8,6,4,1,3],[1,1,1,1,1,1,1,2,1,1],[null,null,null,null,null,null,null,2,null,null],[1,1,1,2,1,2,2,2,2,1],[" "," "," "," "," ","Rowing Club","University Friends","ODAA","Oxford Latin Speaking Society"," "],[" "," "," "," "," ","Rugby Club","Business Social"," ","Oxford Latin Conversation Society"," "],[" "," "," "," "," "," ","Lecturers/ Advisors"," ","Plato Reading Group"," "],[" "," "," "," "," "," ","Friends from home"," "," "," "],[" "," "," "," "," "," ","Neighbors"," "," "," "],[null,null,null,null,null,7,20,1,2,null],[null,null,null,null,null,7,15,null,4,null],[null,null,null,null,null,null,5,null,1,null],[null,null,null,null,null,null,9,null,null,null],[null,null,null,null,null,null,6,null,null,null],[6,6,7,2,1,1,6,6,7,7],[5,7,7,5,2,2,7,6,7,4],[5,6,7,3,2,3,5,7,6,5],[3,7,7,4,1,2,5,6,6,5],[2,6,7,5,2,2,6,7,5,5],[5,7,6,2,1,2,6,6,3,5],[4,6,6,4,1,2,5,6,3,4],[5,7,8,5,7,6,null,7,6,6],[1,2,3,1,2,4,null,2,2,3],[4,2,4,4,1,2,2,3,3,1],[4,4,5,2,1,4,4,2,5,4],[4,3,4,3,1,4,2,3,4,4],[5,3,4,3,4,4,4,4,2,4],[2,3,4,4,4,4,5,2,2,3],[2,4,4,3,1,3,4,3,1,3],[2,5,4,3,3,4,5,4,2,2],[2,2,4,3,4,3,5,3,2,4],[4,5,5,2,1,2,4,2,4,5],[2,3,2,2,1,4,1,4,4,5],[2,2,1,2,1,2,2,2,2,1],[1,2,2,2,4,4,4,2,2,2],[4,4,4,3,4,2,2,3,2,1],[5,4,2,3,2,4,3,4,4,4],[4,4,2,3,3,1,2,4,5,3],[2,3,4,4,4,5,5,4,4,4],[2,3,5,4,4,3,2,4,2,2],[4,3,4,2,4,3,4,3,3,2],[3,3,5,2,4,4,4,4,3,3],[2,3,1,3,5,4,1,3,2,4],[4,3,4,2,4,4,5,4,3,5],[3,3,5,4,4,3,2,2,3,3],[2,3,1,3,4,3,2,3,4,3],[3,3,1,4,1,2,2,2,3,3],[4,3,5,3,1,2,5,1,4,4],[3,3,3,2,1,3,3,2,3,3],[4,3,1,4,4,4,2,4,1,1],[4,5,4,2,1,3,1,4,4,5],[3,5,4,4,1,2,1,3,5,5],[3,2,2,4,4,2,5,2,2,4],[2,4,2,5,1,2,1,3,2,4],[4,3,1,2,4,2,5,1,1,4],[4,3,2,2,3,3,1,4,3,4],[1,4,3,4,4,2,1,3,2,2],[2,4,2,3,5,4,2,5,1,4],[2,4,2,4,5,4,4,4,2,4],[4,4,4,3,4,4,2,4,2,4],[3,4,2,4,4,4,4,5,5,1],[4,2,2,3,4,4,5,2,2,4],[5,3,3,2,4,2,2,2,2,4],[4,3,2,3,3,3,2,1,2,4],[4,3,5,2,4,4,4,4,4,4],[1,4,2,3,4,2,2,3,2,3],[1,2,2,4,4,2,1,2,1,2],[4,3,2,3,1,2,1,4,4,2],[4,4,4,2,1,4,1,4,3,3],[3,3,4,3,4,2,4,3,3,4],[1,2,5,4,4,2,1,2,1,2],[2,3,5,4,4,2,1,2,1,2],[4,3,5,4,4,4,3,3,3,4],[4,4,2,5,1,3,5,2,4,5],[2,4,5,3,1,2,5,4,2,4],[2,4,4,3,4,2,1,3,2,2],[4,3,4,3,5,2,4,4,2,4],[2,4,2,2,4,3,3,2,2,4],[2,4,1,2,4,2,2,2,2,2],[1,2,5,4,1,1,4,2,2,2],[3.07692307692308,2.46153846153846,2.38461538461538,2.84615384615385,2.53846153846154,2.07692307692308,1.53846153846154,3,2.61538461538462,3.15384615384615],[4,2,3,3,3,2,1,4,5,3],[2,3,3,3,4,2,2,3,2,4],[4,3,4,4,3,3,1,3,3,5],[4,3,1,2,2,2,2,3,2,4],[4,2,2,3,2,2,2,3,2,3],[3,2,1,2,1,2,2,3,2,1],[4,2,2,3,3,2,1,4,2,3],[3,2,2,2,2,2,1,4,3,4],[3,2,3,3,2,2,2,1,2,3],[2,3,3,3,3,2,1,3,4,3],[4,2,3,4,3,2,2,2,1,2],[3,4,4,3,5,4,4,4,5,4],[2,4,2,3,2,2,2,3,3,2],[1,2,2,2,3,2,1,3,3,4],[34.9,35.8,34,36.9,35.3,34.7,36,36.5,34.7,35.9],[35.9,36,36.2,37,36.1,36.4,36.3,36.6,36.3,36.3],[2,2,2,2,4,4,2,2,2,2],[35.4,35.9,35.1,36.95,35.7,35.55,36.15,36.55,35.5,36.1],[1,1,1,1,1,1,1,1,1,1],[-1.66666666666667,-1.66666666666667,-1.66666666666667,-1.66666666666667,-1.66666666666667,-1.66666666666667,-1.66666666666667,-1.66666666666667,-1.66666666666667,-1.66666666666667],[5,5,5,5,5,5,5,5,5,5],[1,1,1,1,1,1,1,1,1,1],[0.186319647739787,0.472682850177291,-1.77049556891649,1.0454092550523,-1.19776916404149,-1.29322356485399,-0.815951560791479,-0.386406757135223,0.0431380465210342,0.759046052614796],[-0.00190690952064737,-0.249805147204807,-1.48929633562561,0.593048860921336,1.48548251658431,0.44430991831084,2.82413300007877,-0.0514865570574791,0.642628508458168,1.7829604018053],["Oxford","Oxford","Oxford","Oxford","Oxford","Oxford","Oxford","Oxford","Oxford","Oxford"]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>age<\/th>\n <th>ALEX1<\/th>\n <th>ALEX2<\/th>\n <th>ALEX3<\/th>\n <th>ALEX4<\/th>\n <th>ALEX5<\/th>\n <th>ALEX6<\/th>\n <th>ALEX7<\/th>\n <th>ALEX8<\/th>\n <th>ALEX9<\/th>\n <th>ALEX10<\/th>\n <th>ALEX11<\/th>\n <th>ALEX12<\/th>\n <th>ALEX13<\/th>\n <th>ALEX14<\/th>\n <th>ALEX15<\/th>\n <th>ALEX16<\/th>\n <th>anxiety<\/th>\n <th>artgluctot<\/th>\n <th>attachhome<\/th>\n <th>attachphone<\/th>\n <th>AvgHumidity<\/th>\n <th>avoidance<\/th>\n <th>cigs<\/th>\n <th>DIDF<\/th>\n <th>eatdrink<\/th>\n <th>ECR1<\/th>\n <th>ECR2<\/th>\n <th>ECR3<\/th>\n <th>ECR4<\/th>\n <th>ECR5<\/th>\n <th>ECR6<\/th>\n <th>ECR7<\/th>\n <th>ECR8<\/th>\n <th>ECR9<\/th>\n <th>ECR10<\/th>\n <th>ECR11<\/th>\n <th>ECR12<\/th>\n <th>ECR13<\/th>\n <th>ECR14<\/th>\n <th>ECR15<\/th>\n <th>ECR16<\/th>\n <th>ECR17<\/th>\n <th>ECR18<\/th>\n <th>ECR19<\/th>\n <th>ECR20<\/th>\n <th>ECR21<\/th>\n <th>ECR22<\/th>\n <th>ECR23<\/th>\n <th>ECR24<\/th>\n <th>ECR25<\/th>\n <th>ECR26<\/th>\n <th>ECR27<\/th>\n <th>ECR28<\/th>\n <th>ECR29<\/th>\n <th>ECR30<\/th>\n <th>ECR31<\/th>\n <th>ECR32<\/th>\n <th>ECR33<\/th>\n <th>ECR34<\/th>\n <th>ECR35<\/th>\n <th>ECR36<\/th>\n <th>endtime<\/th>\n <th>EOT<\/th>\n <th>exercise<\/th>\n <th>gluctot<\/th>\n <th>health<\/th>\n <th>HOME1<\/th>\n <th>HOME2<\/th>\n <th>HOME3<\/th>\n <th>HOME4<\/th>\n <th>HOME5<\/th>\n <th>HOME6<\/th>\n <th>HOME7<\/th>\n <th>HOME8<\/th>\n <th>HOME9<\/th>\n <th>KAMF1<\/th>\n <th>KAMF2<\/th>\n <th>KAMF3<\/th>\n <th>KAMF4<\/th>\n <th>KAMF5<\/th>\n <th>KAMF6<\/th>\n <th>KAMF7<\/th>\n <th>networksize<\/th>\n <th>nostalgia<\/th>\n <th>onlineid<\/th>\n <th>onlineid1<\/th>\n <th>onlineid2<\/th>\n <th>onlineid3<\/th>\n <th>onlineid4<\/th>\n <th>onlineid5<\/th>\n <th>onlineid6<\/th>\n <th>onlineid7<\/th>\n <th>onlineid8<\/th>\n <th>onlineid9<\/th>\n <th>onlineid10<\/th>\n <th>onlineide11<\/th>\n <th>phone1<\/th>\n <th>phone2<\/th>\n <th>phone3<\/th>\n <th>phone4<\/th>\n <th>phone5<\/th>\n <th>phone6<\/th>\n <th>phone7<\/th>\n <th>phone8<\/th>\n <th>phone9<\/th>\n <th>romantic<\/th>\n <th>scontrol1<\/th>\n <th>scontrol2<\/th>\n <th>scontrol3<\/th>\n <th>scontrol4<\/th>\n <th>scontrol5<\/th>\n <th>scontrol6<\/th>\n <th>scontrol7<\/th>\n <th>scontrol8<\/th>\n <th>scontrol9<\/th>\n <th>scontrol10<\/th>\n <th>scontrol11<\/th>\n <th>scontrol12<\/th>\n <th>scontrol13<\/th>\n <th>selfcontrol<\/th>\n <th>sex<\/th>\n <th>smoke<\/th>\n <th>SNI1<\/th>\n <th>SNI2<\/th>\n <th>SNI3<\/th>\n <th>SNI4<\/th>\n <th>SNI5<\/th>\n <th>SNI6<\/th>\n <th>SNI7<\/th>\n <th>SNI8<\/th>\n <th>SNI9<\/th>\n <th>SNI10<\/th>\n <th>SNI11<\/th>\n <th>SNI12<\/th>\n <th>SNI13<\/th>\n <th>SNI14<\/th>\n <th>SNI15<\/th>\n <th>SNI16<\/th>\n <th>SNI17<\/th>\n <th>SNI18<\/th>\n <th>SNI19<\/th>\n <th>SNI20<\/th>\n <th>SNI21<\/th>\n <th>SNI22<\/th>\n <th>SNI23<\/th>\n <th>SNI24<\/th>\n <th>SNI25<\/th>\n <th>SNI26<\/th>\n <th>SNI27<\/th>\n <th>SNI28<\/th>\n <th>SNI29<\/th>\n <th>SNI30<\/th>\n <th>SNI31<\/th>\n <th>SNI32<\/th>\n <th>SNS1<\/th>\n <th>SNS2<\/th>\n <th>SNS3<\/th>\n <th>SNS4<\/th>\n <th>SNS5<\/th>\n <th>SNS6<\/th>\n <th>SNS7<\/th>\n <th>socialdiversity<\/th>\n <th>socialembedded<\/th>\n <th>STRAQ_1<\/th>\n <th>STRAQ_2<\/th>\n <th>STRAQ_3<\/th>\n <th>STRAQ_4<\/th>\n <th>STRAQ_6<\/th>\n <th>STRAQ_7<\/th>\n <th>STRAQ_8<\/th>\n <th>STRAQ_9<\/th>\n <th>STRAQ_10<\/th>\n <th>STRAQ_11<\/th>\n <th>STRAQ_12<\/th>\n <th>STRAQ_19<\/th>\n <th>STRAQ_20<\/th>\n <th>STRAQ_21<\/th>\n <th>STRAQ_22<\/th>\n <th>STRAQ_23<\/th>\n <th>STRAQ_24<\/th>\n <th>STRAQ_25<\/th>\n <th>STRAQ_26<\/th>\n <th>STRAQ_27<\/th>\n <th>STRAQ_28<\/th>\n <th>STRAQ_29<\/th>\n <th>STRAQ_30<\/th>\n <th>STRAQ_31<\/th>\n <th>STRAQ_32<\/th>\n <th>STRAQ_33<\/th>\n <th>STRAQ_5<\/th>\n <th>STRAQ_13<\/th>\n <th>STRAQ_14<\/th>\n <th>STRAQ_15<\/th>\n <th>STRAQ_16<\/th>\n <th>STRAQ_17<\/th>\n <th>STRAQ_18<\/th>\n <th>STRAQ_34<\/th>\n <th>STRAQ_35<\/th>\n <th>STRAQ_36<\/th>\n <th>STRAQ_37<\/th>\n <th>STRAQ_38<\/th>\n <th>STRAQ_39<\/th>\n <th>STRAQ_40<\/th>\n <th>STRAQ_41<\/th>\n <th>STRAQ_42<\/th>\n <th>STRAQ_43<\/th>\n <th>STRAQ_44<\/th>\n <th>STRAQ_45<\/th>\n <th>STRAQ_46<\/th>\n <th>STRAQ_47<\/th>\n <th>STRAQ_48<\/th>\n <th>STRAQ_49<\/th>\n <th>STRAQ_50<\/th>\n <th>STRAQ_51<\/th>\n <th>STRAQ_52<\/th>\n <th>STRAQ_53<\/th>\n <th>STRAQ_54<\/th>\n <th>STRAQ_55<\/th>\n <th>STRAQ_56<\/th>\n <th>STRAQ_57<\/th>\n <th>stress<\/th>\n <th>stress1<\/th>\n <th>stress2<\/th>\n <th>stress3<\/th>\n <th>stress4<\/th>\n <th>stress5<\/th>\n <th>stress6<\/th>\n <th>stress7<\/th>\n <th>stress8<\/th>\n <th>stress9<\/th>\n <th>stress10<\/th>\n <th>stress11<\/th>\n <th>stress12<\/th>\n <th>stress13<\/th>\n <th>stress14<\/th>\n <th>Temperature_t1<\/th>\n <th>Temperature_t2<\/th>\n <th>thermotype<\/th>\n <th>avgtemp<\/th>\n <th>filter_.<\/th>\n <th>mintemp<\/th>\n <th>language<\/th>\n <th>langfamily<\/th>\n <th>Zanxiety<\/th>\n <th>Zavoidance<\/th>\n <th>Site<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":5,"columnDefs":[{"className":"dt-right","targets":[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,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,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]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[5,10,25,50,100]}},"evals":[],"jsHooks":[]}</script>
---
# <h1 lang="zh-CN">准备数据</h1>
# <h3 lang="zh-CN">对感兴趣问题所涉及题目求均值</h3>
```r
df.pg.mean <- df.pg.raw %>%
dplyr::filter(sex > 0 & sex < 3) %>% # 筛选出男性和女性的数据
dplyr::mutate(ECR_mean=rowMeans(select(., starts_with("ECR")), na.rm = T),
HOME_mean=rowMeans(select(., starts_with("HOME")), na.rm = T),
sex=as.factor(sex)
) %>%
dplyr::select(age, sex, romantic, ECR_mean, HOME_mean, Temperature_t1, Temperature_t2)
```
<div id="htmlwidget-ec9b6318d91b5cce956e" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-ec9b6318d91b5cce956e">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5"],[1975,1995,1995,1988,1991],["1","1","1","2","1"],[2,2,1,1,2],[3.5,3.52777777777778,1.52777777777778,4.33333333333333,3.52777777777778],[3.11111111111111,4.88888888888889,1.33333333333333,3.88888888888889,4.66666666666667],[34.9,35.8,34,36.9,35.3],[35.9,36,36.2,37,36.1]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>age<\/th>\n <th>sex<\/th>\n <th>romantic<\/th>\n <th>ECR_mean<\/th>\n <th>HOME_mean<\/th>\n <th>Temperature_t1<\/th>\n <th>Temperature_t2<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":5,"columnDefs":[{"className":"dt-right","targets":[1,3,4,5,6,7]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[5,10,25,50,100]}},"evals":[],"jsHooks":[]}</script>
---
# <h1 lang="zh-CN">TTEST():bruceR中*t*检验函数的参数</h1>
![](./picture/chp7/process_19.png)
---
# <h1 lang="en">bruceR::TTEST</h1>
## <h2 lang="zh-CN">问题1: 单样本*t*检验</h2>
```r
# bruceR::TTEST(df.pg.mean, "HOME_mean", test.value = 3.5, test.sided = ">")
# bruceR::TTEST(df.pg.mean, "HOME_mean", test.value = 3.5, test.sided = "<")
result.ttest <- capture.output({
bruceR::TTEST(data=df.pg.mean,
y="HOME_mean",
test.value = 3.5,
test.sided = "=",
file = "./output/chp7/one_sample_t.doc")
})
```
```
## t is large; approximation invoked.
```
```r
writeLines(result.ttest, "./output/chp7/single_t.md") # .md最整齐
```
---
# <h1 lang="en">bruceR::TTEST</h1>
## <h2 lang="en">OUTPUT</h2>
![](./picture/chp7/process_1.png)
---
# <h1 lang="en">bruceR::TTEST</h1>
## <h2 lang="zh-CN">问题2: 独立样本*t*检验</h2>
```r
result.ttest <- capture.output({
bruceR::TTEST(data=df.pg.mean,
y="ECR_mean",
x="sex")
})
writeLines(result.ttest, "./output/chp7/inde_t.md")
```
---
# <h1 lang="en">bruceR::TTEST</h1>
## <h2 lang="en">OUTPUT</h2>
![](./picture/chp7/process_2.png)
---
# <h1 lang="en">bruceR::TTEST</h1>
## <h2 lang="zh-CN">问题3: 配对样本*t*检验</h2>
```r
result.ttest <- capture.output({
bruceR::TTEST(data=df.pg.mean,
y = c("Temperature_t1",
"Temperature_t2"),
paired = T) #是否为配对样本*t*检验?默认是FALSE
})
```
```
## t is large; approximation invoked.
```
```r
writeLines(result.ttest, "./output/chp7/pair_t.md")
```
---
# <h1 lang="en">bruceR::TTEST</h1>
## <h2 lang="en">OUTPUT</h2>
![](./picture/chp7/process_3.png)
---
# <h1 lang="en">Reporting *t*-test results in APA</h1>
<br>
<br>
<h3 lang="en">The basic format for reporting the result of a *t*-test is the same in each case:</h3>
<h3 lang="en">*t*(degress of freedom) = the t-value , *p* = p-value, Cohen's *d* = effect size</h3>
<br>
![](./picture/chp7/process_8.png)
<h4 lang="zh-CN">以上均与假想的问题无关,大家可结合自己的数据改写代码</h4>
---
class: center, middle
<span style="font-size: 60px;">7.3 anova</span> <br>
---
# <h1 lang="zh-CN">回顾ANOVA的基础知识</h1>
![](./picture/chp7/process_4.png)
<br>
---
# <h1 lang="zh-CN">回顾ANOVA的基础知识</h1>
![](./picture/chp7/process_5.png)
---
# <h1 lang="zh-CN">回顾ANOVA的基础知识</h1>
![](./picture/chp7/process_6.png)
---
# <h1 lang="zh-CN">回顾ANOVA的基础知识</h1>
<br>
<h2 lang="zh-CN">多因素方差分析概述</h2>
<br>
<h3 lang="zh-CN">多因素方差分析原理与单因素方差分析基本一致,即利用方差比较的方法,通过假设检验的过程来判断多个因素是否对因变量产生显著性影响。当有两个及以上因素对因变量产生影响时,使用多因素方差分析。多因素方差分析不仅要考虑每个因素的主效应,往往还要考虑因素之间的交互效应。</h3>
<br>
![](./picture/chp7/process_7.png)
---
class: center, middle
<span style="font-size: 60px;">7.4 anova in R</span> <br>
---
# <h1 lang="zh-CN">假想的问题</h1>
<br>
<br>
<h2 lang="zh-CN">1. “在match数据中,被试在匹配任务和不匹配任务上的反应时是否存在差异?”</h2>
<br>
<h2 lang="zh-CN">2. “在match数据中,被试在不同身份(self vs other)与不同效价(moral vs immoral)的条件组合下反应时是否存在差异?”</h2>
<br>
---
# <h1 lang="zh-CN">载入包</h1>
```r
pacman::p_load("tidyverse", "bruceR")
```
---
# <h1 lang="zh-CN">设置工作路径 - 导入数据与预处理</h1>
```r
df.match <- read.csv('./data/match/match_raw.csv',
header = T, sep=",", stringsAsFactors = FALSE) %>%
# 拆分单元格内字符串
tidyr::separate(col=Shape,
into=c("Valence","Identity"),
sep="(?<=moral|immoral)(?=Self|Other)") %>%
dplyr::select(Sub, Valence, Identity, everything()) %>%
dplyr::filter(ACC == 1) %>% # 只选择回答正确的数据
dplyr::filter(!is.na(RT)) %>% # 剔除缺失值
# remove outliers below and above 3rd sd
dplyr::filter(RT > quantile(RT, 0.0015) & RT < quantile(RT, 0.9985)) %>%
dplyr::mutate(RT = as.numeric(RT)) %>%
dplyr::mutate(Valence = as.factor(Valence),
Identity = as.factor(Identity))
```
---
<br>
<br>
```r
DT::datatable(head(df.match, 10),
fillContainer = TRUE, options = list(pageLength = 5))
```
<div id="htmlwidget-2d599b408819a5a6ba11" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-2d599b408819a5a6ba11">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5","6","7","8","9","10"],[7302,7302,7302,7302,7302,7302,7302,7302,7302,7302],["moral","immoral","immoral","immoral","moral","moral","moral","immoral","moral","immoral"],["Other","Other","Self","Self","Other","Self","Other","Self","Other","Other"],["02-May-2018_14:23:08","02-May-2018_14:23:10","02-May-2018_14:23:15","02-May-2018_14:23:17","02-May-2018_14:23:19","02-May-2018_14:23:21","02-May-2018_14:23:24","02-May-2018_14:23:26","02-May-2018_14:23:28","02-May-2018_14:23:30"],["Exp","Exp","Exp","Exp","Exp","Exp","Exp","Exp","Exp","Exp"],[22,22,22,22,22,22,22,22,22,22],["female","female","female","female","female","female","female","female","female","female"],["R","R","R","R","R","R","R","R","R","R"],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[2,3,5,6,7,8,9,10,11,12],["moralOther","immoralOther","immoralSelf","immoralSelf","moralOther","moralSelf","moralOther","immoralSelf","moralOther","immoralOther"],["mismatch","mismatch","match","match","match","match","mismatch","mismatch","mismatch","match"],["n","n","m","m","m","m","n","n","n","m"],["n","n","m","m","m","m","n","n","n","m"],[1,1,1,1,1,1,1,1,1,1],[0.7043,0.9903,0.8207,0.7547,0.5429,0.9009,0.9551,0.6952,0.7593,0.7135]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>Sub<\/th>\n <th>Valence<\/th>\n <th>Identity<\/th>\n <th>Date<\/th>\n <th>Prac<\/th>\n <th>Age<\/th>\n <th>Sex<\/th>\n <th>Hand<\/th>\n <th>Block<\/th>\n <th>Bin<\/th>\n <th>Trial<\/th>\n <th>Label<\/th>\n <th>Match<\/th>\n <th>CorrResp<\/th>\n <th>Resp<\/th>\n <th>ACC<\/th>\n <th>RT<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":5,"columnDefs":[{"className":"dt-right","targets":[1,6,9,10,11,16,17]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[5,10,25,50,100]}},"evals":[],"jsHooks":[]}</script>
---
# <h1 lang="zh-CN">CODE_问题1处理</h1>
# <h2 lang="zh-CN">计算被试匹配与不匹配任务上的平均反应时间及其标准差</h2>
```r
df.match.mean <- df.match %>%
dplyr::group_by(Sub, Match) %>%
dplyr::summarise(
n = n(),
rt_sd = sd(as.numeric(RT), na.rm = T),
rt_mean = mean(RT, na.rm = T), # 对于factor数据,先转成character,再变成numeric
) %>%
dplyr::ungroup()
```
```
## `summarise()` has grouped output by 'Sub'. You can override using the `.groups`
## argument.
```
---
<br>
<br>
```r
DT::datatable(head(df.match.mean, 10),
fillContainer = TRUE, options = list(pageLength = 5))
```
<div id="htmlwidget-9123a1bb6e93eaccd389" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-9123a1bb6e93eaccd389">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5","6","7","8","9","10"],[7302,7302,7303,7303,7304,7304,7305,7305,7306,7306],["match","mismatch","match","mismatch","match","mismatch","match","mismatch","match","mismatch"],[160,149,148,133,245,194,274,226,270,252],[0.106612719566056,0.0994899939093511,0.115424425115395,0.0881816560548197,0.137261728375444,0.106883820747074,0.128272398592039,0.121570749496483,0.135007585074087,0.109344555569399],[0.66311875,0.743634228187919,0.704106756756757,0.756554887218045,0.681796326530612,0.751001546391753,0.604401094890511,0.719050442477876,0.708901481481481,0.77705]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>Sub<\/th>\n <th>Match<\/th>\n <th>n<\/th>\n <th>rt_sd<\/th>\n <th>rt_mean<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":5,"columnDefs":[{"className":"dt-right","targets":[1,3,4,5]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[5,10,25,50,100]}},"evals":[],"jsHooks":[]}</script>
---
# <h1 lang="zh-CN">CODE_问题1处理</h1>
# <h2 lang="zh-CN">单因素被试内设计</h2>
```r
#比较被试在匹配任务和不匹配任务上的反应时是否存在差异
#单因素被试内设计(长型数据)
result.anova <- capture.output({
df_within <- df.match.mean %>%
bruceR::MANOVA(subID="Sub", # 被试id
dv="rt_mean", # 因变量,因为是MANOVA,实际上因变量可以再后面设置很多个
within="Match") # 设置条件,因素分析的条件
})
```
```
##
## * Data are aggregated to mean (across items/trials)
## if there are >=2 observations per subject and cell.
## You may use Linear Mixed Model to analyze the data,
## e.g., with subjects and items as level-2 clusters.
```
```r
writeLines(result.anova, "./output/chp7/anova_1.md")
# 若不符合球形假设要加上:sph.correction = "GG"
```
---
# <h1 lang="en">bruceR::MANOVA</h1>
## <h2 lang="en">OUTPUT</h2>
![](./picture/chp7/process_10.png)
---
# <h1 lang="zh-CN">ANOVA问题2</h1>
# <h2 lang="zh-CN">计算被试在不同身份和效价条件下的平均反应时间及其标准差</h2>
```r
df.match.mean <- df.match %>%
dplyr::group_by(Sub,Sex, Valence,Identity) %>%
dplyr::summarise(
n = n(),
rt_sd = sd(as.numeric(RT), na.rm = T),
rt_mean = mean(as.numeric(RT), na.rm = T)
) %>%
dplyr::ungroup()
```
```
## `summarise()` has grouped output by 'Sub', 'Sex', 'Valence'. You can override
## using the `.groups` argument.
```
---
<br>
<br>
```r
DT::datatable(head(df.match.mean, 10),
fillContainer = TRUE, options = list(pageLength = 5))
```
<div id="htmlwidget-f8aa113af01315c08456" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-f8aa113af01315c08456">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5","6","7","8","9","10"],[7302,7302,7302,7302,7303,7303,7303,7303,7304,7304],["female","female","female","female","male","male","male","male","female","female"],["immoral","immoral","moral","moral","immoral","immoral","moral","moral","immoral","immoral"],["Other","Self","Other","Self","Other","Self","Other","Self","Other","Self"],[78,76,84,71,75,67,69,70,95,114],[0.106546391467748,0.10670443377723,0.114524632384628,0.103786896685185,0.0914622051219287,0.0996700756728033,0.118901185681732,0.109463312146144,0.110340817703657,0.118828048280786],[0.705970512820513,0.724085526315789,0.661688095238095,0.721443661971831,0.741282666666667,0.753758208955224,0.720326086956522,0.700415714285714,0.756267368421053,0.729615789473684]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>Sub<\/th>\n <th>Sex<\/th>\n <th>Valence<\/th>\n <th>Identity<\/th>\n <th>n<\/th>\n <th>rt_sd<\/th>\n <th>rt_mean<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":5,"columnDefs":[{"className":"dt-right","targets":[1,5,6,7]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[5,10,25,50,100]}},"evals":[],"jsHooks":[]}</script>
---
# <h1 lang="zh-CN">MANOVA():多因素被试间/被试内/混合设计方差分析ANOVA</h1>
# <h2 lang="zh-CN">主要参数</h2>
![](./picture/chp7/process_16.png)
---
# <h1 lang="zh-CN">问题2的R语法</h1>
# <h2 lang="zh-CN">长数据转换为宽数据</h2>
```r
df.match.within <- df.match.mean %>%
dplyr::select(-c(n, rt_sd)) %>%
dplyr::mutate(Valence = paste("A_", Valence, sep = ""),#将变量的名称进行修改转换,不生产新的变量
Identity = paste("B_", Identity, sep = "")) %>%
# 将morality和identity组合名称起来生成一个新的变量"Conds"
tidyr::unite("Conds", Valence:Identity, sep = "&",remove=TRUE) %>%
tidyr::pivot_wider(names_from = Conds,
values_from = rt_mean)
head(df.match.within)
```
```
## # A tibble: 6 × 6
## Sub Sex `A_immoral&B_Other` `A_immoral&B_Self` `A_moral&B_Other` A_mora…¹
## <int> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 7302 female 0.706 0.724 0.662 0.721
## 2 7303 male 0.741 0.754 0.720 0.700
## 3 7304 female 0.756 0.730 0.757 0.628
## 4 7305 male 0.685 0.659 0.641 0.638
## 5 7306 male 0.778 0.763 0.660 0.771
## 6 7307 female 0.751 0.677 0.716 0.728
## # … with abbreviated variable name ¹`A_moral&B_Self`
```
```r
#str(df.match.within)
```
---
# <h1 lang="zh-CN">问题2的R语法</h1>
# <h2 lang="zh-CN">两因素被试内设计_宽数据</h2>
```r
#被试在不同身份(self vs other)与不同效价(moral vs moral)的条件组合下反应时是否存在差异)
result.anova <- capture.output({
res_rmANOVA_1 <- bruceR::MANOVA(data=df.match.within,
dvs="A_immoral&B_Other:A_moral&B_Self",
dvs.pattern="A_(.+)B_(.+)",#正则表达式
within=c("A_","B_"))#表示2个主条件
})
```
```
##
## Note:
## dvs="A_immoral&B_Other:A_moral&B_Self" is matched to variables:
## A_immoral&B_Other, A_immoral&B_Self, A_moral&B_Other, A_moral&B_Self
```
```r
writeLines(result.anova, "./output/chp7/anova_2.md")
```
---
# <h1 lang="en">bruceR::MANOVA</h1>
## <h2 lang="en">OUTPUT</h2>
![](./picture/chp7/process_17.png)
---
# <h1 lang="en">bruceR::MANOVA</h1>
## <h2 lang="en">OUTPUT</h2>
![](./picture/chp7/process_18.png)
---
# <h1 lang="zh-CN">问题2的R语法</h1>
# <h2 lang="zh-CN">两因素被试内设计_长数据</h2>
```r
#被试在不同身份(self vs other)与不同效价(good vs bad)的条件组合下反应时是否存在差异)
#head(df.match.mean)
result.anova <- capture.output({
res_rmANOVA_2 <- bruceR::MANOVA(data=df.match.mean,
dv="rt_mean",
within=c("Valence","Identity"),
subID="Sub")
})
```
```
##
## * Data are aggregated to mean (across items/trials)
## if there are >=2 observations per subject and cell.
## You may use Linear Mixed Model to analyze the data,
## e.g., with subjects and items as level-2 clusters.
```
```r
writeLines(result.anova, "./output/chp7/anova_3.md")
```
---
# <h1 lang="zh-CN">简单效应检验与多重比较</h1>
# <h2 lang="zh-CN">主要参数</h2>
![](./picture/chp7/process_13.png)
---
# <h1 lang="zh-CN">简单效应检验与多重比较</h1>
# <h2 lang="zh-CN">问题2的简单效应比较</h2>
```r
result.check <- capture.output({
sim_eff_1 <- res_rmANOVA_1 %>%
bruceR::EMMEANS("A_", by="B_")#简单效应分析
})
writeLines(result.check, "./output/chp7/check.md")
#sim_eff_1 <- res_rmANOVA_1 %>%
#EMMEANS("B_", by="A_") 和上一个同理,只是转换了不同的形式
```
---
# <h1 lang="zh-CN">被试间设计的语法实现</h1>
# <h2 lang="en">性别与亲密关系对ECR得分的效应</h2>
```r
# MANOVA(data, dv="SCORE", between=c("A", "B"))
# data 代表两因素的数据类型
# dv 因变量
# between 被试间因素的名称,此处表示两因素被试间设计,如果只有一个就是单因素被试间设计
res_ANOVA_btw <- df.pg.mean %>%
bruceR::MANOVA(.,
dv="ECR_mean",
between = c("sex", "romantic"))
```
```
##
## ====== ANOVA (Between-Subjects Design) ======
##
## Descriptives:
## ───────────────────────────────────
## "sex" "romantic" Mean S.D. n
## ───────────────────────────────────
## sex1 romantic1 3.006 (0.843) 236
## sex1 romantic2 3.686 (0.651) 206
## sex2 romantic1 2.867 (0.845) 594
## sex2 romantic2 3.747 (0.742) 454
## ───────────────────────────────────
## Total sample size: N = 1490
##
## ANOVA Table:
## Dependent variable(s): ECR_mean
## Between-subjects factor(s): sex, romantic
## Within-subjects factor(s): –
## Covariate(s): –
## ─────────────────────────────────────────────────────────────────────────────────
## MS MSE df1 df2 F p η²p [90% CI of η²p] η²G
## ─────────────────────────────────────────────────────────────────────────────────
## sex 0.457 0.623 1 1486 0.733 .392 .000 [.000, .004] .000
## romantic 187.454 0.623 1 1486 300.682 <.001 *** .168 [.141, .228] .168
## sex * romantic 3.091 0.623 1 1486 4.959 .026 * .003 [.000, .010] .003
## ─────────────────────────────────────────────────────────────────────────────────
## MSE = mean square error (the residual variance of the linear model)
## η²p = partial eta-squared = SS / (SS + SSE) = F * df1 / (F * df1 + df2)
## ω²p = partial omega-squared = (F - 1) * df1 / (F * df1 + df2 + 1)
## η²G = generalized eta-squared (see Olejnik & Algina, 2003)
## Cohen’s f² = η²p / (1 - η²p)
##
## Levene’s Test for Homogeneity of Variance:
## ───────────────────────────────────────────
## Levene’s F df1 df2 p
## ───────────────────────────────────────────
## DV: ECR_mean 12.755 3 1486 <.001 ***
## ───────────────────────────────────────────
```
---
# <h1 lang="zh-CN">混合设计的语法实现</h1>
# <h2 lang="en">CODE</h2>
<h3 lang="zh-CN">以2个被试间1个被试内的混合设计形式为例</h3>
```r
# 假设数据集是mixed.3_2b1w
# A、C是被试间变量,B是被试内变量
# MANOVA(mixed.3_2b1w, dvs="B1:B2", dvs.pattern="B(.)",
#between=c("A", "C"), within="B")
res_ANOVA_mix <- df.match.mean %>%
dplyr::mutate(Sex = ifelse(Sex =="1", "male",
ifelse(Sex == "2", "female", Sex))) %>%
bruceR::MANOVA(.,
subID = "Sub",
dv="rt_mean",
between = c("Sex"),
within = c("Valence", "Identity"))
```
```
##
## * Data are aggregated to mean (across items/trials)
## if there are >=2 observations per subject and cell.
## You may use Linear Mixed Model to analyze the data,
## e.g., with subjects and items as level-2 clusters.
```
```
##
## ====== ANOVA (Mixed Design) ======
##
## Descriptives:
## ─────────────────────────────────────────────
## "Sex" "Valence" "Identity" Mean S.D. n
## ─────────────────────────────────────────────
## female immoral Other 0.717 (0.065) 27
## female immoral Self 0.733 (0.073) 27
## female moral Other 0.709 (0.077) 27
## female moral Self 0.677 (0.063) 27
## male immoral Other 0.721 (0.050) 17
## male immoral Self 0.736 (0.043) 17
## male moral Other 0.704 (0.049) 17
## male moral Self 0.689 (0.044) 17
## ─────────────────────────────────────────────
## Total sample size: N = 44
##
## ANOVA Table:
## Dependent variable(s): rt_mean
## Between-subjects factor(s): Sex
## Within-subjects factor(s): Valence, Identity
## Covariate(s): –
## ───────────────────────────────────────────────────────────────────────────────────────
## MS MSE df1 df2 F p η²p [90% CI of η²p] η²G
## ───────────────────────────────────────────────────────────────────────────────────────
## Sex 0.000 0.012 1 42 0.042 .839 .001 [.000, .053] .001
## Valence 0.042 0.001 1 42 46.148 <.001 *** .524 [.346, .649] .062
## Sex * Valence 0.000 0.001 1 42 0.000 .990 .000 [.000, .000] .000
## Identity 0.001 0.001 1 42 0.626 .433 .015 [.000, .123] .001
## Sex * Identity 0.001 0.001 1 42 0.459 .502 .011 [.000, .113] .001
## Valence * Identity 0.015 0.001 1 42 12.169 .001 ** .225 [.065, .394] .023
## Sex * Valence * Identity 0.001 0.001 1 42 0.605 .441 .014 [.000, .122] .001
## ───────────────────────────────────────────────────────────────────────────────────────
## MSE = mean square error (the residual variance of the linear model)
## η²p = partial eta-squared = SS / (SS + SSE) = F * df1 / (F * df1 + df2)
## ω²p = partial omega-squared = (F - 1) * df1 / (F * df1 + df2 + 1)
## η²G = generalized eta-squared (see Olejnik & Algina, 2003)
## Cohen’s f² = η²p / (1 - η²p)
##
## Levene’s Test for Homogeneity of Variance:
## ───────────────────────────────────────────────
## Levene’s F df1 df2 p
## ───────────────────────────────────────────────
## DV: immoral_Other 0.691 1 42 .410
## DV: immoral_Self 1.866 1 42 .179
## DV: moral_Other 2.753 1 42 .105
## DV: moral_Self 1.679 1 42 .202
## ───────────────────────────────────────────────
##
## Mauchly’s Test of Sphericity:
## The repeated measures have only two levels. The assumption of sphericity is always met.
```
---
# <h1 lang="zh-CN">课堂练习</h1>
<br>
<br>
<h2 lang="zh-CN">1. “在Penguin数据中,不同站点(“Site”)的被试在主观压力上是否有差异?”</h2>
<br>
<h2 lang="zh-CN">2. “在match数据中,如何分析 *d'*”</h2>
<br>
</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');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>