-
Notifications
You must be signed in to change notification settings - Fork 256
/
chapter_8.html
1345 lines (1092 loc) · 48.7 KB
/
chapter_8.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_8.knit</title>
<meta charset="utf-8" />
<meta name="author" content="Pac_B" />
<script src="libs/header-attrs-2.25/header-attrs.js"></script>
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link href="libs/panelset-0.2.6/panelset.css" rel="stylesheet" />
<script src="libs/panelset-0.2.6/panelset.js"></script>
<script src="libs/htmlwidgets-1.6.4/htmlwidgets.js"></script>
<link href="libs/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="libs/datatables-binding-0.32/datatables.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<link href="libs/dt-core-1.13.6/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="libs/dt-core-1.13.6/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="libs/dt-core-1.13.6/js/jquery.dataTables.min.js"></script>
<link href="libs/crosstalk-1.2.1/css/crosstalk.min.css" rel="stylesheet" />
<script src="libs/crosstalk-1.2.1/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: 50px;">**第八章**</span> <br>
<span style="font-size: 50px;">回归模型(一)</span> <br>
<span style="font-size: 30px;">胡传鹏</span> <br>
<span style="font-size: 20px;"> </span> <br>
<span style="font-size: 30px;">2024-04-19</span> <br>
<span style="font-size: 20px;"> Made with Rmarkdown</span> <br>
<style type="text/css">
/* ---- extra.css ---- */
.bigfont {
font-size: 30px;
}
.size5{
font-size: 20px;
}
.tit_font{
font-size: 60px;
}
</style>
---
## 每个小组均有对应的助教同学负责
---
<h1 lang="zh-CN" style="font-size: 60px;"> </h1>
<br>
<br>
## 纯粹的R代码学习 → 使用R语言来实现**统计知识** <br>
<br>
## (1) R: 更灵活的统计分析方法,与统计知识结合更加紧密<br>
<br>
## (2) 心理学/社会科学中常用的统计检验均是回归模型<br>
---
<h1 lang="zh-CN" style="font-size: 60px;">研究问题</h1>
<br>
## 在penguin数据中,恋爱状态(romantic)对被试核心体温(Temperature)的影响
<br>
<br>
## 在penguin数据中,距赤道距离(DEQ)和恋爱状态(romantic)对于被试核心体温(Temperature)的影响
<br>
<br>
---
<h1 lang="en" style="font-size: 60px;">Contents</h1>
<br>
<span style="font-size: 45px;">8.1 *t*-test & linear regression</span></center> <br>
<span style="font-size: 30px;">&emsp;8.1.1 独立样本*t*检验</span></center> <br>
<span style="font-size: 30px;">&emsp;8.1.2 线性回归</span></center> <br>
<span style="font-size: 30px;">&emsp;8.1.3 单样本*t*检验</span></center> <br>
<span style="font-size: 30px;">&emsp;8.1.4 配对样本*t*检验</span></center> <br>
<span style="font-size: 30px;">&emsp;8.1.5 bruceR::TTEST</span></center> <br>
<br>
<span style="font-size: 45px;">8.2 ANOVA & linear regression</span></center> <br>
<span style="font-size: 30px;">&emsp;8.2.1 研究问题</span></center> <br>
<span style="font-size: 30px;">&emsp;8.2.2 代码实操</span></center> <br>
<span style="font-size: 30px;">&emsp;8.2.3 线性回归</span></center> <br>
<span style="font-size: 30px;">&emsp;8.2.4 知识延申</span></center> <br>
---
class: center, middle
<span style="font-size: 60px;">8.1 *t*-test & linear regression</span> <br>
---
class: left, middle
<span style="font-size: 60px;">8.1 研究问题</span> <br>
![](picture/chp8/IJzerman2018fig.png)<!-- -->
(引自[IJzerman et al., 2018](https://doi.org/10.1525/collabra.165))
<br>
<span style="font-size: 35px;">Q: 如何检查恋爱状态(romantic)对核心体温(Temperature)的影响?</span> <br>
<br>
<span style="font-size: 40px;">A:独立样本*t*检验</span> <br>
---
# 8.1 *t*-test
## 8.1.1 独立样本*t*检验(independent *t*-test)
.panelset[
.panel[.panel-name[基础知识]
<br>
**比较两个独立样本群体的均值是否有显著差异。**
<br>
**前提条件**
* 正态性:两个样本数据都应该来自正态分布的总体。样本量足够大时,即使不严格服从正态分布,结果也是稳健的。
* 同方差性:两个样本的方差应该是相等的。
* 独立性:两个样本应该是独立的,即一个样本的观测值不应影响另一个样本的观测值。<br>
<br>
**假设**
* `\(H_0\)`: 两个独立样本群体的均值没有显著差异,即 `\(μ_1\)` = `\(μ_2\)`
* `\(H_1\)`: 两个独立样本群体的均值有显著差异,即 `\(μ_1\)` ≠ `\(μ_2\)`
<br>
`$$t = \frac{\bar{X}_1 - \bar{X}_2}{\sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}}$$`
.panel[.panel-name[数据清理]
```r
df.penguin <- bruceR::import(here::here('data', 'penguin', 'penguin_rawdata.csv')) %>%
dplyr::mutate(subjID = row_number()) %>%
dplyr::select(subjID,Temperature_t1, Temperature_t2, socialdiversity,
Site, DEQ, romantic, ALEX1:ALEX16) %>% # 选择变量
dplyr::filter(!is.na(Temperature_t1) & !is.na(Temperature_t2) & !is.na(DEQ)) %>% # 处理缺失值
dplyr::mutate(romantic = factor(romantic, levels = c(1,2), labels = c("恋爱", "单身")), # 转化为因子
Temperature = rowMeans(select(., starts_with("Temperature"))), # 计算两次核心温度的均值
ALEX4 = case_when(TRUE ~ 6 - ALEX4),
ALEX12 = case_when(TRUE ~ 6 - ALEX12),
ALEX14 = case_when(TRUE ~ 6 - ALEX14),
ALEX16 = case_when(TRUE ~ 6 - ALEX16),
ALEX = rowSums(select(., starts_with("ALEX")))) # 反向计分后计算总分
```
<div class="datatables html-widget html-fill-item" id="htmlwidget-27888db58a4768197891" style="width:100%;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-27888db58a4768197891">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5","6"],[1,2,3,4,5,6],[36.8,34.2,35,36.1,35.72222222,35.3],[36.7,36.6,35.2,35.8,36.44444444,35.9],[8,6,5,7,5,7],["Tsinghua","Oxford","Oxford","Oxford","Chile","Bamberg"],[26.88780212,51.75,51.75,51.75,42.07719421,51.65660095],["单身","恋爱","恋爱","恋爱","恋爱","恋爱"],[2,1,2,3,1,1],[2,1,2,4,1,2],[2,1,4,1,1,1],[4,5,4,4,5,4],[2,1,2,4,1,1],[2,1,2,2,1,3],[2,1,2,3,2,4],[2,1,2,4,1,2],[2,1,2,2,2,4],[1,1,1,1,1,2],[2,1,2,2,1,2],[4,4,3,4,4,2],[2,3,2,2,2,4],[4,4,4,4,4,5],[2,3,2,4,1,1],[4,4,4,4,2,5],[36.75,35.40000000000001,35.1,35.95,36.08333333,35.59999999999999],[31,23,34,40,24,35]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>subjID<\/th>\n <th>Temperature_t1<\/th>\n <th>Temperature_t2<\/th>\n <th>socialdiversity<\/th>\n <th>Site<\/th>\n <th>DEQ<\/th>\n <th>romantic<\/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>Temperature<\/th>\n <th>ALEX<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":4,"columnDefs":[{"className":"dt-right","targets":[1,2,3,4,6,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]},{"orderable":false,"targets":0},{"name":" ","targets":0},{"name":"subjID","targets":1},{"name":"Temperature_t1","targets":2},{"name":"Temperature_t2","targets":3},{"name":"socialdiversity","targets":4},{"name":"Site","targets":5},{"name":"DEQ","targets":6},{"name":"romantic","targets":7},{"name":"ALEX1","targets":8},{"name":"ALEX2","targets":9},{"name":"ALEX3","targets":10},{"name":"ALEX4","targets":11},{"name":"ALEX5","targets":12},{"name":"ALEX6","targets":13},{"name":"ALEX7","targets":14},{"name":"ALEX8","targets":15},{"name":"ALEX9","targets":16},{"name":"ALEX10","targets":17},{"name":"ALEX11","targets":18},{"name":"ALEX12","targets":19},{"name":"ALEX13","targets":20},{"name":"ALEX14","targets":21},{"name":"ALEX15","targets":22},{"name":"ALEX16","targets":23},{"name":"Temperature","targets":24},{"name":"ALEX","targets":25}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[4,10,25,50,100]}},"evals":[],"jsHooks":[]}</script>
.panel[.panel-name[代码实操]
```r
stats::t.test(data = df.penguin, # 数据框
Temperature ~ romantic, # 因变量~自变量
var.equal = TRUE) %>%
capture.output() # 将输出变整齐
```
```
## [1] ""
## [2] "\tTwo Sample t-test"
## [3] ""
## [4] "data: Temperature by romantic"
## [5] "t = -0.34664, df = 1425, p-value = 0.7289"
## [6] "alternative hypothesis: true difference in means between group 恋爱 and group 单身 is not equal to 0"
## [7] "95 percent confidence interval:"
## [8] " -0.0555949 0.0388971"
## [9] "sample estimates:"
## [10] "mean in group 恋爱 mean in group 单身 "
## [11] " 36.38498 36.39333 "
## [12] ""
```
]]]]
---
# 8.1 *t*-test作为回归模型的特例
## 8.1.2 线性回归(linear regression)
<br>
**回归分析**
* 回归分析用于研究一个或多个自变量(预测变量)与一个因变量(响应变量)之间的关系。<br>
<br>
<br>
--
**线性回归**
* 线性回归的基本思想是通过数据拟合一条直线,使得这条直线尽可能地接近所有的数据点,从而实现对新数据点的预测。<br>
* 线性回归模型可以表示为:
`$$y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... + \beta_p x_p + \epsilon$$`
* 其中,$y$是因变量,x是自变量, `\(\beta_i\)` 是模型参数,表示截距和斜率,$\epsilon$是误差项,表示模型未能解释的随机误差。
---
# 8.1 *t*-test & linear regression
## 8.1.2 线性回归(linear regression)
<br>
* 独立样本*t*检验是线性模型的特殊形式(自变量为二分变量)
--
<p align="center">
<img src="./picture/chp8/indet-lm.png" width="55%">
</p>
---
# 8.1 *t*-test & linear regression
.pull-left[
```r
# t检验
stats::t.test(
data = df.penguin,
Temperature ~ romantic,
var.equal = TRUE) %>%
capture.output() # 将输出变整齐
```
<p align="center">
<img src="./picture/chp8/compare1.1.png" width="100%">
</p>
]
.pull-right[
```r
# 线性回归
model.inde <- stats::lm(
data = df.penguin,
formula = Temperature ~ 1 + romantic
)
summary(model.inde)
```
<p align="center">
<img src="./picture/chp8/compare1.2.png" width="100%">
</p>
]
---
# 8.1 *t*-test系列均为回归模型的特例
## 8.1.3 单样本*t*检验(one sample *t*-test)
* 例如:在penguin数据中,全体被试的核心体温(Temperature)是否等于36.6?<br>
<br>
--
<br>
**比较单个样本的平均值(m)与已知的总体平均值(μ)之间是否存在显著差异**<br>
<br>
**前提条件**
* 正态性:样本数据应来自正态分布的总体。样本量足够大时,即使不严格服从正态分布,结果也是稳健的。
* 独立性:样本中的观测值必须是独立的,即一个观测值不应影响另一个观测值。<br>
<br>
**假设**
* `\(H_0\)`: 样本的均值(m)与给定的总体均值或假设的总体均值(μ)之间没有显著差异。
* `\(H_1\)`: 样本的均值(m)与给定的总体均值或假设的总体均值(μ)之间有显著差异。
<br>
`$$t = \frac{\bar{X} - \mu}{s / \sqrt{n}}$$`
---
# 8.1 *t*-test系列均为回归模型的特例
## 8.1.3 单样本*t*检验(one sample *t*-test)
.pull-left[
<br>
<br>
`$$y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... + \beta_p x_p + \epsilon$$`
单样本*t*检验中,仅截距不为0。此时公式为:<br>
--
`$$y = \beta_0$$`
`$$H_0: \beta_0 = 0$$`
]
.pull-right[
<p align="center">
<img src="./picture/chp8/singlet-lm.png" width="75%">
</p>
]
---
# 8.1 *t*-test系列均为回归模型的特例
## 8.1.3 单样本*t*检验(one sample *t*-test)
.pull-left[
```r
stats::t.test(
x = df.penguin$Temperature, # 核心体温均值
mu = 36.6)
```
<br>
<br>
<p align="center">
<img src="./picture/chp8/compare2.1.png" width="100%">
</p>
]
.pull-right[
```r
model.single <- lm(
data = df.penguin,
formula = Temperature - 36.6 ~ 1
)
summary(model.single)
```
<br>
<br>
<p align="center">
<img src="./picture/chp8/compare2.2.png" width="100%">
</p>
]
---
# 8.1 *t*-test系列均为回归模型的特例
## 8.1.4 配对样本*t*检验(paired *t*-test)
* 例如,在penguin数据中,被试报告的两次核心温度(Temperature_t1, Temperature_t2)是否有显著差异?<br>
<br>
--
**比较两个相关的样本组(例如,同一组受试者在不同条件下的测量)的平均值是否存在显著差异。**<br>
<br>
**前提条件**
* 正态性:样本数据应来自正态分布的总体。样本量足够大时,即使不严格服从正态分布,结果也是稳健的。
* 独立性:配对样本中的观测值必须是独立的,即每一对观测值不应影响其他对的观测值。
* 配对设计:数据必须是以配对形式收集的。<br>
<br>
**假设**
* `\(H_0\)`: 配对样本的总体平均差与零没有显著差异(两个配对样本的均值没有显著差异)。
* `\(H_1\)`: 配对样本的总体平均差与零有显著差异(两个配对样本的均值存在显著差异)。
<br>
`$$t = \frac{\bar{X} - \mu}{s / \sqrt{n}}$$`
---
# 8.1 *t*-test系列均为回归模型的特例
## 8.1.4 配对样本*t*检验(paired *t*-test)
$$y_1 - y_2 = \beta_0 $$
$$H_0: \beta_0 = 0 $$
--
可以将配对样本*t*检验理解为对差值进行的单样本*t*检验,即单独用一个数字来预测对应的差值(见图的右半部分)。<br>
也可以认为这些组间之差是斜率(见图的左半部分)。<br>
<p align="center">
<img src="./picture/chp8/pairt-lm.png" width="70%">
</p>
---
# 8.1 *t*-test系列均为回归模型的特例
## 8.1.4 配对样本*t*检验(paired *t*-test)
.pull-left[
```r
stats::t.test(
x = df.penguin$Temperature_t1,
y = df.penguin$Temperature_t2,
paired = TRUE
)
```
<br>
<br>
<p align="center">
<img src="./picture/chp8/compare3.1.png" width="100%">
</p>
]
.pull-right[
```r
model.paired <- lm(
Temperature_t1 - Temperature_t2 ~ 1,
data = df.penguin
)
summary(model.paired)
```
<br>
<br>
<p align="center">
<img src="./picture/chp8/compare3.2.png" width="100%">
</p>
]
---
# 8.1 *t*-test
## 8.1.5 bruceR::TTEST
* 如果偏好按传统方式使用*t*检验,推荐`bruceR::TTEST`函数
<p align="center">
<img src="./picture/chp8/TTEST.png" width="70%">
</p>
中文帮助文档:https://zhuanlan.zhihu.com/p/281150493
---
# 8.1 *t*-test
## 8.1.5 bruceR::TTEST
.panelset[
.panel[.panel-name[独立样本t检验]
.pull-left[
```r
stats::t.test(
data = df.penguin,
Temperature ~ romantic,
var.equal = TRUE
)
```
<br>
<p align="center">
<img src="./picture/chp8/compare1.1.png" width="100%">
</p>
]
.pull-right[
```r
bruceR::TTEST(
data = df.penguin, # 数据
y = "Temperature", # 因变量
x = "romantic" # 自变量
)
```
<br>
<p align="center">
<img src="./picture/chp8/leven.png" width="100%">
</p>
]
.panel[.panel-name[单样本t检验]
.pull-left[
```r
stats::t.test(
x = df.penguin$Temperature,
mu = 36.6
)
```
<br>
<p align="center">
<img src="./picture/chp8/compare2.1.png" width="100%">
</p>
]
.pull-right[
```r
bruceR::TTEST(
data = df.penguin, # 数据
y = "Temperature", # 确定变量
test.value = 36.6, # 固定值
test.sided = "=") # 假设的方向
```
<br>
<p align="center">
<img src="./picture/chp8/sample.png" width="100%">
</p>
]
.panel[.panel-name[配对样本t检验]
.pull-left[
```r
stats::t.test(
x = df.penguin$Temperature_t1, #第1次
y = df.penguin$Temperature_t2, #第2次
paired = TRUE)
```
<br>
<p align="center">
<img src="./picture/chp8/compare3.1.png" width="100%">
</p>
]
.pull-right[
```r
bruceR::TTEST(
data = df.penguin, # 数据
y = c("Temperature_t1",
"Temperature_t2"), # 变量为两次核心体温
paired = T) # 配对数据,默认是FALSE
```
<br>
<p align="center">
<img src="./picture/chp8/pair.png" width="100%">
</p>
]
]]]]
---
# 8.1 *t*-test系列均为回归模型的特例
<br>
<br>
| | R自带函数 | 线性模型 | 解释 |
|-------|-------|-------|-------|
| 单样本*t* | t.test(y, mu = 0) | lm(y ~ 1)| 仅有截距的回归模型 |
| 独立样本*t* | t.test($y_1$, `\(y_2\)`) | lm(y ~ 1 + `\(G_2\)`)| 自变量为二分变量的回归模型 |
| 配对样本*t* | t.test($y_1$, `\(y_2\)`, paired=T) | lm($y_1$ - `\(y_2\)` ~ 1)| 仅有截距的回归模型)|
---
class: center, middle
<span style="font-size: 60px;">8.2 ANOVA & linear regression</span> <br>
---
class: left, middle
<span style="font-size: 60px;">8.2.1 研究问题</span> <br>
<br>
<span style="font-size: 35px;">Q: 如何同时检验距赤道距离(DEQ)和恋爱状态(romantic)对于被试体温的影响</span> <br>
<br>
<span style="font-size: 40px;">A:双因素被试间方差分析</span> <br>
---
# 8.2 ANOVA & linear regression
## 8.2.2 代码实操 & 知识回顾
.pull-left[
<br>
**方法简介:**
当研究者想要比较两个或多个组之间的均值差异时,可使用方差分析(Analysis of Variance,简称ANOVA)。<br>
<br>
它由英国统计学家R.A.Fisher提出,基本思想是将测量数据的总变异(即总方差)按照变异来源分为处理(组间)效应和误差(组内)效应,并作出其数量估计,从而确定实验处理对研究结果影响力的大小。
**假设:**
* `\(H_0\)`: 各因素各个水平下,因变量的均值完全相同
* `\(H_1\)`: 各因素各个水平下,因变量的均值不完全相同
]
.pull-right[
<br>
<br>
<br>
<br>
**前提条件:**
* 可加性:各效应可加,即观测值是由各主效应,交互作用以及误差通过相加得到的<br>
* 随机性:各样本(观测值)是随机样本<br>
* 正态性:各样本来自于正态分布的总体<br>
* 独立性:各样本观测值互相独立<br>
* 方差齐性:各样本来自的总体方差相同<br>
* 因变量应为连续变量<br>
]
---
# 8.2 ANOVA
## 8.2.2 代码实操|数据预处理
.panelset[
.panel[.panel-name[vars]
```r
summary(df.penguin$DEQ)
```
```
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.293 34.433 39.912 39.842 51.317 60.391
```
```r
# 设定分割点
# [0-23.5 热带, 23.5-35 亚热带], [35-40 暖温带, 40-50 中温带], [50-66.5 寒温带]
breaks <- c(0, 35, 50, 66.5)
# 设定相应的标签
labels <- c('热带', '温带', '寒温带')
# 创建新的变量
df.penguin$climate <- cut(df.penguin$DEQ,
breaks = breaks,
labels = labels)
summary(df.penguin$climate)
```
```
## 热带 温带 寒温带
## 396 592 439
```
.panel[.panel-name[tidy data]
```r
df <- df.penguin %>%
select(subjID, climate, romantic, Temperature)
```
<div class="datatables html-widget html-fill-item" id="htmlwidget-bec36c95e7b22d5fe1bc" style="width:100%;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-bec36c95e7b22d5fe1bc">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5","6"],[1,2,3,4,5,6],["热带","寒温带","寒温带","寒温带","温带","寒温带"],["单身","恋爱","恋爱","恋爱","恋爱","恋爱"],[36.75,35.40000000000001,35.1,35.95,36.08333333,35.59999999999999]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>subjID<\/th>\n <th>climate<\/th>\n <th>romantic<\/th>\n <th>Temperature<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"columnDefs":[{"className":"dt-right","targets":[1,4]},{"orderable":false,"targets":0},{"name":" ","targets":0},{"name":"subjID","targets":1},{"name":"climate","targets":2},{"name":"romantic","targets":3},{"name":"Temperature","targets":4}],"order":[],"autoWidth":false,"orderClasses":false}},"evals":[],"jsHooks":[]}</script>
]]]
---
# 8.2 ANOVA & linear regression
## 8.2.2 代码实操|正态性检验
.panelset[
.panel[.panel-name[KS检验]
```r
# 正态性检验-Kolmogorov-Smirnov检验
# 若p >.05,不能拒绝数据符合正态分布的零假设
ks.test(df$Temperature, 'pnorm')
```
```
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: df$Temperature
## D = 1, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
```
```r
# 进行数据转换,转换后仍非正态分布
df$Temperature_log <- log(df$Temperature)
ks.test(df$Temperature_log, 'pnorm')
```
```
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: df$Temperature_log
## D = 0.99981, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
```
.panel[.panel-name[qq图]
```r
# 正态性检验-qq图
qqnorm(df$Temperature)
qqline(df$Temperature, col = "red") # 添加理论正态分布线
```
![](chapter_8_files/figure-html/unnamed-chunk-20-1.png)<!-- -->
.panel[.panel-name[直方图]
```r
ggplot(df, aes(Temperature)) +
geom_histogram(aes(y =..density..), color='black', fill='white', bins=30) +
geom_density(alpha=.5, fill='red')
```
![](chapter_8_files/figure-html/unnamed-chunk-21-1.png)<!-- -->
]]]]
---
# 8.2 ANOVA & linear regression
## 8.2.2 代码实操|双因素被试间方差分析
.panelset[
.panel[.panel-name[stats::aov()]
```r
aov1 <- stats::aov(Temperature ~ climate * romantic, data = df)
summary(aov1)
```
```
## Df Sum Sq Mean Sq F value Pr(>F)
## climate 2 18.82 9.408 49.392 < 0.0000000000000002 ***
## romantic 1 0.24 0.244 1.280 0.25807
## climate:romantic 2 1.91 0.955 5.014 0.00676 **
## Residuals 1421 270.65 0.190
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
```
.panel[.panel-name[SPSS]
![](picture/chp8/SPSS.png)<!-- -->
]]]
---
class: center, middle
<span style="font-size: 60px;">结果为什么不相同?</span> <br>
---
# 8.2 ANOVA & linear regression
## 8.2.2 代码实操|平方和(SS)的计算
<br>
在平衡设计中,三种类型的平方和的结果会很清晰,并且方差分析的结果独立于平方和的类型;<br>
而在非平衡设计中,尤其是当各组样本量差距较大时,三种类型的平方和计算结果可能会不同,此时需要根据具体研究设计和问题来选择使用哪一种类型的平方和。<br>
<br>
对于 Y ~ A + B + A * B<br>
<br>
.panelset[
.panel[.panel-name[Type I SS]
<br>
解释变量的顺序会影响到类型I平方和的计算结果,通常用于顺序重要的模型。<br>
效应根据表达式中先出现的效应做调整。A不做调整,B根据A调整,A:B交互项根据A和B调整。<br>
<br>
**stats::aov()函数默认采用的就是Type I SS,它逐步将每一个因子引入模型进行计算。**<br>
.panel[.panel-name[Type II SS]
<br>
忽略了因子之间可能存在的交互作用,适用于所有主效应不涉及交互效应的情况。<br>
假定所有的因子都是同时进入模型的,并且它们都是等价的。<br>
效应根据同水平或低水平的效应做调整。A根据B调整,B依据A调整,A:B交互项同时根据A和B调整。<br>
<br>
**car::Anova()函数默认计算Type II SS,可以通过type = 3调整为Type III SS。**<br>
.panel[.panel-name[Type III SS]
<br>
更全面,假定所有因子(以及它们的交互项)都是重要的,并考虑所有因素。<br>
每个效应根据模型其他各效应做相应调整。A根据B和A:B做调整,A:B交互项根据A和B调整。<br>
<br>
**SPSS默认采用Type III SS。**<br>
**bruceR::MANOVA*()函数也默认采用Type III SS,可以通过ss.type = 2调整为Type II SS。**<br>
]]]]
---
# 8.2 ANOVA & linear regression
## 8.2.2 代码实操|双因素被试间方差分析
.panelset[
.panel[.panel-name[car::Anova()]
```r
# 结果不一致,原因PPT显示不全,请回到rmd文档查看
aov1 <- car::Anova(stats::aov(Temperature ~ climate * romantic, data = df))
aov1
```
```
## Anova Table (Type II tests)
##
## Response: Temperature
## Sum Sq Df F value Pr(>F)
## climate 19.034 2 49.9680 < 0.00000000000000022 ***
## romantic 0.244 1 1.2801 0.258072
## climate:romantic 1.910 2 5.0136 0.006765 **
## Residuals 270.654 1421
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
```
```r
# 原因debug
# 查看R的默认对比设置
options("contrasts")
```
```
## $contrasts
## unordered ordered
## "contr.treatment" "contr.poly"
```
```r
# 从输出结果可知,无序默认为contr.treatment(),有序默认为contr.poly()
# factor()函数来创建无序因子,ordered()函数创建有序因子
is.factor(df$climate)
```
```
## [1] TRUE
```
```r
is.ordered(df$climate)
```
```
## [1] FALSE
```
```r
# climate是无序因子
# 创建一个3水平的因子的基准对比
c1 <- contr.treatment(3)
# 创建一个新的对比,这个编码假设分类水平之间的差异被等分,每一个水平与总均值的差异等于1/3
my.coding <- matrix(rep(1/3, 6), ncol=2)
# 将对比调整为每个水平与第一个水平的振幅减去1/3
# 可能的原因:除了关心每个水平对应的效果,同时也关心水平与水平之间的效果
my.simple <- c1-my.coding
my.simple
```
```
## 2 3
## 1 -0.3333333 -0.3333333
## 2 0.6666667 -0.3333333
## 3 -0.3333333 0.6666667
```
```r
# 更改climate的对比
contrasts(df$climate) <- my.simple
# 将数据集df的romantic列的对比设为等距对比,它假设分类水平之间的差异为等距离
contrasts(df$romantic) <- contr.sum(2)/2
# 方差分析
aov1 <- car::Anova(lm(Temperature ~ climate * romantic, data = df),
type = 3)
aov1
```
```
## Anova Table (Type III tests)
##
## Response: Temperature
## Sum Sq Df F value Pr(>F)
## (Intercept) 1768309 1 9284069.6498 < 0.00000000000000022 ***
## climate 19 2 50.8832 < 0.00000000000000022 ***
## romantic 0 1 1.0006 0.317336
## climate:romantic 2 2 5.0136 0.006765 **
## Residuals 271 1421
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
```
.panel[.panel-name[afex::aov_ez()]
```r
afex::aov_ez(id = "subjID",
dv = "Temperature",
data = df,
between = c("climate", "romantic"),
type = 3)
```
```
## Anova Table (Type 3 tests)
##
## Response: Temperature
## Effect df MSE F ges p.value
## 1 climate 2, 1421 0.19 50.88 *** .067 <.001
## 2 romantic 1, 1421 0.19 1.00 <.001 .317
## 3 climate:romantic 2, 1421 0.19 5.01 ** .007 .007
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
```
```r
# afex中的其他函数可以得到同样的结果
afex::aov_car(Temperature ~ climate * romantic + Error(subjID), data = df, type = 3)
afex::aov_4(Temperature ~ climate * romantic + (1|subjID), data = df)
```
]]]
---
# 8.2 ANOVA & linear regression
## 8.2.3 线性回归
.pull-left[
<br>
<br>
<br>
这里的ANOVA仍然是线性回归模型的特例,即自变量是离散变量的情况。
<br>
如果使用哑变量(dummy coding)对自变量进行编码后进入回归方程,线性模型中的斜率即是对组间差异的估计。<br>
<br>
**双因素方差分析可以看作是一种特殊的线性回归模型,自变量为两个分类变量。**<br>
]
.pull-right[
<br>
<br>
![](picture/chp8/aovLM.png)<!-- -->
]
---
# 8.2 ANOVA & linear regression
## 8.2.3 线性回归
.pull-left[
```r
aov1 <- car::Anova(
aov(Temperature ~ climate * romantic,
data = df),
type = 3
)
aov1
```
<br>
![](./picture/chp8/compare8231.png)
]
.pull-right[
```r
lm1 <- car::Anova(
lm(Temperature ~ climate * romantic,
data = df),
type = 3
)
lm1
```
<br>
![](./picture/chp8/compare8232.png)
]
---
# 8.2 ANOVA & linear regression
## 8.2.2 代码实操: `bruceR`
* 使用`bruceR`可以更简单地实现心理学中习惯的ANOVA,但要注意数据格式
<p align="center">
<img src="./picture/chp8/ANOVA data.png" width="65%">
</p>
---
# 8.2 ANOVA & linear regression
## 8.2.2 代码实操: `bruceR::MANOVA`
<p align="center">
<img src="./picture/chp8/MANOVA.png" width="70%">
</p>
---
# 8.2 ANOVA & linear regression
## 8.2.2 代码实操: `bruceR::MANOVA`
```r
res1 <- bruceR::MANOVA(data = df,
dv = "Temperature",
between = c("climate", "romantic"))
```
```
## [1] "Anova Table (Type III tests)"
## [2] ""
## [3] "Response: Temperature"
## [4] " Effect df MSE F ges p.value"
## [5] "1 climate 2, 1421 0.19 50.88 *** .067 <.001"
## [6] "2 romantic 1, 1421 0.19 1.00 <.001 .317"
## [7] "3 climate:romantic 2, 1421 0.19 5.01 ** .007 .007"
## [8] "---"
## [9] "Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1"
```