-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path15-three-step.Rmd
More file actions
607 lines (454 loc) · 19 KB
/
Copy path15-three-step.Rmd
File metadata and controls
607 lines (454 loc) · 19 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
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
message = FALSE) #Here, I have made it so that when you knit your .rmd, warnings and messages will not show up in the html markdown.
library(extrafont)
loadfonts()
```
# Manual 3-Step Covariate and Distal
------------------------------------------------------------------------
**Data source:**
This example utilizes the public-use dataset, *The Longitudinal Survey of American Youth* (**LSAY**): [See documentation here](https://www.lsay.org/)
------------------------------------------------------------------------
## Load packages
```{r}
library(MplusAutomation)
library(tidyverse) #collection of R packages designed for data science
library(here) #helps with filepaths
library(janitor) #clean_names
library(gt) # create tables
library(cowplot) # a ggplot theme
library(DiagrammeR) # create path diagrams
library(glue) # allows us to paste expressions into R code
library(data.table) # used for `melt()` function
library(poLCA)
library(reshape2)
```
------------------------------------------------------------------------
This example uses Gender and Mother's Education as predictors of latent class membership and Math IRT scores as a distal outcome in a single model.
```{r, echo=FALSE, eval=TRUE, fig.align='center'}
grViz(" digraph lca_model {
# The `graph` statement - No editing needed
graph [layout = dot, overlap = true]
# Two `node` statements
# One for measured variables (box)
node [shape=box]
Enjoy Useful Logical Job Adult Female[label = 'Female\nMothEd'] MathScore;
# One for latent variables (circle)
node [shape=circle]
science [label=<Science <br/>Attitudes <br/>C<sub>k=4</sub>>];
# `edge` statements
edge [minlen = 2]
science -> {Enjoy Useful Logical Job Adult}
science -> MathScore [minlen = 4];
Female -> science [minlen = 4];
Female -> MathScore [minlen = 4];
{rank = same; science; Female; MathScore}
}
}")
```
------------------------------------------------------------------------
Application: Longitudinal Study of American Youth, **Science Attitudes**
```{r, eval=TRUE, echo=FALSE}
### Take a look at the indicators that compose the LCA ###
tribble(
~"Name", ~" Variable Description",
#----------|-------------|,
"enjoy" , "I enjoy math." ,
"useful" , "Math is useful in everyday problems." ,
"logical" , "Math helps a person think logically." ,
"job" , "It is important to know math to get a good job." ,
"adult" , "I will use math in many ways as an adult.",
"female" , "Self-reported student gender (0=Male, 1=Female).",
"math_irt" , "Standardized IRT math test score - 12th grade.",
"mothed" , "Level of education: (1) less than high school, (2) high school diploma, (3) some college, (4) 4-year college, and (5) an advanced degree."
) %>%
gt() %>%
tab_header(title = md("**LCA Indicators & Auxiliary Variables: Math Attitudes Example**"), subtitle = md(" ")) %>%
tab_row_group(group = "", rows = 1:5) %>%
tab_row_group(group = "Auxiliary Variables", rows = 6:8) %>%
row_group_order(groups = c("","Auxiliary Variables")) %>%
tab_options(column_labels.font.weight = "bold", row_group.font.weight = "bold")
```
------------------------------------------------------------------------
The data can be found in the `data` folder and is called `lsay_subset.csv`.
```{r}
lsay_data <- read_csv(here("three_step","data","lsay_subset.csv")) %>%
clean_names() %>% # make variable names lowercase
mutate(female = recode(gender, `1` = 0, `2` = 1)) # relabel values from 1,2 to 0,1
```
------------------------------------------------------------------------
## Descriptive Statistics
```{r}
dframe <- lsay_data %>%
pivot_longer(
c(enjoy, useful, logical, job, adult),
names_to = "Variable"
) %>%
group_by(Variable) %>%
summarise(
Count = sum(value == 1, na.rm = TRUE),
Total = n(),
.groups = "drop"
) %>%
mutate(`Proportion Endorsed` = round(Count / Total, 3)) %>%
dplyr::select(Variable, `Proportion Endorsed`, Count)
gt(dframe) %>%
tab_header(
title = md("**LCA Indicator Endorsement**"),
subtitle = md(" ")
) %>%
tab_options(
column_labels.font.weight = "bold",
row_group.font.weight = "bold"
)
```
Gender
```{r}
summary(factor(lsay_data$mothed))
```
Mother's Education
```{r}
summary(factor(lsay_data$female))
```
Math IRT Score
```{r}
summary(lsay_data$math_irt)
```
------------------------------------------------------------------------
## Manual ML Three-step
------------------------------------------------------------------------
### Step 1 - Class Enumeration w/ Auxiliary Specification
------------------------------------------------------------------------
This step is done after class enumeration (or after you have selected the best latent class model). In this example, the four class model was the best. Now, we re-estimate the four-class model using `optseed` for efficiency. The difference here is the `SAVEDATA` command, where I can save the posterior probabilities and the modal class assignment that will be used in steps two and three.
The `optseed` is associated with each loglikelihood replication found in the Mplus output created during the enumeration process. You can copy-past `optseed` to replicate that specific model (associated with the LL replications) for step 1. This will reproduce the same parameter estimates from the enumeration process.
```{r, eval = FALSE}
step1 <- mplusObject(
TITLE = "Step 1 - Three-Step using LSAL",
VARIABLE =
"categorical = enjoy useful logical job adult;
usevar = enjoy useful logical job adult;
classes = c(4);
auxiliary = ! list all potential covariates and distals here
female mothed ! covariate
math_irt; ! distal math test score in 12th grade ",
ANALYSIS =
"estimator = mlr;
type = mixture;
starts = 0;
optseed = 568405;",
SAVEDATA =
"File=savedata.dat;
Save=cprob;",
OUTPUT = "residual tech11 tech14",
PLOT =
"type = plot3;
series = enjoy-adult(*);",
usevariables = colnames(lsay_data),
rdata = lsay_data)
step1_fit <- mplusModeler(step1,
dataout=here("three_step", "manual_3step", "Step1.dat"),
modelout=here("three_step", "manual_3step", "one.inp") ,
check=TRUE, run = TRUE, hashfilename = FALSE)
```
------------------------------------------------------------------------
```{r, out.width="90%"}
source(here("functions", "plot_lca.R"))
output_lsay <- readModels(here("three_step", "manual_3step","one.out"))
plot_lca(model_name = output_lsay)
```
------------------------------------------------------------------------
### Step 2 - Determine Measurement Error
------------------------------------------------------------------------
Extract logits for the classification probabilities for the most likely latent class
```{r}
logit_cprobs <- as.data.frame(output_lsay[["class_counts"]]
[["logitProbs.mostLikely"]])
```
Extract saved dataset which is part of the mplusObject "step1_fit"
```{r}
savedata <- as.data.frame(output_lsay[["savedata"]])
```
Rename the column in savedata named "C" and change to "N"
```{r}
colnames(savedata)[colnames(savedata)=="C"] <- "N"
```
------------------------------------------------------------------------
### Step 3 - LCA Auxiliary Variable Model with 2 covariates and 1 distal outcome
------------------------------------------------------------------------
Model with 2 covariates (gender and mother's education) and 1 distal outcome (math IRT scores)
```{r, cache = TRUE}
step3 <- mplusObject(
TITLE = "Step3 - 3step LSAY",
VARIABLE =
"nominal=N;
usevar = n;
classes = c(4);
usevar = female mothed math_irt;" ,
ANALYSIS =
"estimator = mlr;
type = mixture;
starts = 0;",
DEFINE =
"center female mothed (grandmean);",
MODEL =
glue(
" %OVERALL%
math_irt on female mothed; ! covariate as a related to the distal outcome
C on female (f1-f3);
c on mothed (e1-e3); ! covariate as predictor of C
%C#1%
[n#1@{logit_cprobs[1,1]}]; ! MUST EDIT if you do not have a 4-class model.
[n#2@{logit_cprobs[1,2]}];
[n#3@{logit_cprobs[1,3]}];
[math_irt](m1); ! conditional distal mean
math_irt; ! conditional distal variance (freely estimated)
%C#2%
[n#1@{logit_cprobs[2,1]}];
[n#2@{logit_cprobs[2,2]}];
[n#3@{logit_cprobs[2,3]}];
[math_irt](m2);
math_irt;
%C#3%
[n#1@{logit_cprobs[3,1]}];
[n#2@{logit_cprobs[3,2]}];
[n#3@{logit_cprobs[3,3]}];
[math_irt](m3);
math_irt;
%C#4%
[n#1@{logit_cprobs[4,1]}];
[n#2@{logit_cprobs[4,2]}];
[n#3@{logit_cprobs[4,3]}];
[math_irt](m4);
math_irt; "),
MODELCONSTRAINT =
"New (diff12 diff13 diff23
diff14 diff24 diff34
d_fem_12 d_fem_13
d_fem_23
d_ed_12 d_ed_13
d_ed_23
);
diff12 = m1-m2; ! test pairwise distal mean differences
diff13 = m1-m3;
diff23 = m2-m3;
diff14 = m1-m4;
diff24 = m2-m4;
diff34 = m3-m4;
d_fem_12 = f1-f2;
d_fem_13 = f1-f3;
d_fem_23 = f2-f3;
d_ed_12 = e1-e2;
d_ed_13 = e1-e3;
d_ed_23 = e2-e3;
",
MODELTEST = " ! omnibus test of distal means
! m1=m2;
! m2=m3;
! m3=m4;
! f1=f2; ! omnibus test of covariate logits (female)
! f1=f3;
e1=e2; ! omnibus test of covariate logits (mothers ed)
e1=e3;
",
usevariables = colnames(savedata),
rdata = savedata)
step3_fit <- mplusModeler(step3,
dataout=here("three_step", "manual_3step", "Step3.dat"),
modelout=here("three_step", "manual_3step", "three.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
------------------------------------------------------------------------
#### Wald Test Table
This is testing if there is a relation between the latent class variable and the distal outcome (mathirt)
```{r}
modelParams <- readModels(here("three_step", "manual_3step", "three.out"))
# Extract information as data frame
wald <- as.data.frame(modelParams[["summaries"]]) %>%
dplyr::select(WaldChiSq_Value:WaldChiSq_PValue) %>%
mutate(WaldChiSq_DF = paste0("(", WaldChiSq_DF, ")")) %>%
unite(wald_test, WaldChiSq_Value, WaldChiSq_DF, sep = " ") %>%
rename(pval = WaldChiSq_PValue) %>%
mutate(pval = ifelse(pval<0.001, paste0("<.001*"),
ifelse(pval<0.05, paste0(scales::number(pval, accuracy = .001), "*"),
scales::number(pval, accuracy = .001))))
# Create table
wald_table <- wald %>%
gt() %>%
tab_header(
title = "Wald Test Distal Means (Math IRT Scores)") %>%
cols_label(
wald_test = md("Wald Test (*df*)"),
pval = md("*p*-value")) %>%
cols_align(align = "center") %>%
opt_align_table_header(align = "left") %>%
gt::tab_options(table.font.names = "serif")
wald_table
```
Save figure
```{r}
gtsave(wald_table, here("figures","wald_table.docx"))
```
------------------------------------------------------------------------
#### Table of Pairwise Distal Outcome Differences
```{r}
modelParams <- readModels(here("three_step", "manual_3step", "three.out"))
# Extract information as data frame
diff <- as.data.frame(modelParams[["parameters"]][["unstandardized"]]) %>%
filter(grepl("DIFF", param)) %>%
dplyr::select(param:pval) %>%
mutate(se = paste0("(", format(round(se,2), nsmall =2), ")")) %>%
unite(estimate, est, se, sep = " ") %>%
mutate(param = str_remove(param, "DIFF"),
param = as.numeric(param)) %>%
separate(param, into = paste0("Group", 1:2), sep = 1) %>%
mutate(class = paste0("Class ", Group1, " vs ", Group2)) %>%
dplyr::select(class, estimate, pval) %>%
mutate(pval = ifelse(pval<0.001, paste0("<.001*"),
ifelse(pval<0.05, paste0(scales::number(pval, accuracy = .001), "*"),
scales::number(pval, accuracy = .001))))
# Create table
diff %>%
gt() %>%
tab_header(
title = "Distal Outcome Differences") %>%
cols_label(
class = "Class",
estimate = md("Mean (*se*)"),
pval = md("*p*-value")) %>%
sub_missing(1:3,
missing_text = "") %>%
cols_align(align = "center") %>%
opt_align_table_header(align = "left") %>%
gt::tab_options(table.font.names = "serif")
```
------------------------------------------------------------------------
#### Plot Distal Outcome Means
```{r}
modelParams <- readModels(here("three_step", "manual_3step", "three.out"))
# Extract class size
c_size <- as.data.frame(modelParams[["class_counts"]][["modelEstimated"]][["proportion"]]) %>%
rename("cs" = 1) %>%
mutate(cs = round(cs*100, 2))
c_size_val <- paste0("C", 1:nrow(c_size), glue(" ({c_size[1:nrow(c_size),]}%)"))
# Extract information as data frame
estimates <- as.data.frame(modelParams[["parameters"]][["unstandardized"]]) %>%
filter(paramHeader == "Intercepts") %>%
dplyr::select(param, est, se) %>%
filter(param == "MATH_IRT") %>%
mutate(across(c(est, se), as.numeric)) %>%
mutate(LatentClass = c_size_val)
# Add labels (NOTE: You must change the labels to match the significance testing!!)
#value_labels <- paste0(estimates$est, c("a"," bc"," abd"," cd"))
estimates$LatentClass <- fct_inorder(estimates$LatentClass)
# Plot bar graphs
estimates %>%
ggplot(aes(x=LatentClass, y = est, fill = LatentClass)) +
geom_col(position = "dodge", stat = "identity", color = "black") +
geom_errorbar(aes(ymin=est-se, ymax=est+se),
size=.3, # Thinner lines
width=.2,
position=position_dodge(.9)) +
geom_text(aes(label = est),
family = "serif", size = 4,
position=position_dodge(.9),
vjust = 8) +
# scale_fill_grey(start = .4, end = .7) + # Remove for colorful bars
labs(y="Math Scores", x="") +
theme_cowplot() +
theme(text = element_text(family = "serif", size = 15),
axis.text.x = element_text(size=15),
legend.position="none")
# Save plot
ggsave(here("figures","ManualDistal_Plot.jpeg"),
dpi=300, width=10, height = 7, units="in")
```
------------------------------------------------------------------------
#### Covariates Relations
As of 1/1/2026, this code is outdated. See here: https://github.com/michaelhallquist/MplusAutomation/issues/228
```{r, eval = FALSE}
modelParams <- readModels(here("three_step", "manual_3step", "three.out"))
# Extract information as data frame
cov <- as.data.frame(modelParams[["parameters"]][["unstandardized"]]) %>%
filter(str_detect(paramHeader, "^C#\\d+\\.ON$")) %>%
mutate(param = str_replace(param, "FEMALE", "Gender")) %>% # Change this to your own covariates
mutate(param = str_replace(param, "MOTHED", "Mother's Education")) %>%
mutate(est = format(round(est, 3), nsmall = 3),
se = round(se, 2),
pval = round(pval, 3)) %>%
mutate(latent_class = str_replace(paramHeader, "^C#(\\d+)\\.ON$", "Class \\1")) %>%
dplyr::select(param, est, se, pval, latent_class) %>%
mutate(se = paste0("(", format(round(se,2), nsmall =2), ")")) %>%
unite(logit, est, se, sep = " ") %>%
dplyr::select(param, logit, pval, latent_class) %>%
mutate(pval = ifelse(pval<0.001, paste0("<.001*"),
ifelse(pval<0.05, paste0(scales::number(pval, accuracy = .001), "*"),
scales::number(pval, accuracy = .001))))
or <- as.data.frame(modelParams[["parameters"]][["odds"]]) %>%
filter(str_detect(paramHeader, "^C#\\d+\\.ON$")) %>%
mutate(param = str_replace(param, "FEMALE", "Gender")) %>% # Change this to your own covariates
mutate(param = str_replace(param, "MOTHED", "Mother's Education")) %>%
mutate(est = format(round(est, 3), nsmall = 3)) %>%
mutate(latent_class = str_replace(paramHeader, "^C#(\\d+)\\.ON$", "Class \\1")) %>%
mutate(CI = paste0("[", format(round(lower_2.5ci, 3), nsmall = 3), ", ", format(round(upper_2.5ci, 3), nsmall = 3), "]")) %>%
dplyr::select(param, est, CI, latent_class) %>%
rename(or = est)
combined <- or %>%
full_join(cov) %>%
dplyr::select(param, latent_class, logit, pval, or, CI)
# Create table
combined %>%
gt(groupname_col = "latent_class", rowname_col = "param") %>%
tab_header(
title = "Predictors of Class Membership") %>%
cols_label(
logit = md("Logit (*se*)"),
or = md("Odds Ratio"),
CI = md("95% CI"),
pval = md("*p*-value")) %>%
sub_missing(1:3,
missing_text = "") %>%
sub_values(values = c("999.000"), replacement = "-") %>%
cols_align(align = "center") %>%
opt_align_table_header(align = "left") %>%
gt::tab_options(table.font.names = "serif") %>%
tab_footnote(
footnote = "Reference Class: 4",
locations = cells_title(groups = "title")
)
```
------------------------------------------------------------------------
#### Distal outcome regressed on the covariate
Is there a relation between the distal outcome (Math IRT Scores) and the covariate (Gender)?
```{r}
modelParams <- readModels(here("three_step", "manual_3step", "three.out"))
# Extract information as data frame
donx <- as.data.frame(modelParams[["parameters"]][["unstandardized"]]) %>%
filter(param %in% c("FEMALE", "MOTHED")) %>%
mutate(param = str_replace(param, "FEMALE", "Gender")) %>%
mutate(param = str_replace(param, "MOTHED", "Mother's Education")) %>%
mutate(LatentClass = sub("^","Class ", LatentClass)) %>%
dplyr::select(!paramHeader) %>%
mutate(se = paste0("(", format(round(se,2), nsmall =2), ")")) %>%
unite(estimate, est, se, sep = " ") %>%
dplyr::select(param, estimate, pval) %>%
distinct(param, .keep_all=TRUE) %>%
mutate(pval = ifelse(pval<0.001, paste0("<.001*"),
ifelse(pval<0.05, paste0(scales::number(pval, accuracy = .001), "*"),
scales::number(pval, accuracy = .001))))
# Create table
donx %>%
gt(groupname_col = "LatentClass", rowname_col = "param") %>%
tab_header(
title = "Gender Predicting Math Scores") %>%
cols_label(
estimate = md("Estimate (*se*)"),
pval = md("*p*-value")) %>%
sub_missing(1:3,
missing_text = "") %>%
sub_values(values = c("999.000"), replacement = "-") %>%
cols_align(align = "center") %>%
opt_align_table_header(align = "left") %>%
gt::tab_options(table.font.names = "serif")
```
<div style="text-align: center;"><img src="images/ucsb_logo.png" width="75%" /></div>