-
Notifications
You must be signed in to change notification settings - Fork 7
/
plssemc.ado
2512 lines (2335 loc) · 87 KB
/
plssemc.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
*!plssem version 0.6.3
*!Written 05Sep2024
*!Written by Sergio Venturini and Mehmet Mehmetoglu
*!The following code is distributed under GNU General Public License version 3 (GPL-3)
program plssemc, byable(onecall)
version 15.1
syntax [anything] [if] [in] [, * ]
if replay() {
if ("`e(cmd)'" != "plssemc") {
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_c, `options' `nostructural'
}
else {
Display_c, `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_c `0' // version is not necessary
}
}
else {
`version' `BY' Estimate_c `0' // version is not necessary
}
end
program Estimate_c, eclass byable(recall)
version 15.1
syntax anything(id="Measurement model" name=blocks) [if] [in], ///
[ STRuctural(string) 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(string) --> 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
/* Parse the blocks */
local blocks : list clean blocks
tokenize `"`blocks'"', parse(" ()<>")
tempname inblock startblock
scalar `inblock' = 0
scalar `startblock' = 0
local j = 0
local tok_i = 1
while ("``tok_i''" != "") {
if ("``tok_i''" == "(") {
if (`inblock') {
display as error "unexpected ("
error 197
}
scalar `inblock' = 1
scalar `startblock' = 1
local ++j
}
else if (`inblock') {
if ("``tok_i''" == ")") {
if ("LV`j'" == "" | "i`j'" == "") {
display as error "incomplete measurement model specification"
error 197
}
else {
scalar `inblock' = 0
local i`LV`j'' `i`j''
local allindicators "`allindicators' `i`j''"
local alllatents "`alllatents' `LV`j''"
}
}
else if ("``tok_i''" == "<" | "``tok_i''" == ">") {
scalar `startblock' = 0
if ("``tok_i''" == ">") {
local modeA "`modeA' `LV`j''"
}
else if ("``tok_i''" == "<") {
local modeB "`modeB' `LV`j''"
}
}
else if (`startblock') {
if ("`LV`j''" != "") {
display as error "missing ="
error 197
}
if (_byindex() == 1) { // this provides the LVs predictions when by'd
capture confirm new variable ``tok_i''
if (_rc == 110) {
capture quietly drop ``tok_i''
}
}
local LV`j' ``tok_i''
}
else {
foreach var of varlist ``tok_i'' {
local i`j' "`i`j'' `var'"
}
}
}
else {
error 197
}
local ++tok_i
}
local modeA : list clean modeA
local modeB : list clean modeB
local modeA : list uniq modeA
local modeB : list uniq modeB
if (`inblock') {
display as error "missing )"
error 197
}
local allindicators : list clean allindicators
local alllatents : list clean alllatents
/* End of parsing the blocks */
/* 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'
local num_ind: word count `allindicators'
tempname isproduct
local tempnamelist "`tempnamelist' `isproduct'"
mata: `isproduct' = J(`num_ind', 1, 0)
if ("`structural'" != "") {
tokenize `"`structural'"', parse(",")
local reg3eqs = "("
local tok_i = 1
local allorigindicators "`allindicators'"
while ("``tok_i''" != "") {
// Parse the interactions
if (strpos("``tok_i''", "*")) {
local inter "``tok_i''"
gettoken depvar : inter
if (strpos("`depvar'", "*")) {
display as error "`depvar': interactions not allowed within a dependent variable"
exit
}
local new_tok "`depvar'"
gettoken blk inter : inter
local inter : list clean inter
while ("`inter'" != "") {
gettoken toadd inter : inter
local inter : list clean inter
if (strpos("`toadd'", "*")) {
local inter_ind ""
gettoken var1 toadd : toadd, parse("*")
gettoken ast toadd : toadd, parse("*")
gettoken var2 toadd : toadd, parse("*")
if (strpos("`toadd'", "*")) {
display as error "interactions can involve only two variables"
exit
}
local internm = "`var1'`var2'"
if (_byindex() == 1) { // this provides the LVs predictions when by'd
capture confirm new variable `internm'
if (_rc == 110) {
capture quietly drop `internm'
}
}
// Add the interaction to the list of LVs
if (!`: list internm in alllatents') {
local alllatents "`alllatents' `internm'"
local modeA "`modeA' `internm'"
local num_lv: word count `alllatents'
local LV`num_lv' `internm'
// Create macros of product indicators to use for scaling
local inter_latent "`inter_latent' `var1'%£%]_[%£%`var2'"
// Check that the new interaction is reflective
local int_in_modeB : list internm in modeB
if (`int_in_modeB') {
display as error "interactions of latent variables must be reflective"
exit
}
// Generate the product indicators
foreach ind1 in `i`var1'' {
mata: st_local("is_std", strofreal(abs(mean(st_data(., "`ind1'", "`touse'"))) < epsilon(1e10)))
if (!`is_std') mata: st_local("is_std", strofreal( ///
variance(st_data(., "`ind1'", "`touse'")) < 1 + epsilon(1e10) & ///
variance(st_data(., "`ind1'", "`touse'")) > 1 - epsilon(1e10)))
if (!`is_std') {
display as error "interactions require indicators that are already standardized"
error 197
}
foreach ind2 in `i`var2'' {
mata: st_local("is_std", strofreal(abs(mean(st_data(., "`ind2'", "`touse'"))) < epsilon(1e10)))
if (!`is_std') mata: st_local("is_std", strofreal( ///
variance(st_data(., "`ind2'", "`touse'")) < 1 + epsilon(1e10) & ///
variance(st_data(., "`ind2'", "`touse'")) > 1 - epsilon(1e10)))
if (!`is_std') {
display as error "interactions require indicators that are already standardized"
error 197
}
local indnm = "`ind1'`ind2'"
if (_byindex() == 1) { // this provides the indicators when by'd
capture confirm new variable `indnm'
if (_rc == 110) {
capture quietly drop `indnm'
}
quietly generate double `indnm' = `ind1'*`ind2' if `touse'
}
else {
quietly replace `indnm' = `ind1'*`ind2' if `touse'
}
// Add the product indicator to the list of indicators
local allindicators "`allindicators' `indnm'"
mata: `isproduct' = (`isproduct' \ 1)
local inter_ind "`inter_ind' `indnm'"
// Create macros of product indicators to use for scaling
local inter_for_scale "`inter_for_scale' `ind1'%£%]_[%£%`ind2'"
}
}
local i`num_lv' `inter_ind'
local i`internm' `inter_ind'
}
local new_tok "`new_tok' `var1' `var2' `internm'"
}
else {
local new_tok "`new_tok' `toadd'"
}
}
local reg3eqs "`reg3eqs'`new_tok') ("
local 0 `new_tok'
}
else {
local reg3eqs "`reg3eqs'``tok_i'') ("
local 0 ``tok_i''
}
syntax namelist(min=2)
// 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 `namelist'
local ivars : list namelist - 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 tok_i = `tok_i' + 2
}
local parenthesis = "("
local reg3eqs : list reg3eqs - parenthesis
local reg3eqs : list clean reg3eqs
tokenize `reg3eqs', parse("()")
local tok_i = 1
while ("``tok_i''" != "") {
if ("``tok_i''" != "(" & "``tok_i''" != ")") {
local check_lv_new : word 1 of ``tok_i''
local check_lv "`check_lv' `check_lv_new'"
}
local ++tok_i
}
local check_lv : list clean check_lv
local check_lv_uniq : list uniq check_lv
if (`: word count `check_lv'' != `: word count `check_lv_uniq'') {
display as error "specify a single equation for each construct in the structural model"
error 198
exit
}
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_meas 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
}
matrix `adj_meas' = J(`num_ind', `num_lv', 0)
local i = 1
local j = 1
foreach var in `alllatents' {
foreach var2 in `i`var'' {
matrix `adj_meas'[`i', `j'] = 1
local ++i
}
if (`: list var in modeA') {
local loadcolnames `loadcolnames' "Reflective:`var'"
}
else {
local loadcolnames `loadcolnames' "Formative:`var'"
}
local loadrownames `loadrownames' `i`var''
local ++j
}
matrix rownames `adj_meas' = `loadrownames'
matrix colnames `adj_meas' = `loadcolnames'
local adj_sum = 0
local adj_nrows = rowsof(`adj_meas')
local adj_ncols = colsof(`adj_meas')
forvalues adj_i = 1(1)`adj_nrows' {
forvalues adj_j = 1(1)`adj_ncols' {
local adj_el = `adj_meas'[`adj_i', `adj_j']
local adj_sum = `adj_sum' + `adj_el'
}
}
if (`adj_sum' == 0) {
display as error "the adjacency matrix of the measurement model is empty"
exit
}
matrix `adj_struct' = J(`num_lv', `num_lv', 0)
local B_j = 1
foreach var in `alllatents' {
if ("`regest`var''" != "") {
forvalues lv_i = 1/`num_lv' {
local xn : word `lv_i' of `alllatents'
local isthere : list xn in regest`var'
if (`isthere') {
local B_i : list posof "`xn'" in alllatents
matrix `adj_struct'[`B_i', `B_j'] = 1
}
}
}
local ++B_j
}
matrix rownames `adj_struct' = `alllatents'
matrix colnames `adj_struct' = `alllatents'
local adj_sum = 0
local adj_nrows = rowsof(`adj_struct')
local adj_ncols = colsof(`adj_struct')
forvalues adj_i = 1(1)`adj_nrows' {
forvalues adj_j = 1(1)`adj_ncols' {
local adj_el = `adj_struct'[`adj_i', `adj_j']
local adj_sum = `adj_sum' + `adj_el'
}
}
if (`adj_sum' == 0) {
* display as error "the adjacency matrix of the structural model is empty"
* exit
}
/* 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'"), ///
1, ///
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'" == "") {
// note: in PLSc variances are available only using bootstrap
mata: `xload_v' = J(rows(`res'.loadings), cols(`res'.loadings), .)
mata: `path_v' = J(rows(`res'.path), cols(`res'.path), .)
}
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'"), ///
1, ///
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 allorigindicators'"), "`__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'", `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)