-
Notifications
You must be signed in to change notification settings - Fork 7
/
plssemmat.ado
2357 lines (2191 loc) · 81.8 KB
/
plssemmat.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
*!plssemmat version 0.6.1
*!Written 29May2024
*!Written by Sergio Venturini and Mehmet Mehmetoglu
*!The following code is distributed under GNU General Public License version 3 (GPL-3)
program plssemmat, byable(onecall)
version 15.1
syntax [anything] [if] [in] [, * ]
if replay() {
if ("`e(cmd)'" != "plssem") {
error 301
}
if (_by()) {
error 190
}
// Extract all the options from 'cmdline'
local cmdline = `"`e(cmdline)'"'
local cmdlen = strlen(`"`cmdline'"')
local cmdcomma = strpos(`"`cmdline'"', ",")
local old_options = substr(`"`cmdline'"', `cmdcomma' + 1, `cmdlen' - `cmdcomma')
local old_options : list clean old_options
local hasstruct = strpos(`"`old_options'"', "str")
if (!`hasstruct') {
local nostructural "nostructural"
}
if (`"`options'"' != "") {
Display_mat, `options' `nostructural'
}
else {
Display_mat, `old_options' `nostructural'
}
exit
}
if _by() {
local BY `"by `_byvars' `_byrc0':"'
}
if (_caller() < 8) {
local version : display "version " string(_caller()) ", missing :"
}
else {
local version : display "version " string(_caller()) " :"
}
local isgroup = strpos(`"`options'"', "gr")
capture quietly mi query
if (`isgroup') {
if _by() {
display as error "the 'group()' option is not allowed with by"
exit
}
else if (r(M) > 0) {
display as error "the 'group()' option is not allowed with multiple imputation"
exit
}
else {
`version' Compare_mat `0' // version is not necessary
}
}
else {
`version' `BY' Estimate_mat `0' // version is not necessary
}
end
program Estimate_mat, eclass byable(recall) properties(mi)
version 15.1
syntax namelist(id="Measurement model" name=adj_meas min=1 max=1) [if] [in], ///
[ STRuctural(name) Wscheme(string) BINary(namelist min=1) ///
Boot(numlist integer >0 min=1 max=1) Seed(numlist max=1) Tol(real 1e-7) ///
MAXiter(integer 100) MISsing(string) k(numlist integer >0 max=1) ///
INIT(string) DIGits(integer 3) noHEADer noMEAStable noDISCRIMtable ///
noSTRUCTtable LOADPval STATs GRoup(string) CORRelate(string) RAWsum ///
noSCale CONVcrit(string) ORDinal(varlist) ROBust(string) noCLeanup ]
/* Options:
--------
structural(name) --> structural model specification
wscheme(string) --> weighting scheme ("centroid", "factorial"
or "path")
binary(namelist min=1) --> latent variables to fit using logit
boot(numlist integer >0
min=1 max=1) --> bootstrap estimation (# of replications)
seed(numlist max=1) --> bootstrap seed number
tol(real 1e-7) --> tolerance (default 1e-7)
maxiter(integer 100) --> maximum number of iterations (default
100)
missing(string) --> imputation method ("mean" or "knn")
k(numlist integer >0 max=1) --> neighbor size; to use with
missing("knn") (default 5)
init(string) --> initialization method ("eigen" or "indsum")
digits(integer 3) --> number of digits to display (default 3)
noheader --> do not show the header
nomeastable --> do not show the measurement table
nodiscrimtable --> do not show the discriminant validity
table
nostructtable --> do not show the structural table
loadpval --> show the loadings' p-values
stats --> print a table of summary statistics for
the indicators
group(string) --> perform multigroup analysis; accepts
the suboptions reps(#), method(), plot,
alpha, unequal and groupseed(numlist max=1);
method() accepts either 'permutation',
'bootstrap' or 'normal'; groupseed(numlist
max=1) accepts an optional seed for
multigroup analysis
correlate(string) --> report the correlation among the
indicators, the LVs as well as the cross
loadings; accepts any combination of 'mv',
'lv' and 'cross' (i.e. cross-loadings);
suboption 'cutoff()' can be used to avoid
showing correlations smaller than the
specified value
rawsum --> estimate the latent scores as the raw
sum of the indicators
noscale --> manifest variables are not standardized
convcrit --> convergence criterion (either 'relative'
or 'square')
ordinal(varlist) --> list of variables to be considered as ordinal
robust(string) --> robustness correlation method (either 'spearman'
or 'mcd')
nocleanup --> Mata temporary objects are not removed
(undocumented)
*/
/* Macros:
* LV`i' - latent variable name
* i`i' - indicators for latent, by latent index
* istd`i' - indicators for latent, by latent index (standardized)
* i`LV`i'' - indicators for latent, by latent name
* istd`LV`i'' - indicators for latent, by latent name (standardized)
* allindicators - all indicators
* allstdindicators - all standardized indicators
* alllatents - all latents
*/
local cmdline : list clean 0
local tempnamelist
local adj_meas : list clean adj_meas
local structural : list clean structural
/* Some checks on the adjacency matrices */
if ("`structural'" != "") {
if (`: colsof `adj_meas'' != `: rowsof `structural'') {
display as error "the sizes of the adjacency matrices do not match"
exit
}
if (`: colsof `structural'' != `: rowsof `structural'') {
display as error "the sizes of the adjacency matrices do not match"
exit
}
if ("`: colnames `adj_meas''" != "`: colnames `structural''") {
display as error "the adjacency matrices column names do not match"
exit
}
if ("`: rownames `structural''" != "`: colnames `structural''") {
display as error "the adjacency matrices column names do not match"
exit
}
}
tempname zeroone
mata: `zeroone' = uniqrows(vec(st_matrix("`adj_meas'")))
mata: st_local("isadjmeas", strofreal(`zeroone' != e(2, 2)'))
if (`isadjmeas') {
display as error "the measurement adjacency matrix is not correct"
display as error "at least one element different from 0 or 1"
exit
}
if ("`structural'" != "") {
mata: `zeroone' = uniqrows(vec(st_matrix("`structural'")))
mata: st_local("isadjstruc", strofreal(`zeroone' != e(2, 2)'))
if (`isadjstruc') {
display as error "the structural adjacency matrix is not correct"
display as error "at least one element different from 0 or 1"
exit
}
}
mata: st_local("isadjmeas", ///
strofreal(any(colsum(st_matrix("`adj_meas'")) :== 0)))
if (`isadjmeas') {
display as error "the measurement adjacency matrix is not correct"
display as error "at least one column filled with zeros"
exit
}
mata: st_local("isadjmeas", ///
strofreal(any(rowsum(st_matrix("`adj_meas'")) :== 0)))
if (`isadjmeas') {
display as error "the measurement adjacency matrix is not correct"
display as error "at least one row filled with zeros"
exit
}
mata: st_local("isadjmeas", ///
strofreal(any(rowsum(st_matrix("`adj_meas'")) :!= 1)))
if (`isadjmeas') {
display as error "the measurement adjacency matrix is not correct"
display as error "more than one column filled for at least one row"
exit
}
mata: st_local("isadjmeas", ///
strofreal(any(rowsum(st_matrix("`adj_meas'")) :!= 1)))
if (`isadjmeas') {
display as error "the measurement adjacency matrix is not correct"
display as error "more than one column filled for at least one row"
exit
}
/* End of checking the adjacency matrices */
/* Parse the adjacency matrices */
local allindicators : rownames `adj_meas'
local alllatents : colnames `adj_meas'
local allmodes : coleq `adj_meas'
local j = 1
foreach var in `alllatents' {
local iscoleq : word `j' of `allmodes'
if ("`iscoleq'" != "Reflective" & "`iscoleq'" != "Formative") {
display as error "the measurement adjacency matrix is not correct"
display as error "the equation names of the columns must be either " _continue
display as error "'Reflective' or 'Formative'"
exit
}
local LV`j' `var'
if ("`: word `j' of `allmodes''" == "Reflective") {
local modeA "`modeA' `LV`j''"
}
else {
local modeB "`modeB' `LV`j''"
}
mata: st_local("i`j'", ///
invtokens(tokens( ///
st_local("allindicators"))[ ///
selectindex(st_matrix("`adj_meas'")[., `j'])]))
local i`LV`j'' `i`j''
local ++j
}
/* End parsing the adjacency matrices */
/* Parse the binary() option */
if ("`binary'" != "") {
noisily {
display
display as error "warning: the use of binary latent variables " _continue
display as error "goes beyond the original scopes of PLS-SEM. "
display as error " They are provided here only for exploratory " _continue
display as error "purposes."
}
local binary : list clean binary
foreach var in `binary' {
local nind_bin : word count `i`var''
if (`nind_bin' > 1) {
display as error "latent variables in 'binary()' option must be defined through a single binary indicator"
exit
}
}
}
/* End of parsing the binary() option */
/* Set obs to use (1/2) */
marksample touse
/* End of setting observations to use (1/2) */
/* Save original data set */
if ("`missing'" != "") {
tempname original_data
local tempnamelist "`tempnamelist' `original_data'"
mata: `original_data' = st_data(., "`: list uniq allindicators'", "`touse'")
}
/* End saving original data set */
/* Imputation of missing values */
if ("`missing'" != "") {
local missing : list clean missing
foreach var in `binary' {
local indbin "`indbin' `i`var''"
}
local indbin : list clean indbin
if ("`missing'" == "mean") {
mata: meanimp( ///
st_data(., "`allindicators'", "`touse'"), ///
"`allindicators'", ///
"`indbin'", ///
"`touse'")
}
else if ("`missing'" == "knn") {
tempname ind_mata allmiss
local tempnamelist "`tempnamelist' `ind_mata'"
mata: `ind_mata' = st_data(., "`allindicators'", "`touse'")
mata: st_numscalar("`allmiss'", anyof(rowmissing(`ind_mata'), ///
cols(`ind_mata')))
if (`allmiss') {
display as error "some observations have all indicators missing; " _continue
display as error "remove them manually before proceeding"
exit
}
if ("`k'" == "") {
local k = 5
}
mata: knnimp( ///
st_data(., "`allindicators'", "`touse'"), ///
"`allindicators'", ///
strtoreal("`k'"), ///
"`indbin'", ///
"`touse'", ///
1)
}
else {
display as error "missing can be either 'mean' or 'knn'"
exit
}
}
/* End of imputing the missing values */
/* Set obs to use (2/2) */
tempvar touse_nomiss
quietly generate `touse_nomiss' = `touse'
markout `touse' `allindicators'
quietly count if `touse'
if (r(N) == 0) {
error 2000
}
else if (r(N) < 10) {
display as error "at least 10 complete observations required"
error 2001
}
quietly misstable patterns `allindicators' if `touse'
local nobs = r(N_complete)
/* End of setting observations to use (2/2) */
/* Check that there are no "zero-variance" indicators */
tempname zerovar
mata: st_numscalar("`zerovar'", any(selectindex( ///
sd(st_data(., "`allindicators'", "`touse'")) :== 0)))
if (`zerovar') {
display as error "at least one indicator has zero variance in one of the groups"
exit
}
/* End of checking that there are no "zero-variance" indicators */
/* Parse the inner weight scheme */
if ("`wscheme'" == "") {
local wscheme "path"
}
if !("`wscheme'" == "centroid" | "`wscheme'" == "factorial" | "`wscheme'" == "path") {
display as error "scheme can be either 'centroid', 'factorial', or 'path'"
exit
}
/* End of parsing the inner weight scheme */
/* Check the robust option */
if ("`robust'" == "") {
local robust "none"
}
else if !("`robust'" == "none" | "`robust'" == "spearman" | "`robust'" == "mcd") {
display as error "robust option can be either 'none', 'spearman' or 'mcd'"
exit
}
/* End of checking the robust option */
/* Check the ordinal option */
if ("`ordinal'" != "") {
local ordinal : list clean ordinal
local ordinal : list uniq ordinal
foreach var of local ordinal {
if (!`: list var in allindicators') {
display as error "one of the variables included in the 'ordinal' option is not an indicator"
exit
}
}
}
else {
local ordinal ""
}
/* End of checking the ordinal option */
/* Check convergence parameters */
if (`maxiter' <= 0) {
display as error "maximum number of iterations must be a positive integer"
exit
}
if (`tol' <= 0) {
display as error "tolerance must be a strictly positive number"
exit
}
if ("`convcrit'" == "") {
local convcrit "relative"
}
if !("`convcrit'" == "relative" | "`convcrit'" == "square") {
display as error "convergence criterion can be either 'relative' or 'square'"
exit
}
/* End of checking convergence parameters */
/* Check that digits is nonnegative */
if (`digits' < 0) {
display as error "number of digits to display must be a nonnegative integer"
exit
}
/* End of checking that digits is nonnegative */
/* Check the initialization method */
if ("`init'" == "") {
local init "indsum"
}
if !("`init'" == "eigen" | "`init'" == "indsum") {
display as error "initialization method can be either 'eigen' or 'indsum'"
error 198
}
if (("`structural'" == "") & ("`init'" != "eigen")) {
display as error "'init()' option must be set to 'eigen' when no structural model is provided"
error 198
}
/* End of checking the initialization method */
/* Parse the correlate() option */
if ("`correlate'" != "") {
local tok_i = 1
tokenize "`correlate'", parse(",() ")
while ("``tok_i''" != "") {
if ("``tok_i''" == "mv") {
local corrind = "corrind"
}
if ("``tok_i''" == "lv") {
local corrlv = "corrlv"
}
if ("``tok_i''" == "cross") {
local crossload = "crossload"
}
if ("``tok_i''" == "cutoff") {
local tok_tmp = `tok_i' + 2
local cutoff = "``tok_tmp''"
if (`cutoff' < 0 | `cutoff' > 1) {
display as error "cutoff must be in between 0 and 1"
exit
}
}
local ++tok_i
}
if ("`corrind'" == "" & "`corrlv'" == "" & "`crossload'" == "") {
display as error "'correlate()' option requires at least one of 'mv', 'lv' or 'cross'"
exit
}
}
/* End of parsing the correlate() option */
/* Parse inner relationships */
local num_lv: word count `alllatents'
tempname isendo
if ("`structural'" != "") {
local reg3eqs = "("
local j = 1
foreach var in `alllatents' {
mata: st_numscalar("`isendo'", sum(st_matrix("`structural'")[., `j']) > 0)
if (`isendo') {
mata: st_local("allindep", invtokens(tokens( ///
st_local("alllatents"))[ ///
selectindex(st_matrix("`structural'")[., `j'])]))
local reg3eqs "`reg3eqs'`var'"
local reg3eqs "`reg3eqs' `allindep'"
if (`j' == `num_lv') {
local reg3eqs "`reg3eqs')"
}
else {
local reg3eqs "`reg3eqs') ("
}
local str_eqn "`var' `allindep'"
// Use macros r`var' and c`latentname' to store whether the adjacency is
// treated as directional in path weighting scheme.
// The regest`var' macro is needed to store the path coefficients.
local dvar : word 1 of `str_eqn'
local ivars : list str_eqn - dvar
local r`dvar' `r`dvar'' `ivars' // r`dvar' contains the predecessors of each LV
local regest`dvar' `regest`dvar'' `ivars'
foreach iv in `ivars' {
local c`iv' `c`iv'' `dvar' // c`iv' contains the successors of each LV
}
}
local ++j
}
foreach var in `alllatents' {
if ("`r`var''`c`var''" == "") {
display as error "latent `var' is not adjacent to any other latent"
exit
}
local regest`var' : list uniq regest`var'
if ("`wscheme'" == "path") {
local c`var' : list uniq c`var'
local r`var' : list uniq r`var'
local c`var' : list c`var' - r`var'
}
else {
local c`var' `c`var'' `r`var''
local c`var' : list uniq c`var'
local r`var' ""
}
}
}
/* End of parsing the inner relationships */
/* Create other macros and scalars */
foreach var in `allindicators' {
local allstdindicators "`allstdindicators' std`var'"
}
local allstdindicators : list clean allstdindicators
forvalues k = 1/`num_lv' {
foreach var in `i`k'' {
local istd`k' "`istd`k'' std`var'"
local istd`LV`k'' `istd`k''
}
}
tempname rawsum_sc struct_sc
if ("`rawsum'" == "") {
scalar `rawsum_sc' = 0
}
else {
scalar `rawsum_sc' = 1
}
if ("`structural'" == "") {
scalar `struct_sc' = 0
}
else {
scalar `struct_sc' = 1
}
/* End of creating other macros */
/* Create the adjacency matrices */
tempname modes adj_struct
local num_ind : word count `allindicators'
matrix `modes' = J(`num_lv', 1, 1)
local i = 1
foreach var in `alllatents' {
if (`: list var in modeA') {
matrix `modes'[`i', 1] = 0
}
local ++i
}
local loadrownames : rownames `adj_meas'
local loadcolnames : colfullnames `adj_meas'
matrix `adj_struct' = J(`num_lv', `num_lv', 0)
if ("`structural'" != "") {
matrix `adj_struct' = `structural'
}
/* End of creating the adjacency matrices */
capture noisily {
/* Standardize the MVs (if requested) */
mata: ///
plssem_scale( ///
st_data(., "`allindicators'", "`touse'"), ///
"`allstdindicators'", ///
"`touse'", ///
"`scale'")
/* End of standardizing the MVs */
/* Initialize LVs */
mata: ///
plssem_init( ///
st_data(., "`allstdindicators'", "`touse'"), ///
st_matrix("`adj_meas'"), ///
"`allindicators'", ///
"`allstdindicators'", ///
"`alllatents'", ///
"`touse'", ///
st_numscalar("`rawsum_sc'"), ///
"`init'")
/* End of initializing the LVs */
/* Run the PLS algorithm */
tempname res converged outerW iter matreldiff Whistory
local tempnamelist "`tempnamelist' `res'"
mata: `res' = ///
plssem_base( ///
st_data(., "`allstdindicators'", "`touse'"), ///
st_data(., "`alllatents'", "`touse'"), ///
st_matrix("`adj_meas'"), ///
st_matrix("`adj_struct'"), ///
st_matrix("`modes'"), ///
"`alllatents'", ///
"`binary'", ///
strtoreal("`tol'"), ///
strtoreal("`maxiter'"), ///
"`touse'", ///
"`wscheme'", ///
"`convcrit'", ///
st_numscalar("`struct_sc'"), ///
st_numscalar("`rawsum_sc'"), ///
0, ///
0, ///
0, ///
st_local("robust"), ///
st_local("allindicators"), ///
st_local("ordinal"))
mata: st_numscalar("`converged'", `res'.converged)
mata: st_numscalar("`iter'", `res'.niter)
if (("`structural'" != "") & ("`rawsum'" == "")) {
mata: st_matrix("`matreldiff'", `res'.diff)
mata: st_matrix("`Whistory'", `res'.evo)
foreach var in `alllatents' {
foreach var2 in `i`var'' {
local Whistcolnames `Whistcolnames' "`var2':`var'"
}
}
matrix colnames `Whistory' = `Whistcolnames'
mata: st_matrix("`outerW'", `res'.outer_weights)
matrix rownames `outerW' = `loadrownames'
matrix colnames `outerW' = `loadcolnames'
}
/* End of the PLS algorithm */
/* Label the LVs */
local now "`c(current_date)', `c(current_time)'"
local now : list clean now
foreach var of varlist `alllatents' {
label variable `var' "Scores of `var' latent variable [`now']"
}
/* End of labeling the LVs */
/* Compute the model parameter variances */
tempname xload_v path_v ow_v
local tempnamelist "`tempnamelist' `xload_v' `path_v' `ow_v'"
if ("`boot'" == "") {
mata: `xload_v' = ///
plssem_lv( ///
st_data(., "`allstdindicators'", "`touse'"), ///
st_data(., "`alllatents'", "`touse'"), ///
"`alllatents'", ///
"`binary'")
if ("`structural'" != "") {
mata: `path_v' = `res'.path_v
}
}
else {
tempname res_bs
local tempnamelist "`tempnamelist' `res_bs'"
mata: `res_bs' = ///
plssem_boot( ///
st_data(., "`allstdindicators'", "`touse'"), ///
st_data(., "`alllatents'", "`touse'"), ///
st_matrix("`adj_meas'"), ///
st_matrix("`adj_struct'"), ///
st_matrix("`modes'"), ///
"`alllatents'", ///
"`binary'", ///
strtoreal("`tol'"), ///
strtoreal("`maxiter'"), ///
"`touse'", ///
"`wscheme'", ///
"`convcrit'", ///
st_numscalar("`struct_sc'"), ///
st_numscalar("`rawsum_sc'"), ///
0, ///
strtoreal("`boot'"), ///
strtoreal("`seed'"), ///
1, ///
st_local("robust"), ///
st_local("allindicators"), ///
st_local("ordinal"))
mata: st_local("n_inadmissible", strofreal(`res_bs'.n_inadmiss))
mata: `xload_v' = `res_bs'.xloadings_v
if ("`structural'" != "") {
mata: `path_v' = `res_bs'.path_v
mata: `ow_v' = `res_bs'.ow_v
}
}
/* End of computing the model parameter variances */
} // end of -capture-
local rc = _rc
if (`rc' == 1) {
display
display as error "you pressed the Break key; " _continue
display as error "calculation interrupted"
}
if (`rc' >= 1) {
/* Clean up */
foreach var in `allstdindicators' {
capture quietly drop `var'
}
if ("`rawsum'" != "") {
foreach var in `alllatents' {
quietly replace `var' = rs_`var'
capture quietly drop rs_`var'
}
}
if ("`missing'" != "") {
tempvar __touse__
quietly generate `__touse__' = e(sample)
mata: st_store(., tokens("`: list uniq allindicators'"), "`__touse__'", ///
`original_data')
}
if ("`cleanup'" == "") {
capture mata: cleanup(st_local("tempnamelist"))
}
/* End of cleaning up */
error `rc'
}
/* Compute the table of measurement loadings */
tempname loadings xloadings annihilate_se loadings_se xloadings_se
local tempnamelist "`tempnamelist' `annihilate_se'"
mata: st_matrix("`loadings'", `res'.loadings)
matrix rownames `loadings' = `loadrownames'
matrix colnames `loadings' = `loadcolnames'
mata: st_matrix("`xloadings'", `res'.xloadings)
matrix rownames `xloadings' = `loadrownames'
matrix colnames `xloadings' = `loadcolnames'
if ("`boot'" != "") {
tempname loadings_bs xloadings_bs ow_bs
mata: st_matrix("`loadings_bs'", `res_bs'.loadings_bs)
matrix rownames `loadings_bs' = `loadrownames'
matrix colnames `loadings_bs' = `loadcolnames'
mata: st_matrix("`xloadings_bs'", `res_bs'.xloadings_bs)
matrix rownames `xloadings_bs' = `loadrownames'
matrix colnames `xloadings_bs' = `loadcolnames'
if ("`structural'" != "") {
mata: st_matrix("`ow_bs'", `res_bs'.ow_bs)
matrix rownames `ow_bs' = `loadrownames'
matrix colnames `ow_bs' = `loadcolnames'
}
}
mata: `annihilate_se' = editvalue(st_matrix("`adj_meas'"), 0, .)
mata: st_matrix("`loadings_se'", sqrt(`xload_v') :* `annihilate_se')
matrix rownames `loadings_se' = `loadrownames'
matrix colnames `loadings_se' = `loadcolnames'
mata: st_matrix("`xloadings_se'", sqrt(`xload_v'))
matrix rownames `xloadings_se' = `loadrownames'
matrix colnames `xloadings_se' = `loadcolnames'
if ("`boot'" != "" & "`structural'" != "") {
tempname ow_se
mata: st_matrix("`ow_se'", sqrt(`ow_v') :* `annihilate_se')
matrix rownames `ow_se' = `loadrownames'
matrix colnames `ow_se' = `loadcolnames'
}
/* End of computing the table of measurement loadings */
/* Compute the table of average variance extracted (AVE) */
local num_ind: word count `allindicators'
local num_lv_A: word count `modeA'
if (`num_lv_A' > 0) {
tempname sqcorr
/*
mata: st_matrix("`sqcorr'", ///
correlation(st_data(., "`modeA'", "`touse'")) :^ 2)
matrix rownames `sqcorr' = `modeA'
matrix colnames `sqcorr' = `modeA'
*/
// mata: st_matrix("`sqcorr'", ///
// correlation(st_data(., "`alllatents'", "`touse'")) :^ 2)
mata: st_matrix("`sqcorr'", `res'.construct_vcv :^ 2)
matrix rownames `sqcorr' = `alllatents'
matrix colnames `sqcorr' = `alllatents'
}
tempname ave_num ave_mata whichB ave
local tempnamelist "`tempnamelist' `ave_mata' `whichB'"
mata: st_matrix("`ave_num'", colsum(st_matrix("`adj_meas'")))
mata: `ave_mata' = ///
colsum(st_matrix("`loadings'") :^ 2) :/ st_matrix("`ave_num'")
if ("`modeB'" != "") {
mata: `whichB' = colsum(strmatch(tokens("`alllatents'"), tokens("`modeB'")'))
mata: `ave_mata'[., selectindex(`whichB')] = J(1, sum(`whichB'), .)
}
mata: st_matrix("`ave'", `ave_mata')
matrix colnames `ave_num' = `alllatents'
matrix rownames `ave' = "AVE"
matrix colnames `ave' = `alllatents'
/* End of computing the AVE table */
/* Compute the table of summary statistics for indicators */
// ("if `touse'" is not used here to correctly account for the missing values)
if ("`stats'" != "") {
tempname indstats
local kk = 1
matrix `indstats' = J(`num_ind', 7, .)
foreach ind in `allindicators' {
quietly summarize `ind' if `touse_nomiss', detail
matrix `indstats'[`kk', 1] = r(mean)
matrix `indstats'[`kk', 2] = r(sd)
matrix `indstats'[`kk', 3] = r(p50)
matrix `indstats'[`kk', 4] = r(min)
matrix `indstats'[`kk', 5] = r(max)
mata: st_local("indstats_mata", ///
strofreal(nonmissing(st_data(., "`ind'", "`touse_nomiss'"))))
matrix `indstats'[`kk', 6] = real("`indstats_mata'")
mata: st_local("indstats_mata", ///
strofreal(missing(st_data(., "`ind'", "`touse_nomiss'"))))
matrix `indstats'[`kk', 7] = real("`indstats_mata'")
local ++kk
}
matrix rownames `indstats' = `allindicators'
matrix colnames `indstats' = "mean" "sd" "median" "min" "max" "N" "missing"
}
/* End of computing the table of summary statistics for indicators */
/* Compute the table of structural path coefficients */
if ("`structural'" != "") {
tempname path path_se rsquared redundancy path_pval pathtab path_toteff
local tempnamelist "`tempnamelist' `path_pval'"
mata: st_matrix("`path'", `res'.path)
matrix rownames `path' = `alllatents'
matrix colnames `path' = `alllatents'
if ("`boot'" != "") {
tempname path_bs
mata: st_matrix("`path_bs'", `res_bs'.path_bs)
matrix rownames `path_bs' = `alllatents'
matrix colnames `path_bs' = `alllatents'
}
mata: st_matrix("`path_se'", sqrt(`path_v'))
matrix rownames `path_se' = `alllatents'
matrix colnames `path_se' = `alllatents'
mata: st_matrix("`rsquared'", `res'.r2)
matrix rownames `rsquared' = "r2"
matrix colnames `rsquared' = `alllatents'
mata: st_matrix("`redundancy'", `res'.r2 :* st_matrix("`ave'"))
matrix rownames `redundancy' = "redundancy"
matrix colnames `redundancy' = `alllatents'
mata: st_local("regest_all", ///
invtokens(tokens("`alllatents'")[selectindex(rowsum(`res'.S))]))
mata: st_local("lv_regest_all", ///
invtokens(tokens("`alllatents'")[selectindex(colsum(`res'.S))]))
local num_lv_path : word count `lv_regest_all'
mata: `path_pval' = ///
plssem_pval( ///
st_matrix("`path'"), ///
st_matrix("`path_se'"), ///
"`alllatents'", ///
"`binary'", ///
strtoreal("`nobs'"), ///
("`boot'" != ""))
mata: st_matrix("`pathtab'", ///
plssem_pathtab(st_matrix("`path'"), `path_pval', `res'.r2_a))
foreach var in `regest_all' {
local pathtab_rownames "`pathtab_rownames' `var' ."
}
matrix rownames `pathtab' = `pathtab_rownames' "r2_a"
matrix colnames `pathtab' = `lv_regest_all'
mata: st_matrix("`path_toteff'", `res'.total_effects)
matrix rownames `path_toteff' = `alllatents'
matrix colnames `path_toteff' = `alllatents'
}
/* End of computing the table of structural path coefficients */
/* Compute the reliability coefficients */
tempname relcoef
mata: st_matrix("`relcoef'", ///
plssem_reliability( ///
st_data(., "`allindicators'", "`touse'"), ///
st_matrix("`loadings'"), ///
st_matrix("`modes'"), ///
st_matrix("`outerW'"), ///
st_local("robust"), ///
st_local("allindicators"), ///
st_local("ordinal")))
matrix rownames `relcoef' = "Cronbach" "DG" "rho_A"
matrix colnames `relcoef' = `loadcolnames'
/* End of computing the reliability coefficients */
/* Setup the tables of structural coefficients */
if ("`structural'" != "") {
tempname path_mata strcoef path_se_mata strse path_vif_mata strvif ///
rowi_path coli_path R_Y
local tempnamelist "`tempnamelist' `path_mata' `path_se_mata' `path_vif_mata' `rowi_path' `coli_path'"
mata: `path_mata' = st_matrix("`path'")
mata: `rowi_path' = selectindex(rownonmissing(`path_mata'))
mata: `coli_path' = selectindex(colnonmissing(`path_mata'))
mata: st_matrix("`strcoef'", `path_mata'[`rowi_path', `coli_path'])
foreach var in `regest_all' {
local struct_rownames "`struct_rownames' `var'"
}
matrix rownames `strcoef' = `struct_rownames'
matrix colnames `strcoef' = `lv_regest_all'
mata: `path_se_mata' = st_matrix("`path_se'")
mata: st_matrix("`strse'", `path_se_mata'[`rowi_path', `coli_path'])
matrix rownames `strse' = `struct_rownames'
matrix colnames `strse' = `lv_regest_all'
// mata: st_matrix("`R_Y'", correlation(st_data(., "`alllatents'", "`touse'")))
mata: st_matrix("`R_Y'", `res'.construct_vcv)
matrix rownames `R_Y' = `alllatents'
matrix colnames `R_Y' = `alllatents'
// mata: `path_vif_mata' = ///
// plssem_vif(st_matrix("`R_Y'"), st_matrix("`adj_struct'"))
// mata: st_matrix("`strvif'", ///
// `path_vif_mata'[selectindex(rownonmissing(`path_vif_mata')), ///
// selectindex(colnonmissing(`path_vif_mata'))])
mata: st_matrix("`strvif'", `res'.vif)
// matrix rownames `strvif' = `struct_rownames'
matrix rownames `strvif' = `alllatents'
// matrix colnames `strvif' = `lv_regest_all'
matrix colnames `strvif' = `alllatents'
}
/* End of setting up the tables of structural coefficients */
/* Calculate the model assessment indexes (GOF, etc.) */
if (("`structural'" != "") & (`num_lv_A' > 0)) {
tempname gof_all gof gof_r avrsq avave avred mod_assessment tmp tmp_w
local tempnamelist "`tempnamelist' `gof_all'"
matrix `tmp' = (`rsquared')'
mata: st_numscalar("`avrsq'", mean(st_matrix("`tmp'")))
matrix `tmp' = (`ave')'
matrix `tmp_w' = (`ave_num')'
mata: st_numscalar("`avave'", mean(st_matrix("`tmp'"), ///
st_matrix("`tmp_w'")))
matrix `tmp' = (`redundancy')'
mata: st_numscalar("`avred'", mean(st_matrix("`tmp'")))
// scalar `gof_all' = sqrt(`avrsq' * `avave')
mata: `gof_all' = plssem_gof( ///
st_matrix("`rsquared'"), ///
st_matrix("`loadings'"), ///
st_data(., "`allindicators'", "`touse'"), ///
st_matrix("`adj_meas'"), ///
st_matrix("`adj_struct'"), ///
st_matrix("`modes'"), ///
st_local("robust"), ///
st_local("allindicators"), ///
st_local("ordinal"))
mata: st_numscalar("`gof'", `gof_all'[1])
mata: st_numscalar("`gof_r'", `gof_all'[2])
matrix `mod_assessment' = J(1, 5, .)
matrix `mod_assessment'[1, 1] = `avrsq'
matrix `mod_assessment'[1, 2] = `avave'
matrix `mod_assessment'[1, 3] = `gof'
matrix `mod_assessment'[1, 4] = `gof_r'
matrix `mod_assessment'[1, 5] = `avred'
matrix colnames `mod_assessment' = "Average R-squared" ///
"Average communality (AVE)" "Absolute GoF" "Relative GoF" "Average redundancy"
}
/* End of calculating the model assessment indexes (GOF, etc.) */
/* Return values */
tempname b_coef vcov_coef
local lv_nm : colnames `adj_meas'
local mv_nm : rownames `adj_meas'
if ("`structural'" != "") {
mata: st_matrix("`b_coef'", plssem_param_vec(st_matrix("`loadings'"), st_matrix("`path'"), ///
st_matrix("`adj_meas'"), st_matrix("`adj_struct'")))
mata: st_local("param_names", plssem_param_vec_names(st_matrix("`adj_meas'"), st_matrix("`adj_struct'"), ///
st_local("lv_nm"), st_local("mv_nm")))
}
else {
mata: st_matrix("`b_coef'", plssem_param_vec(st_matrix("`loadings'"), J(0, 0, .), ///
st_matrix("`adj_meas'"), J(0, 0, .)))
mata: st_local("param_names", plssem_param_vec_names(st_matrix("`adj_meas'"), J(0, 0, .), ///
st_local("lv_nm"), st_local("mv_nm")))
}
matrix colnames `b_coef' = `param_names'
if ("`boot'" != "") {
tempname loadings_reps param_reps
mata: st_matrix("`loadings_reps'", `res_bs'.loadings_reps)