-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter12.Rmd
901 lines (660 loc) · 20.1 KB
/
chapter12.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
---
title: "第12章 中介与调节效应及检验"
author: "wmj"
date: "`r Sys.Date()`"
output:
officedown::rdocx_document:
number_sections: yes
df_print: kable
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
warning = FALSE,
message = FALSE,
fig.asp = 0.618,
dpi = 300
)
options(digits = 3)
```
# 效应检验软件操作(p237)
## 1.直接效应检验(p237)
```{r}
library(tidyverse)
library(lavaan)
d <- haven::read_sav("rawdata/直接效应检验.sav")
d %>% sjPlot::view_df()
d
```
```{r}
d1 <- d %>%
rowwise() %>%
mutate(
CW_new = mean(c_across(num_range("CW", 1:5))),
LH_new = mean(c_across(num_range("LH", 1:5)))
) %>%
ungroup() %>%
rename(age = `年龄`, edu = `学历`) %>%
mutate(
across(c(age, edu, LH, CW), ~ (.x - mean(.x))/sd(.x))
)
d1
```
```{r}
M1 <- lm(LH ~ 1 + age + edu, data = d1)
M2 <- lm(LH ~ 1 + age + edu + CW, data = d1)
```
表12-1,直接效应分层回归分析结果(p241)
```{r}
library(modelsummary)
mlist <- lst(M1, M2)
mlist %>%
modelsummary::modelsummary(
estimate = "{estimate}{stars}",
statistic = c("conf.low", "conf.high", "p.value"),
shape = term ~ model + statistic,
fmt = fmt_statistic(estimate = 3),
coef_omit = "(Intercept)",
gof_map = c("r.squared", "adj.r.squared", "F")
)
```
可以用以下命令查看并定制参数
```{r, eval=FALSE}
modelsummary::gof_map
```
## 2.中介效应检验(p241)
```{r}
library(tidyverse)
library(lavaan)
d <- haven::read_sav("rawdata/SPSS和Smartpls中介效应检验.sav")
d %>% sjPlot::view_df()
d
```
```{r}
d2 <- d %>%
rowwise() %>%
mutate(
CW_new = mean(c_across(num_range("CW", 1:5))), # 薪酬福利
WS_new = mean(c_across(num_range("CW", 1:4))), # 工作满意度
LH_new = mean(c_across(num_range("LH", 1:5))) # 生活幸福感
) %>%
ungroup() %>%
rename(age = `年龄`, edu = `学历`) %>%
mutate(
across(c(age, edu, LH, CW, WS), ~ (.x - mean(.x))/sd(.x))
)
d2
```
```{r}
M1 <- lm(LH ~ 1 + age + edu, data = d2)
M2 <- lm(LH ~ 1 + age + edu + CW, data = d2)
M3 <- lm(LH ~ 1 + age + edu + CW + WS, data = d2)
```
表12-2,中介效应分层回归分析结果(p245)
```{r}
library(modelsummary)
mlist <- lst(M1, M2, M3)
mlist %>%
modelsummary::modelsummary(
estimate = "{estimate}{stars}",
statistic = c("conf.low", "conf.high", "p.value"),
shape = term ~ model + statistic,
fmt = fmt_statistic(estimate = 3),
coef_omit = "(Intercept)",
gof_map = c("r.squared", "adj.r.squared", "F"),
output = "gt"
) %>%
gt::data_color(
columns = c(6, 10),
rows = 3,
colors = "orange"
) %>%
gt::data_color(
columns = 10,
rows = 4,
colors = "red"
) %>%
gt::data_color(
columns = c(6, 10),
rows = 5,
colors = "pink"
)
```
- 观测模型M1,控制变量(age, edu)与因变量间不存在因果关系。
- 观测模型M2,加入自变量之后,回归模型解释因变量(生活幸福感)的变异比例为0.277,自变量(薪酬福利)与因变量(生活幸福感)标准化系数为0.525,具有统计显著性,证明了自变量与因变量间的直接效应。
- 观测模型M3,同时加入自变量和中介变量之后,回归模型解释因变量(生活幸福感)的变异比例为0.365,回归模型的总体显著性也良好,模型M3整体拟合度大于模型M2,显著性更优,且中介变量(工作满意度)与因变量之间的标准化系数为0.417,中介变量的加入,导致自变量与因变量间的标准化系数减少为0.237,依然具有显著性,因此可以说明工作满意度发挥了部分中介效果。(p245)
## 3.调节效应检验(p245)
```{r}
library(tidyverse)
library(lavaan)
d <- haven::read_sav("rawdata/调节效应检验.sav")
d %>% sjPlot::view_df()
d
```
第一步,计算各变量和交互项。在利用分层回归验证调节效应时,模型中的自变量、调节项和相互项均需要进行中心化处理,而因变量不需要中心化处理。(p245)
```{r}
d3 <- d %>%
rowwise() %>%
mutate(
IL = mean(c_across(IL1:IL6)), # 包容型领导
OC = mean(c_across(OC1:OC5)), # 组织承诺
VB = mean(c_across(VB1:VB5)) # 建言行为
) %>%
ungroup() %>%
rename(age = `年龄`, edu = `学历`) %>%
mutate(
across(c(age, edu, VB), ~ (.x - mean(.x)) / sd(.x)),
across(c(IL, OC), ~ .x - mean(.x))
)
```
```{r}
M1 <- lm(VB ~ 1 + age + edu, data = d3)
M2 <- lm(VB ~ 1 + age + edu + IL + OC, data = d3)
M3 <- lm(VB ~ 1 + age + edu + IL + OC + IL:OC, data = d3)
```
表12-3,调节效应分层回归分析结果(p249)
```{r}
library(modelsummary)
mlist <- lst(M1, M2, M3)
mlist %>%
modelsummary::modelsummary(
estimate = "{estimate}{stars}",
statistic = c("conf.low", "conf.high", "p.value"),
shape = term ~ model + statistic,
fmt = fmt_statistic(estimate = 3),
coef_omit = "(Intercept)",
gof_map = c("r.squared", "adj.r.squared", "F"),
output = "gt"
) %>%
gt::data_color(
columns = 10,
rows = c(5, 6),
colors = "orange"
)
```
- 观测模型M1,控制变量年龄与因变量不存在因果关系。
- 观测模型M2,加入自变量和调节变量后,回归模型解释因变量(建言行为)的变异比例为0.427,回归模型的总体显著性良好,并且自变量(包容型领导)与因变量(建言行为)的标准化系数为0.223,具有统计显著性,证明了自变量与因变量的正向直接效应。
- 观测模型M3,加入交互项之后,回归模型解释因变量(建言行为)的变异比例为0.494,回归模型的总体显著性良好,并且交互项与因变量(建言行为)的标准化系数为0.382,具有统计显著性,证明了(组织承诺)调节了包容型领导与建言行为的正向因果关系。(p247)
图12-24,简单斜率检测(p249)
```{r, fig.asp=0.618, fig.width = 5}
library(marginaleffects)
mean_sd <- function(x) mean(x) + c(-sd(x), sd(x))
M3 %>%
marginaleffects::predictions(
newdata = datagrid(
IL = mean_sd,
OC = mean_sd
)
) %>%
as_tibble() %>%
mutate(across(c(IL, OC), as.factor)) %>%
ggplot(aes(x = IL, y = estimate, group = OC)) +
geom_line(aes(linetype = OC), linewidth = 2) +
scale_x_discrete(
name = NULL,
labels = c("Low IL", "High IL")
) +
scale_y_continuous(
name = "VB",
limits = c(-1, 2),
expand = c(0, 0)
) +
scale_linetype_manual(
name = NULL,
values = c("solid", "dotted"),
labels = c("Low OC", "High OC")
) +
guides(linetype = guide_legend(reverse = TRUE)) +
theme_classic() +
theme(axis.ticks.x = element_blank())
```
```{r, fig.asp=0.618, fig.width = 5}
library(interactions)
interact_plot(M3, pred = "IL", modx = "OC")
```
# SmartPLS软件操作检验分析(p250)
SmartPLS是什么玩意?本章大多无法重复。
```{r}
library(tidyverse)
library(lavaan)
d <- haven::read_sav("rawdata/SPSS和Smartpls中介效应检验.sav")
d %>% sjPlot::view_df()
d
```
```{r}
model <- '
CW =~ CW1 + CW2 + CW3 + CW4 + CW5
WS =~ WS1 + WS2 + WS3 + WS4
LH =~ LH1 + LH2 + LH3 + LH4 + LH5
'
fit_cfa <- cfa(model,
data = d %>% select(-CW, -WS, -LH),
estimator = "MLR",
mimic = "Mplus")
```
```{r}
CR <- semTools::compRelSEM(fit_cfa)
Cronbach_alpha <- semTools::reliability(fit_cfa)[1, ]
AVE <- semTools::AVE(fit_cfa)
tibble(
items = names(CR),
alpha = Cronbach_alpha,
CR = CR,
AVE = AVE,
)
```
```{r}
m <- lavInspect(fit_cfa, what = "cor.lv")
m[upper.tri(m)] <- NA
diag(m) <- semTools::AVE(fit_cfa) %>% sqrt()
m
```
```{r}
model <- '
CW =~ CW1 + CW2 + CW3 + CW4 + CW5
WS =~ WS1 + WS2 + WS3 + WS4
LH =~ LH1 + LH2 + LH3 + LH4 + LH5
WS ~ a * CW
LH ~ b * WS + cprime * CW
indirect := a*b
total := a*b + cprime
'
fit_sem <- sem(model,
data = d %>% select(-CW, -WS, -LH),
estimator = "ML",
se = "bootstrap",
mimic = "Mplus")
```
表12-8 中介效应分析结果(p257)
```{r}
fit_sem %>%
parameterEstimates(standardized = T) %>%
filter(op == "~") %>%
select(lhs, rhs, label, "coef" = std.all, se, "p" = pvalue) %>%
mutate(
Path = str_c(label, ": ", rhs, "-->", lhs),
.keep = "unused",
.before = 1
) %>%
flextable::flextable() %>%
flextable::colformat_double(digits = 3) %>%
flextable::autofit()
```
## PLS-PM
在数学上结构方程模型有两种实现方法
- CBSEM (covariance-based SEM)
- PLS-PM (Partial Least Squares Path Modeling)
前者常用lavaan宏包,后者往往用cSEM、seminr等宏包
### 使用cSEM
```{r}
library(tidyverse)
d <- haven::read_sav("rawdata/SPSS和Smartpls中介效应检验.sav") %>%
select(-CW, -WS, -LH)
d %>% sjPlot::view_df()
```
```{r}
library(cSEM)
model <- '
CW =~ CW1 + CW2 + CW3 + CW4 + CW5
WS =~ WS1 + WS2 + WS3 + WS4
LH =~ LH1 + LH2 + LH3 + LH4 + LH5
WS ~ CW
LH ~ WS + CW
'
fit <- csem(.data = d,
.model = model,
.approach_weights = "PLS-PM",
.resample_method = "bootstrap")
```
```{r}
summarize(fit)
assess(fit)
infer(fit)
predict(fit)
verify(fit)
```
### 使用seminr
```{r}
library(tidyverse)
d <- haven::read_sav("rawdata/SPSS和Smartpls中介效应检验.sav") %>%
select(-CW, -WS, -LH)
d %>% sjPlot::view_df()
```
```{r}
library(seminr)
mm <- constructs(
reflective("CW", multi_items("CW", 1:5)),
reflective("WS", multi_items("WS", 1:4)),
reflective("LH", multi_items("LH", 1:5))
)
sm <- relationships(
paths(from = "WS", to = "LH"),
paths(from = "CW", to = c("WS", "LH"))
)
pls_model <- estimate_pls(
data = d,
measurement_model = mm,
structural_model = sm
)
fit <- summary(pls_model)
summary(fit)
```
```{r}
fit$paths
fit$fSquare
fit$total_effects
fit$total_indirect_effects
fit$loadings
fit$validity
fit$reliability
```
```{r}
boot_estimates <- bootstrap_model(pls_model, nboot = 1000, cores = parallel::detectCores())
```
```{r}
boot <- summary(boot_estimates)
boot$bootstrapped_paths
boot$bootstrapped_loadings
```
```{r}
plot(boot_estimates)
```
# 复杂模型检验(p261)
## 1. 链式多重中介模型检验(p263)
本节以包容型领导、组织信任、心理安全感和创新行为组成的链式多重中介模型为案例介绍检验步骤。
```{r}
library(tidyverse)
library(lavaan)
d <- haven::read_sav("rawdata/多重链式中介模型检验.sav")
d %>% sjPlot::view_df()
d
```
```{r}
d5 <- d %>%
rowwise() %>%
mutate(
IL = mean(c_across(IL1:IL5)), # 包容型领导
OT = mean(c_across(OT1:OT4)), # 组织信任
SPS = mean(c_across(SPS1:SPS6)), # 心理安全感
IB = mean(c_across(IB1:IB5)) # 创新行为
) %>%
ungroup() %>%
rename(age = `年龄`, edu = `学历`) %>%
mutate(
across(c(age, edu, IL, OT, SPS, IB), ~ (.x - mean(.x)) / sd(.x))
)
```
```{r}
M1 <- lm(OT ~ 1 + age + edu + IL, data = d5)
M2 <- lm(SPS ~ 1 + age + edu + IL + OT, data = d5)
M3 <- lm(IB ~ 1 + age + edu + IL + OT + SPS, data = d5)
```
表12-13,链式多重中介变量回归分析表(p266)
```{r}
library(modelsummary)
mlist <- lst(M1, M2, M3)
mlist %>%
modelsummary::modelsummary(
estimate = "{estimate}{stars}",
statistic = c("conf.low", "conf.high", "p.value"),
shape = term ~ model + statistic,
fmt = fmt_statistic(estimate = 3),
coef_omit = "(Intercept)",
gof_map = c("r.squared", "adj.r.squared", "F"),
output = "gt"
)
```
表12-14,bootstrap中介效应检验表(p268)
```{r}
model <- '
OT ~ a1 * IL
SPS ~ a2 * IL + d12 * OT
IB ~ cprime * IL + b1 * OT + b2 * SPS
Ind_X_M1_Y := a1*b1
Ind_X_M2_Y := a2*b2
Ind_X_M1_M2_Y := a1*d12*b2
total_indrect := a1*b1 + a2*b2 + a1*d12*b2
direct := cprime
total := a1*b1 + a2*b2 + a1*d12*b2 + cprime
'
fit_sem <- sem(model,
data = d5,
estimator = "ML",
se = "bootstrap",
bootstrap = 5000,
mimic = "Mplus")
```
```{r}
fit_sem %>%
parameterEstimates(standardized = T) %>%
filter(op == ":=") %>%
select(label, est, se, ci.lower, ci.upper, pvalue, std.all) %>%
flextable::flextable() %>%
flextable::colformat_double(digits = 3) %>%
flextable::autofit()
```
## 2.有调节的中介模型检验(p268)
本节以包容型领导、组织支持感、心理授权和建言行为组成的有调节的中介模型为案例。
```{r}
library(tidyverse)
library(lavaan)
d <- haven::read_sav("rawdata/有调节的中介模型检验.sav")
d %>% sjPlot::view_df()
d
```
```{r}
d6 <- d %>%
rowwise() %>%
mutate(
IL = mean(c_across(IL1:IL5)), # 包容型领导
PE = mean(c_across(PE1:PE6)), # 心理授权
POS = mean(c_across(POS1:POS5)), # 组织支持感
VB = mean(c_across(VB1:VB5)) # 建言行为
) %>%
ungroup() %>%
rename(age = `年龄`, edu = `学历`) %>%
mutate(
across(c(age, edu, IL, PE, POS, VB), ~ (.x - mean(.x)) / sd(.x))
)
```
```{r}
M1 <- lm(PE ~ 1 + age + edu + IL, data = d6)
M2 <- lm(VB ~ 1 + age + edu + IL, data = d6)
M3 <- lm(VB ~ 1 + age + edu + IL + PE, data = d6)
```
表12-15,有调节的中介模型的中介效应检验表(p270)
```{r}
library(modelsummary)
mlist <- lst(M1, M2, M3)
mlist %>%
modelsummary::modelsummary(
estimate = "{estimate}{stars}",
statistic = c("conf.low", "conf.high", "p.value"),
shape = term ~ model + statistic,
fmt = fmt_statistic(estimate = 3),
coef_omit = "(Intercept)",
gof_map = c("r.squared", "adj.r.squared", "F"),
output = "gt"
) %>%
gt::data_color(
columns = c(6, 10),
rows = 3,
colors = "orange"
) %>%
gt::data_color(
columns = c(6, 10),
rows = 5,
colors = "pink"
)
```
- 观测模型M1,该模型包容型领导为自变量,心理授权为因变量,回归模型解释因变量心理授权的变异比例为0.318,模型总体显著性良好。
- 观测模型M2,该模型包容型领导为自变量,建言行为为因变量,回归模型解释因变量建言行为的变异比例为0.296,模型总体显著性良好。
- 观测模型M3,该模型包容型领导和心理授权为自变量,建言行为因变量,回归模型解释因变量建言行为的变异比例为0.39,模型总体显著性良好。并且模型M3加入中介变量(心理授权)后,中介变量与因变量之间的标准化系数为0.373,具有统计显著性,自变量(包容型领导)与因变量(建言行为)间的标准化系数**减少**为0.352,且依然具有统计显著性。表明直接效应通过了检验,心理授权的中介效应得到了初步验证。(p270)
表12-16,有调节的中介模型的中介效应bootstrap检验(p271)
```{r}
model <- '
PE ~ a * IL
VB ~ cprime * IL + b * PE
indrect := a*b
direct := cprime
total := a*b + cprime
'
fit_sem <- sem(model,
data = d6,
estimator = "ML",
se = "bootstrap",
bootstrap = 5000,
mimic = "Mplus")
```
```{r}
fit_sem %>%
parameterEstimates(standardized = T) %>%
filter(op == ":=") %>%
select(label, est, se, ci.lower, ci.upper, pvalue, std.all) %>%
flextable::flextable() %>%
flextable::colformat_double(digits = 3) %>%
flextable::autofit()
```
观察心理授权的间接效应,bootstrap运算结果的区间并未包含0,因此心理授权的中介效应通过了检验。(p270)
表12-17,有调节的中介模型调节效应检验表(p273)
```{r}
d7 <- d %>%
rowwise() %>%
mutate(
IL = mean(c_across(IL1:IL5)), # 包容型领导
PE = mean(c_across(PE1:PE6)), # 心理授权
POS = mean(c_across(POS1:POS5)), # 组织支持感
VB = mean(c_across(VB1:VB5)) # 建言行为
) %>%
ungroup() %>%
rename(age = `年龄`, edu = `学历`) %>%
mutate(
across(c(age, edu, PE, POS, IL, VB), ~ .x - mean(.x))
)
```
```{r}
M1 <- lm(PE ~ 1 + age + edu + IL, data = d7)
M2 <- lm(VB ~ 1 + age + edu + IL + PE + POS + PE:POS, data = d7)
```
```{r}
library(modelsummary)
mlist <- lst(M1, M2)
mlist %>%
modelsummary::modelsummary(
estimate = "{estimate}{stars}",
statistic = c("conf.low", "conf.high", "p.value"),
shape = term ~ model + statistic,
fmt = fmt_statistic(estimate = 3),
coef_omit = "(Intercept)",
gof_map = c("r.squared", "adj.r.squared", "F"),
output = "gt"
)
```
交互项的系数0.187,且具有统计显著性。说明组织支持感正向调节了心理授权与建言行为间的正向直接效应。(p272)
图12-61,Johnson-Neyman 法简单斜率图(p274)
```{r}
library(interactions)
interactions::johnson_neyman(
M2,
pred = "PE",
modx = "POS"
)
```
图中横轴表示调节变量(组织支持感),纵轴表示简单斜率。
两条竖直虚线在横轴上形成的区域并没有包含0,说明调节效应显著。(p274)
表12-18,在组织支持感的不同水平上的中介效应(p275)
```{r}
model <- '
PE ~ a * IL
VB ~ cprime * IL + b1 * PE + b2 * POS + b3*PE:POS
POS ~ POS_mean*1
POS ~~ POS_var*POS
indirect.above := (b1 + b3*(POS_mean + sqrt(POS_var)))*(a)
indirect := (b1 + b3*POS_mean)*(a)
indirect.below := (b1 + b3*(POS_mean - sqrt(POS_var)))*(a)
'
fit_sem <- sem(model,
data = d7,
estimator = "ML",
se = "bootstrap",
bootstrap = 5000,
mimic = "Mplus")
```
有调节的中介效应
```{r}
fit_sem %>%
parameterEstimates() %>%
filter(op %in% c(":=")) %>%
select(-lhs, -op, -rhs, -z) %>%
flextable::flextable() %>%
flextable::colformat_double(digits = 3)
```
# 课后作业
```{r}
library(tidyverse)
library(lavaan)
d <- haven::read_sav("rawdata/有调节的中介模型检验.sav")
d %>% sjPlot::view_df()
d
```
假定模型如下
```{r}
knitr::include_graphics("./images/homeworkmodel.jpg")
```
计算在组织支持感不同水平的间接效应和直接效应
## 答案
```{r}
d7 <- d %>%
rowwise() %>%
mutate(
IL = mean(c_across(IL1:IL5)), # 包容型领导
PE = mean(c_across(PE1:PE6)), # 心理授权
POS = mean(c_across(POS1:POS5)), # 组织支持感
VB = mean(c_across(VB1:VB5)) # 建言行为
) %>%
ungroup() %>%
rename(age = `年龄`, edu = `学历`) %>%
mutate(
across(c(age, edu, PE, POS, IL, VB), ~ .x - mean(.x))
)
```
```{r}
model <- '
PE ~ a*IL
VB ~ c1*IL + c2*POS + c3*IL:POS + b1*PE + b2*PE:POS
POS ~ POS.mean*1
POS ~~ POS.var*POS
CE.MonY := b1 + b2*POS.mean
CE.MonY.below := b1 + b2*(POS.mean-sqrt(POS.var))
CE.MonY.above := b1 + b2*(POS.mean+sqrt(POS.var))
indirect := (a)*(b1 + b2*POS.mean)
indirect.below := (a)*(b1 + b2*(POS.mean - sqrt(POS.var)))
indirect.above := (a)*(b1 + b2*(POS.mean + sqrt(POS.var)))
index.mod.med := a*b2
direct := c1 + c3*POS.mean
direct.below := c1 + c3*(POS.mean - sqrt(POS.var))
direct.above := c1 + c3*(POS.mean + sqrt(POS.var))
total := direct + indirect
total.below := direct.below + indirect.below
total.above := direct.above + indirect.above
prop.mediated := indirect / total
prop.mediated.below := indirect.below / total.below
prop.mediated.above := indirect.above / total.above
'
fit_sem <- sem(model,
data = d7,
estimator = "ML",
se = "bootstrap",
bootstrap = 5000,
mimic = "Mplus")
```
```{r}
fit_sem %>%
parameterEstimates() %>%
filter(op %in% c(":=")) %>%
select(-lhs, -op, -rhs, -z) %>%
flextable::flextable() %>%
flextable::colformat_double(digits = 3)
```
## 参考
- <https://rpubs.com/cardiomoon/468602>