-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlca-enumeration.Rmd
131 lines (93 loc) · 3.13 KB
/
lca-enumeration.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
---
title: "Lab 1 - Latent Class Analysis Enumeration in MplusAutomation"
author: "Adam Garber"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document: default
pdf_document: default
subtitle: ""
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE, message = FALSE, warning = FALSE, tidy = TRUE)
```
### **Note:** All models are estimated in Mplus via the interface R package {`MplusAutomation`}.
## ____________________________________
### Getting started:
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 (avoid long filepaths)
Within R-studio under the files pane (bottom right):
1. click "New Folder" and name folder "data"
2. click "New Folder" and name folder "enum_mplus"
3. click "New Folder" and name folder "figures"
### loading packages...
```{r}
library(MplusAutomation)
library(tidyverse)
library(relimp)
library(texreg)
library(rhdf5)
library(here)
library(glue)
library(kableExtra)
```
### filepaths are set using R-Projects & here::here()
### read in data
```{r}
example_data <- read_csv(here("data","example_data.csv"),
na=c("",".", "999"))
```
## ____________________________________
## Enumeration (compare *k*-class models 1 - 6)
## ____________________________________
####Example has 6 indicator (x1 x2 x3 x4 x5)
```{r}
lca_k1_6 <- lapply(1:6, function(k) {
lca_enum <- mplusObject(
TITLE = glue("C{k}_LCA_enumerate"),
VARIABLE =
glue(
"categorical = x1 x2 x3 x4 x5;
usevar = x1 x2 x3 x4 x5;
classes = c({k});"),
ANALYSIS =
"estimator = mlr;
type = mixture;
starts = 500 100;",
MODEL = "",
OUTPUT = "",
PLOT =
"type = plot3;
series = x1 x2 x3 x4 x5(*);",
usevariables = colnames(ies_data),
rdata = ies_data)
lca_enum_fit <- mplusModeler(lca_enum,
dataout=glue(here("enum_mplus", "c_lca_enumerate.dat")),
modelout=glue(here("enum_mplus", "c{k}_lca_enumerate.inp")) ,
check=TRUE, run = TRUE, hashfilename = FALSE)
})
```
#### compare model fit for enumerated models
```{r}
output_enum <- readModels(here("enum_mplus"))
enum_summary <- LatexSummaryTable(output_enum,
keepCols=c("Title",
"LL",
"BIC",
"aBIC"),
sortBy = "Title")
enum_summary %>%
kable(booktabs = T, linesep = "") %>%
kable_styling(c("striped"),
full_width = F,
position = "left")
```
## ____________________________________
### 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.
Muthén, L.K. and Muthén, B.O. (1998-2017). Mplus User’s Guide. Eighth Edition. Los Angeles, CA: Muthén & Muthén
## ____________________________________