set.seed(1234)
N <- 1000
J <- 6
# parameters
psi <- .3 # factor correlation
sigma <- .1 # residual varaince
FS <- mvtnorm::rmvnorm(N, mean = c(0, 0), sigma = matrix(c(1, psi, psi, 1), 2, 2, byrow = T))
Lambda <- matrix(
c(
0.7, 0,
0.5, 0,
0.3, 0,
0, 0.7,
0, 0.5,
0, 0.3
), 6, 2,
byrow = T
)
mu <- matrix(rep(0.1, J), nrow = 1, byrow = T)
residual <- mvtnorm::rmvnorm(N, mean = rep(0, J), sigma = diag(sigma^2, J))
Y <- t(apply(FS %*% t(Lambda), 1, \(x) x + mu)) + residual
Q <- matrix(
c(
1, 0,
1, 0,
1, 0,
0, 1,
0, 1,
0, 1
), 6, 2,
byrow = T
)
loc <- Q |>
as.data.frame() |>
rename(`1` = V1, `2` = V2) |>
rownames_to_column("Item") |>
pivot_longer(c(`1`, `2`), names_to = "Theta", values_to = "q") |>
mutate(across(Item:q, as.numeric)) |>
filter(q == 1) |>
as.matrix()