By the end of this lab, you can:
level: blocks and cluster=srmr_within and srmr_betweenWe simulate:
This is not “true” ordinal data generation (threshold model), but it is adequate for practicing MCFA workflow.
n_id <- 139
n_rep <- 21
N <- n_id * n_rep
ID <- rep(sprintf("S%03d", 1:n_id), each = n_rep)
# Between-person factor (trait-like)
eta_b <- rnorm(n_id, 0, 0.7)
eta_b_long <- rep(eta_b, each = n_rep)
# Within-person factor (state-like)
eta_w <- rnorm(N, 0, 1.0)
lambda <- c(0.80, 0.70, 0.75, 0.65)
eps_sd <- c(0.70, 0.80, 0.75, 0.85)
# Continuous responses (before discretizing to Likert)
y_cont <- sapply(seq_along(lambda), function(j) {
lambda[j] * (eta_b_long + eta_w) + rnorm(N, 0, eps_sd[j])
})
ESMdata <- data.frame(
ID = ID,
d1 = cut_likert(y_cont[,1]),
d2 = cut_likert(y_cont[,2]),
d3 = cut_likert(y_cont[,3]),
d4 = cut_likert(y_cont[,4])
)
head(ESMdata, 8) ID d1 d2 d3 d4
1 S001 1 1 1 2
2 S001 3 5 5 4
3 S001 2 1 2 1
4 S001 1 2 1 1
5 S001 1 1 1 2
6 S001 1 1 1 2
7 S001 4 4 1 2
8 S001 1 1 1 1
d1–d4 (are categories used?).We specify a one-factor model at both levels:
TD_wTD_bestimator = "MLR" and cluster = "ID".rmsea.robust, cfi.robust, tli.robust, srmr_within, srmr_between.srmr_between were much worse than srmr_within.A common (bad) habit is to ignore clustering and just fit a single-level CFA.
cluster=).We now create a dataset where between-level residual correlations exist (not captured by the factor).
# Create a new dataset ESMdata2 with extra between correlations among d1 and d2
eta_b2 <- rnorm(n_id, 0, 0.7)
eta_b2_long <- rep(eta_b2, each = n_rep)
eta_w2 <- rnorm(N, 0, 1.0)
# Add a shared between-only disturbance u_b to items 1 and 2
u_b <- rnorm(n_id, 0, 0.6)
u_b_long <- rep(u_b, each = n_rep)
y2_cont <- sapply(seq_along(lambda), function(j) {
base <- lambda[j] * (eta_b2_long + eta_w2) + rnorm(N, 0, eps_sd[j])
if (j %in% c(1,2)) base <- base + u_b_long
base
})
ESMdata2 <- data.frame(
ID = ID,
d1 = cut_likert(y2_cont[,1]),
d2 = cut_likert(y2_cont[,2]),
d3 = cut_likert(y2_cont[,3]),
d4 = cut_likert(y2_cont[,4])
)ESMdata2.srmr_within and srmr_between to the original dataset.srmr_between.In MCFA, it is common to encounter:
ESMdata2.We test whether loadings are equal across levels:
conf and weak on the original dataset (ESMdata) and compare fit (focus on srmr_between).refs/references.bib and cite them in the slides/lab.