-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10-wls.Rmd
935 lines (689 loc) · 30.5 KB
/
10-wls.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
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
---
title: "Weighted Least Squares Estimator - Factor Analysis with Categorical Indicators"
author: "Norwegian University of Science and Technology - A Course in `MplusAutomation`"
subtitle: '*Adam Garber*'
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE)
library(here);library(glue)
```
______________________________________________
## **Outline**
- Prepare data
- EFA (model 0)
- CFA (models 1-3)
- Invariance (models 4-7)
- Generate summary table for model comparison
______________________________________________
DATA SOURCE: This lab exercise utilizes a subset of the HSLS public-use dataset: High School Longitudinal Study of 2009 (Ingels et al., 2011) [$\color{blue}{\text{See website: nces.ed.gov}}$](https://nces.ed.gov/pubsearch/pubsinfo.asp?pubid=2018142)
FURTHER READING: A description of the models run in this lab exercise can be found in chapter 16 (page 411) in Kline, 2016. All specifications for invariance with the WLS estimator follow the recomendations found in this book. [$\color{blue}{\text{Example Mplus Syntax}}$](https://www.guilford.com/companion-site/Principles-and-Practice-of-Structural-Equation-Modeling-Fourth-Edition/9781462523344)
______________________________________________
## When to use the Weighted Least Squared (`WSL`) method?
1. you have ordinal indicators (e.g., Likert scales)
2. you have categorical indicators (e.g., binary, or nominal)
3. you have a combination of ordinal, categorical, and/or continuous indicators
4. you have skewed continuous indicators
## How to specify the Robust WSL estimator (`WLSMV`) in Mplus
Include the following lines of syntax:
- `categorical = X1 X2 X3... ;` *identify any categorical indicators*
- `estimator = wlsmv;`
- `parameterization = theta;` *to allow for error variances to be freely estimated across groups*
## Interpretation of $\chi^2$ with WLS estimator
- Cannot do normal chi-square difference tests (use the `DIFFTEST` option)
- The scaled Satorra-Bentler correction is necessary
______________________________________________
```{r, eval=TRUE}
# load packages
library(MplusAutomation)
library(tidyverse)
library(here)
library(corrplot)
library(semPlot)
library(gt)
```
______________________________________________
## Prepare data
______________________________________________
## Read in data
```{r, eval=TRUE}
data_raw <- read_csv("https://garberadamc.github.io/project-site/data/hsls_fa_data_subset.csv")
```
## Reverse code for factor interpretation
```{r, eval=TRUE}
hsls_data <- data_raw
cols = c("S1MPERS1", "S1MPERS2", "S1MUSELI", "S1MUSECL", "S1MUSEJO",
"S1MTESTS", "S1MTEXTB", "S1MSKILL", "S1MASSEX", "S1MENJNG",
"S1SPERS1", "S1SPERS2", "S1SUSELI", "S1SUSECL", "S1SUSEJO",
"S1STESTS", "S1STEXTB", "S1SSKILL", "S1SASSEX", "S1SENJNG")
hsls_data[ ,cols] <- 5 - hsls_data[ ,cols]
```
## Check correlations of science indicators
```{r, eval=TRUE}
sci_cor <- cor(hsls_data[27:38], use = "pairwise.complete.obs")
#sci_cor
corrplot(sci_cor,
method = "circle",
type = "upper")
# NOTE: correlations should be reported from Mplus output as
# the matrix estimated using `cor()` are not polychoric correlations.
```
______________________________________________
## Model 0 - Exploratory Factor Analysis (EFA) with WLS Estimator
______________________________________________
```{r, eval=FALSE}
m.step0 <- mplusObject(
TITLE = " WSL FACTOR ANALYSIS EFA - HSLS SCIENCE DEMO",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;",
ANALYSIS =
"type = efa 1 7;
estimator=wlsmv;
parameterization=theta;",
MODEL = "" ,
PLOT = "type = plot3;",
OUTPUT = "sampstat standardized residual modindices (3.84);",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step0.fit <- mplusModeler(m.step0,
dataout=here("10-wls", "wls_efa", "efa_sci_HSLS_wls.dat"),
modelout=here("10-wls", "wls_efa", "efa_sci_HSLS_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
______________________________________________
## **BEGIN: SINGLE GROUP INVARIANCE MODELS**
______________________________________________
## Model 1 - Confirmatory Factor Analysis (CFA) - Full Sample
______________________________________________
```{r, eval=FALSE}
m.step1 <- mplusObject(
TITLE = " WSL FACTOR ANALYSIS CFA - HSLS SCIENCE DEMO",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;",
ANALYSIS =
"estimator=wlsmv;
parameterization=theta;",
MODEL =
"SCI_ID BY S1SPERS1* S1SPERS2;
SCI_ID@1;
SCI_UT BY S1SUSELI* S1SUSECL S1SUSEJO;
SCI_UT@1;
SCI_EFF BY S1STESTS* S1STEXTB S1SSKILL S1SASSEX ;
SCI_EFF@1;
SCI_INT BY S1SENJNG* S1SWASTE S1SBORIN;
SCI_INT@1;" ,
PLOT = "type = plot3;",
OUTPUT = "sampstat standardized residual modindices (3.84);",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step1.fit <- mplusModeler(m.step1,
dataout=here("10-wls", "wls_invar", "m1_cfa_wls.dat"),
modelout=here("10-wls", "wls_invar", "m1_cfa_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
## END: CONFIRMATORY FACTOR ANALYSIS OF SCIENCE INDICATORS
```
______________________________________________
## Model 2 - CFA for group SEX = 0 (Male)
______________________________________________
```{r, eval=FALSE}
m.step2 <- mplusObject(
TITLE = " WSL FACTOR ANALYSIS CFA MALE GROUP SCIENCE",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
!X1SEX (1 = MALE, 2 = FEMALE)
USEOBS = X1SEX == 1;",
ANALYSIS =
"estimator=wlsmv; parameterization=theta;",
MODEL =
"SCI_ID BY S1SPERS1* S1SPERS2;
SCI_ID@1;
SCI_UT BY S1SUSELI* S1SUSECL S1SUSEJO;
SCI_UT@1;
SCI_EFF BY S1STESTS* S1STEXTB S1SSKILL S1SASSEX ;
SCI_EFF@1;
SCI_INT BY S1SENJNG* S1SWASTE S1SBORIN;
SCI_INT@1;" ,
PLOT = "type = plot3;",
OUTPUT = "sampstat standardized residual modindices (3.84);",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step2.fit <- mplusModeler(m.step2,
dataout=here("10-wls", "wls_invar", "m2_cfa_males_wls.dat"),
modelout=here("10-wls", "wls_invar", "m2_cfa_males_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
______________________________________________
## Model 3 - CFA for group SEX = 1 (Female)
______________________________________________
```{r, eval=FALSE}
m.step3 <- mplusObject(
TITLE = " WSL FACTOR ANALYSIS CFA - FEMALE GROUP SCIENCE",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
!X1SEX (1 = MALE, 2 = FEMALE)
USEOBS = X1SEX==2;",
ANALYSIS = "estimator=wlsmv; parameterization=theta;",
MODEL =
"SCI_ID BY S1SPERS1* S1SPERS2;
SCI_ID@1;
SCI_UT BY S1SUSELI* S1SUSECL S1SUSEJO;
SCI_UT@1;
SCI_EFF BY S1STESTS* S1STEXTB S1SSKILL S1SASSEX ;
SCI_EFF@1;
SCI_INT BY S1SENJNG* S1SWASTE S1SBORIN;
SCI_INT@1;" ,
PLOT = "type = plot3;",
OUTPUT = "sampstat standardized residual modindices (3.84);",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step3.fit <- mplusModeler(m.step3,
dataout=here("10-wls", "wls_invar", "m3_cfa_females_wls.dat"),
modelout=here("10-wls", "wls_invar", "m3_cfa_females_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
______________________________________________
## **BEGIN: MULTI-GROUP INVARIANCE MODELS**
______________________________________________
## How does `update()` work with MplusAutomation?
- Instead of copying the whole model above and altering one line of syntax we can use the `update()` model function.
- A key advantage of this is that it highlights changes in code which otherwise might be easy to overlook.
- **tilde (~)** will replace everything in that section of the input.
- **tilde-dot-plus (~.+)** will update the section by adding the specified code into that section.
This coding strategy although space-efficient was not utilized for following models. Rather, code is made explicit for pedagogy.
______________________________________________
## Model 4 - Configural Invariance:
______________________________________________
- free item loadings
- free intercepts
- free residuals (group 2)
Number of parameters for configural model = 108
- 16 item loadings (`8 freeloadings * 2 groups = 16`)
- 56 intercepts (`36 * 2groups - 16 fixed = 56`)
- 12 residual variances
- 12 factor co-variances
- 04 factor means
- 08 factor variances
______________________________________________
```{r, eval=FALSE}
m.step4 <- mplusObject(
TITLE = " WSL FACTOR ANALYSIS - CONFIGURAL",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
grouping = X1SEX (1=MALE 2=FEMALE);",
ANALYSIS = "estimator=wlsmv; parameterization=theta;",
MODEL =
"SCI_ID BY S1SPERS1@1 S1SPERS2;
SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
! Threshold 1 for each item is constrained across groups1
! One additional threshold constrained across groups for
! the identification variable (ULI).
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3] ;
[S1SPERS2$1](f1_21); [S1SPERS2$2] ; [S1SPERS2$3] ;
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3] ;
[S1SUSECL$1](f2_21); [S1SUSECL$2] ; [S1SUSECL$3] ;
[S1SUSEJO$1](f2_31); [S1SUSEJO$2] ; [S1SUSEJO$3] ;
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3] ;
[S1STEXTB$1](f3_21); [S1STEXTB$2] ; [S1STEXTB$3] ;
[S1SSKILL$1](f3_31); [S1SSKILL$2] ; [S1SSKILL$3] ;
[S1SASSEX$1](f3_41); [S1SASSEX$2] ; [S1SASSEX$3] ;
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3] ;
[S1SWASTE$1](f4_21); [S1SWASTE$2] ; [S1SWASTE$3] ;
[S1SBORIN$1](f4_31); [S1SBORIN$2] ; [S1SBORIN$3] ;
[SCI_ID-SCI_INT@0]; ! factor means are fixed to 0 in group 1
SCI_ID-SCI_INT; ! factor variances are free in group 1
S1SPERS1-S1SBORIN@1; ! residual variances are fixed to 1 in group 1
MODEL FEMALE:
SCI_ID BY S1SPERS1@1 S1SPERS2;
SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3] ;
[S1SPERS2$1](f1_21); [S1SPERS2$2] ; [S1SPERS2$3] ;
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3] ;
[S1SUSECL$1](f2_21); [S1SUSECL$2] ; [S1SUSECL$3] ;
[S1SUSEJO$1](f2_31); [S1SUSEJO$2] ; [S1SUSEJO$3] ;
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3] ;
[S1STEXTB$1](f3_21); [S1STEXTB$2] ; [S1STEXTB$3] ;
[S1SSKILL$1](f3_31); [S1SSKILL$2] ; [S1SSKILL$3] ;
[S1SASSEX$1](f3_41); [S1SASSEX$2] ; [S1SASSEX$3] ;
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3] ;
[S1SWASTE$1](f4_21); [S1SWASTE$2] ; [S1SWASTE$3] ;
[S1SBORIN$1](f4_31); [S1SBORIN$2] ; [S1SBORIN$3] ;
[SCI_ID-SCI_INT]; ! factor means are free in group 2
SCI_ID-SCI_INT; ! factor variances are free in group 2
S1SPERS1-S1SBORIN; ! residual variances are free in group 2
savedata: DIFFTEST=diff_1_2.dat;",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step4.fit <- mplusModeler(m.step4,
dataout=here("10-wls", "wls_invar", "m4_configural_wls.dat"),
modelout=here("10-wls", "wls_invar", "m4_configural_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
______________________________________________
## Model 5 - Metric Invariance:
______________________________________________
- item loadings (set to equal)
- free intercepts
- free residuals (group 2)
Number of parameters for metric model = 100
- 08 item loadings (8 free loadings)
- 56 intercepts (36*2groups - 16 fixed = 56)
- 12 residual variances
- 12 factor co-variances
- 04 factor means
- 08 factor variances
______________________________________________
```{r, eval=FALSE}
m.step5 <- mplusObject(
TITLE = "WSL FACTOR ANALYSIS - METRIC",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
grouping = X1SEX (1=MALE 2=FEMALE);",
ANALYSIS = "estimator=wlsmv; parameterization=theta;",
MODEL =
"SCI_ID BY S1SPERS1@1 S1SPERS2;
SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
! threshold 1 for each item is constrained across groups
! one additional threshold constrained across groups for the identification variable (ULI)
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3] ;
[S1SPERS2$1](f1_21); [S1SPERS2$2] ; [S1SPERS2$3] ;
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3] ;
[S1SUSECL$1](f2_21); [S1SUSECL$2] ; [S1SUSECL$3] ;
[S1SUSEJO$1](f2_31); [S1SUSEJO$2] ; [S1SUSEJO$3] ;
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3] ;
[S1STEXTB$1](f3_21); [S1STEXTB$2] ; [S1STEXTB$3] ;
[S1SSKILL$1](f3_31); [S1SSKILL$2] ; [S1SSKILL$3] ;
[S1SASSEX$1](f3_41); [S1SASSEX$2] ; [S1SASSEX$3] ;
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3] ;
[S1SWASTE$1](f4_21); [S1SWASTE$2] ; [S1SWASTE$3] ;
[S1SBORIN$1](f4_31); [S1SBORIN$2] ; [S1SBORIN$3] ;
[SCI_ID-SCI_INT@0]; ! factor means are fixed to 0 in group 1
SCI_ID-SCI_INT; ! factor variances are free in group 1
S1SPERS1-S1SBORIN@1; ! residual variances are fixed to 1 in group 1
MODEL FEMALE:
! ***NEW TO METRIC INV*** (comment out to hold loadings equal)
!SCI_ID BY S1SPERS1@1 S1SPERS2;
!SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
!SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
!SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3] ;
[S1SPERS2$1](f1_21); [S1SPERS2$2] ; [S1SPERS2$3] ;
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3] ;
[S1SUSECL$1](f2_21); [S1SUSECL$2] ; [S1SUSECL$3] ;
[S1SUSEJO$1](f2_31); [S1SUSEJO$2] ; [S1SUSEJO$3] ;
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3] ;
[S1STEXTB$1](f3_21); [S1STEXTB$2] ; [S1STEXTB$3] ;
[S1SSKILL$1](f3_31); [S1SSKILL$2] ; [S1SSKILL$3] ;
[S1SASSEX$1](f3_41); [S1SASSEX$2] ; [S1SASSEX$3] ;
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3] ;
[S1SWASTE$1](f4_21); [S1SWASTE$2] ; [S1SWASTE$3] ;
[S1SBORIN$1](f4_31); [S1SBORIN$2] ; [S1SBORIN$3] ;
[SCI_ID-SCI_INT]; ! factor means are free in group 2
SCI_ID-SCI_INT; ! factor variances are free in group 2
S1SPERS1-S1SBORIN; ! residual variances are free in group 2
savedata: DIFFTEST=diff_2_3.dat;",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step5.fit <- mplusModeler(m.step5,
dataout=here("10-wls", "wls_invar", "m5_metric_wls.dat"),
modelout=here("10-wls", "wls_invar", "m5_metric_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
______________________________________________
## Model 6 - Scalar Invariance:
______________________________________________
- item loadings (set to equal)
- intercepts (set to equal)
- free residuals (group 2)
Number of parameters for metric model = 80
- 08 item loadings (8 free loadings)
- 36 intercepts
- 12 residual variances
- 12 factor co-variances
- 04 factor means
- 08 factor variances
______________________________________________
```{r, eval=FALSE}
m.step6 <- mplusObject(
TITLE = "WSL FACTOR ANALYSIS - SCALAR",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
grouping = X1SEX (1=MALE 2=FEMALE);",
ANALYSIS = "estimator=wlsmv; parameterization=theta;",
MODEL =
"SCI_ID BY S1SPERS1@1 S1SPERS2;
SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
! ***NEW TO SCALAR INV*** (all thresholds set to equality)
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3](f1_13);
[S1SPERS2$1](f1_21); [S1SPERS2$2](f1_22); [S1SPERS2$3](f1_23);
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3](f2_13);
[S1SUSECL$1](f2_21); [S1SUSECL$2](f2_22); [S1SUSECL$3](f2_23);
[S1SUSEJO$1](f2_31); [S1SUSEJO$2](f2_32); [S1SUSEJO$3](f2_33);
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3](f3_13);
[S1STEXTB$1](f3_21); [S1STEXTB$2](f3_22); [S1STEXTB$3](f3_23);
[S1SSKILL$1](f3_31); [S1SSKILL$2](f3_32); [S1SSKILL$3](f3_33);
[S1SASSEX$1](f3_41); [S1SASSEX$2](f3_42); [S1SASSEX$3](f3_43);
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3](f4_13);
[S1SWASTE$1](f4_21); [S1SWASTE$2](f4_22); [S1SWASTE$3](f4_23);
[S1SBORIN$1](f4_31); [S1SBORIN$2](f4_32); [S1SBORIN$3](f4_33);
[SCI_ID-SCI_INT@0]; ! factor means are fixed to 0 in group 1
SCI_ID-SCI_INT; ! factor variances are free in group 1
S1SPERS1-S1SBORIN@1; ! residual variances are fixed to 1 in group 1
MODEL FEMALE:
!SCI_ID BY S1SPERS1@1 S1SPERS2;
!SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
!SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
!SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
! ***NEW TO SCALAR INV*** (all thresholds set to equality)
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3](f1_13);
[S1SPERS2$1](f1_21); [S1SPERS2$2](f1_22); [S1SPERS2$3](f1_23);
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3](f2_13);
[S1SUSECL$1](f2_21); [S1SUSECL$2](f2_22); [S1SUSECL$3](f2_23);
[S1SUSEJO$1](f2_31); [S1SUSEJO$2](f2_32); [S1SUSEJO$3](f2_33);
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3](f3_13);
[S1STEXTB$1](f3_21); [S1STEXTB$2](f3_22); [S1STEXTB$3](f3_23);
[S1SSKILL$1](f3_31); [S1SSKILL$2](f3_32); [S1SSKILL$3](f3_33);
[S1SASSEX$1](f3_41); [S1SASSEX$2](f3_42); [S1SASSEX$3](f3_43);
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3](f4_13);
[S1SWASTE$1](f4_21); [S1SWASTE$2](f4_22); [S1SWASTE$3](f4_23);
[S1SBORIN$1](f4_31); [S1SBORIN$2](f4_32); [S1SBORIN$3](f4_33);
[SCI_ID-SCI_INT]; ! factor means are free in group 2
SCI_ID-SCI_INT; ! factor variances are free in group 2
S1SPERS1-S1SBORIN; ! residual variances are free in group 2
savedata: DIFFTEST=diff_3_4.dat;",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step6.fit <- mplusModeler(m.step6,
dataout=here("10-wls", "wls_invar", "m6_scalar_wls.dat"),
modelout=here("10-wls", "wls_invar", "m6_scalar_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
______________________________________________
## Model 7 - Strict Invariance:
______________________________________________
- item loadings (set to equal)
- intercepts (set to equal)
- residuals (fixed to 1)
Number of parameters for metric model = 68
- 08 item loadings (8 free loadings)
- 36 intercepts
- 00 residual variances
- 12 factor co-variances
- 04 factor means
- 08 factor variances
______________________________________________
```{r, eval=FALSE}
m.step7 <- mplusObject(
TITLE = "WSL FACTOR ANALYSIS - STRICT",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
grouping = X1SEX (1=MALE 2=FEMALE);",
ANALYSIS = "estimator=wlsmv; parameterization=theta;",
MODEL =
"SCI_ID BY S1SPERS1@1 S1SPERS2;
SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3](f1_13);
[S1SPERS2$1](f1_21); [S1SPERS2$2](f1_22); [S1SPERS2$3](f1_23);
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3](f2_13);
[S1SUSECL$1](f2_21); [S1SUSECL$2](f2_22); [S1SUSECL$3](f2_23);
[S1SUSEJO$1](f2_31); [S1SUSEJO$2](f2_32); [S1SUSEJO$3](f2_33);
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3](f3_13);
[S1STEXTB$1](f3_21); [S1STEXTB$2](f3_22); [S1STEXTB$3](f3_23);
[S1SSKILL$1](f3_31); [S1SSKILL$2](f3_32); [S1SSKILL$3](f3_33);
[S1SASSEX$1](f3_41); [S1SASSEX$2](f3_42); [S1SASSEX$3](f3_43);
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3](f4_13);
[S1SWASTE$1](f4_21); [S1SWASTE$2](f4_22); [S1SWASTE$3](f4_23);
[S1SBORIN$1](f4_31); [S1SBORIN$2](f4_32); [S1SBORIN$3](f4_33);
[SCI_ID-SCI_INT@0]; ! factor means are fixed to 0 in group 1
SCI_ID-SCI_INT; ! factor variances are free in group 1
S1SPERS1-S1SBORIN@1; ! residual variances are fixed to 1 in group 1
MODEL FEMALE:
!SCI_ID BY S1SPERS1@1 S1SPERS2;
!SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
!SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
!SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3](f1_13);
[S1SPERS2$1](f1_21); [S1SPERS2$2](f1_22); [S1SPERS2$3](f1_23);
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3](f2_13);
[S1SUSECL$1](f2_21); [S1SUSECL$2](f2_22); [S1SUSECL$3](f2_23);
[S1SUSEJO$1](f2_31); [S1SUSEJO$2](f2_32); [S1SUSEJO$3](f2_33);
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3](f3_13);
[S1STEXTB$1](f3_21); [S1STEXTB$2](f3_22); [S1STEXTB$3](f3_23);
[S1SSKILL$1](f3_31); [S1SSKILL$2](f3_32); [S1SSKILL$3](f3_33);
[S1SASSEX$1](f3_41); [S1SASSEX$2](f3_42); [S1SASSEX$3](f3_43);
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3](f4_13);
[S1SWASTE$1](f4_21); [S1SWASTE$2](f4_22); [S1SWASTE$3](f4_23);
[S1SBORIN$1](f4_31); [S1SBORIN$2](f4_32); [S1SBORIN$3](f4_33);
[SCI_ID-SCI_INT]; ! factor means are free in group 2
SCI_ID-SCI_INT; ! factor variances are free in group 2
! ***NEW TO STRICT INV*** (all residual variances fixed to 1 in group 2)
S1SPERS1-S1SBORIN@1; ! residual variances are fixed in group 2
savedata: DIFFTEST=diff_4_5.dat;",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step7.fit <- mplusModeler(m.step7,
dataout=here("10-wls", "wls_invar", "m7_strict_wls.dat"),
modelout=here("10-wls", "wls_invar", "m7_strict_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
______________________________________________
## Model 8 - Structural Invariance (using scalar model):
______________________________________________
- item loadings (set to equal)
- intercepts (set to equal)
- free residuals (in group 2)
- **factor variances (set to equal)**
- **factor covariances (set to equal)**
Number of parameters for metric model = 70
- 08 item loadings (8 free loadings)
- 36 intercepts
- 12 residual variances
- 06 factor co-variances (set to equal)
- 04 factor means
- 04 factor variances (set to equal)
______________________________________________
```{r, eval=FALSE}
m.step8 <- mplusObject(
TITLE = "WSL FACTOR ANALYSIS - STRICT",
VARIABLE =
"usevar =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
categorical =
S1SPERS1 S1SPERS2 S1SUSELI S1SUSECL
S1SUSEJO S1STESTS S1STEXTB S1SSKILL
S1SASSEX S1SENJNG S1SWASTE S1SBORIN;
grouping = X1SEX (1=MALE 2=FEMALE);",
ANALYSIS = "estimator=wlsmv; parameterization=theta;",
MODEL =
"SCI_ID BY S1SPERS1@1 S1SPERS2;
SCI_UT BY S1SUSELI@1 S1SUSECL S1SUSEJO;
SCI_EFF BY S1STESTS@1 S1STEXTB S1SSKILL S1SASSEX ;
SCI_INT BY S1SENJNG@1 S1SWASTE S1SBORIN;
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3](f1_13);
[S1SPERS2$1](f1_21); [S1SPERS2$2](f1_22); [S1SPERS2$3](f1_23);
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3](f2_13);
[S1SUSECL$1](f2_21); [S1SUSECL$2](f2_22); [S1SUSECL$3](f2_23);
[S1SUSEJO$1](f2_31); [S1SUSEJO$2](f2_32); [S1SUSEJO$3](f2_33);
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3](f3_13);
[S1STEXTB$1](f3_21); [S1STEXTB$2](f3_22); [S1STEXTB$3](f3_23);
[S1SSKILL$1](f3_31); [S1SSKILL$2](f3_32); [S1SSKILL$3](f3_33);
[S1SASSEX$1](f3_41); [S1SASSEX$2](f3_42); [S1SASSEX$3](f3_43);
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3](f4_13);
[S1SWASTE$1](f4_21); [S1SWASTE$2](f4_22); [S1SWASTE$3](f4_23);
[S1SBORIN$1](f4_31); [S1SBORIN$2](f4_32); [S1SBORIN$3](f4_33);
[SCI_ID-SCI_INT@0]; ! factor means are fixed to 0 in group 1
SCI_ID-SCI_INT (V1-V4); ! factor variances (set to equality)
S1SPERS1-S1SBORIN@1; ! residual variances are fixed to 1 in group 1
! factor covariances set to equality
SCI_ID with SCI_UT-SCI_INT(C1-C3);
SCI_EFF with SCI_ID(C4);
SCI_EFF with SCI_UT(C5);
SCI_UT with SCI_ID (C6);
MODEL FEMALE:
[S1SPERS1$1](f1_11); [S1SPERS1$2](f1_12); [S1SPERS1$3](f1_13);
[S1SPERS2$1](f1_21); [S1SPERS2$2](f1_22); [S1SPERS2$3](f1_23);
[S1SUSELI$1](f2_11); [S1SUSELI$2](f2_12); [S1SUSELI$3](f2_13);
[S1SUSECL$1](f2_21); [S1SUSECL$2](f2_22); [S1SUSECL$3](f2_23);
[S1SUSEJO$1](f2_31); [S1SUSEJO$2](f2_32); [S1SUSEJO$3](f2_33);
[S1STESTS$1](f3_11); [S1STESTS$2](f3_12); [S1STESTS$3](f3_13);
[S1STEXTB$1](f3_21); [S1STEXTB$2](f3_22); [S1STEXTB$3](f3_23);
[S1SSKILL$1](f3_31); [S1SSKILL$2](f3_32); [S1SSKILL$3](f3_33);
[S1SASSEX$1](f3_41); [S1SASSEX$2](f3_42); [S1SASSEX$3](f3_43);
[S1SENJNG$1](f4_11); [S1SENJNG$2](f4_12); [S1SENJNG$3](f4_13);
[S1SWASTE$1](f4_21); [S1SWASTE$2](f4_22); [S1SWASTE$3](f4_23);
[S1SBORIN$1](f4_31); [S1SBORIN$2](f4_32); [S1SBORIN$3](f4_33);
[SCI_ID-SCI_INT]; ! factor means are free in group 2
SCI_ID-SCI_INT (V1-V4); ! factor variances (set to equality)
S1SPERS1-S1SBORIN; ! residual variances are free group 2
! factor covariances set to equality
SCI_ID with SCI_UT-SCI_INT(C1-C3);
SCI_EFF with SCI_ID(C4);
SCI_EFF with SCI_UT(C5);
SCI_UT with SCI_ID (C6); ",
usevariables = colnames(hsls_data),
rdata = hsls_data)
m.step8.fit <- mplusModeler(m.step8,
dataout=here("10-wls", "wls_invar", "m8_structural_wls.dat"),
modelout=here("10-wls", "wls_invar", "m8_structural_wls.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
______________________________________________
Mean differences: Females are...
`Means`
`SCI_ID -0.866 0.452 -1.914 0.056`
`SCI_UT 0.010 0.054 0.187 0.852`
`SCI_EFF -0.577 0.094 -6.149 0.000`
`SCI_INT -0.062 0.094 -0.656 0.512`
______________________________________________
## Create and customize tables using the package {`gt`}
______________________________________________
## Read into R summary of all models
```{r}
all_models <- readModels(here("10-wls", "wls_invar"), quiet = TRUE)
```
______________________________________________
## Extract relevant data and generate table
```{r}
invar_summary <- LatexSummaryTable(all_models,
keepCols=c("Filename", "Parameters","ChiSqM_Value", "CFI","TLI",
"SRMR", "RMSEA_Estimate", "RMSEA_90CI_LB", "RMSEA_90CI_UB"),
sortBy = "Filename")
model_table <- invar_summary %>%
gt() %>%
tab_header(
title = "Fit Indices", # Add a title
subtitle = md(" ")
) %>%
tab_options(
table.width = pct(80)
) %>%
tab_footnote(
footnote = "Data from HSLS 2009",
location = cells_title()
) %>%
data_color( # Update cell colors...
columns = vars(CFI), # ...for column
alpha = .5,
autocolor_text = FALSE,
colors = scales::col_numeric(
palette = c(
"yellow", "blue"), # choose color
domain = c(.89,.91)) # Scale endpoints (outside will be gray)
) %>%
cols_label(
Filename = "Model",
Parameters = "Par",
ChiSqM_Value = "ChiSq",
RMSEA_Estimate = "RMSEA",
RMSEA_90CI_LB = "Lower CI",
RMSEA_90CI_UB = "Upper CI")
model_table
```
______________________________________________
## Save table
```{r, eval=FALSE}
model_table %>% gtsave("model_fit_table_wls.png", path = here("10-wls", "figures"))
```
______________________________________________
## How to use `DIFFTEST`?
https://stats.idre.ucla.edu/mplus/faq/how-can-i-compute-a-chi-squared-test-for-nested-models-with-the-mlmv-or-wlsmv-estimators-difftest/
______________________________________________
## End
______________________________________________
## References
Hallquist, M. N., & Wiley, J. F. (2018). MplusAutomation: An R Package for Facilitating Large-Scale Latent Variable Analyses in Mplus. Structural equation modeling: a multidisciplinary journal, 25(4), 621-638.
Horst, A. (2020). Course & Workshop Materials. GitHub Repositories, https://https://allisonhorst.github.io/
Ingels, S. J., Pratt, D. J., Herget, D. R., Burns, L. J., Dever, J. A., Ottem, R., ... & Leinwand, S. (2011). High School Longitudinal Study of 2009 (HSLS: 09): Base-Year Data File Documentation. NCES 2011-328. National Center for Education Statistics.
Muthén, L.K. and Muthén, B.O. (1998-2017). Mplus User’s Guide. Eighth Edition. Los Angeles, CA: Muthén & Muthén
R Core Team (2017). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL http://www.R-project.org/
Wickham et al., (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686, https://doi.org/10.21105/joss.01686