-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmplus.qmd
More file actions
913 lines (694 loc) · 25.8 KB
/
mplus.qmd
File metadata and controls
913 lines (694 loc) · 25.8 KB
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
---
title: "Mplus"
---
# Overview of `Mplus`
[`Mplus`](https://www.statmodel.com) is software for [structural equation modeling](sem.qmd).
A summary of the [`Mplus`](https://www.statmodel.com) language syntax is [here](https://www.statmodel.com/language.html) (archived at: <https://perma.cc/962G-QUEG>).
The [Mplus User's Guide](https://www.statmodel.com/html_ug.shtml) is located [here](https://www.statmodel.com/download/usersguide/MplusUserGuideVer_8.pdf) (archived at: <https://perma.cc/W39W-NRGH>).
# Prepare Data {#sec-prepareData}
To prepare the data in `R` for use in `Mplus`:
```{r}
#| eval: false
library("MplusAutomation")
prepareMplusData(
mydata,
file.path(path, "/GitHub/Project_Name/Data/mplusdata.dat"))
```
# Model Example {#sec-example}
```
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!! MPLUS SYNTAX LINES CANNOT EXCEED 90 CHARACTERS;
!!!!! VARIABLE NAMES AND PARAMETER LABELS CANNOT EXCEED 8 CHARACTERS EACH;
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TITLE: Example Mplus Model
DATA:
FILE = "C:/[insert_filepath]/filename.dat";
VARIABLE:
NAMES = ID age x1 x2 x3 x4 x5 x6 y1 y2;
MISSING = .;
USEVARIABLES = ID age x1 x2 x3 x4 x5 x6 y1 y2;
ANALYSIS:
TYPE = COMPLEX;
ESTIMATOR = MLR;
MODEL:
! Factor loadings
latent1 BY x1* x2 x3;
latent2 BY x4* x5 x6;
! Covariances between latent factors
latent1 WITH latent2
! Regression paths
y1 ON latent1 + latent2
y2 ON latent1 + latent 2
! Standardize latent factors: fix means to zero
[latent1@0];
[latent2@0];
! Standardize latent factors: fix variances to one
latent1@1;
latent2@1;
OUTPUT:
STDYX;
TECH1;
TECH4;
SAMPSTAT;
MODINDICES (3);
CINTERVAL;
RESIDUAL;
SAVEDATA:
FILE = "C:/[insert_filepath]/filename.dat";
SAVE = FSCORES;
```
# Model Title {#sec-modelTitle}
```
TITLE: INSERT TITLE HERE
```
# Read Data {#sec-readData}
```
DATA:
FILE = "C:/[insert_filepath]/filename.dat";
```
# Variables {#sec-variables}
## Specify Variables {#sec-specifyVariables}
```
VARIABLE:
NAMES = ID age x1 x2 x3 y1;
MISSING = .;
USEVARIABLES = ID age x1 x2 y1;
CLUSTER = ID;
```
## Categorical Variables {#sec-categoricalVariables}
```
VARIABLE:
NAMES = ID age x1 x2 x3 y1;
MISSING = .;
USEVARIABLES = ID age x1 x2 y1;
CATEGORICAL = x1 x2;
```
## Count Variables {#sec-countVariables}
```
VARIABLE:
NAMES = ID age x1 x2 x3 y1;
MISSING = .;
USEVARIABLES = ID age x1 x2 y1;
COUNT = x1 x2;
```
## ID Variable {#sec-idVariable}
```
VARIABLE:
NAMES = ID age x1 x2 x3 y1;
MISSING = .;
USEVARIABLES = ID age x1 x2 y1;
IDVARIABLE = ID;
```
## Cluster Variable {#sec-clusterVariable}
There are multiple ways of accounting for nested data in [structural equation modeling](sem.qmd).
One way to account for nested data is to use multilevel structural equation modeling.
Another approach is to use a cluster variable to generate cluster-robust standard errors of parameters.
To use a cluster variable, specify `CLUSTER` under the `VARIABLE` section, and specify `TYPE = COMPLEX` under the `ANALYSIS` section:
```
VARIABLE:
NAMES = ID age x1 x2 x3 y1;
MISSING = .;
USEVARIABLES = ID age x1 x2 y1;
CLUSTER = ID;
ANALYSIS:
TYPE = COMPLEX;
```
## Auxiliary Variables {#sec-auxiliaryVariables}
```
VARIABLE:
NAMES = ID age x1 x2 x3 y1;
MISSING = .;
USEVARIABLES = ID x1 x2 y1;
AUXILIARY = age;
```
## Sampling Weight Variable {#sec-samplingWeight}
```
VARIABLE:
NAMES = ID wt age x1 x2 x3 y1;
MISSING = .;
USEVARIABLES = ID age x1 x2 y1;
WEIGHT = wt;
```
## Multilevel Variables {#sec-multilevelVariables}
Between- and within-cluster variables:
```
VARIABLE:
NAMES = ID wt age x1 x2 x3 y1;
MISSING = .;
USEVARIABLES = ID age x1 x2 y1;
WITHIN = x1;
BETWEEN = x2;
```
# Analysis {#sec-analysis}
## Analysis Types {#sec-analysisTypes}
- `TYPE = COMPLEX`
- `TYPE = TWOLEVEL`
- `TYPE = EFA`
- `TYPE = MIXTURE`
## Model Estimators {#sec-estimators}
```
ANALYSIS:
ESTIMATOR = MLR;
```
- `MLR`: for likert/continuous data
- `WLSMV`: for ordinal/categorical data
- `BAYES`
## Bootstrap Draws {#sec-bootstrap}
```
BOOTSTRAP = 2000; ! insert number of bootstrap draws
```
## Starts {#sec-starts}
```
STARTS = 20; ! insert number of initial stage starts and number of final stage optimizations
```
## Low Covariance Coverage {#sec-lowCovarianceCoverage}
To estimate a model with low covariance coverage, lower the `COVERAGE` value under the `ANALYSIS` section:
```
ANALYSIS:
COVERAGE = 0;
```
# Exploratory Factor Analysis {#sec-efa}
## Oblique Rotation {#sec-oblique}
```
ANALYSIS:
TYPE = EFA 1 5; ! extract 1-5 factors
ROTATION = GEOMIN;
```
## Orthogonal Rotation {#sec-orthogonal}
```
ANALYSIS:
TYPE = EFA 1 5; ! extract 1-5 factors
ROTATION = VARIMAX;
```
# Bayesian SEM {#sec-bayesian}
Other settings you can specify under the "ANALYSIS" section include:
```
ANALYSIS:
ESTIMATOR = BAYES;
BCONVERGENCE = .05; ! value of the Gelman-Rubin convergence criterion; ! default is .05; van de Schoot et al. (2014) recommend .01
BITERATIONS = a (b); ! a = maximum, b = minumum number of iterations for each MCMC chain
CHAINS = 4; ! number of chains
PROCESSORS = 4; ! number of computer processors to use
BSEED = 52242; ! set seed for replicability
STVALUES = ml; ! set starting values based on ML estimation
```
You can specify model priors under the "MODEL PRIORS" section.
Other settings you can specify under the "OUTPUT" section include:
```
OUTPUT:
STAND; ! standardized estimates
TECH1; ! model priors
TECH8; ! potential scale reduction (PSR); to evaluate convergence (should be near one)
CINTERVAL; ! posterior predictive intervals (credible intervals)
```
Other settings you can specify under the "PLOT" section include:
```
PLOT:
TYPE = PLOT3; ! trace plots, histogram, and kernel density
```
# Model {#sec-model}
## Define Latent Variables {#sec-latentVariables}
```
MODEL:
latent1 BY x1 x2 x3;
```
## Regression Paths {#sec-regressionPaths}
Regress outcome variable on predictor variable(s):
```
MODEL:
y1 ON x1 x2;
```
## Covariance Paths {#sec-covariancePaths}
```
MODEL:
x1 WITH x2;
```
## Indirect Effects {#sec-indirectEffects}
```
ANALYSIS:
TYPE = GENERAL;
ESTIMATOR = ML;
BOOTSTRAP = 1000;
MODEL:
MODEL INDIRECT:
y IND x;
OUTPUT:
STAND;
CINTERVAL (BOOTSTRAP); !percentile boostrap CI
CINTERVAL (BCBOOTSTRAP); !bias-corrected boostrap CI
```
## Means/Intercepts {#sec-means}
Freely estimate:
```
MODEL:
[x1];
```
Fix to zero:
```
MODEL:
[x1@0];
```
## Variances {#sec-variances}
Freely estimate:
```
MODEL:
x1;
```
Fix to one:
```
MODEL:
x1@1;
```
## Parameter Label {#sec-parameterLabel}
To specify a parameter label, provide the label in parentheses after the parameter:
```
MODEL:
latent1 BY x1* x2 x3 (load1-3);
latent2 BY x4* x5 (load5) x6;
```
## Multigroup Model {#sec-multigroup}
```
VARIABLE:
NAMES = group x1 x2 x3 y1;
GROUPING = group (0=boys, 1=girls);
MISSING = .;
USEVARIABLES = group x1 x2 x3 y1;
MODEL:
Model boys:
latent BY x1* x2 x3;
[latent@0];
latent@1;
y ~ latent;
Model girls:
latent BY x1* x2 x3;
[latent@0];
latent@1;
y ~ latent;
```
## Multigroup Measurement Invariance {#sec-measurementInvariance}
### Configural Invariance {#sec-configuralInvariance}
```
VARIABLE:
NAMES = group x1 x2 x3 y1;
GROUPING = group (0=boys, 1=girls);
MISSING = .;
USEVARIABLES = group x1 x2 x3;
MODEL:
Model boys:
latent BY x1* x2 x3;
[latent@0];
latent@1;
Model girls:
latent BY x1* x2 x3;
[latent@0];
latent@1;
```
### Metric (Weak Factorial) Invariance {#sec-metricInvariance}
```
VARIABLE:
NAMES = group x1 x2 x3 y1;
GROUPING = group (0=boys, 1=girls);
MISSING = .;
USEVARIABLES = group x1 x2 x3;
MODEL:
Model boys:
latent BY x1* (load1); ! constrain factor loading across groups (same parameter label)
latent BY x2* (load2); ! constrain factor loading across groups (same parameter label)
latent BY x3* (load3); ! constrain factor loading across groups (same parameter label)
[latent@0];
latent@1;
Model girls:
latent BY x1* (load1); ! constrain factor loading across groups (same parameter label)
latent BY x2* (load2); ! constrain factor loading across groups (same parameter label)
latent BY x3* (load3); ! constrain factor loading across groups (same parameter label)
[latent@0];
latent@1;
```
### Scalar (Strong Factorial) Invariance {#sec-scalarInvariance}
```
VARIABLE:
NAMES = group x1 x2 x3 y1;
GROUPING = group (0=boys, 1=girls);
MISSING = .;
USEVARIABLES = group x1 x2 x3;
MODEL:
Model boys:
latent BY x1* (load1); ! constrain factor loading across groups (same parameter label)
latent BY x2* (load2); ! constrain factor loading across groups (same parameter label)
latent BY x3* (load3); ! constrain factor loading across groups (same parameter label)
[x1] (int1); ! constrain intercept across groups (same parameter label)
[x2] (int2); ! constrain intercept across groups (same parameter label)
[x3] (int3); ! constrain intercept across groups (same parameter label)
[latent@0];
latent@1;
Model girls:
latent BY x1* (load1); ! constrain factor loading across groups (same parameter label)
latent BY x2* (load2); ! constrain factor loading across groups (same parameter label)
latent BY x3* (load3); ! constrain factor loading across groups (same parameter label)
[x1] (int1); ! constrain intercept across groups (same parameter label)
[x2] (int2); ! constrain intercept across groups (same parameter label)
[x3] (int3); ! constrain intercept across groups (same parameter label)
[latent@0];
latent@1;
```
# Model Constraint {#sec-modelConstrain}
## Estimating Coefficient Omega {#sec-omega}
<https://www.statmodel.com/download/Omega%20coefficient%20in%20Mplus.pdf> (archived at <https://perma.cc/VW88-6NCQ>)
# Comments {#sec-comments}
```
!This is a comment in Mplus
```
# Setting Parameter Constraints {#sec-parameterConstraints}
## Freeing a Parameter {#sec-freeParameter}
By default, the first loading on a factor is fixed to zero.
You can freely estimate the parameter by adding an asterisk:
```
MODEL:
latent1 BY x1* x2 x3;
```
## Constraining a Parameter {#sec-constrainParameter}
```
MODEL:
latent1 BY x1@1 x2 x3;
[latent1@0];
latent1@1;
```
## Setting Two Parameters to be Equal {#sec-equalParameters}
To set two parameters to be equal, provide the same [parameter label](#sec-parameterLabel) for each parameter.
## Setting Lower and Upper Bounds on a Parameter {#sec-parameterBounds}
To set lower and upper bounds on a parameter, you can assign the parameter a [parameter label](#sec-parameterLabel).
Then, you can assign the constraint to the parameter (via the label) under the `MODEL CONSTRAINT` section.
For example, to constrain a parameter between 0–1,
```
MODEL:
latent1 BY x1* x2 x3 (load3);
MODEL CONSTRAINT:
load3 > 0; load3 < 1;
```
# Output {#sec-output}
```
OUTPUT:
STDYX;
TECH1;
TECH4;
SAMPSTAT;
MODINDICES (ALL); ! specify number in parentheses to print only those mod indices that are above a particular chi-square value
CINTERVAL;
RESIDUAL;
```
# Plot {#sec-plot}
```
PLOT:
SERIES =
ext36 (36) ext45 (45) ext54 (54) ext63 (63)
ext72 (72) ext81 (81) ext90 (90);
TYPE = PLOT3;
OUTLIERS =
MAHALANOBIS ! Mahalanobis distance and its p-value
LOGLIKELIHOOD ! Loglikelihood contribution
INFLUENCE ! Loglikelihood distance influence measure
COOKS; ! Cook's D parameter estimate influence measure
```
# Save Data {#sec-saveData}
```
SAVEDATA:
FILE = "C:/[insert_filepath]/filename.dat";
SAVE =
FSCORES ! Factor scores
LOGLIKELIHOOD ! Loglikelihood contribution from each observation
INFLUENCE ! Loglikelihood distance influence measure (Cook & Weisberg, 1982) for each observation
COOKS; !Cook's D (Cook, 1977) for each observation
```
# Multiple Imputation {#sec-multipleImputation}
For examples of how to conduct [multiple imputation](multipleImputation.qmd) in [`Mplus`](https://www.statmodel.com), see [here](multipleImputation.qmd#sec-mplus).
To run a model on multiply imputed data, use the following:
```
DATA: FILE = "implist.dat"; ! where implist.dat is the name of the *list.dat file saved from the imputation step
TYPE = IMPUTATION;
```
# Latent Growth Curve Model {#sec-lgcm}
## Equally Spaced Time Points {#sec-equallySpaced}
```
VARIABLE:
NAMES = id y0 y1 y2 y3 y4 x;
USEVARIABLES = y0 y1 y2 y3 y4 x;
ANALYSIS:
TYPE = RANDOM;
MODEL:
i s q | y0@0 y1@1 y2@2 y3@3 y4@4; ! intercept, linear slope, quadratic slope
! OR ALTERNATIVELY:
!i BY y0@1 y1@1 y2@1 y3@1 y4@1;
!s BY y0@0 y1@1 y2@2 y3@3 y4@4;
!q BY y0@0 y1@1 y2@4 y3@9 y4@16;
i s q ON x;
```
## Unequally Spaced Time Points (Individually Varying Times of Observation) {#sec-tScores}
Unequally spaced time points can be accounted for using the `TSCORES` option (i.e., person-specific factor loadings; Sterba, 2014).
<https://stats.oarc.ucla.edu/mplus/faq/how-can-i-run-a-growth-model-in-wide-form-with-unequally-spaced-time-points-tscore/> (archived at <https://perma.cc/Z7QY-TL6W>)
```
VARIABLE:
NAMES = id t0 t1 t2 t3 t4 y0 y1 y2 y3 y4 x;
USEVARIABLES = t0 t1 t2 t3 t4 y0 y1 y2 y3 y4 x;
TSCORES = t0 t1 t2 t3 t4;
ANALYSIS:
TYPE = RANDOM;
MODEL:
i s q | y0 y1 y2 y3 y4 AT t0 t1 t2 t3 t4; ! intercept, linear slope, quadratic slopes
i s q ON x;
```
# Latent Class Growth Analysis {#sec-lcga}
```
VARIABLE:
NAMES = id y1 y2 y3 y4;
USEVARIABLES = y1 y2 y3 y4;
IDVARIABLE = id;
CLASSES = c(3); ! 3 latent classes
MISSING = .;
ANALYSIS:
TYPE = MIXTURE;
ESTIMATOR = MLR;
STARTS = 500 20;
STITERATIONS = 20;
MODEL:
%OVERALL%
i s q | y1@0 y2@1 y3@2 y4@3; ! intercept, linear slope, quadratic slope
i@0; s@0; q@0; ! fix variances to zero (LCGA assumption)
%c#1%
[i s q]; ! freely estimate class-specific means
%c#2%
[i s q];
%c#3%
[i s q];
OUTPUT:
standardized TECH1 TECH4 TECH8 TECH11 TECH14;
PLOT:
TYPE = PLOT3;
SERIES = y1(0) y2(1) y3(2) y4(3);
SAVEDATA:
FILE = lcga_3class_prob.dat;
SAVE = CPROBABILITIES;
```
# Run Models in Batch {#sec-runModels}
Run models in batch via `R`:
```{r}
#| eval: false
library("MplusAutomation")
runModels(
file.path(path, "/GitHub/Project_Name/Analyses/Mplus/"),
recursive = TRUE,
replaceOutfile = "always",
showOutput = TRUE)
```
# Monte Carlo Simulation/Power Analysis {#sec-monteCarlo}
## Single Group Model {#sec-monteCarloSingleGroup}
```
TITLE: Single-Group Monte Carlo Simulation with Ordinal Items and Common Factor;
MONTECARLO:
NAMES = v1-v5; ! variable names
NOBSERVATIONS = 500; ! number of participants in each sample
NREPS = 100000; ! number of samples to create
SEED = 52242; ! random seed
GENERATE = v1-v5 (3 p); ! specify the scale of the DVs; number of thresholds; probit (p)
CATEGORICAL = v1-v5; ! specify the variables that are (ordered) categorical
ANALYSIS:
PROCESSORS = 4 1; ! number of processors; number of threads
ESTIMATOR = WLSMV;
PARAMETERIZATION = THETA;
MODEL POPULATION: ! tell Mplus how to generate the population data; can use asterisks (*) and at symbols (@) interchangeably here, but they differ in the MODEL command (see below); I use the same symbol as in the MODEL command
dep BY v1-v5*.7; ! factor loadings
[dep@0]; ! set factor mean to 0
dep@1; ! set factor variance to 1 (standardize)
[v1$1*0.5] (v1t1); [v1$2*1.0] (v1t2); [v1$3*1.5] (v1t3); ! item thresholds for v1
[v2$1*0.5] (v2t1); [v2$2*1.0] (v2t2); [v2$3*1.5] (v2t3); ! item thresholds for v2
[v3$1*0.5] (v3t1); [v3$2*1.0] (v3t2); [v3$3*1.5] (v3t3); ! item thresholds for v3
[v4$1*0.0] (v4t1); [v4$2*0.5] (v4t2); [v4$3*1.0] (v4t3); ! item thresholds for v4
[v5$1*0.0] (v5t1); [v5$2*0.5] (v5t2); [v5$3*1.0] (v5t3); ! item thresholds for v5
v1-v5@1; ! item residual variances
MODEL: ! tell Mplus to estimate our model; asterisks (*) are free estimates with a starting value; at symbols (@) are fixed estimates
dep BY v1-v5*.7; ! factor loadings
[dep@0]; ! set factor mean to 0
dep@1; ! set factor variance to 1 (standardize)
[v1$1*0.5] (v1t1); [v1$2*1.0] (v1t2); [v1$3*1.5] (v1t3); ! item thresholds for v1
[v2$1*0.5] (v2t1); [v2$2*1.0] (v2t2); [v2$3*1.5] (v2t3); ! item thresholds for v2
[v3$1*0.5] (v3t1); [v3$2*1.0] (v3t2); [v3$3*1.5] (v3t3); ! item thresholds for v3
[v4$1*0.0] (v4t1); [v4$2*0.5] (v4t2); [v4$3*1.0] (v4t3); ! item thresholds for v4
[v5$1*0.0] (v5t1); [v5$2*0.5] (v5t2); [v5$3*1.0] (v5t3); ! item thresholds for v5
v1-v5@1; ! item residual variances
MODEL CONSTRAINT:
NEW (stdt stdt1 stdt2 stdt3 noninvt noninvt1 noninvt2 noninvt3 diff);
stdt1 = (v1t1 + v2t1 + v3t1) / 3;
stdt2 = (v1t2 + v2t2 + v3t2) / 3;
stdt3 = (v1t3 + v2t3 + v3t3) / 3;
noninvt1 = (v4t1 + v5t1) / 2;
noninvt2 = (v4t2 + v5t2) / 2;
noninvt3 = (v4t3 + v5t3) / 2;
stdt = (stdt1 + stdt2 + stdt3) / 3;
noninvt = (noninvt1 + noninvt2 + noninvt3) / 3;
diff = noninvt - stdt;
OUTPUT:
TECH9;
```
## Multi-Group Model {#sec-monteCarloMultigroup}
```
TITLE: Multi-Group Monte Carlo Simulation with Ordinal Items and Common Factor;
MONTECARLO:
NAMES = v1-v5; ! variable names
NGROUPS = 2; ! number of groups
NOBSERVATIONS = 500 300; ! number of participants in each sample
NREPS = 100000; ! number of samples to create
SEED = 52242; ! random seed
GENERATE = v1-v5 (3 p); ! specify the scale of the DVs; number of thresholds; probit (p)
CATEGORICAL = v1-v5; ! specify the variables that are (ordered) categorical
ANALYSIS:
PROCESSORS = 4 1; ! number of processors; number of threads
ESTIMATOR = WLSMV;
PARAMETERIZATION = THETA;
MODEL POPULATION: ! tell Mplus how to generate the population data; can use asterisks (*) and at symbols (@) interchangeably here, but they differ in the MODEL command (see below); I use the same symbol as in the MODEL command
dep BY v1-v5*.7; ! factor loadings
[dep@0]; ! set factor mean to 0
dep@1; ! set factor variance to 1 (standardize)
[v1$1*0.5] (v1t1g1); [v1$2*1.0] (v1t2g1); [v1$3*1.5] (v1t3g1); ! item thresholds for v1
[v2$1*0.5] (v2t1g1); [v2$2*1.0] (v2t2g1); [v2$3*1.5] (v2t3g1); ! item thresholds for v2
[v3$1*0.5] (v3t1g1); [v3$2*1.0] (v3t2g1); [v3$3*1.5] (v3t3g1); ! item thresholds for v3
[v4$1*0.5] (v4t1g1); [v4$2*1.0] (v4t2g1); [v4$3*1.5] (v4t3g1); ! item thresholds for v4
[v5$1*0.5] (v5t1g1); [v5$2*1.0] (v5t2g1); [v5$3*1.5] (v5t3g1); ! item thresholds for v5
v1-v5@1; ! item residual variances
MODEL POPULATION-g2: ! tell Mplus how to generate the population data for group 2; can use asterisks (*) and at symbols (@) interchangeably here, but they differ in the MODEL command (see below); I use the same symbol as in the MODEL command
!dep BY v1-v5*.7; ! factor loadings
![dep@0]; ! set factor mean to 0
!dep@1; ! set factor variance to 1 (standardize)
[v1$1*0.5] (v1t1g2); [v1$2*1.0] (v1t2g2); [v1$3*1.5] (v1t3g2); ! item thresholds for v1
[v2$1*0.5] (v2t1g2); [v2$2*1.0] (v2t2g2); [v2$3*1.5] (v2t3g2); ! item thresholds for v2
[v3$1*0.5] (v3t1g2); [v3$2*1.0] (v3t2g2); [v3$3*1.5] (v3t3g2); ! item thresholds for v3
[v4$1*0.0] (v4t1g2); [v4$2*0.5] (v4t2g2); [v4$3*1.0] (v4t3g2); ! item thresholds for v4
[v5$1*0.0] (v5t1g2); [v5$2*0.5] (v5t2g2); [v5$3*1.0] (v5t3g2); ! item thresholds for v5
!v1-v5@1; ! item residual variances
MODEL: ! tell Mplus to estimate our model; asterisks (*) are free estimates with a starting value; at symbols (@) are fixed estimates
dep BY v1-v5*.7; ! factor loadings
[dep@0]; ! set factor mean to 0
dep@1; ! set factor variance to 1 (standardize)
[v1$1*0.5] (v1t1g1); [v1$2*1.0] (v1t2g1); [v1$3*1.5] (v1t3g1); ! item thresholds for v1
[v2$1*0.5] (v2t1g1); [v2$2*1.0] (v2t2g1); [v2$3*1.5] (v2t3g1); ! item thresholds for v2
[v3$1*0.5] (v3t1g1); [v3$2*1.0] (v3t2g1); [v3$3*1.5] (v3t3g1); ! item thresholds for v3
[v4$1*0.5] (v4t1g1); [v4$2*1.0] (v4t2g1); [v4$3*1.5] (v4t3g1); ! item thresholds for v4
[v5$1*0.5] (v5t1g1); [v5$2*1.0] (v5t2g1); [v5$3*1.5] (v5t3g1); ! item thresholds for v5
v1-v5@1; ! item residual variances
MODEL g2: ! tell Mplus to estimate our model in group 2; asterisks (*) are free estimates with a starting value; at symbols (@) are fixed estimates
!dep BY v1-v5*.7; ! factor loadings
![dep@0]; ! set factor mean to 0
!dep@1; ! set factor variance to 1 (standardize)
[v1$1*0.5] (v1t1g2); [v1$2*1.0] (v1t2g2); [v1$3*1.5] (v1t3g2); ! item thresholds for v1
[v2$1*0.5] (v2t1g2); [v2$2*1.0] (v2t2g2); [v2$3*1.5] (v2t3g2); ! item thresholds for v2
[v3$1*0.5] (v3t1g2); [v3$2*1.0] (v3t2g2); [v3$3*1.5] (v3t3g2); ! item thresholds for v3
[v4$1*0.0] (v4t1g2); [v4$2*0.5] (v4t2g2); [v4$3*1.0] (v4t3g2); ! item thresholds for v4
[v5$1*0.0] (v5t1g2); [v5$2*0.5] (v5t2g2); [v5$3*1.0] (v5t3g2); ! item thresholds for v5
!v1-v5@1; ! item residual variances
MODEL CONSTRAINT:
NEW (stdt1g1 stdt2g1 stdt3g1 stdt1g2 stdt2g2 stdt3g2
nonit1g1 nonit2g1 nonit3g1 nonit1g2 nonit2g2 nonit3g2
stdtg1 stdtg2 nonitg1 nonitg2 diffwg diffbg);
stdt1g1 = (v1t1g1 + v2t1g1 + v3t1g1) / 3;
stdt2g1 = (v1t2g1 + v2t2g1 + v3t2g1) / 3;
stdt3g1 = (v1t3g1 + v2t3g1 + v3t3g1) / 3;
stdt1g2 = (v1t1g2 + v2t1g2 + v3t1g2) / 3;
stdt2g2 = (v1t2g2 + v2t2g2 + v3t2g2) / 3;
stdt3g2 = (v1t3g2 + v2t3g2 + v3t3g2) / 3;
nonit1g1 = (v4t1g1 + v5t1g1) / 2;
nonit2g1 = (v4t2g1 + v5t2g1) / 2;
nonit3g1 = (v4t3g1 + v5t3g1) / 2;
nonit1g2 = (v4t1g2 + v5t1g2) / 2;
nonit2g2 = (v4t2g2 + v5t2g2) / 2;
nonit3g2 = (v4t3g2 + v5t3g2) / 2;
stdtg1 = (stdt1g1 + stdt2g1 + stdt3g1) / 3;
stdtg2 = (stdt1g2 + stdt2g2 + stdt3g2) / 3;
nonitg1 = (nonit1g1 + nonit2g1 + nonit3g1) / 3;
nonitg2 = (nonit1g2 + nonit2g2 + nonit3g2) / 3;
diffwg = nonitg2 - stdtg2; ! difference within group
diffbg = nonitg2 - nonitg1; ! difference between groups
OUTPUT:
TECH9;
```
# Multilevel Structural Equation Modeling (M-SEM) {#sec-msem}
## Latent Curve Model with Structured Residuals (LCM-SR) {#sec-msem-lcm-sr}
A latent curve model with structured residuals (LCM-SR) estimated with long-form data in multilevel SEM allows estimating a cross-lagged model to (a) determine the direction of effect between two constructs, (b) account for individual differences in level and change of each construct, (c) account for unbalanced time, and (d) disaggregate within-person from between-person effects.
This example is adapted from Hoffman's [Example 5b](https://www.lesahoffman.com/PSQF7375_AdvLong/index.html) (archived at <https://perma.cc/N2AR-52S5>):
```
TITLE: Latent Curve Model with Structured Residuals with Long-Form
Data in Multilevel SEM
DATA:
FILE = "C:/[insert_filepath]/filename.dat";
VARIABLE:
NAMES = PersonID sex A B time timesq; ! List of variables in data file
MISSING = .; ! Missing data identifier
USEVARIABLES = sex A B time timesq; ! Variables to be analyzed in this model
! MLM options
CLUSTER = PersonID; ! Level-2 ID
BETWEEN = sex; ! Observed ONLY level-2 predictors
WITHIN = time timesq; ! Observed ONLY level-1 predictors
LAGGED = A(1) B(1); ! Create Mplus lag-1 variables
ANALYSIS:
TYPE = TWOLEVEL RANDOM;
ESTIMATOR = BAYES;
BITERATIONS = 50000;
CHAINS = 4; ! number of chains
PROCESSORS = 4; ! number of computer processors to use
BSEED = 52242; ! set seed for replicability
STVALUES = ml; ! set starting values based on ML estimation
MODEL: ! A = construct 1; B = construct 2
%WITHIN%
A B (Aresvar Bresvar); ! L1 residual variances (labels)
Alin | A ON time; ! Placeholder for A linear change
Aquad | A ON timesq; ! Placeholder for A quadratic change
Blin | B ON time; ! Placeholder for B linear change
A WITH B (ResCov); ! L1 WP covariance for concurrent B->A
A^ ON B^1 (BALagEff); ! L1 WP fixed effect of ^residual lagged B->A
B^ ON A^1 (ABLagEff); ! L1 WP fixed effect of ^residual lagged A->B
%BETWEEN%
[A B Alin Aquad Blin]; ! Fixed intercepts, fixed change slopes
A B (Aintvar Bintvar); ! L2 G: Random intercept variances (labels)
Alin Blin (Alinvar Blinvar); ! L2 G: Random linear change variances
A Alin ON sex (XtoYint XtoYlin); ! sex -> A int, A linear change
B Blin ON sex (XtoYint XtoYlin); ! sex -> B int, B linear change
A Alin B Blin WITH A Alin B Blin; ! L2 G: covariances for random intercepts and change across outcomes
Aquad@0; ! No quadratic change variance
MODEL CONSTRAINT:
NEW(A2Bsd B2Asd);
! STD = Unstd * SQRT(Xvar) / SQRT(Yvar)
A2Bsd = ABLagEff * SQRT(8.3538) / SQRT(0.08077); ! STD A->B lagged effect
B2Asd = BALagEff * SQRT(0.08077) / SQRT(8.3538); ! STD B->A lagged effect
OUTPUT:
STAND; ! standardized estimates
TECH1; ! model priors
TECH8; ! potential scale reduction (PSR); to evaluate convergence (should be near one)
CINTERVAL; ! posterior predictive intervals (credible intervals)
PLOT:
TYPE = PLOT3; ! trace plots, histogram, and kernel density
```
Further extensions:
- Distinguish between (a) time in study versus (b) age at baseline (as a moderator)
- Allow the cross-lagged effects to vary as a function of time (add time as a moderator of those effects)
# Session Info
```{r}
#| code-fold: true
sessionInfo()
```