-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21-lta-estimation.Rmd
More file actions
322 lines (231 loc) · 9.9 KB
/
Copy path21-lta-estimation.Rmd
File metadata and controls
322 lines (231 loc) · 9.9 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
```{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.
```
# Measurement Invariance
--------------------------------------------------------------------------------------
**Data Source**: The data used to illustrate these analyses include elementary school student *Science Attitude* survey items collected during 7th and 10th grades from the **Longitudinal Study of American Youth** (LSAY; Miller, 2015).
--------------------------------------------------------------------------------------
To install package {`rhdf5`}
```{r}
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
#BiocManager::install("rhdf5")
```
*Load packages*
```{r, eval=TRUE}
library(MplusAutomation)
library(rhdf5)
library(tidyverse)
library(here)
library(glue)
library(janitor)
library(gt)
library(reshape2)
library(cowplot)
library(ggrepel)
library(haven)
library(modelsummary)
library(corrplot)
library(DiagrammeR)
library(filesstrings)
library(PNWColors)
```
--------------------------------------------------------------------------------------
Read in LSAY data file, `lsay_new.csv`.
```{r, eval=TRUE}
lsay_data <- read_csv(here("data","lsay_lta.csv"), na = c("9999")) %>%
mutate(across(everything(), as.numeric))
```
--------------------------------------------------------------------------------------
## Estimate Latent Transition Analysis (LTA) Model
--------------------------------------------------------------------------------------
### Estimate Invariant LTA Model
```{r, eval = FALSE}
lta_inv <- mplusObject(
TITLE =
"Invariant LTA",
VARIABLE =
"usev = ab39m ab39t ab39u ab39w ab39x ! 7th grade indicators
ga33a ga33h ga33i ga33k ga33l; ! 10th grade indicators
categorical = ab39m-ab39x ga33a-ga33l;
classes = c1(4) c2(4);",
ANALYSIS =
"estimator = mlr;
type = mixture;
starts = 500 100;
processors=10;",
MODEL =
"%overall%
c2 on c1;
MODEL c1:
%c1#1%
[AB39M$1-AB39X$1] (1-5); !!! labels that are repeated will constrain parameters to equality !!!
%c1#2%
[AB39M$1-AB39X$1] (6-10);
%c1#3%
[AB39M$1-AB39X$1] (11-15);
%c1#4%
[AB39M$1-AB39X$1] (16-20);
MODEL c2:
%c2#1%
[GA33A$1-GA33L$1] (1-5);
%c2#2%
[GA33A$1-GA33L$1] (6-10);
%c2#3%
[GA33A$1-GA33L$1] (11-15);
%c2#4%
[GA33A$1-GA33L$1] (16-20);",
SAVEDATA =
"file = LTA_Inv_CPROBS.dat;
save = cprob;
missflag = 9999;",
OUTPUT = "tech1 tech15 svalues;",
usevariables = colnames(lsay_data),
rdata = lsay_data)
lta_inv_fit <- mplusModeler(lta_inv,
dataout=here("lta","lta_model","lta.dat"),
modelout=here("lta","lta_model","4-class-invariant.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
--------------------------------------------------------------------------------------
### Estimate Non-Invariant Estimated LTA Model
```{r, eval = FALSE}
lta_non_inv <- mplusObject(
TITLE =
"Non-Invariant LTA",
VARIABLE =
"usev = ab39m ab39t ab39u ab39w ab39x ! 7th grade indicators
ga33a ga33h ga33i ga33k ga33l; ! 10th grade indicators
categorical = ab39m-ab39x ga33a-ga33l;
classes = c1(4) c2(4);",
ANALYSIS =
"estimator = mlr;
type = mixture;
starts = 500 100;
processors=10;",
MODEL =
"%overall%
c2 on c1; !!! estimate all multinomial logistic regressions !!!
!!! The above syntax can also be written as: !!!
! c2#1 on c1#1 c1#2 c1#3; !
! c2#2 on c1#1 c1#2 c1#3; !
! c2#3 on c1#1 c1#2 c1#3; !
MODEL c1: !!! the following syntax will allow item thresholds to be estimated for each class (e.g. noninvariance) !!!
%c1#1%
[AB39M$1-AB39X$1];
%c1#2%
[AB39M$1-AB39X$1];
%c1#3%
[AB39M$1-AB39X$1];
%c1#4%
[AB39M$1-AB39X$1];
MODEL c2:
%c2#1%
[GA33A$1-GA33L$1];
%c2#2%
[GA33A$1-GA33L$1];
%c2#3%
[GA33A$1-GA33L$1];
%c2#4%
[GA33A$1-GA33L$1];",
OUTPUT = "tech1 tech15 svalues;",
usevariables = colnames(lsay_data),
rdata = lsay_data)
lta_non_inv_fit <- mplusModeler(lta_non_inv,
dataout=here("lta","lta_model","lta.dat"),
modelout=here("lta","lta_model","4-class-non-invariant.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
------------------------------------------------------------------------
### Nested Model Testing: Sattorra-Bentler adjusted Log Likelihood Ratio Difference Testing
- non-invariant (comparison): This model has **more** parameters.
- invariant (nested): This model has **less** parameters.
```{r}
# *0 = null or nested model & *1 = comparison or parent model
lta_models <- readModels(here("lta","lta_model"), quiet = TRUE)
# Log Likelihood Values
L0 <- lta_models[["X4.class.invariant.out"]][["summaries"]][["LL"]]
L1 <- lta_models[["X4.class.non.invariant.out"]][["summaries"]][["LL"]]
# LRT equation
lr <- -2*(L0-L1)
# Parameters
p0 <- lta_models[["X4.class.invariant.out"]][["summaries"]][["Parameters"]]
p1 <- lta_models[["X4.class.non.invariant.out"]][["summaries"]][["Parameters"]]
# Scaling Correction Factors
c0 <- lta_models[["X4.class.invariant.out"]][["summaries"]][["LLCorrectionFactor"]]
c1 <- lta_models[["X4.class.non.invariant.out"]][["summaries"]][["LLCorrectionFactor"]]
# Difference Test Scaling correction
cd <- ((p0*c0)-(p1*c1))/(p0-p1)
# Chi-square difference test(TRd)
TRd <- (lr)/(cd)
# Degrees of freedom
df <- abs(p0 - p1)
# Significance test
(p_diff <- pchisq(TRd, df, lower.tail=FALSE))
```
**RESULT**: The Log Likelihood $\chi^2$ difference test comparing the invariant and non-invariant LTA models was, $\chi^2 (20) = 21.542, p = .624$.
[Reference](https://stats.idre.ucla.edu/mplus/faq/how-can-i-compute-a-chi-square-test-for-nested-models-with-the-mlr-or-mlm-estimators/)
#### Alternative: Nested Model Testing Using `compareModels` in MplusAutomation
```{r}
compareModels(lta_models$X4.class.invariant.out, lta_models$X4.class.non.invariant.out, show="all", diffTest=TRUE)
```
--------------------------------------------------------------------------------------
Read invariance model and extract parameters (intercepts and multinomial regression coefficients)
```{r, eval=TRUE}
lta_inv1 <- readModels(here("lta","lta_model","4-Class-Invariant.out" ), quiet = TRUE)
par <- as_tibble(lta_inv1[["parameters"]][["unstandardized"]]) %>%
select(1:3) %>%
filter(grepl('ON|Means', paramHeader)) %>%
mutate(est = as.numeric(est))
```
Manual method to calculate transition probabilities:
Although possible to extract transition probabilities directly from the output the following code illustrates how the parameters are used to calculate each transition. This is useful for conducting advanced LTA model specifications such as making specific constraints within or between transition matrices, or testing the equivalence of specific transition probabilities.
```{r, eval=TRUE}
# Name each parameter individually to make the subsequent calculations more readable
a1 <- unlist(par[13,3]); a2 <- unlist(par[14,3]); a3 <- unlist(par[15,3]); b11 <- unlist(par[1,3]);
b21 <- unlist(par[4,3]); b31 <- unlist(par[7,3]); b12 <- unlist(par[2,3]); b22 <- unlist(par[5,3]);
b32 <- unlist(par[8,3]); b13 <- unlist(par[3,3]); b23 <- unlist(par[6,3]); b33 <- unlist(par[9,3])
# Calculate transition probabilities from the logit parameters
t11 <- exp(a1+b11)/(exp(a1+b11)+exp(a2+b21)+exp(a3+b31)+exp(0))
t12 <- exp(a2+b21)/(exp(a1+b11)+exp(a2+b21)+exp(a3+b31)+exp(0))
t13 <- exp(a3+b31)/(exp(a1+b11)+exp(a2+b21)+exp(a3+b31)+exp(0))
t14 <- 1 - (t11 + t12 + t13)
t21 <- exp(a1+b12)/(exp(a1+b12)+exp(a2+b22)+exp(a3+b32)+exp(0))
t22 <- exp(a2+b22)/(exp(a1+b12)+exp(a2+b22)+exp(a3+b32)+exp(0))
t23 <- exp(a3+b32)/(exp(a1+b12)+exp(a2+b22)+exp(a3+b32)+exp(0))
t24 <- 1 - (t21 + t22 + t23)
t31 <- exp(a1+b13)/(exp(a1+b13)+exp(a2+b23)+exp(a3+b33)+exp(0))
t32 <- exp(a2+b23)/(exp(a1+b13)+exp(a2+b23)+exp(a3+b33)+exp(0))
t33 <- exp(a3+b33)/(exp(a1+b13)+exp(a2+b23)+exp(a3+b33)+exp(0))
t34 <- 1 - (t31 + t32 + t33)
t41 <- exp(a1)/(exp(a1)+exp(a2)+exp(a3)+exp(0))
t42 <- exp(a2)/(exp(a1)+exp(a2)+exp(a3)+exp(0))
t43 <- exp(a3)/(exp(a1)+exp(a2)+exp(a3)+exp(0))
t44 <- 1 - (t41 + t42 + t43)
```
### Create Transition Table
```{r, eval=TRUE}
t_matrix <- tibble(
"Time1" = c("C1=Anti-Science","C1=Amb. w/ Elevated","C1=Amb. w/ Minimal","C1=Pro-Science"),
"C2=Anti-Science" = c(t11,t21,t31,t41),
"C2=Amb. w/ Elevated" = c(t12,t22,t32,t42),
"C2=Amb. w/ Minimal" = c(t13,t23,t33,t43),
"C2=Pro-Science" = c(t14,t24,t34,t44))
t_matrix %>%
gt(rowname_col = "Time1") %>%
tab_stubhead(label = "7th grade") %>%
tab_header(
title = md("**Student transitions from 7th grade (rows) to 10th grade (columns)**"),
subtitle = md(" ")) %>%
fmt_number(2:5,decimals = 2) %>%
tab_spanner(label = "10th grade",columns = 2:5) %>%
tab_footnote(
footnote = md(
"*Note.* Transition matrix values are the identical to Table 5, however Table 5
has the values rearranged by class for interpretation purposes. Classes may be arranged
directly through Mplus syntax using start values."),
locations = cells_title())
```
<div style="text-align: center;"><img src="images/ucsb_logo.png" width="75%" /></div>