set.seed(124)
n <- 1200
Sigma <- matrix(c(
1.00, 0.60, 0.40,
0.60, 1.00, 0.50,
0.40, 0.50, 1.00
), 3, 3, byrow = TRUE)
Z <- mnormt::rmnorm(n = n, mean = rep(0, 3), varcov = Sigma)
Y <- data.frame(
y1 = ordered(cut(
Z[, 1],
breaks = c(-Inf, -0.7, 0.4, Inf),
labels = c("low", "mid", "high")
)),
y2 = ordered(cut(
Z[, 2],
breaks = c(-Inf, -1.0, -0.1, 0.8, Inf),
labels = c("1", "2", "3", "4")
)),
y3 = ordered(cut(
Z[, 3],
breaks = c(-Inf, -0.4, 0.2, 1.1, Inf),
labels = c("A", "B", "C", "D")
))
)
pc <- polychoric(Y)
print(pc, digits = 3)
summary(pc)
plot(pc)
# Interactive viewing (requires shiny)
if (interactive() && requireNamespace("shiny", quietly = TRUE)) {
view_corr_shiny(pc)
}
# latent Pearson correlations used to generate the ordinal variables
round(stats::cor(Z), 2)
Run the code above in your browser using DataLab