-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5-Longitudinal-invariance.R
50 lines (41 loc) · 1.92 KB
/
5-Longitudinal-invariance.R
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
# Title: Test longitudinal measurement invariance
# Author: Ewan Carr
# This script fits a series of models to test longitudinal measurement
# invariance for the PACE mediators and outcomes (at 0, 12, and 52 weeks).
# These input files models have been specified by hand, as it would have been
# too complicated to specify them programatically.
library(tidyverse)
library(here)
library(MplusAutomation)
source(here("analysis", "functions", "misc_functions.R"))
# Run all input files =========================================================
input_files <- list.files(here("analysis", "longitudinal_invariance"),
recursive = TRUE,
pattern = ".*\\.inp",
full.names = TRUE) %>%
map(fit_model)
# Gather output ===============================================================
output <- readModels(here("analysis", "longitudinal_invariance"),
recursive = TRUE)
# Make table of fit statistics (and DIFFTEST results) =========================
fit_statistics <- output %>%
map_dfr("summaries", .id = "path") %>%
select(χ2 = ChiSqM_Value,
df = ChiSqM_DF,
cfi = CFI,
rmsea = RMSEA_Estimate,
diff = ChiSqDiffTest_Value,
diffdf = ChiSqDiffTest_DF,
diffpval = ChiSqDiffTest_PValue,
filename = Filename) %>%
mutate(relchi = χ2 / df) %>%
tidyr::extract(filename, c("factor", "invariance"),
"([A-Z_]+[SL]*)_([a-z]+).*") %>%
gather(key, value, -factor, -invariance) %>%
mutate(invariance = case_when(invariance == "configural" ~ "c",
invariance == "metric" ~ "m",
invariance == "scalar" ~ "s")) %>%
unite(id, c(key, invariance)) %>%
spread(id, value) %>%
select(-diff_c, -diffdf_c, -diffpval_c)
write_csv(fit_statistics, "fit.csv")