-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab6-CFA-Roulette.Rmd
638 lines (473 loc) · 18.6 KB
/
Lab6-CFA-Roulette.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
---
title: "Lab 6 - CFA *Roulette*"
author: "Factor Analysis ED 216B - Instructor: Karen Nylund-Gibson"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document: default
pdf_document: default
subtitle: '**Adam Garber**'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE, message = FALSE, warning = FALSE, tidy = TRUE)
library(here)
```
```{r, echo=FALSE, eval=TRUE, out.width = "70%", out.height= "70%", fig.align = "center"}
knitr::include_graphics(here("figures", "wheel.png"))
```
## ____________________________________
### Outline lab 6 - Factor Analysis Content
1. Unit loading identification (ULI)
2. Unit Variance identification (UVI)
3. Interpreting Residuals
4. Modification Indices
## ____________________________________
### CFA *Roulette* - rules of the game:
- Create a pool of items ordered based on similarity of relationship (correlation) using the hclust algorithm
- Split into 2 pools or clusters of items
- Spin the wheel: randomly choose 5 items from each pool (each of our CFA's will be different!)
- Use these 2 sets of items as the indicators in a 2 factor CFA
- Choose between 1 - 2 modifications from the mod indices to "improve" your model
- Upload your **final** model output file (.out) to GS portal. Use the following naming convention:
#### **firstname_lastinitial_cfa.out** e.g., **adam_g_cfa.out**
- Lastly, we will upload all models into a table and see who had the "best" model
- Let the best BIC wins!
## ____________________________________
### A visual way to understand the variance / covariance matrix
```{r, echo=FALSE, eval=TRUE, out.width = "60%", out.height= "60%", fig.align = "left"}
knitr::include_graphics(here("figures", "unrestrict_model.png"))
```
#### *Figure.* Picture adapted from {`OpenMx`} documentation.
```{r, echo=FALSE, eval=TRUE, out.width = "50%", out.height= "50%", fig.align = "left"}
knitr::include_graphics(here("figures", "tip_of_the_iceberg.jpg"))
```
#### *Figure.* Seeing the forest from the trees.
## ____________________________________
## Getting started - following the routine...
1. Create an R-Project
2. Install packages
3. Load packages
### R-Project instructions:
1. click "NEW PROJECT" (upper right corner of window)
2. choose option "NEW DIRECTORY"
3. choose location of project (on desktop OR in a designated class folder)
Within R-studio under the files pane (bottom right):
1. click "New Folder" and name folder "data"
2. click "New Folder" and name folder "class_mplus"
3. click "New Folder" and name folder "cfa_mplus"
# ____________________________________
## Lab 5 - Begin
# ____________________________________
DATA SOURCE: This lab exercise utilizes the NCES public-use dataset: Education Longitudinal Study of 2002 (Lauff & Ingels, 2014) [$\color{blue}{\text{See website: nces.ed.gov}}$](https://nces.ed.gov/surveys/els2002/avail_data.asp)
# ____________________________________
### loading packages...
```{r, eval=TRUE}
library(tidyverse)
library(MplusAutomation)
library(rhdf5)
library(here)
library(semPlot)
library(stargazer)
library(corrplot)
library(glue)
library(kableExtra)
library(beepr)
library(praise)
beep(2)
```
#### read in data
```{r}
lab_data <- read_csv(here("data", "els_sub4.csv"))
beep(1)
praise("You are totally ${adjective}! Super ${EXCLAMATION}!")
# praise(0) # picks a random sound
```
#### Subsetting all Ordinal type variables
```{r}
ordinal_data <- lab_data %>%
select(21:145)
beep(1)
praise()
```
### Order variables based on correlations & create 2 cluster item pools to pull from
```{r}
big_matrix <- cor(ordinal_data, use = "pairwise.complete.obs")
corrplot(big_matrix,
method = "color",
type = "upper",
order = "hclust",
addrect = 2,
tl.cex = .5, tl.col = "black")
order <- corrMatOrder(big_matrix, order="hclust")
order_data <- ordinal_data %>%
select(order)
clust1 <- order_data %>%
select(BYS89G:BYS86D)
clust2 <- order_data %>%
select(BYS87E:BYS38B)
cor_c1 <- cor(clust1, use = "pairwise.complete.obs")
corrplot(cor_c1,
method = "color",
type = "upper",
tl.col = "black")
cor_c2 <- cor(clust2, use = "pairwise.complete.obs")
corrplot(cor_c2,
method = "color",
type = "upper",
tl.col = "black")
beep(1)
praise()
```
# ____________________________________
### Try your luck! (select columns at random)
# ____________________________________
```{r}
# select 5 columns at random for factor1
# set.seed(*******) # setting a seed is optional, use to replicate same solution
roulette_1 <- clust1 %>%
select(sample(ncol(clust1), 5))
f1_vars <- colnames(roulette_1)
beep(1)
praise()
```
```{r}
# select 5 columns at random for factor2
roulette_2 <- clust2 %>%
select(sample(ncol(clust2), 5))
f2_vars <- colnames(roulette_2)
beep(1)
praise()
```
#### take a look at the items in roulette 1
```{r}
stargazer(as.data.frame(roulette_1), type="text", digits=1)
```
#### take a look at the items in roulette 2
```{r}
stargazer(as.data.frame(roulette_2), type="text", digits=1)
```
```{r, echo=FALSE, eval=TRUE, out.width = "80%", out.height= "80%", fig.pos="h"}
knitr::include_graphics(here("figures", "unit_id.png"))
```
#### *Figure*: Picture adapted from slide by Dr. Karen Nylund-Gibson
## ____________________________________
### CFA *Roulette*
## ____________________________________
```{r}
# DEFAULT: Unit Loading Identification (ULI)
cfa_ULI <- mplusObject(
TITLE = "CFA - ULI - LAB 6 DEMO",
VARIABLE =
glue(
"usevar =
{noquote(f1_vars[1])}
{noquote(f1_vars[2])}
{noquote(f1_vars[3])}
{noquote(f1_vars[4])}
{noquote(f1_vars[5])}
{noquote(f2_vars[1])}
{noquote(f2_vars[2])}
{noquote(f2_vars[3])}
{noquote(f2_vars[4])}
{noquote(f2_vars[5])}
;"),
ANALYSIS =
"estimator = mlr;",
MODEL =
glue(
"FACTOR_1 by
{noquote(f1_vars[1])}
{noquote(f1_vars[2])}
{noquote(f1_vars[3])}
{noquote(f1_vars[4])}
{noquote(f1_vars[5])};
FACTOR_2 by
{noquote(f2_vars[1])}
{noquote(f2_vars[2])}
{noquote(f2_vars[3])}
{noquote(f2_vars[4])}
{noquote(f2_vars[5])};") ,
PLOT = "type = plot3;",
OUTPUT = "sampstat standardized residual modindices (3.84);",
usevariables = colnames(order_data),
rdata = order_data)
cfa_ULI_fit <- mplusModeler(cfa_ULI,
dataout=here("cfa_mplus", "lab6_cfa_ULI.dat"),
modelout=here("cfa_mplus", "lab6_cfa_ULI.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
beep(1)
praise()
```
```{r}
# OVERRIDE DEFAULT: Unit Varianvce Identification
cfa_UVI <- mplusObject(
TITLE = "CFA - UVI - LAB 6 DEMO",
VARIABLE =
glue(
"usevar =
{noquote(f1_vars[1])}
{noquote(f1_vars[2])}
{noquote(f1_vars[3])}
{noquote(f1_vars[4])}
{noquote(f1_vars[5])}
{noquote(f2_vars[1])}
{noquote(f2_vars[2])}
{noquote(f2_vars[3])}
{noquote(f2_vars[4])}
{noquote(f2_vars[5])};" ),
ANALYSIS =
"estimator = mlr;",
MODEL =
glue(
"FACTOR_1 by
{noquote(f1_vars[1])}* !estimate first variable loading
{noquote(f1_vars[2])}
{noquote(f1_vars[3])}
{noquote(f1_vars[4])}
{noquote(f1_vars[5])};
FACTOR_1@1; !fix variance of factor to 1
FACTOR_2 by
{noquote(f2_vars[1])}*
{noquote(f2_vars[2])}
{noquote(f2_vars[3])}
{noquote(f2_vars[4])}
{noquote(f2_vars[5])};
FACTOR_2@1;" ) ,
PLOT = "type = plot3;",
OUTPUT = "sampstat standardized residual modindices (3.84);",
usevariables = colnames(order_data),
rdata = order_data)
cfa_UVI_fit <- mplusModeler(cfa_UVI,
dataout=here("cfa_mplus", "lab6_cfa_UVI.dat"),
modelout=here("cfa_mplus", "lab6_cfa_UVI.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
beep(1)
praise()
```
## ____________________________________
### Residual Output:
## ____________________________________
- The "output = residual;" option is used to request residuals for the observed variables in the analysis.
- Residuals are computed for the model estimated means/intercepts/thresholds and the model estimated covariances/correlations/residual correlations.
- Residuals are computed as the difference between the value of the observed sample statistic and its model estimated value.
- Standardized and normalized residuals are available for continuous outcomes with TYPE=GENERAL and maximum likelihood estimation.
- Standardized residuals are computed as the difference between the value of the observed sample statistic and its model estimated value divided by the standard deviation of the difference between the value of the observed sample statistic and its model estimated value. Standardized residuals are approximate z-scores.
- Normalized residuals are computed as the difference between the value of the observed sample statistic and its model estimated value divided by the standard deviation of the value of the observed sample statistic (Mplus 6 User’s Guide, p. 644).
```{r}
# Standardized Residuals (z-scores) for Covariances
#
# BYS86D BYS89M BYS89B BYS89H BYS90A
# ________ ________ ________ ________ ________
# BYS86D 999.000
# BYS89M 3.191 999.000
# BYS89B -2.207 0.157 999.000
# BYS89H 0.462 21.983 -0.213 999.000
# BYS90A 1.244 0.520 -1.780 -1.448 0.315
# BYS87E -2.402 -8.142 3.679 -4.084 -2.417
# BYS87C 2.602 6.951 999.000 999.000 0.143
# BYS21D -1.319 2.332 1.374 0.811 -0.302
# BYS20B -0.440 2.480 1.689 1.533 -1.888
# BYS20F 1.784 1.677 0.370 1.931 -1.238
#
# BYS87E BYS87C BYS21D BYS20B BYS20F
# ________ ________ ________ ________ ________
# BYS87E 0.320
# BYS87C 3.282 999.000
# BYS21D -1.831 -1.951 0.043
# BYS20B -3.563 -1.255 2.936 999.000
# BYS20F -1.944 -1.790 999.000 999.000 0.365
```
Each value can be mapped to a z-score distribution in which they can be interpreted as standard deviations from an ideal zero residual that signifying perfect reproduction of the variance-covariance matrix. Values above 1.96 or 2.00 indicates statistically significantly over- or under- estimation at a p<.05 level.
## ____________________________________
### Modification Indices:
## ____________________________________
```{r}
# M.I. E.P.C. Std E.P.C. StdYX E.P.C.
#
# BY Statements
#
# FACTOR_1 BY BYS87E 18.781 -2.612 -0.546 -0.603
# FACTOR_1 BY BYS87C 10.943 -1.988 -0.416 -0.500
# FACTOR_1 BY BYS20B 5.174 1.199 0.251 0.327
# FACTOR_1 BY BYS20F 7.830 1.676 0.351 0.497
# FACTOR_2 BY BYS89M 11.748 1.582 0.463 0.518
# FACTOR_2 BY BYS89B 7.505 -1.359 -0.398 -0.419
# FACTOR_2 BY BYS90A 4.788 -0.650 -0.190 -0.315
#
# WITH Statements
#
# BYS89M WITH BYS86D 4.507 0.050 0.050 0.115
# BYS89B WITH BYS86D 4.497 -0.053 -0.053 -0.115
# BYS87E WITH BYS89M 26.032 -0.169 -0.169 -0.261
# BYS87E WITH BYS89B 7.894 0.096 0.096 0.143
# BYS87C WITH BYS86D 6.548 0.053 0.053 0.124
# BYS87C WITH BYS89M 13.201 0.108 0.108 0.196
# BYS87C WITH BYS89B 60.860 -0.242 -0.242 -0.419
# BYS21D WITH BYS89M 4.395 0.053 0.053 0.107
# BYS21D WITH BYS87E 4.022 -0.054 -0.054 -0.095
# BYS21D WITH BYS87C 5.370 -0.057 -0.057 -0.117
# BYS20B WITH BYS89M 4.366 0.058 0.058 0.110
# BYS20B WITH BYS87E 11.120 -0.097 -0.097 -0.163
# BYS20F WITH BYS87E 4.211 -0.057 -0.057 -0.117
# BYS20F WITH BYS21D 10.269 0.066 0.066 0.175
# BYS20F WITH BYS20B 15.315 0.093 0.093 0.235
```
## ____________________________________
### Add a modification indice
#### Note: alter the modification following modification statement for your model
```{r}
cfa_mod1 <- mplusObject(
TITLE = "CFA - mod1 - LAB 6 DEMO",
VARIABLE =
glue(
"usevar =
{noquote(f1_vars[1])}
{noquote(f1_vars[2])}
{noquote(f1_vars[3])}
{noquote(f1_vars[4])}
{noquote(f1_vars[5])}
{noquote(f2_vars[1])}
{noquote(f2_vars[2])}
{noquote(f2_vars[3])}
{noquote(f2_vars[4])}
{noquote(f2_vars[5])};" ),
ANALYSIS =
"estimator = mlr;",
MODEL =
glue(
"FACTOR_1 by
{noquote(f1_vars[1])}* !estimate first variable loading
{noquote(f1_vars[2])}
{noquote(f1_vars[3])}
{noquote(f1_vars[4])}
{noquote(f1_vars[5])};
FACTOR_1@1; !fix variance of factor to 1
FACTOR_2 by
{noquote(f2_vars[1])}*
{noquote(f2_vars[2])}
{noquote(f2_vars[3])}
{noquote(f2_vars[4])}
{noquote(f2_vars[5])};
FACTOR_2@1;
! ****CHANGE TO REFLECT YOUR MODS****
BYS87C WITH BYS89B; !estimate residual correlation mod indice" ) ,
PLOT = "type = plot3;",
OUTPUT = "sampstat standardized residual modindices (3.84);",
usevariables = colnames(order_data),
rdata = order_data)
cfa_mod1_fit <- mplusModeler(cfa_mod1,
dataout=here("cfa_mplus", "lab6_cfa_mod1.dat"),
modelout=here("cfa_mplus", "lab6_cfa_mod1.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
beep(1)
praise()
```
#### Add a second modification from the mod indices statements
```{r}
cfa_mod2 <- mplusObject(
TITLE = "CFA - mod1 - LAB 6 DEMO",
VARIABLE =
glue(
"usevar =
{noquote(f1_vars[1])}
{noquote(f1_vars[2])}
{noquote(f1_vars[3])}
{noquote(f1_vars[4])}
{noquote(f1_vars[5])}
{noquote(f2_vars[1])}
{noquote(f2_vars[2])}
{noquote(f2_vars[3])}
{noquote(f2_vars[4])}
{noquote(f2_vars[5])};" ),
ANALYSIS =
"estimator = mlr;",
MODEL =
glue(
"FACTOR_1 by
{noquote(f1_vars[1])}* !estimate first variable loading
{noquote(f1_vars[2])}
{noquote(f1_vars[3])}
{noquote(f1_vars[4])}
{noquote(f1_vars[5])};
FACTOR_1@1; !fix variance of factor to 1
FACTOR_2 by
{noquote(f2_vars[1])}*
{noquote(f2_vars[2])}
{noquote(f2_vars[3])}
{noquote(f2_vars[4])}
{noquote(f2_vars[5])};
FACTOR_2@1;
! ****CHANGE TO REFLECT YOUR MODS****
BYS87E WITH BYS89M; !estimate residual correlation mod indice
BYS87C WITH BYS89B; ") ,
PLOT = "type = plot3;",
OUTPUT = "sampstat standardized residual modindices (3.84);",
usevariables = colnames(order_data),
rdata = order_data)
cfa_mod2_fit <- mplusModeler(cfa_mod2,
dataout=here("cfa_mplus", "lab6_cfa_mod2.dat"),
modelout=here("cfa_mplus", "lab6_cfa_mod2.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
beep(1)
praise()
```
## ____________________________________
### Collect class output files - upload to GauchoSpace portal
- Download 1 class .out file per person using the naming convention
- Read in all files & create a table
- The best BIC wins!
## ____________________________________
```{r}
best_models <- readModels(here("class_mplus"), recursive = TRUE)
best_table <- LatexSummaryTable(best_models,
keepCols=c("Filename",
"BIC"),
sortBy = "BIC")
best_table %>%
kable(booktabs = T, linesep = "") %>%
kable_styling(c("striped"),
full_width = F,
position = "left")
beep(1)
praise("${EXCLAMATION}!")
```
### Calculate Satora-Bentler scaled Chi-square difference test (use with MLR estimator)
#### [$\color{blue}{\text{See website: stats.idre.ucla.edu-mplus-faq-how-can-i-compute-a-chi-square-test-for-nested-models-with-the-mlr}}$](https://stats.idre.ucla.edu/mplus/faq/how-can-i-compute-a-chi-square-test-for-nested-models-with-the-mlr-or-mlm-estimators/)
- SB0 = null model Chi-square value
- SB1 = alternate model Chi-square value
- c0 = null model scaling correction factor
- c1 = alternate model scaling correction factor
- d0 = null model degrees of freedom
- d1 = alternate model degrees of freedom
- df = Chi-square test degrees of freedom
```{r}
# Identifying all the necessary variables
cfa_models <-readModels(here("cfa_mplus"))
SB0 <- cfa_models[["lab6_cfa_UVI.out"]][["summaries"]][["ChiSqM_Value"]]
SB1 <- cfa_models[["lab6_cfa_mod1.out"]][["summaries"]][["ChiSqM_Value"]]
c0 <- cfa_models[["lab6_cfa_UVI.out"]][["summaries"]][["ChiSqM_ScalingCorrection"]]
c1 <- cfa_models[["lab6_cfa_mod1.out"]][["summaries"]][["ChiSqM_ScalingCorrection"]]
d0 <- cfa_models[["lab6_cfa_UVI.out"]][["summaries"]][["ChiSqM_DF"]]
d1 <- cfa_models[["lab6_cfa_mod1.out"]][["summaries"]][["ChiSqM_DF"]]
df <- d0-d1
# Satora-Bentler scaled Difference test equations
cd <- (((d0*c0)-(d1*c1))/(d0-d1))
t <- (((SB0*c0)-(SB1*c1))/(cd))
# Chi-square and degrees of freedom
t
df
# Significance test
pchisq(t, df, lower.tail=FALSE)
```
## ____________________________________
### End of Lab 6
## ____________________________________
```{r, echo=FALSE, eval=TRUE, out.width = "30%", out.height= "30%", fig.align = "center"}
knitr::include_graphics(here("figures", "dice.jpg"))
```
## 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/
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
![](figures/UCSB_Navy_mark.png){ width=75% }