## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5)
has_cub <- requireNamespace("CUB", quietly = TRUE)
knitr::opts_chunk$set(eval = has_cub)

## ----eval = !has_cub, echo = FALSE, results = "asis"--------------------------
# cat("**This vignette needs the 'CUB' package (for the `univer` data), which is not installed: code is shown but not run.**")

## -----------------------------------------------------------------------------
library(DDAWASS)
library(ggplot2)
data(univer, package = "CUB")
univer$Faculty <- factor(sprintf("F%02d", univer$faculty))
univer$Gender  <- factor(univer$gender, levels = 0:1, labels = c("M", "W"))
items <- c("informat", "willingn", "officeho", "compete", "global")
M <- data2MatD(univer, unit = c("Faculty", "Gender"), vars = items,
               support = 1:7, min.n = 10)
gender <- sub(".*_", "", get.MatD.rownames(M))

## -----------------------------------------------------------------------------
Q <- WD.quantile.matrix(M, quantiles = 10, vars = "global")
Q[1:5, ]

## -----------------------------------------------------------------------------
pca_g <- WD.1d.PCA(M, var = "global", quantiles = 10, plots = FALSE)
round(pca_g$eig[1:4, ], 2)
round(pca_g$quanti.sup$cor[, 1:2], 3)

## -----------------------------------------------------------------------------
WD.plot.Spanish.fan(pca_g)

## -----------------------------------------------------------------------------
ind <- data.frame(pca_g$ind$coord[, 1:2], label = rownames(pca_g$ind$coord),
                  mean = get.MatD.stats(M, "mean")$mat[, "global"], gender = gender)
ggplot(ind, aes(Dim.1, Dim.2, colour = mean, shape = gender, label = label)) +
  geom_hline(yintercept = 0, colour = "grey70") + geom_vline(xintercept = 0, colour = "grey70") +
  geom_point(size = 2.5) + geom_text(vjust = -0.8, size = 3, show.legend = FALSE) +
  scale_colour_gradient(low = "#c62320", high = "#275024") +
  labs(title = "Global satisfaction: groups on the first factorial plane",
       x = sprintf("Dim 1 (%.1f%%)", pca_g$eig[1, 2]),
       y = sprintf("Dim 2 (%.1f%%)", pca_g$eig[2, 2])) +
  theme_minimal()

## -----------------------------------------------------------------------------
mfa <- WD.MultiplePCA(M, list.of.vars = c("informat", "willingn", "officeho", "compete"),
                      quantiles = 6, plots = FALSE)
round(mfa$eig[1:4, ], 2)

## -----------------------------------------------------------------------------
round(mfa$group$contrib[, 1:2], 1)
round(mfa$group$coord[, 1:2], 3)

## -----------------------------------------------------------------------------
ind <- data.frame(mfa$ind$coord[, 1:2], label = rownames(mfa$ind$coord), gender = gender)
ggplot(ind, aes(Dim.1, Dim.2, colour = gender, label = label)) +
  geom_hline(yintercept = 0, colour = "grey70") + geom_vline(xintercept = 0, colour = "grey70") +
  geom_point(size = 2.5) + geom_text(vjust = -0.8, size = 3, show.legend = FALSE) +
  labs(title = "MFA of the four items", x = sprintf("Dim 1 (%.1f%%)", mfa$eig[1, 2]),
       y = sprintf("Dim 2 (%.1f%%)", mfa$eig[2, 2])) +
  theme_minimal()

## -----------------------------------------------------------------------------
perf <- sort(mfa$ind$coord[, 1], decreasing = TRUE)
round(head(perf, 5), 2)
round(tail(perf, 5), 2)

