-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCFA.Rmd
More file actions
374 lines (280 loc) · 7.67 KB
/
Copy pathCFA.Rmd
File metadata and controls
374 lines (280 loc) · 7.67 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
---
title: "HOMEWORK 3: CFA (version 2)"
author: "Maria Tikhonova"
output:
html_document:
keep_md: no
toc: yes
editor_options:
chunk_output_type: inline
---
# Load packages and data
```{r}
# setwd()
```
## Load packages
```{r, warning=FALSE, message=FALSE}
library(lavaan)
library(semPlot)
library(rio)
library(car)
library(dplyr)
```
## Import data
```{r}
pisa <- import("PISA_MR.sav")
```
Version 2: items ST161Q01HA, ST161Q02HA, ST161Q03HA, ST161Q06HA, ST161Q07HA, ST161Q08HA
```{r}
pisa <- pisa[c('ST161Q01HA', 'ST161Q02HA', 'ST161Q03HA', 'ST161Q06HA', 'ST161Q07HA', 'ST161Q08HA')]
pisa <- rename(pisa,
"item_1" = 'ST161Q01HA',
"item_2" = 'ST161Q02HA',
"item_3" = 'ST161Q03HA',
"item_6" = 'ST161Q06HA',
"item_7" = 'ST161Q07HA',
"item_8" = 'ST161Q08HA')
```
**Codebook**
Response options:
Strongly disagree (1)
Disagree (2)
Agree (3)
Strongly agree (4)
item_1: I am a good reader
item_2: I am able to understand difficult texts
item_3: I read fluently
item_6: I have always had difficulty with reading
item_7: I have to read a text several times before completely understanding it.
item_8: I find it difficult to answer questions about a text.
Look at the data structure
```{r}
glimpse(pisa)
```
Delete missing values
```{r}
pisa <- na.omit(pisa)
```
Descriptive statistics
```{r}
cat("\nSummary\n")
summary(pisa)
cat("\nFrequency table\n")
table_freq <- sapply(pisa[, 1:6], table)
table_freq
table_freq <- as.data.frame(table_freq)
hist(pisa[,1:6])
```
Next, we will use function `cfa` from `lavaan` for model parameters estimation.
# MODEL SELECTION
## STEP 1. FIRST ATTEMPT
```{r}
model1 <- "F1 =~ item_1 + item_2 + item_3 + item_6 + item_7 + item_8"
fit_mod1 <- cfa(model = model1, data = pisa, estimator = "WLSMV", ordered = TRUE, std.lv = T)
summary(fit_mod1, fit=T, std=T)
```
```{r}
# factor scores only
head(standardizedSolution(fit_mod1, type = "std.all"), 6)
```
```{r}
# fit indexes
fitmeasures(fit_mod1, c("cfi.scaled", "tli.scaled", "rmsea.scaled", "srmr"))
```
```{r, warning = FALSE, message = FALSE}
# reliability
options(scipen = 999)
semTools::reliability(fit_mod1)
```
## STEP 2. RECODING
Recoding items
```{r}
pisa$item_6r <- car::recode(pisa$item_6, '1=4;2=3;3=2;4=1')
pisa$item_7r <- car::recode(pisa$item_7, '1=4;2=3;3=2;4=1')
pisa$item_8r <- car::recode(pisa$item_8, '1=4;2=3;3=2;4=1')
```
Creating the second model
```{r}
model2 <- "F1 =~ item_1 + item_2 + item_3 + item_6r + item_7r + item_8r"
fit_mod2 <- cfa(model = model2, data = pisa, estimator = "WLSMV", ordered = TRUE, std.lv = T)
summary(fit_mod2, fit=T, std=T)
```
```{r}
# factor scores only
head(standardizedSolution(fit_mod2, type = "std.all"), 6)
```
```{r}
# fit indexes
fitmeasures(fit_mod2, c("cfi.scaled", "tli.scaled", "rmsea.scaled", "srmr"))
```
```{r, warning = FALSE, message = FALSE}
# reliability
semTools::reliability(fit_mod2)
```
## STEP 3. DROP THE ITEM
```{r}
model3 <- "F1 =~ item_1 + item_2 + item_6r + item_7r + item_8r"
fit_mod3 <- cfa(model = model3, data = pisa, estimator = "WLSMV", ordered = TRUE, std.lv = T)
summary(fit_mod3, fit=T, std=T)
```
```{r}
# factor scores only
head(standardizedSolution(fit_mod3, type = "std.all"), 5)
```
```{r}
# fit indexes
fitmeasures(fit_mod3, c("cfi.scaled", "tli.scaled", "rmsea.scaled", "srmr"))
```
```{r, warning = FALSE, message = FALSE}
# reliability
semTools::reliability(fit_mod3)
```
## STEP 4. MODIFICATION INDEXES
```{r}
modindices(fit_mod3, minimum.value = 10, sort. = TRUE)
```
```{r}
model4 <- "F1 =~ item_1 + item_2 + item_6r + item_7r + item_8r
item_1 ~~ item_2"
fit_mod4 <- cfa(model = model4, data = pisa, estimator = "WLSMV", ordered = TRUE, std.lv = T)
summary(fit_mod4, fit=T, std=T)
```
```{r}
# factor scores only
head(standardizedSolution(fit_mod4, type = "std.all"), 6)
```
```{r}
# fit indexes
fitmeasures(fit_mod4, c("cfi.scaled", "tli.scaled", "rmsea.scaled", "srmr"))
```
```{r, warning = FALSE, message = FALSE}
# reliability
semTools::reliability(fit_mod4)
```
No further mi
```{r}
modindices(fit_mod4, minimum.value = 10, sort. = TRUE)
# no modindices
```
## STEP 5. DIMENSIONALITY CHANGE
```{r}
model5 <- "
F1 =~ item_1 + item_2
F2 =~ item_6r + item_7r + item_8r
"
fit_mod5 <- cfa(model = model5, data = pisa, estimator = "WLSMV", ordered = TRUE, std.lv = T)
summary(fit_mod5, fit=T, std=T)
```
```{r}
# factor scores only
head(standardizedSolution(fit_mod5, type = "std.all"), 5)
```
```{r}
# fit indexes
fitmeasures(fit_mod5, c("cfi.scaled", "tli.scaled", "rmsea.scaled", "srmr"))
```
```{r, warning = FALSE, message = FALSE}
# reliability
semTools::reliability(fit_mod5)
```
No further mi
```{r}
modindices(fit_mod5, minimum.value = 10, sort. = TRUE)
# no modindeces
```
--------------------------------------------------------------------------------
# Model comparison
fit_mod3: one-factor model, no MI; ("baseline")
fit_mod4: one-factor model, MI;
fit_mod5: two-factor model, no MI;
```{r}
anova34 <- anova(fit_mod3, fit_mod4, method = "satorra.2000")
anova34
```
```{r}
anova35 <- anova(fit_mod3, fit_mod5, method = "satorra.2000")
anova35
```
--------------------------------------------------------------------------------
# Plots
## Model 1
```{r}
lavaanPlot::lavaanPlot(model = fit_mod1, coefs = TRUE, covs = TRUE, stand = TRUE)
```
## Model 5
```{r}
lavaanPlot::lavaanPlot(model = fit_mod5, coefs = TRUE, covs = TRUE, stand = TRUE)
```
--------------------------------------------------------------------------------
# Tables
AI was used to create these tables.
## Fit indexes for all models
```{r}
# list of models
fits_all <- list(
mod1 = fit_mod1,
mod2 = fit_mod2,
mod3 = fit_mod3,
mod4 = fit_mod4,
mod5 = fit_mod5
)
# extract values needed
get_table <- function(fit, name){
data.frame(
Model = name,
Chisq_scaled = round(fitmeasures(fit, "chisq.scaled"), 3),
Df_scaled = fitmeasures(fit, "df.scaled"),
CFI_scaled = round(fitmeasures(fit, "cfi.scaled"), 3),
TLI_scaled = round(fitmeasures(fit, "tli.scaled"), 3),
RMSEA_scaled = round(fitmeasures(fit, "rmsea.scaled"), 3),
SRMR = round(fitmeasures(fit, "srmr"), 3)
)
}
table_fit <- do.call(rbind,
mapply(get_table,
fits_all,
names(fits_all),
SIMPLIFY = FALSE))
knitr::kable(table_fit)
```
## Loading table
```{r}
extract_loadings <- function(fit, model_name){
pe <- parameterEstimates(fit, standardized = TRUE)
loadings <- pe[pe$op == "=~", c("rhs", "std.all", "pvalue")]
# for asterisks
stars <- ifelse(loadings$pvalue < .001, "***",
ifelse(loadings$pvalue < .01, "**",
ifelse(loadings$pvalue < .05, "*", "")))
# round
values <- paste0(round(loadings$std.all, 3), stars)
df <- data.frame(
Item = loadings$rhs,
value = values
)
colnames(df)[2] <- model_name
df
}
# extract loadings
loads <- list(
load1 = extract_loadings(fit_mod1, "Mod1"),
load2 = extract_loadings(fit_mod2, "Mod2"),
load3 = extract_loadings(fit_mod3, "Mod3"),
load4 = extract_loadings(fit_mod4, "Mod4"),
load5 = extract_loadings(fit_mod5, "Mod5")
)
# combine by Item
table_load <- Reduce(function(x, y) merge(x, y, by = "Item", all = TRUE),
loads)
# round and NA
table_load[is.na(table_load)] <- "×"
knitr::kable(table_load)
```
## All Reliabilities in one output
```{r, warning = FALSE, message = FALSE}
round(semTools::reliability(fit_mod1, what = "omega3"), 3)
round(semTools::reliability(fit_mod2, what = "omega3"), 3)
round(semTools::reliability(fit_mod3, what = "omega3"), 3)
round(semTools::reliability(fit_mod4, what = "omega3"), 3)
round(semTools::reliability(fit_mod5, what = "omega3"), 3)
```