Factor Analysis for Mixed Data
famd.RdCompute a low‑dimensional representation of a data frame that may contain both numeric and categorical variables.
The routine dispatches to FactoMineR::PCA(), FactoMineR::MCA(), or FactoMineR::FAMD() based on the variable types present.
Arguments
- data
(data.frame)
The data frame of interest. Must not contain missing values.- weights
(Scalar character or
NULL)
The column name indataproviding row weights. IfNULL, equal weights are used.- ncp
(Scalar integer)
The number of principal dimensions to compute and return. Must be an integer wherencp >= 1andncp <= min(nrow(data) - 1, d), wheredis the effective dimensionality of the analysis: for PCA,dequals the number of numeric variables; for MCA,dequals the total number of factor levels minus the number of factor variables; for FAMD,dis the sum of both.
Value
An object returned by the corresponding FactoMineR function, of class "FAMD", "PCA", or "MCA" depending on the input variable types.
Details
If missing values are present in your data, you must either impute them or subset to complete cases before analysis. For example,
data_cc <- data[complete.cases(data), ]If variables with zero-variance or less than two factor levels are present in your data, you must exclude them before analysis. For example,
w <- data$weights
valid_vars <- function(x) {
if(is.numeric(x)) {
vclust:::weighted_var(x, w) > .Machine$double.eps
} else if(is.factor(x) || is.character(x) || is.logical(x)) {
nlevels(droplevels(as.factor(x))) > 1
} else {
FALSE
}
}
are_valid <- vapply(data, valid_vars, logical(1))
data_valid <- data[are_valid]Character and logical columns are coerced to factors, and factors have unused levels dropped. Observation weights are validated to be numeric, finite, non‑negative, and with positive sum.
Numeric variables are centered and standardized. Factor variables are encoded as a set of indicator variables and scaled so that the total contribution of that factor variable is comparable to a single quantitative variable. FAMD can be seen as a PCA performed on a concatenation of standardized numeric variables and properly scaled indicator variables for factor variables so that each original variable contributes equally.
Examples
#----------------------------------------------------------------------------
# famd() examples
#----------------------------------------------------------------------------
library(vclust)
# Mixed data example with weights
set.seed(123)
n <- 100
df <- data.frame(
x1 = rnorm(n),
x2 = rnorm(n, mean = 2),
grp = factor(sample(letters[1:3], n, replace = TRUE)),
w = rexp(n)
)
res_mix <- famd(df, weights = "w", ncp = 3)
res_mix$eig
#> eigenvalue percentage of variance cumulative percentage of variance
#> comp 1 1.3099718 32.74930 32.74930
#> comp 2 1.0183821 25.45955 58.20885
#> comp 3 0.9989294 24.97323 83.18208
# individual scores
head(res_mix$ind$coord)
#> Dim.1 Dim.2 Dim.3
#> 1 -1.4589519 -0.1398538 -0.7280949
#> 2 -0.5388565 0.2397381 -0.6289524
#> 3 -0.4271341 2.0044762 -0.2075953
#> 4 0.5977247 -0.4779488 -0.8144091
#> 5 0.1029722 -0.4497487 -0.8123913
#> 6 -0.2089503 2.1711774 -0.1658212
# Numeric‑only (PCA)
res_pca <- famd(iris[1:4], ncp = 2)
res_pca$eig
#> eigenvalue percentage of variance cumulative percentage of variance
#> comp 1 2.9184978 72.96245 72.96245
#> comp 2 0.9140305 22.85076 95.81321
# Factor‑only (MCA)
toy <- data.frame(
a = factor(sample(c("yes", "no"), 50, TRUE)),
b = factor(sample(c("L", "M", "H"), 50, TRUE))
)
res_mca <- famd(toy, ncp = 2)
res_mca$eig
#> eigenvalue percentage of variance cumulative percentage of variance
#> dim 1 0.5615457 37.43638 37.43638
#> dim 2 0.5000000 33.33333 70.76972