-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11-homogeneity.Rmd
More file actions
203 lines (156 loc) · 6.49 KB
/
Copy path11-homogeneity.Rmd
File metadata and controls
203 lines (156 loc) · 6.49 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
```{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.
```
# Class Homogeneity
------------------------------------------------------------------------
## Load packages
```{r}
library(naniar)
library(tidyverse)
library(haven)
library(glue)
library(MplusAutomation)
library(here)
library(janitor)
library(gt)
library(tidyLPA)
library(pisaUSA15)
library(cowplot)
library(filesstrings)
library(patchwork)
library(RcppAlgos)
```
------------------------------------------------------------------------
## LCA
Continuing the LCA example (\@ref(lca-enum)) in this bookdown, use R to evaluate the LCA class homogeneity.
------------------------------------------------------------------------
We want a high degree of homogeneity (small variance) within each class (i.e., people in each class are not identical but are more similar to each other than to people in different classes).
Class-specific response probabilities above .70 or below .30 indicate high homogeneity in that particular class for that specific item. For a binary variable, $X$, $Var(X) = p(1-p)$. The largest variance possible for a binary variable is when $p = .5$, the smallest variance (0) is when $p = 0$ or $p = 1$.
The easiest way is to visualize the item probability plot:
```{r out.width="90%"}
source(here("functions", "plot_lca.R"))
# Read in model
output_enum <- readModels(here("enum"), filefilter = "bully", quiet = TRUE)
# Change this to look at your chosen LCA model
chosen_model <- output_enum$c4_bully.out
# Add lines
plot_lca(chosen_model) + geom_hline(yintercept = c(0.7, 0.3))
```
Or create a table:
```{r}
# Read in model
output_enum <- readModels(here("enum"), filefilter = "bully", quiet = TRUE)
# Change this to look at your chosen LCA model
chosen_model <- output_enum$c4_bully.out
# Extract table of probabilities
probabilities <- data.frame(chosen_model$parameters$probability.scale) %>%
mutate(LatentClass = sub("^", "Class ", LatentClass)) %>%
filter(category == 2) %>%
dplyr::select(est, LatentClass, param) %>%
pivot_wider(names_from = LatentClass, values_from = est) %>%
relocate(param, .after = last_col()) %>%
rename(Items = param)
# Bold above .7 and below .3
bold_condition <- function(x) {
ifelse(x > 0.7 | x < 0.3, paste0("**", sprintf("%.3f", x), "**"),
sprintf("%.3f", x))
}
# Apply the bold formatting condition to the table
homogeneity <- probabilities %>%
mutate(across(where(is.numeric), ~ sapply(., bold_condition))) %>%
relocate(Items, .before = 1)
# Display the formatted table and percentage
homogeneity %>%
gt() %>%
cols_label(
Items = "Item"
) %>%
fmt_number(
columns = everything(),
decimals = 3
) %>%
tab_header(
title = "Class Homogeneity Table"
) %>%
fmt_markdown(columns = everything()) %>%
tab_footnote(
footnote = "Cells in bold indicate probabilities > 0.7 or < 0.3"
)
```
------------------------------------------------------------------------
## LPA
Continuing the LPA example (\@ref(lpa-enum)) in this bookdown, use R to evaluate the LPA profile homogeneity.
------------------------------------------------------------------------
Profile homogeneity is the model-estimated within-profile variances for each indicator m across the *k*-profiles and comparing them to the total overall sample variance:
$$ \frac{\theta_{mk}}{\theta_{m}} $$
Where $\theta{mk}$ is the item variance for item $m$, class $k$ and $\theta{m}$ is the item variance for item $m$ not conditioned on class.
Individuals belonging to the same class are more similar to other members of that class than they are compared to members of other classes. Individuals belonging to the same class are closer to the class mean than they are to the overall population mean. Within-class variance for each indicator is smaller than overall population variance, therefore:
- Values \> .90 corresponds to a high degree of homogeneity
- Values \< .60 corresponds to a low degree of homogeneity
To calculate the values in a table:
```{r}
# Read in model
output_enum <- readModels(here("lpa", "tidyLPA"), quiet = TRUE)
# Change this to look at your chosen LPA model
chosen_model <- output_enum$model_3_class_4.out
# Extract overall and profile-specific variances
overall_variances <- data.frame(chosen_model$sampstat$univariate.sample.statistics) %>%
dplyr::select(Variance) %>%
rownames_to_column(var = "item") %>%
clean_names() %>%
mutate(item = str_sub(item, 1, 8))
profile_variances <- data.frame(chosen_model$parameters$unstandardized) %>%
filter(paramHeader == "Variances") %>%
dplyr::select(param, est, LatentClass) %>%
rename(item = param,
variance = est,
k = LatentClass) %>%
mutate(k = paste0("Profile ", k)) %>%
mutate(item = str_sub(item, 1, 8))
# Bold above 0.90 and italicize below 0.60
format_condition <- function(x) {
ifelse(x > 0.9,
paste0("**", sprintf("%.3f", x), "**"), # bold
ifelse(x < 0.6,
paste0("*", sprintf("%.3f", x), "*"), # italic
sprintf("%.3f", x))) # normal
}
# Evaluate the ratio
homogeneity <- profile_variances %>%
left_join(overall_variances, by = "item") %>%
mutate(homogeneity_ratio = round((variance.x / variance.y),2)) %>%
mutate(across(where(is.numeric), ~ as.character(sapply(., format_condition))))
# Create a gt table
homogeneity %>%
dplyr::select(homogeneity_ratio, k, item) %>%
pivot_wider(
names_from = k,
values_from = homogeneity_ratio
) %>%
gt() %>%
cols_label(
item = "Item"
) %>%
fmt_number(
columns = everything(),
decimals = 3
) %>%
tab_header(
title = "Profile Homogeneity Table"
) %>%
tab_footnote(
footnote = "Cells in bold indicate proportions > 0.90 and cells in italics indicate proportions < 0.60.
Note: In this example, the class variances are set to be equal.") %>%
fmt_markdown(columns = everything())
```
You can also visualize the plot:
```{r out.width="90%"}
source(here("functions", "plot_lpa.R"))
output_enum <- readModels(here("lpa", "tidyLPA"), quiet = TRUE)
plot_lpa(model_name = output_enum$model_3_class_4.out)
```
::: {style="text-align: center;"}
<img src="images/ucsb_logo.png" width="75%"/>
:::