-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path09-observed-response-patterns.Rmd
More file actions
187 lines (146 loc) · 5.73 KB
/
Copy path09-observed-response-patterns.Rmd
File metadata and controls
187 lines (146 loc) · 5.73 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
```{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.
```
# (PART) Understanding the Data {-}
# Observed Response Patterns
------------------------------------------------------------------------
## 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)
```
------------------------------------------------------------------------
## Prepare Data
```{r, eval=TRUE}
df_bully <- read_csv(here("data", "crdc_lca_data.csv")) %>%
clean_names() %>%
dplyr::select(report_dis, report_race, report_sex, counselors_fte, psych_fte, law_fte)
```
------------------------------------------------------------------------
Continuing the LCA example (\@ref(lca-enum)) in this bookdown, save response frequencies for the 3-class model with `response is _____.dat` under `SAVEDATA.`
```{r, cache = TRUE}
patterns <- mplusObject(
TITLE = "C3 LCA - Save response patterns",
VARIABLE =
"categorical = report_dis-law_fte;
usevar = report_dis-law_fte;
classes = c(3);",
ANALYSIS =
"estimator = mlr;
type = mixture;
starts = 0;
processors = 10;
optseed = 802779;",
SAVEDATA =
"File=savedata.dat;
Save=cprob;
! Code to save response frequency data
response is resp_patterns.dat;",
OUTPUT = "residual patterns tech11 tech14",
usevariables = colnames(df_bully),
rdata = df_bully)
patterns_fit <- mplusModeler(patterns,
dataout=here("mplus", "bully.dat"),
modelout=here("mplus", "patterns.inp") ,
check=TRUE, run = TRUE, hashfilename = FALSE)
```
------------------------------------------------------------------------
Read in observed response pattern data and relabel the columns
```{r}
# Read in response frequency data that we just created:
patterns <- read_table(here("mplus", "resp_patterns.dat"),
col_names=FALSE, na = "*")
# Extract the column names
names <- names(readModels(here("mplus", "patterns.out"))[['savedata']])
# Add the names back to the dataset
colnames(patterns) <- c("Frequency", names)
# Rename the variable
patterns <- patterns %>%
rename(C = dplyr::any_of(c("C", "MLCC")))
```
Create a table with the top 5 unconditional response pattern, then top of conditional response pattern for each modal class assignment
```{r,eval=TRUE}
# Order responses by highest frequency
order_highest <- patterns %>%
rename(C = dplyr::any_of(c("C", "MLCC"))) %>%
arrange(desc(Frequency))
# Loop `patterns` data to list top 5 conditional response patterns for each class
loop_cond <- lapply(1:max(patterns$C), function(k) {
order_cond <- patterns %>%
filter(C == k) %>%
arrange(desc(Frequency)) %>%
head(5)
})
# Convert loop into data frame
table_data <- as.data.frame(bind_rows(loop_cond))
# Combine unconditional and conditional responses patterns
response_patterns <- rbind(order_highest[1:5,], table_data)
```
Finally, use `{gt}` to make a nicely formatted table
```{r}
resp_table <- response_patterns %>%
gt() %>%
tab_header(
title = "Observed Response Patterns",
subtitle = html("Response patterns, estimated frequencies, estimated posterior class probabilities and modal assignments")) %>%
tab_source_note(
source_note = md("Data Source: **Civil Rights Data Collection (CRDC)**")) %>%
cols_label(
Frequency = html("<i>f</i><sub>r</sub>"),
REPORT_D = "Harrassment: Disability",
REPORT_R = "Harrassment: Race",
REPORT_S = "Harrassment: Sex",
COUNSELO = "Staff: Counselor",
PSYCH_FT = "Staff: Psychologist",
LAW_FTE = "Staff: Law Enforcement",
CPROB1 = html("P<sub><i>k</i></sub>=1"),
CPROB2 = html("P<sub><i>k</i></sub>=2"),
CPROB3 = html("P<sub><i>k</i></sub>=3"),
C = md("*k*")) %>%
tab_row_group(
label = "Unconditional response patterns",
rows = 1:5) %>%
tab_row_group(
label = md("*k* = 1 Conditional response patterns"),
rows = 6:10) %>% #EDIT THESE VALUES BASED ON THE LAST COLUMN
tab_row_group(
label = md("*k* = 2 Conditional response patterns"),
rows = 11:15) %>% #EDIT THESE VALUES BASED ON THE LAST COLUMN
tab_row_group(
label = md("*k* = 3 Conditional response patterns"),
rows = 16:20) %>% #EDIT THESE VALUES BASED ON THE LAST COLUMN
row_group_order(
groups = c("Unconditional response patterns",
md("*k* = 1 Conditional response patterns"),
md("*k* = 2 Conditional response patterns"),
md("*k* = 3 Conditional response patterns"))) %>%
tab_footnote(
footnote = html(
"<i>Note.</i> <i>f</i><sub>r</sub> = response pattern frequency; P<sub><i>k</i></sub> = posterior class probabilities"
)
) %>%
cols_align(align = "center") %>%
opt_align_table_header(align = "left") %>%
gt::tab_options(table.font.names = "Times New Roman")
resp_table
```
------------------------------------------------------------------------
Save table:
```{r, eval = FALSE}
gtsave(resp_table, here("figures","resp_table.png"))
```
<div style="text-align: center;"><img src="images/ucsb_logo.png" width="75%" /></div>