forked from FreyaWen/R4Psy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_9.html
602 lines (536 loc) · 32.6 KB
/
chapter_9.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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>chapter_9.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/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;">如何进行基本的数据分析</span> <br>
<span style="font-size: 50px;">中介和调节分析</span> <br>
<br>
<span style="font-size: 30px;">bruceR::PROCESS & lavaan</span> <br>
<br>
<br>
<span style="font-size: 30px;">胡传鹏</span> <br>
<span style="font-size: 30px;">2023/04/24</span> <br>
---
class: center, middle
<span style="font-size: 60px;">9.1 事前准备</span> <br>
---
# <h1 lang="zh-CN">相关知识点</h1>
<br>
<center>
<h2 lang="en">中介调节的鼻祖:Baron & Kenny (1986)</h2>
</center>
<br>
<div style="display:flex; align-items:center;">
<img src="http://3.bp.blogspot.com/-6OBEhzon_Bk/VfHWHZXlSYI/AAAAAAAAd_Q/chk77Yk2Ohc/s1600/B%2526K%2Bmediator.png" style="width:50%; height: 50%;">
<img src="http://1.bp.blogspot.com/-LoVbz4fImR8/VfHWKAH073I/AAAAAAAAd_Y/gPJHy1moJFk/s1600/B%2526K%2Bmoderator.png" style="width:50%;">
</div>
<br>
_<h5 lang="en">- Baron, R. M., & Kenny, D. A. (1986). The moderator–mediator variable distinction in social psychological research: Conceptual, strategic, and statistical considerations. Journal of personality and social psychology, 51(6), 1173. </h5>_
---
# <h1 lang="zh-CN">相关知识点</h1>
<br>
<center>
<h2 lang="en">PROCESS的作者:Andrew F. Hayes </h2>
</center>
<br>
<div style="display:flex; align-items:center; justify-content:center;">
<img src="https://m.media-amazon.com/images/I/51h5ymu3dpL._SX348_BO1,204,203,200_.jpg" style="width:30%; height: 30%; margin-right: 20px;">
<img src="https://www.guilford.com/covers/large/9781462549030.jpg" style="width:30%; height: 30%;">
</div>
<br>
_<h5 lang="en">- Hayes, A. F. (2017). Introduction to mediation, moderation, and conditional process analysis: A regression-based approach. Guilford publications. </h5>_
---
# <h1 lang="zh-CN">相关的R包:</h1>
<br>
<center>
<h2 lang="en">bruceR::PROCESS & lavaan </h2>
</center>
<br>
<div style="display:flex; align-items:center; justify-content:center;">
<img src="https://raw.githubusercontent.com/psychbruce/bruceR/main/man/figures/logo.png" style="width:30%; height: 30%; margin-right: 40px;">
<img src="https://assets.datacamp.com/production/course_6419/shields/original/shield_image_course_6419_20180703-12-1hbh8vg?1530639951" style="width:30%; height: 30%;">
</div>
<br>
_<h5 lang="en">- Bao, H.-W.-S. (2022). bruceR: Broadly useful convenient and efficient R functions. R package version 0.8.x. https://CRAN.R-project.org/package=bruceR </h5>_
_<h5 lang="en">- Rosseel Y (2012). “lavaan: An R Package for Structural Equation Modeling.” Journal of Statistical Software, 48(2), 1–36. doi:10.18637/jss.v048.i02. </h5>_
---
# <h1 lang="zh-CN">载入包 - 设置工作路径 - 导入原始数据</h1>
```r
# 检查是否已安装 pacman
if (!requireNamespace("pacman", quietly = TRUE)) {
install.packages("pacman") } # 如果未安装,则安装包
# 使用p_load来载入需要的包
pacman::p_load("tidyverse", "bruceR", "performance", "lavaan", "lavaanPlot")
```
```
##
## 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
df.pg.raw <- read.csv('./data/penguin/penguin_rawdata_new.csv',
header = T, sep=",", stringsAsFactors = FALSE) %>%
dplyr::select(., age, language, avgtemp,socialdiversity, DEQ,
starts_with("ALEX"),
starts_with("ECR"),
starts_with("HOME"),
starts_with("KAMF"),
starts_with("SNI"),
)
# 这些包含原始题目的数据集,将在SEM lavaan中直接使用
```
<div id="htmlwidget-5ae54b8cc4e742daf14f" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-5ae54b8cc4e742daf14f">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5","6","7","8","9","10"],[1922,1940,1945,1948,1948,1951,1952,1952,1952,1953],[7,5,5,5,2,1,5,5,5,4],[36.75,35.4,35.1,35.95,36.08333333,35.6,35.4,35.8,35.85,35.6],[8,6,5,7,5,7,4,5,5,7],[26.88780212,51.75,51.75,51.75,42.07719421,51.65660095,51.75,51.75,51.75,59.91270447],[2,1,2,3,1,1,2,1,3,1],[2,1,2,4,1,2,2,2,3,1],[2,1,4,1,1,1,2,1,4,1],[2,1,2,2,1,2,4,2,3,2],[2,1,2,4,1,1,2,2,4,2],[2,1,2,2,1,3,2,1,4,1],[2,1,2,3,2,4,2,1,3,1],[2,1,2,4,1,2,3,1,3,2],[2,1,2,2,2,4,2,2,2,1],[1,1,1,1,1,2,2,2,3,3],[2,1,2,2,1,2,2,1,4,1],[2,2,3,2,2,4,2,2,2,1],[2,3,2,2,2,4,2,1,2,1],[2,2,2,2,2,1,3,1,2,1],[2,3,2,4,1,1,3,2,2,2],[2,2,2,2,4,1,2,1,2,1],[4,1,5,1,2,1,3,5,5,5],[2,1,3,1,2,2,5,5,5,2],[2,1,3,1,4,1,4,5,5,3],[2,1,2,1,2,1,5,2,5,2],[5,1,4,2,5,1,6,4,4,3],[2,1,3,2,6,3,5,2,5,5],[2,1,2,2,1,1,5,3,5,1],[2,1,2,1,1,1,2,5,5,1],[5,1,3,7,4,1,5,5,3,2],[2,1,4,1,2,2,4,2,4,2],[2,1,5,1,2,1,5,6,3,2],[2,1,4,2,5,6,3,3,4,5],[4,1,2,1,4,5,3,4,4,2],[2,4,2,1,1,1,3,2,5,5],[2,1,2,1,1,1,4,3,5,1],[5,1,5,2,2,1,4,4,4,5],[5,1,3,4,2,2,3,4,6,1],[2,1,2,7,5,1,2,2,5,2],[5,7,2,1,2,1,3,2,3,7],[2,1,2,1,2,2,2,3,3,2],[6,3,2,1,2,7,4,5,6,5],[5,1,2,7,2,2,2,2,5,2],[4,1,2,7,2,1,3,4,5,2],[5,1,1,3,1,1,2,3,3,2],[5,1,1,2,2,1,2,3,5,2],[2,1,2,2,2,2,3,2,4,2],[5,1,3,6,4,2,2,5,5,3],[2,4,2,1,1,2,2,2,4,2],[3,4,5,1,2,6,3,2,3,2],[5,4,2,1,2,2,3,3,3,3],[2,2,3,1,2,2,2,2,4,2],[5,1,2,1,1,1,2,2,3,2],[4,4,4,1,6,7,3,5,5,4],[6,4,4,2,5,6,3,6,5,4],[6,1,3,1,1,3,3,2,3,2],[2,1,6,2,5,5,5,4,4,5],[4,5,5,5,5,5,4,5,5,5],[4,3,3,5,5,5,3,5,3,5],[4,3,4,5,5,5,4,5,4,5],[4,3,4,5,5,4,4,5,5,5],[3,3,3,5,5,2,3,5,3,2],[3,1,4,4,3,5,4,5,5,5],[2,1,4,3,2,4,5,3,5,5],[2,1,5,4,4,5,4,5,5,5],[3,1,4,4,3,4,4,5,5,5],[2,1,5,4,4,4,4,4,5,5],[2,1,5,4,5,7,4,6,6,8],[4,1,5,4,5,6,2,4,4,7],[2,1,2,4,3,1,3,4,6,1],[2,1,3,5,3,6,2,4,3,3],[2,1,2,1,2,3,2,3,2,2],[2,1,1,2,1,7,2,3,1,5],[2,3,1,1,5,1,5,4,4,4],[1,8,6,3,4,3,2,3,2,2],[0,4,4,3,4,3,2,3,2,2],[1,1,2,1,1,1,1,2,1,2],[1,null,2,null,1,null,null,2,null,2],[5,2,1,1,1,1,1,1,1,5],[0,2,null,null,null,null,null,null,null,null],[6,1,1,4,6,7,1,2,1,3],[2,null,null,3,4,2,null,2,null,1],[7,8,3,1,7,7,3,5,7,2],[3,5,2,1,5,2,1,3,3,2],[1,2,1,2,1,2,1,1,1,1],[1,1,null,8,null,1,null,null,null,null],[2,1,1,1,1,1,1,1,2,1],[5,null,null,null,null,null,null,null,4,null],[3,2,1,1,1,1,1,1,2,2],[1,1,null,null,null,null,null,null,1,1],[2,8,null,null,null,null,null,null,7,1],[3,1,4,3,4,2,2,3,4,1],[2,1,1,2,1,2,1,1,1,2],[1,null,null,8,null,8,null,null,null,1],[1,2,1,1,1,2,2,2,1,1],["ʮ<c1><f9><b5><e3><e6><e4>վ","Pensioners",null,null,null,"Telefonseelsorge","badminton","street band",null,null],[null,"Men/women with foreign spouses not in UK",null,null,null,"Seniorenheim","bridge 1","samba band",null,null],[null,null,null,null,null,null,"bridge 2",null,null,null],[null,null,null,null,null,null,"bridge 3",null,null,null],[null,null,null,null,null,null,"badminton 2",null,null,null],[3,25,null,null,null,7,12,10,null,null],[null,30,null,null,null,2,5,10,null,null],[null,null,null,null,null,null,6,null,null,null],[null,null,null,null,null,null,1,null,null,null],[null,null,null,null,null,null,4,null,null,null]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>age<\/th>\n <th>language<\/th>\n <th>avgtemp<\/th>\n <th>socialdiversity<\/th>\n <th>DEQ<\/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>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>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>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 <\/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,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,101,102,103,104,105]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[5,10,25,50,100]}},"evals":[],"jsHooks":[]}</script>
---
# <h1 lang="zh-CN">查找量表题目与维度的对应关系</h1>
ALEX:16题(TAS-20)<br> DIF 1,3,5,6,7,10,11; DDF 2,4,8,9; EOT 12,13,14,15,16 <br>
_<h5 lang="en">- Taylor, G. J., Bagby, R. M., & Parker, J. D. (2003). The 20-Item Toronto Alexithymia Scale: IV. Reliability and factorial validity in different languages and cultures. Journal of psychosomatic research, 55(3), 277-283. </h5>_
<br>
ECR:36题 1-18为焦虑,19-36为回避 <br>
_<h5 lang="en">- Fraley, R. C., Waller, N. G., & Brennan, K. A. (2000). An item response theory analysis of self-report measures of adult attachment. Journal of personality and social psychology, 78(2), 350. </h5>_
<br>
HOME:9题 <br>
_<h5 lang="zh-CN">来源未知</h5>_
<br>
KAMF:7题 <br>
_<h5 lang="zh-CN">来源未知</h5>_
<br>
在PROCESS中由于一个变量内的题目都会求平均分,所以是否知道题目与维度的对应并不重要<br>
但在lavaan(SEM)中,必须知道题目与维度的对应关系,才可以构建模型
---
# <h1 lang="zh-CN">清洗数据</h1>
# <h2 lang="zh-CN">对模型涉及变量的题目求均值</h2>
```r
# 如果你需要探究某个维度对其他变量的影响,可以按照维度求和,做更复杂的模型
df.pg.mean <- df.pg.raw %>%
dplyr::mutate(ALEX_mean = rowMeans(select(., starts_with("ALEX"))),
ECR_mean = rowMeans(select(., starts_with("ECR"))),
HOME_mean = rowMeans(select(., starts_with("HOME"))),
KAMF_mean = rowMeans(select(., starts_with("KAMF"))),) %>%
dplyr::select(age, language, avgtemp,
ALEX_mean, ECR_mean, HOME_mean, KAMF_mean)
```
<div id="htmlwidget-8511246f49d4efafb6e9" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-8511246f49d4efafb6e9">{"x":{"filter":"none","vertical":false,"fillContainer":true,"data":[["1","2","3","4","5","6","7","8","9","10"],[1922,1940,1945,1948,1948,1951,1952,1952,1952,1953],[7,5,5,5,2,1,5,5,5,4],[36.75,35.4,35.1,35.95,36.08333333,35.6,35.4,35.8,35.85,35.6],[1.9375,1.4375,2.125,2.5,1.5,2.1875,2.3125,1.4375,2.875,1.375],[3.5,1.75,2.88888888888889,2.19444444444444,2.63888888888889,2.36111111111111,3.33333333333333,3.41666666666667,4.30555555555556,2.83333333333333],[3.22222222222222,2.33333333333333,4,4.44444444444444,4.11111111111111,4.33333333333333,3.88888888888889,4.77777777777778,4.44444444444444,4.66666666666667],[2.28571428571429,1,3.28571428571429,3.42857142857143,3.28571428571429,4.85714285714286,2.71428571428571,4,3.85714285714286,4.42857142857143]],"container":"<table class=\"display fill-container\">\n <thead>\n <tr>\n <th> <\/th>\n <th>age<\/th>\n <th>language<\/th>\n <th>avgtemp<\/th>\n <th>ALEX_mean<\/th>\n <th>ECR_mean<\/th>\n <th>HOME_mean<\/th>\n <th>KAMF_mean<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":5,"columnDefs":[{"className":"dt-right","targets":[1,2,3,4,5,6,7]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[5,10,25,50,100]}},"evals":[],"jsHooks":[]}</script>
---
class: center, middle
<span style="font-size: 60px;">9.2 PROCESS in R</span> <br>
---
# <h1 lang="en">Penguin Model 4</h1>
<div style="display:flex; align-items:center;">
<img src="picture/chp9/model4_hayse.png" style="width:45%; height: 45%;margin-right: 90px;">
<font size=20> Model 4 </font>
</div>
---
# <h1 lang="en">Penguin Model 4</h1>
<br>
<div style="display:flex; align-items:center;">
<img src="picture/chp9/model4.png" style="width:100%; height: 100%;"> <br>
</div>
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">CODE</h2>
```r
result.PROCESS <- capture.output({
# capture.output 可以将这段代码的结果存储进一个变量中
bruceR::PROCESS(df.pg.raw,
x = "DEQ", #自变量
y = "avgtemp", # 因变量
meds = c("socialdiversity"), # 中介变量
#mods = c(), # 调节变量
#covs = c(), # 控制变量(协变量)
#clusters = c(), #类似调节变量 分组变量
#hlm.re.m = "",hlm.re.y = "",hlm.type = c("1-1-1", "2-1-1", "2-2-1"),
#med.type = c("parallel", "serial"), # 中介变量之间是并列还是连续
#mod.type = c("2-way", "3-way"), # 两项交互还是三项交互
#mod.path = c(), # 调节哪条路径
cov.path = c("both"), # 控制变量进入几个方程,both意味着中介和因变量都会考虑控制变量
#mod1.val = NULL,mod2.val = NULL, # 调节变量高低水平对应的值
ci = c("boot"), # 置信区间使用bootstrap 或其他抽样方法
nsim = 1000, # 抽样的次数
#seed = NULL, # 设置seed可以让每次的抽样结果都一样
center = FALSE, # 是否对自变量和调节变量中心化。非中心化在解释时更方便
std = FALSE, # 是否在计算钱对所有变量进行标准化
digits = 3, # 保留的小数位数
#nsmall = digits,file = NULL
)
})
writeLines(result.PROCESS, "./output/chp9/PROCESS_model_4.md") # .md最整齐
```
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">OUTPUT</h2>
<img src="./picture/chp9/PROCESS_model_4_1.png" width="90%">
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">OUTPUT</h2>
<img src="./picture/chp9/PROCESS_model_4_2.png" width="100%">
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">OUTPUT</h2>
<img src="./picture/chp9/PROCESS_model_4_3.png" width="100%">
---
# <h1 lang="zh-CN">想~~编~~一个模型</h1>
<div style="display:flex; align-items:center;">
<img src="picture/chp9/model7_hayse.png" style="width:45%; height: 45%;margin-right: 90px;">
<font size=20> Model 7 </font>
</div>
---
# <h1 lang="zh-CN">想~~编~~一个模型</h1>
<div style="display:flex; align-items:center;">
<img src="picture/chp9/model7.png" style="width:100%; height: 100%;"> <br>
</div>
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">CODE</h2>
```r
result.PROCESS <- capture.output({
# capture.output 可以将这段代码的结果存储进一个变量中
bruceR::PROCESS(df.pg.mean,
x = "ALEX_mean", #自变量
y = "HOME_mean", # 因变量
meds = c("ECR_mean"), # 中介变量
mods = c("KAMF_mean"), # 调节变量
covs = c("age", "language", "avgtemp"), # 控制变量(协变量)
#clusters = c(), #类似调节变量 分组变量
#hlm.re.m = "",hlm.re.y = "",hlm.type = c("1-1-1", "2-1-1", "2-2-1"),
#med.type = c("parallel", "serial"), # 中介变量之间是并列还是连续
#mod.type = c("2-way", "3-way"), # 两项交互还是三项交互
mod.path = c("x-m"), # 调节哪条路径
cov.path = c("both"), # 控制变量进入几个方程,both意味着中介和因变量都会考虑控制变量
#mod1.val = NULL,mod2.val = NULL, # 调节变量高低水平对应的值
ci = c("boot"), # 置信区间使用bootstrap 或其他抽样方法
nsim = 100, # 抽样的次数
#seed = NULL, # 设置seed可以让每次的抽样结果都一样
center = FALSE, # 是否对自变量和调节变量中心化。非中心化在解释时更方便
std = FALSE, # 是否在计算钱对所有变量进行标准化
digits = 3, # 保留的小数位数
#nsmall = digits,file = NULL
)
})
writeLines(result.PROCESS, "./output/chp9/PROCESS_model_7.md") # .md最整齐
```
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">OUTPUT</h2>
<img src="./picture/chp9/PROCESS_model_7_1.png" width="100%">
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">OUTPUT</h2>
<img src="./picture/chp9/PROCESS_model_7_2.png" width="80%">
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">OUTPUT</h2>
<img src="./picture/chp9/PROCESS_model_7_3.png" width="100%">
---
# <h1 lang="en">bruceR::PROCESS</h1>
## <h2 lang="en">OUTPUT</h2>
<img src="./picture/chp9/PROCESS_model_7_4.png" width="100%">
---
class: center, middle
<span style="font-size: 60px;">9.3 SEM in R (lavaan)</span> <br>
---
# <h1 lang="en">lavaan::cfa</h1>
# <h2 lang="en">CODE</h2>
```r
result.CFA <- capture.output({
CFA<- 'DIDF =~ ALEX1 + ALEX3 + ALEX5 + ALEX6 + ALEX7 + ALEX10 + ALEX11 + ALEX2 + ALEX4 + ALEX8 + ALEX9
EOF =~ ALEX12 + ALEX13 + ALEX14 + ALEX15 + ALEX16
ANX =~ ECR1 + ECR2 + ECR3 + ECR4 + ECR5 + ECR6 + ECR7 + ECR8 + ECR9 + ECR10 + ECR11 + ECR12 + ECR13 + ECR14 + ECR15 + ECR16 + ECR17 + ECR18
AVO =~ ECR19 + ECR20 + ECR21 + ECR22 + ECR23 + ECR24 + ECR25 + ECR26 + ECR27 + ECR28 + ECR29 + ECR30 + ECR31 + ECR32 + ECR33 + ECR34 + ECR35 + ECR36
HOME =~ HOME1 + HOME2 + HOME3 + HOME4 + HOME5 + HOME6 + HOME7 + HOME8 + HOME9
KAMF =~ KAMF1 + KAMF2 + KAMF3 + KAMF4 + KAMF5 + KAMF6 + KAMF7' %>%
# ALEX =~ DIDF + EOF
# ECR =~ ANX + AVO
# 为了简化,这里没有做二阶
lavaan::cfa(.,data=df.pg.raw,estimator="ML")
lavaan::summary(CFA,fit.measures=TRUE,standardized=TRUE)
})
writeLines(result.CFA, "./output/chp9/CFA.md")
```
_<h4 lang="zh-CN">- 这里并不是完全正确的做法,该论文penguin对原始量表做了一些修改</h4>_
_<h4 lang="zh-CN">- 原始量表中ALEX是一个二阶潜变量,由三个一阶潜变量DIF, DDF, EOF构成</h4>_
_<h4 lang="zh-CN">- 但是DIF和DDF被合并为DIDF了(penguin没有说这是为什么)</h4>_
_<h4 lang="zh-CN">- 如果你每个维度单独做CFA,就能找到问题所在(是因为这两个维度共线性太高)</h4>_
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">OUTPUT: Model Fit</h2>
<img src="./picture/chp9/CFA_1.png" width="100%">
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">OUTPUT: Model Fit</h2>
<img src="./picture/chp9/CFA_2.png" width="60%">
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">OUTPUT: Factor Loading</h2>
<img src="./picture/chp9/CFA_3.png" width="100%">
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">OUTPUT: Factor Loading</h2>
<img src="./picture/chp9/CFA_4.png" width="100%">
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">OUTPUT: Factor Loading</h2>
<img src="./picture/chp9/CFA_5.png" width="100%">
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">OUTPUT: Factor Loading</h2>
<img src="./picture/chp9/CFA_6.png" width="100%">
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">OUTPUT: Covariances</h2>
<img src="./picture/chp9/CFA_7.png" width="70%">
<br>
如果有两个变量之间协方差(相关)过高,则说明存在共线性,可以删除其中一个
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">OUTPUT: Variances</h2>
<img src="./picture/chp9/CFA_8.png" width="70%">
<br>
残差和方差必须为正且显著,否则模型可能不收敛
_<h5 lang="en">- Heywood Case</h3>_
_<h5 lang="en">- Van Driel, O. P. (1978). On various causes of improper solutions in maximum likelihood factor analysis. Psychometrika, 43, 225-243.</h5>_
---
# <h1 lang="en">lavaan::cfa</h1>
## <h2 lang="en">Plot</h2>
```r
lavaanPlot(model = CFA, edge_options = list(color = "grey"))
```
由于模型的题目比较多,这里的图片已经看不清了,就不打印在PPT上了<br>
lavaan的画图比较鸡肋,AMOS在画图上会更好看一些(如下图) <br>
有兴趣的同学可以跑一下试试看<br>
<img src="./picture/chp9/AMOS.png" width="70%">
---
# <h1 lang="en">lavaan::sem</h1>
## <h2 lang="en">CODE</h2>
```r
# 这里拿model 4举例
# 因为lavaan的调节变量不能是潜变量. 如果是潜变量作为调节变量,需要手动与其他变量相乘
result.SEM <- capture.output({
SEM<- 'DIDF =~ ALEX1 + ALEX3 + ALEX5 + ALEX6 + ALEX7 + ALEX10 + ALEX11 + ALEX2 + ALEX4 + ALEX8 + ALEX9
EOF =~ ALEX12 + ALEX13 + ALEX14 + ALEX15 + ALEX16
ALEX =~ DIDF + EOF
ANX =~ ECR1 + ECR2 + ECR3 + ECR4 + ECR5 + ECR6 + ECR7 + ECR8 + ECR9 + ECR10 + ECR11 + ECR12 + ECR13 + ECR14 + ECR15 + ECR16 + ECR17 + ECR18
AVO =~ ECR19 + ECR20 + ECR21 + ECR22 + ECR23 + ECR24 + ECR25 + ECR26 + ECR27 + ECR28 + ECR29 + ECR30 + ECR31 + ECR32 + ECR33 + ECR34 + ECR35 + ECR36
ECR =~ ANX + AVO
HOME =~ HOME1 + HOME2 + HOME3 + HOME4 + HOME5 + HOME6 + HOME7 + HOME8 + HOME9
ECR ~ a*ALEX + age + language + avgtemp
HOME ~ b*ECR + age + language + avgtemp
ind_eff:=a*b ' %>%
lavaan::sem(.,data=df.pg.raw,estimator="ML")
lavaan::summary(SEM,fit.measures=TRUE, standardized=TRUE, ci=TRUE)
})
writeLines(result.SEM, "./output/chp9/SEM.md")
```
---
# <h1 lang="en">lavaan::sem</h1>
## <h2 lang="en">OUTPUT: Path Analysis and Indirect Effect</h2>
<img src="./picture/chp9/SEM_1.png" width="100%"> <br>
<img src="./picture/chp9/SEM_2.png" width="100%"> <br>
大多数结果与CFA类似,但是多了路径系数,以及我们设定的中介效应值
---
class: center, middle
<span style="font-size: 60px;">9.4 Others</span> <br>
---
# <h1 lang="zh-CN">其他有用的包</h1>
1. 如果你的SEM模型中一定用latent moderator,你可以使用Mplus,或者回到PROCESS <br>
2. Mplus某种意义上也是一个R包,install.packages("MplusAutomation") <br>
3. CFA得到之后,可以借助semTools这个包来提取矩阵lambda, theta, psi, beta. 这些矩阵用于制作收敛和区别效度表 <br>
其他SEM的细节就不在R课里讲述了
---
# <h1 lang="en">Videos</h1>
<div style="display:flex; align-items:center;">
<img src="./picture/chp9/SEM_bilibili.png" style="width:100%; height: 100%;">
</div>
https://www.bilibili.com/video/BV1Bt4y127xq/?spm_id_from=333.999.0.0&vd_source=4b61fa5a135da9b04ce5af0535b1e28c
<br>
有兴趣的同学可以去看这个视频学习怎么做SEM
---
# <h1 lang="en">Videos</h1>
<div style="display:flex; align-items:center;">
<img src="./picture/chp9/SEM_youtube.png" style="width:100%; height: 100%;">
</div>
https://www.youtube.com/watch?v=fGdsiugwO0k&list=PLliBbGBc5nn3m8bXQ4CmOep3UmQ_5tVlC&index=1&ab_channel=SachaEpskamp
<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>