-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplssemc_estat.ado
1724 lines (1544 loc) · 55.4 KB
/
plssemc_estat.ado
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
*!plssemc_estat version 0.6.3
*!Written 9Oct2024
*!Written by Sergio Venturini and Mehmet Mehmetoglu
*!The following code is distributed under GNU General Public License version 3 (GPL-3)
program plssemc_estat, rclass
version 15.1
gettoken subcmd rest : 0 , parse(", ")
local lsubcmd = length("`subcmd'")
local ismi = (e(cmd) == "mi estimate")
if (`ismi') {
display as error "plssemc postestimation is not available after multiple imputation"
exit
}
if ("`subcmd'" == substr("indirect", 1, max(2, `lsubcmd'))) {
indirect_c `rest'
}
else if ("`subcmd'" == substr("total", 1, max(2, `lsubcmd'))) {
total_c `rest'
}
else if ("`subcmd'" == substr("vif", 1, max(2, `lsubcmd'))) {
plssem_vif_c `rest'
}
else if ("`subcmd'" == substr("mediate", 1, max(2, `lsubcmd'))) {
mediate_c `rest'
}
else if ("`subcmd'" == substr("htmt", 1, max(2, `lsubcmd'))) {
htmt_c `rest'
}
else if ("`subcmd'" == substr("f2", 1, max(2, `lsubcmd'))) {
f2_c `rest'
}
else if ("`subcmd'" == substr("ic", 1, max(2, `lsubcmd'))) {
ic_c `rest'
}
else if ("`subcmd'" == substr("fit", 1, max(2, `lsubcmd'))) {
fit_c `rest'
}
else if ("`subcmd'" == substr("ci", 1, max(2, `lsubcmd'))) {
ci_c `rest'
}
else if ("`subcmd'" == substr("blindfolding", 1, max(2, `lsubcmd'))) {
blindf_c `rest'
}
else {
// estat_default `0'
display as error "the `subcmd' postestimation command is not implemented for plssemc"
exit
}
return add
end
program indirect_c, rclass
version 15.1
syntax , Effects(string) [ Boot(numlist integer >0 min=1 max=1) ///
Seed(numlist max=1) Level(real 0.95) DIGits(integer 3) ]
/* Options:
--------
effects(string) --> list of indirect effects
boot(numlist integer >0 min=1 max=1)
--> bootstrap estimation (# of repetions;
default 50)
seed(numlist max=1) --> bootstrap seed number
level(real 0.95) --> confidence level (default 0.95)
digits(integer 3) --> number of digits to display (default 3)
*/
/* Description:
------------
This postestimation command provides the estimates for the indirect
effects mediated by only one LV.
*/
display as error "estat indirect after plssemc will be available in a future version!"
exit
if ("`effects'" == "") {
display as error "effects() option must be provided"
exit
}
if (`digits' < 0) {
display as error "number of digits must be a nonnegative integer"
exit
}
local struct "structural"
local props = e(properties)
local isstruct : list struct in props
if (!`isstruct') {
display as error "the fitted plssem model includes only the measurement part"
exit
}
tempvar __touse__
quietly generate `__touse__' = e(sample)
local reg3eqs = e(struct_eqs)
if (`level' <= 0 | `level' >= 1) {
display as error "confidence level must be in the range (0, 1)"
exit
}
tempname normperc alpha_cl
scalar `alpha_cl' = 1 - ((1 - `level')/2)
scalar `normperc' = invnormal(`alpha_cl')
/* Parse indirect statements (dep1 med1 indep1, dep2 med2 indep2, etc.) */
local effects : list clean effects
tokenize `"`effects'"', parse(",")
local num_effects = 0
local tok_i = 1
while ("``tok_i''" != "") {
local 0 ``tok_i''
syntax varlist(min=3 max=3)
local ind_dvar `ind_dvar' `: word 1 of `varlist''
local ind_mvar `ind_mvar' `: word 2 of `varlist''
local ind_ivar `ind_ivar' `: word 3 of `varlist''
local ++num_effects
local tok_i = `tok_i' + 2
}
if (`num_effects' > 5) {
display as error "a maximum of 5 indirect effects are allowed"
error 103
}
/* End parsing indirect statements */
tempname sobel_se sobel_z sobel_pv indirect sobel_lci sobel_uci
tempname indmeas reg3coef reg3coef_bs reg3var
tempname dommat dommat2 moimat moimat2 //doimat doimat2
if ("`boot'" == "") {
matrix `indmeas' = J(6, `num_effects', .)
}
else {
matrix `indmeas' = J(10, `num_effects', .)
}
tempname ehold
_estimates hold `ehold'
if ("`boot'" != "") {
display
display as text "Bootstrapping indirect effects..."
}
capture {
forvalues ll = 1/`num_effects' {
local dep `: word `ll' of `ind_dvar''
local med `: word `ll' of `ind_mvar''
local indep `: word `ll' of `ind_ivar''
local indmeas_colnames "`indmeas_colnames' `dep'_`med'_`indep'"
if ("`dep'" != "" & "`med'" != "" & "`indep'" != "") {
local doi `dep':`indep'
local moi `med':`indep'
local dom `dep':`med'
if ("`boot'" == "") {
quietly reg3 `reg3eqs' if `__touse__', mvreg corr(independent)
matrix `reg3coef' = e(b)
matrix `reg3var' = e(V)
local reg3eq_nm : colfullnames `reg3coef'
tempname moicoef moivar domcoef domvar
matrix `moimat' = `reg3coef'[1, "`moi'"]
scalar `moicoef' = `moimat'[1, 1]
matrix `moimat2' = `reg3var'["`moi'","`moi'"]
scalar `moivar' = `moimat2'[1, 1]
matrix `dommat' = `reg3coef'[1, "`dom'"]
scalar `domcoef' = `dommat'[1, 1]
matrix `dommat2' = `reg3var'["`dom'", "`dom'"]
scalar `domvar' = `dommat2'[1, 1]
scalar `indirect' = `moicoef'*`domcoef'
scalar `sobel_se' = sqrt(((`domcoef')^2)*`moivar' + ((`moicoef')^2)*`domvar')
scalar `sobel_z' = `indirect'/`sobel_se'
scalar `sobel_pv' = 2*(1 - normal(abs(`sobel_z')))
scalar `sobel_lci' = `indirect' - `normperc'*`sobel_se'
scalar `sobel_uci' = `indirect' + `normperc'*`sobel_se'
matrix `indmeas'[1, `ll'] = `indirect'
matrix `indmeas'[2, `ll'] = `sobel_se'
matrix `indmeas'[3, `ll'] = `sobel_z'
matrix `indmeas'[4, `ll'] = `sobel_pv'
matrix `indmeas'[5, `ll'] = `sobel_lci'
matrix `indmeas'[6, `ll'] = `sobel_uci'
}
else {
tempname reg3ciP reg3ciBC
quietly bootstrap indeff=(_b[`dom']*_b[`moi']), ties reps(`boot') ///
seed(`seed'): reg3 `reg3eqs' if `__touse__', mvreg corr(independent)
quietly estat bootstrap, all
matrix `reg3coef' = e(b)
matrix `reg3coef_bs' = e(b_bs) // this is not used for now
matrix `reg3var' = e(V)
matrix `reg3ciP' = e(ci_percentile)
matrix `reg3ciBC' = e(ci_bc)
tempname iecoef ievar perc_lci perc_uci bc_lci bc_uci
matrix `moimat' = `reg3coef'[1, 1]
scalar `iecoef' = `moimat'[1, 1]
matrix `moimat2' = `reg3var'[1, 1]
scalar `ievar' = `moimat2'[1, 1]
scalar `indirect' = `iecoef'
scalar `sobel_se' = sqrt(`ievar')
scalar `sobel_z' = `indirect'/`sobel_se'
scalar `sobel_pv' = 2*(1 - normal(abs(`sobel_z')))
scalar `sobel_lci' = `indirect' - `normperc'*`sobel_se'
scalar `sobel_uci' = `indirect' + `normperc'*`sobel_se'
scalar `perc_lci' = `reg3ciP'[1, 1]
scalar `perc_uci' = `reg3ciP'[2, 1]
scalar `bc_lci' = `reg3ciBC'[1, 1]
scalar `bc_uci' = `reg3ciBC'[2, 1]
matrix `indmeas'[1, `ll'] = `indirect'
matrix `indmeas'[2, `ll'] = `sobel_se'
matrix `indmeas'[3, `ll'] = `sobel_z'
matrix `indmeas'[4, `ll'] = `sobel_pv'
matrix `indmeas'[5, `ll'] = `sobel_lci'
matrix `indmeas'[6, `ll'] = `sobel_uci'
matrix `indmeas'[7, `ll'] = `perc_lci'
matrix `indmeas'[8, `ll'] = `perc_uci'
matrix `indmeas'[9, `ll'] = `bc_lci'
matrix `indmeas'[10, `ll'] = `bc_uci'
}
}
else {
display as error "provide a single dependent, independent and mediator variable for each indirect effect"
exit
}
}
if ("`boot'" == "") {
matrix rownames `indmeas' = "Indirect effect" "Standard error" ///
"Z statistic" "P-value" "Lower CI" "Upper CI"
}
else {
matrix rownames `indmeas' = "Indirect effect" "Standard error" ///
"Z statistic" "P-value" "Lower CI (N)" "Upper CI (N)" ///
"Lower CI (P)" "Upper CI (P)" "Lower CI (BC)" "Upper CI (BC)"
}
matrix colnames `indmeas' = `indmeas_colnames'
} // end of -capture-
local rc = _rc
_estimates unhold `ehold'
error `rc'
/* Display results */
local ind_line = 5
if ("`boot'" != "") {
local ind_line = `ind_line' + 2
}
mktable_indirect, matrix(`indmeas') digits(`digits') boot(`boot') ///
title("Significance testing of (standardized) indirect effects") ///
firstcolname("Statistics") firstcolwidth(24) colwidth(18) ///
hlines(`ind_line') level(`level') //novlines
/* Return values */
return matrix indirect `indmeas'
end
program total_c, rclass
version 15.1
syntax [ , DIGits(integer 3) Plot ]
/* Options:
--------
digits(integer 3) --> number of digits to display (default 3)
plot --> bar plot of the effects
*/
local struct "structural"
local props = e(properties)
local isstruct : list struct in props
if (!`isstruct') {
display as error "the fitted plssem model includes only the measurement part"
exit
}
if (`digits' < 0) {
display as error "number of digits must be a nonnegative integer"
exit
}
tempname B path direct indirect total
matrix `B' = e(pathcoef)
local nlv = colsof(`B')
local nlv_m1 = `nlv' - 1
matrix `indirect' = J(`nlv', `nlv', 0)
if (`nlv' <= 2) {
matrix `total' = `B'
}
else {
matrix `path' = `B'
forvalues k = 2/`nlv_m1' {
matrix `path' = `path' * `B'
matrix `indirect' = `indirect' + `path'
}
}
matrix `total' = `B' + `indirect'
tempname alleffects alllen maxlen alleffects_new
local nall = `nlv'*(`nlv' - 1)
matrix `alleffects' = J(`nall', 3, .)
matrix `alllen' = J(`nall', 1, .)
local lvnames : colnames `B'
local i = 1
foreach var_f in `lvnames' {
local lvnames_rest : list lvnames - var_f
foreach var_t in `lvnames_rest' {
local allnames "`allnames' `var_f'->`var_t'"
local rowi : list posof "`var_f'" in lvnames
local coli : list posof "`var_t'" in lvnames
if (`B'[`rowi', `coli'] == 0) {
matrix `alleffects'[`i', 1] = .
}
else {
matrix `alleffects'[`i', 1] = `B'[`rowi', `coli']
}
if (`indirect'[`rowi', `coli'] == 0) {
matrix `alleffects'[`i', 2] = .
}
else {
matrix `alleffects'[`i', 2] = `indirect'[`rowi', `coli']
}
if (`total'[`rowi', `coli'] == 0) {
matrix `alleffects'[`i', 3] = .
}
else {
matrix `alleffects'[`i', 3] = `total'[`rowi', `coli']
}
local ++i
}
}
local firstnonmiss = 0
forvalues i = 1/`nall' {
if (!missing(`alleffects'[`i', 3])) {
if (`firstnonmiss' == 0) {
matrix `alleffects_new' = `alleffects'[`i', 1..3]
local ++firstnonmiss
}
else {
matrix `alleffects_new' = (`alleffects_new' \ `alleffects'[`i', 1..3])
}
local efftoleave : word `i' of `allnames'
local allnames_new "`allnames_new' `efftoleave'"
}
}
matrix rownames `alleffects_new' = `allnames_new'
matrix colnames `alleffects_new' = "Direct" "Indirect" "Total"
local nall = rowsof(`alleffects_new')
forvalues i = 1/`nall' {
local nm : word `i' of `allnames_new'
local nm_len : strlen local nm
matrix `alllen'[`i', 1] = `nm_len' + 2
local nm_new = subinstr("`nm'", "->", " -> ", 1)
local lblbar "`lblbar' `i' "`nm_new'""
}
mata: st_numscalar("`maxlen'", max(st_matrix("`alllen'")))
local `maxlen' = `maxlen' + 2
mktable, matrix(`alleffects_new') digits(`digits') firstcolname("Effect") ///
title("Direct, Indirect (overall) and Total Effects") ///
firstcolwidth(``maxlen'') colwidth(12) hlines(`nall') novlines total
if ("`plot'" != "") {
tempvar __id__ // __touse__
// quietly generate `__touse__' = e(sample)
quietly generate `__id__' = _n // if `__touse__'
quietly svmat `alleffects_new', names(col)
graph bar (asis) Direct Indirect, over(`__id__', relabel(`lblbar') ///
label(angle(90) labsize(small))) stack nofill legend(position(12) ///
region(style(none))) ylabel(, nogrid) yline(0, lwidth(thin)) ///
title("Direct and Indirect (overall) Effects") scheme(sj)
quietly drop Direct Indirect Total
}
/* Return values */
return matrix total `alleffects_new'
end
program plssem_vif_c, rclass
version 15.1
syntax [ , DIGits(integer 3) ]
/* Options:
--------
digits(integer 3) --> number of digits to display (default 3)
*/
if (`digits' < 0) {
display as error "number of digits must be a nonnegative integer"
exit
}
local struct "structural"
local boot "bootstrap"
local props = e(properties)
local isstruct : list struct in props
local isboot : list boot in props
if (!`isstruct') {
display as error "the fitted plssem model includes only the measurement part"
exit
}
else if (`isboot') {
display as error "estat vif is not allowed after bootstrap"
exit
}
else {
tempname strvif
matrix `strvif' = e(struct_vif)
local hline_path = rowsof(`strvif')
mktable, matrix(`strvif') digits(`digits') firstcolname("Variable") ///
title("Structural model - Multicollinearity check (VIFs)") ///
firstcolwidth(14) colwidth(12) hlines(`hline_path') novlines
}
/* Return values */
return matrix strvif `strvif'
end
program mediate_c, rclass
version 15.1
syntax , INDep(string) MED(string) DEP(string) ///
[ BReps(numlist integer >0 min=1 max=1) Seed(numlist max=1) ZLC RIT RID ///
BCa Level(real 0.95) DIGits(integer 3) ]
/* Options:
--------
indep(string) --> independent variable
med(string) --> mediator variable
dep(string) --> dependent variable
breps(numlist integer >0 min=1 max=1)
--> number of bootstrap replications (default
is 50)
seed(numlist max=1) --> bootstrap seed number
zlc --> mediation procedures described by Zhao
et al. (2010)
rit --> ratio of the indirect effect to the total
effect
rid --> ratio of the indirect effect to the direct
effect
bca --> returns bias-corrected accelerated bootstrap
confidence intervals instead of percentile
confidence intervals, which is the default
level(real 0.95) --> confidence level (default 0.95)
digits(integer 3) --> number of digits to display (default 3)
*/
/* Description:
------------
This postestimation command provides the details for a single indirect
effect mediated by only one LV.
*/
tempname coef coef_el
matrix `coef' = e(struct_b)
local exo_nm : rownames `coef'
local endo_nm : colnames `coef'
if (!`: list dep in endo_nm') {
display as error "`dep' is not an endogenous variable"
exit
}
if (!`: list med in endo_nm') {
display as error "`med' is not an endogenous variable"
exit
}
if (!`: list indep in exo_nm') {
display as error "`indep' is not an exogenous variable"
exit
}
if (`digits' < 0) {
display as error "number of digits must be a nonnegative integer"
exit
}
local coef_el = `coef'[rownumb(`coef', "`indep'"), colnumb(`coef', "`dep'")]
if (missing(`coef_el')) {
display as error "the `indep' variable has no direct effect on `dep'"
exit
}
local coef_el = `coef'[rownumb(`coef', "`med'"), colnumb(`coef', "`dep'")]
if (missing(`coef_el')) {
display as error "the `med' variable has no direct effect on `dep'"
exit
}
local coef_el = `coef'[rownumb(`coef', "`indep'"), colnumb(`coef', "`med'")]
if (missing(`coef_el')) {
display as error "the `indep' variable has no direct effect on `med'"
exit
}
local struct "structural"
local props = e(properties)
local isstruct : list struct in props
if (!`isstruct') {
display as error "the fitted plssem model includes only the measurement part"
exit
}
tempvar __touse__
quietly generate `__touse__' = e(sample)
local reg3eqs = e(struct_eqs)
if (`level' <= 0 | `level' >= 1) {
display as error "confidence level must be in the range (0, 1)"
exit
}
if ("`breps'" == "") {
local breps = 50
}
tempname normperc alpha_cl
scalar `alpha_cl' = 1 - ((1 - `level')/2)
scalar `normperc' = invnormal(`alpha_cl')
tempname ehold
_estimates hold `ehold'
display as text "Bootstrapping mediation effect..."
capture {
if ("`dep'" != "" & "`med'" != "" & "`indep'" != "") {
local doi `dep':`indep'
local moi `med':`indep'
local dom `dep':`med'
tempname reg3coef_b reg3var_b reg3ciP reg3ciBC reg3coef reg3var
if ("`bca'" == "") {
quietly bootstrap indeff=(_b[`dom']*_b[`moi']), ties reps(`breps') ///
seed(`seed'): reg3 `reg3eqs' if `__touse__', mvreg corr(independent)
}
else {
quietly bootstrap indeff=(_b[`dom']*_b[`moi']), bca ties reps(`breps') ///
seed(`seed'): reg3 `reg3eqs' if `__touse__', mvreg corr(independent)
}
quietly estat bootstrap, all
matrix `reg3coef_b' = e(b)
matrix `reg3var_b' = e(V)
matrix `reg3ciP' = e(ci_percentile)
if ("`bca'" == "") {
matrix `reg3ciBC' = e(ci_bc)
}
else {
matrix `reg3ciBC' = e(ci_bca)
}
tempname perc_lci perc_uci bc_lci bc_uci
scalar `perc_lci' = `reg3ciP'[1, 1]
scalar `perc_uci' = `reg3ciP'[2, 1]
scalar `bc_lci' = `reg3ciBC'[1, 1]
scalar `bc_uci' = `reg3ciBC'[2, 1]
quietly reg3 `reg3eqs' if `__touse__', mvreg corr(independent)
matrix `reg3coef' = e(b)
matrix `reg3var' = e(V)
tempname moimat moicoef moimat2 moivar dommat domcoef dommat2 domvar
matrix `moimat' = `reg3coef'[1, "`moi'"]
scalar `moicoef' = `moimat'[1, 1]
matrix `moimat2' = `reg3var'["`moi'", "`moi'"]
scalar `moivar' = `moimat2'[1, 1]
matrix `dommat' = `reg3coef'[1, "`dom'"]
scalar `domcoef' = `dommat'[1, 1]
matrix `dommat2' = `reg3var'["`dom'", "`dom'"]
scalar `domvar' = `dommat2'[1, 1]
}
else {
display as error "provide a single dependent, independent and mediator variable for each indirect effect"
exit
}
} // end of -capture-
local rc = _rc
error `rc'
/* Description of mediating effect */
tempname indmeas mat_bk mat_zlc mat_rit mat_rid
matrix `indmeas' = J(6, 3, .)
matrix `mat_bk' = J(7, 1, .)
matrix `mat_zlc' = J(4, 1, .)
matrix `mat_rit' = J(3, 1, .)
matrix `mat_rid' = J(3, 1, .)
tempname coef_moi se_moi var_moi coef_dom se_dom var_dom prodterm
scalar `coef_moi' = `moicoef'
scalar `var_moi' = `moivar'
scalar `se_moi' = sqrt(`var_moi')
scalar `coef_dom' = `domcoef'
scalar `var_dom' = `domvar'
scalar `se_dom' = sqrt(`var_dom')
scalar `prodterm' = `coef_moi'*`coef_dom'
// Sobel's method
tempname sobel_se sobel_z sobel_pv sobel_lci sobel_uci
scalar `sobel_se' = sqrt(((`coef_dom')^2)*`var_moi' + ((`coef_moi')^2)*`var_dom')
scalar `sobel_z' = `prodterm'/`sobel_se'
scalar `sobel_pv' = 2*(1 - normal(abs(`sobel_z')))
scalar `sobel_lci' = `prodterm' - `normperc'*`sobel_se'
scalar `sobel_uci' = `prodterm' + `normperc'*`sobel_se'
matrix `indmeas'[1, 1] = `prodterm'
matrix `indmeas'[2, 1] = `sobel_se'
matrix `indmeas'[3, 1] = `sobel_z'
matrix `indmeas'[4, 1] = `sobel_pv'
matrix `indmeas'[5, 1] = `sobel_lci'
matrix `indmeas'[6, 1] = `sobel_uci'
// Delta method
tempname v delta_var delta_se delta_z delta_pv delta_lci delta_uci
quietly nlcom _b[`moi']*_b[`dom']
matrix `v' = r(V)
scalar `delta_var' = `v'[1, 1]
scalar `delta_se' = sqrt(`delta_var')
scalar `delta_z' = `prodterm'/`delta_se'
scalar `delta_pv' = 2*(1 - normal(abs(`delta_z')))
scalar `delta_lci' = `prodterm' - `normperc'*`delta_se'
scalar `delta_uci' = `prodterm' + `normperc'*`delta_se'
matrix `indmeas'[1, 2] = `prodterm'
matrix `indmeas'[2, 2] = `delta_se'
matrix `indmeas'[3, 2] = `delta_z'
matrix `indmeas'[4, 2] = `delta_pv'
matrix `indmeas'[5, 2] = `delta_lci'
matrix `indmeas'[6, 2] = `delta_uci'
// Bootstrap method
tempname boot_prod boot_se boot_z boot_pv
scalar `boot_prod' = `reg3coef_b'[1, 1]
scalar `boot_se' = sqrt(`reg3var_b'[1, 1])
scalar `boot_z' = `boot_prod'/`boot_se'
scalar `boot_pv' = 2*(1 - normal(abs(`boot_z'))) // CHECK THIS!!!
matrix `indmeas'[1, 3] = `boot_prod'
matrix `indmeas'[2, 3] = `boot_se'
matrix `indmeas'[3, 3] = `boot_z'
matrix `indmeas'[4, 3] = `boot_pv'
if ("`bca'" == "") {
matrix `indmeas'[5, 3] = `perc_lci'
matrix `indmeas'[6, 3] = `perc_uci'
}
else {
matrix `indmeas'[5, 3] = `bc_lci'
matrix `indmeas'[6, 3] = `bc_uci'
}
matrix rownames `indmeas' = "Indirect effect" "Standard error" ///
"Z statistic" "P-value" "Lower CI" "Upper CI"
matrix colnames `indmeas' = "Sobel" "Delta" "Bootstrap"
/* Clean up */
_estimates unhold `ehold'
/* Baron-Kenny mediation testing adjusted by Iacobucci et al. */
tempname coef st_tab pval stderr
matrix `coef' = e(struct_b)
local struct_b_rn : rownames `coef'
local struct_b_cn : colnames `coef'
matrix `st_tab' = e(struct_table)
local st_nrow : rowsof(`st_tab')
local st_nrow = (`st_nrow' - 1)/2
local st_ncol : colsof(`st_tab')
matrix `pval' = J(`st_nrow', `st_ncol', .)
forvalues i = 1/`st_nrow' {
matrix `pval'[`i', 1] = `st_tab'[`i'*2, 1...]
}
matrix rownames `pval' = `struct_b_rn'
matrix colnames `pval' = `struct_b_cn'
mat `stderr' = e(struct_se)
// X -> M
tempname coef_moi_m coef_moi moi_pval_m moi_pval
matrix `coef_moi_m' = `coef'["`indep'", "`med'"]
scalar `coef_moi' = `coef_moi_m'[1, 1]
matrix `moi_pval_m' = `pval'["`indep'", "`med'"]
scalar `moi_pval' = `moi_pval_m'[1, 1]
// M -> Y
tempname coef_dom_m coef_dom dom_pval_m dom_pval
matrix `coef_dom_m' = `coef'["`med'", "`dep'"]
scalar `coef_dom' = `coef_dom_m'[1, 1]
matrix `dom_pval_m' = `pval'["`med'", "`dep'"]
scalar `dom_pval' = `dom_pval_m'[1, 1]
// X -> Y
tempname coef_doi_m coef_doi doi_pval_m doi_pval
matrix `coef_doi_m' = `coef'["`indep'", "`dep'"]
scalar `coef_doi' = `coef_doi_m'[1, 1]
matrix `doi_pval_m' = `pval'["`indep'", "`dep'"]
scalar `doi_pval' = `doi_pval_m'[1, 1]
matrix `mat_bk'[1, 1] = `coef_doi'
matrix `mat_bk'[2, 1] = `coef_moi'
matrix `mat_bk'[3, 1] = `coef_dom'
matrix `mat_bk'[4, 1] = `doi_pval'
matrix `mat_bk'[5, 1] = `moi_pval'
matrix `mat_bk'[6, 1] = `dom_pval'
matrix `mat_bk'[7, 1] = `sobel_pv'
/* Zhao et al. mediation testing */
if ("`zlc'" != "") {
tempname axbxc
scalar `axbxc' = `coef_moi'*`coef_dom'*`coef_doi'
matrix `mat_zlc'[1, 1] = `boot_pv'
matrix `mat_zlc'[2, 1] = `doi_pval'
matrix `mat_zlc'[3, 1] = `coef_doi'
matrix `mat_zlc'[4, 1] = `axbxc'
}
if ("`rit'" != "") {
tempname prod toteff
scalar `prod' = abs(`prodterm')
scalar `toteff' = abs(`prodterm' + `coef_doi')
matrix `mat_rit'[1, 1] = `prod'
matrix `mat_rit'[2, 1] = `toteff'
matrix `mat_rit'[3, 1] = `prod'/`toteff'
}
if ("`rid'" != "") {
tempname prod absprod
scalar `prod' = abs(`prodterm')
scalar `absprod' = abs(`coef_doi')
matrix `mat_rid'[1, 1] = `prod'
matrix `mat_rid'[2, 1] = `absprod'
matrix `mat_rid'[3, 1] = `prod'/`absprod'
}
/* Display results */
local med_line = 5
mktable_mediate, matrix(`indmeas') matrix_bk(`mat_bk') ///
matrix_zlc(`mat_zlc') matrix_rit(`mat_rit') matrix_rid(`mat_rid') ///
depv("`dep'") medv("`med'") indepv("`indep'") ///
title("Significance testing of (standardized) indirect effect") ///
firstcolname("Statistics") firstcolwidth(24) colwidth(20) ///
hlines(`med_line') reps(`breps') digits(`digits') level(`level') novlines
/* Return values */
return matrix mediate `indmeas'
end
program htmt_c, rclass
version 15.1
syntax , [ All CUToff(real 0) DIGits(integer 3) ]
/* Options:
--------
all --> include all latent variables
cutoff(real 0) --> do not show correlation smaller than cutoff
digits(integer 3) --> number of digits to display (default 3)
*/
/* Description:
------------
This postestimation command provides the heterotrait-monotrait ratio of
correlations for assessing discriminant validity.
*/
if (`digits' < 0) {
display as error "number of digits must be a nonnegative integer"
exit
}
local is_all = 0
if ("`all'" != "") {
local is_all = 1
}
/* Set temporary variables */
tempvar __touse__
quietly generate `__touse__' = e(sample)
local allindicators = e(mvs)
local alllatents = e(lvs)
foreach var in `allindicators' {
local allstdindicators "`allstdindicators' std`var'"
}
local allstdindicators : list clean allstdindicators
local tempnamelist
/* Compute the heterotrait-monotrait ratios (HTMT) */
tempname res_htmt mata_htmt res_htmt2 mata_htmt2
local tempnamelist "`tempnamelist' `mata_htmt' `mata_htmt2'"
capture noisily {
mata: `mata_htmt' = ///
plssem_htmt( ///
st_matrix("e(ind_vcv)"), ///
st_matrix("e(adj_meas)"), ///
"`allindicators'", ///
"`allstdindicators'", ///
"`alllatents'", ///
"`e(binarylvs)'", ///
"`e(reflective)'", ///
`is_all')
mata: `mata_htmt2' = ///
plssem_htmt2( ///
st_matrix("e(ind_vcv)"), ///
st_matrix("e(adj_meas)"), ///
"`allindicators'", ///
"`allstdindicators'", ///
"`alllatents'", ///
"`e(binarylvs)'", ///
"`e(reflective)'", ///
`is_all')
}
/* Display results */
mata: st_matrix("`res_htmt'", `mata_htmt')
if (`is_all') {
matrix rownames `res_htmt' = `alllatents'
matrix colnames `res_htmt' = `alllatents'
}
else {
matrix rownames `res_htmt' = `e(reflective)'
matrix colnames `res_htmt' = `e(reflective)'
}
mata: st_matrix("`res_htmt2'", `mata_htmt2')
if (`is_all') {
matrix rownames `res_htmt2' = `alllatents'
matrix colnames `res_htmt2' = `alllatents'
}
else {
matrix rownames `res_htmt2' = `e(reflective)'
matrix colnames `res_htmt2' = `e(reflective)'
}
tempname res_htmt2_null
mata: st_numscalar("`res_htmt2_null'", allof(`mata_htmt2', .))
mktable_corr, matrix(`res_htmt') ///
title("Discriminant validity - Heterotrait-monotrait ratio of correlations (HTMT)") ///
cutoff(`cutoff') digits(`digits')
if (!`res_htmt2_null') {
mktable_corr, matrix(`res_htmt2') ///
title("Discriminant validity - Advanced heterotrait-monotrait ratio of correlations (HTMT2)") ///
cutoff(`cutoff') digits(`digits')
}
else {
display as text ""
display as text "Note: advanced heterotrait-monotrait ratios (HTMT2) are not available"
}
/* Return values */
return matrix htmt `res_htmt'
return matrix htmt2 `res_htmt2'
/* Clean up */
capture mata: cleanup(st_local("tempnamelist"))
end
program ci_c, rclass
version 15.1
syntax , Type(string) [ Level(real 0.95) DIGits(integer 3) ]
/* Options:
--------
type(string) --> type of confidence interval; one of
- "standard_z" or "standard_t" ==> standard CI with
bootstrap SE's or OLS SE's; critical quantiles can
be based on either the z or or the t distribution; the
latter case may perform better in small samples but
there is no general consensus on the degrees of freedom
to use
- "percentile" ==> the confidence interval's bounds are
estimated as the alpha/2 and 1-alpha/2 quantiles of the
distribution of the resample estimates; works only when
bootstrap has been used
- "bc" ==> bias corrected (Bc) confidence interval
level(real 0.95) --> confidence level (default 0.95)
digits(integer 3) --> number of digits to display (default 3)
*/
/* Description:
------------
This postestimation command provides the confidence intervals for all
model parameters.
*/
/* Set temporary variables */
tempvar __touse__
quietly generate `__touse__' = e(sample)
local tempnamelist
local isboot = (e(vce) == "bootstrap")
local N = e(N)
if (`level' <= 0 | `level' >= 1) {
display as error "confidence level must be in the range (0, 1)"
exit
}
if (`digits' < 0) {
display as error "number of digits must be a nonnegative integer"
exit
}
/* End of setting temporary variables */
/* Computing confidence intervals */
tempname alpha_cl mata_b mata_se perc mata_load mata_path ///
mata_load_se mata_path_se mata_load_lower mata_load_upper ///
ci_load mata_path_lower mata_path_upper ci_path
local tempnamelist "`alpha_cl' `tempnamelist' `mata_b' `mata_se'" ///
"`perc' `mata_load' `mata_path' `mata_load_se'" ///
"`mata_path_se' `mata_load_lower' `mata_load_upper' `ci_load'" ///
"`mata_path_lower' `mata_path_upper' `ci_path'"
mata: `alpha_cl' = 1 - ((1 - `level')/2)
mata: st_local("num_indep", strofreal(sum(st_matrix("e(adj_meas)"))))
mata: `mata_b' = st_matrix("e(b)")'
mata: `mata_se' = sqrt(diagonal(st_matrix("e(V)")))
mata: `mata_load' = `mata_b'[1::strtoreal(st_local("num_indep"))]
mata: `mata_load_se' = `mata_se'[1::strtoreal(st_local("num_indep"))]
mata: `mata_path' = `mata_b'[(strtoreal(st_local("num_indep")) + 1)::length(`mata_b')]
mata: `mata_path_se' = `mata_se'[(strtoreal(st_local("num_indep")) + 1)::length(`mata_b')]
local b_eqnm : coleq e(b)
local b_nm : colnames e(b)
mata: st_local("load_eqnm", ///
invtokens(tokens(st_local("b_eqnm"))[1::strtoreal(st_local("num_indep"))]))
mata: st_local("load_nm", ///
invtokens(tokens(st_local("b_nm"))[1::strtoreal(st_local("num_indep"))]))
mata: st_local("path_eqnm", ///
invtokens(tokens(st_local("b_eqnm"))[(strtoreal(st_local("num_indep")) + 1)::length(`mata_b')]))
mata: st_local("path_nm", ///
invtokens(tokens(st_local("b_nm"))[(strtoreal(st_local("num_indep")) + 1)::length(`mata_b')]))
local i = 1
local allreflective = e(reflective)
local allnames_load
foreach nm_eq in `load_eqnm' {
local nm_tmp : word `i' of `load_nm'
if (`: list nm_eq in allreflective') {
local allnames_load "`allnames_load' `nm_eq'->`nm_tmp'"
}
else {
local allnames_load "`allnames_load' `nm_eq'<-`nm_tmp'"
}
local ++i
}
local i = 1
local allnames_path
foreach nm_eq in `path_eqnm' {
local nm_tmp : word `i' of `path_nm'
local allnames_path "`allnames_path' `nm_tmp'->`nm_eq'"
local ++i
}
if (`isboot') {
if ("`type'" == "standard_z" | "`type'" == "standard_t") {
if ("`type'" == "standard_z") {
mata: `perc' = invnormal(`alpha_cl')
}
else if ("`type'" == "standard_t") {
mata: `perc' = invt(strtoreal(st_local("N")) - 1, `alpha_cl')
}
mata: `mata_load_lower' = `mata_load' - `perc':*`mata_load_se'
mata: `mata_load_upper' = `mata_load' + `perc':*`mata_load_se'
mata: st_matrix("`ci_load'", ///
(`mata_load', `mata_load_se', `mata_load_lower', `mata_load_upper'))
matrix rownames `ci_load' = `allnames_load'
matrix colnames `ci_load' = "Coefficient" "Std._err." "Lower_CI" "Upper_CI"
mata: `mata_path_lower' = `mata_path' - `perc':*`mata_path_se'
mata: `mata_path_upper' = `mata_path' + `perc':*`mata_path_se'
mata: st_matrix("`ci_path'", ///
(`mata_path', `mata_path_se', `mata_path_lower', `mata_path_upper'))
matrix rownames `ci_path' = `allnames_path'
matrix colnames `ci_path' = "Coefficient" "Std._err." "Lower_CI" "Upper_CI"
}
else if ("`type'" == "percentile") {
tempname load_boot path_boot load_q path_q
local tempnamelist "`tempnamelist' `load_boot' `path_boot'" ///
"`load_q' `path_q'"
mata: `load_boot' = st_matrix("e(loadings_breps)")
mata: `path_boot' = st_matrix("e(pathcoef_breps)")
mata: `path_boot' = `path_boot'[., selectindex(colnonmissing(`path_boot'))]
mata: `load_q' = plssem_quantile(`load_boot', (1 - `alpha_cl' \ `alpha_cl'), 1)
mata: `path_q' = plssem_quantile(`path_boot', (1 - `alpha_cl' \ `alpha_cl'), 1)
mata: `mata_load_lower' = `load_q'[1, .]'
mata: `mata_load_upper' = `load_q'[2, .]'
mata: st_matrix("`ci_load'", ///
(`mata_load', `mata_load_se', `mata_load_lower', `mata_load_upper'))
matrix rownames `ci_load' = `allnames_load'
matrix colnames `ci_load' = "Coefficient" "Std._err." "Lower_CI" "Upper_CI"
mata: `mata_path_lower' = `path_q'[1, .]'
mata: `mata_path_upper' = `path_q'[2, .]'
mata: st_matrix("`ci_path'", ///
(`mata_path', `mata_path_se', `mata_path_lower', `mata_path_upper'))
matrix rownames `ci_path' = `allnames_path'
matrix colnames `ci_path' = "Coefficient" "Std._err." "Lower_CI" "Upper_CI"
}
else if ("`type'" == "bc") {
tempname load_boot path_boot load_q path_q ci_bc
local tempnamelist "`tempnamelist' `load_boot' `path_boot' `ci_bc'"
mata: `load_boot' = st_matrix("e(loadings_breps)")
mata: `path_boot' = st_matrix("e(pathcoef_breps)")
mata: `path_boot' = `path_boot'[., selectindex(colnonmissing(`path_boot'))]
mata: `ci_bc' = plssem_bcCIresample(`load_boot', `mata_load', (1 - `alpha_cl' \ `alpha_cl'))
mata: `mata_load_lower' = `ci_bc'[1, .]'