Linear, manifold, topic, tensor, and graph representations · Linear and multiview representations · REP014 · Model
Multiple correspondence analysis (MCA)
Summarize associations among several categorical variables and visualize relationships between individuals and category levels.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page.
- Multiple correspondence analysis Verified in Python and R
Do it in CodeSplash Run it on your own data, in Python or R.
- Availability
- Runs in CodeSplash
- Code example
- Complete calculation
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through discover.mca. The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Summarize associations among several categorical variables and visualize relationships between individuals and category levels.
Question / hypotheses
Which axes summarize departures from independence across categorical responses? This entry specifies an estimation or representation objective; it does not define a universal null hypothesis or p-value.
Architecture / computational structure
A complete categorical indicator table or its Burt counterpart is analyzed with correspondence geometry.
Assumptions
Categories encode meaningful distinctions; rare categories and redundant variables can dominate.
Formula / computational definition
Apply correspondence-analysis geometry to a complete disjunctive indicator matrix or a related Burt matrix. Category frequencies determine the chi-square metric; singular vectors provide factor coordinates and inertia components.
Parameters and notation
Estimated/learned quantities: Category masses, singular vectors/values, and individual/category coordinates.
Hyperparameters and design choices: Dimensions, category treatment, weighting, supplementary points, and inertia-adjustment convention. Numerical tolerances control computation; rank, priors, neighborhood scales, and loss weights alter the model where applicable.
Fitting, fine-tuning, and adaptation
Fit on complete training indicators, distinguish supplementary from active variables, and retain a consistent category dictionary. Adjusted inertia is a reporting convention, not a new fit.
How to use and calibrate
Examine contributions, squared cosines, bootstrap stability, and effects of rare-category aggregation; label whether raw or adjusted inertia is displayed. Select these choices using the data split appropriate to the intended deployment; retain the final test split for evaluation. Report the actual objective or prediction metric rather than an invented significance level.
Example application
Map co-occurring demographic and preference categories. The included synthetic calculation illustrates the computational mechanism; a real analysis must preserve the corresponding sampling unit and missing-data policy.
Interpretation / pitfalls
Watch out
Distances among row points and among category points have defined interpretations, but arbitrary row-to-column distances on a biplot are not automatically meaningful. Ordinal information is discarded unless encoded through a specialized model.
Python and R code examples
R: FactoMineR MCA. Python: weighted indicator-matrix SVD with matching conventions. Use identical levels and training frequencies when projecting new observations, and distinguish missing categories from genuine category levels.
Scope: Complete MCA standardized-residual SVD for a small complete indicator matrix.
Dependencies/validation: Python: NumPy/SciPy, executed successfully. R: base R, not executed. Run the corresponding shared setup first. These short examples are not evidence of complete package-level validation.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Complete calculation — Complete MCA standardized-residual SVD for a small complete indicator matrix.
Shared setup for this chapter
import numpy as np
from scipy.linalg import eigh
from scipy.spatial.distance import pdist, squareform
rng = np.random.default_rng(17)
X = np.array([[1,2,0,1],[2,1,1,0],[3,4,1,2],[4,3,2,1],
[5,6,2,3],[6,5,3,2],[7,8,3,4],[8,7,4,3]], dtype=float)
X = X - X.mean(axis=0)
V = X[:, :2] + np.array([0.3,-0.2])
N = np.abs(X) + 0.2
D = squareform(pdist(X))
K = np.exp(-D**2 / 20)
H = np.eye(len(X)) - np.ones((len(X),len(X)))/len(X)
G = np.array([[0,1,1,0],[1,0,1,0],[1,1,0,1],[0,0,1,0]], dtype=float)
P = G / G.sum(axis=1, keepdims=True)
E = np.array([[0.,0.],[1.,0.],[0.,1.],[1.,1.]])
C = np.array([[8.,1.,0.,2.],[1.,7.,2.,0.],[2.,0.,8.,1.]])
theta = np.array([[0.8,0.2],[0.3,0.7],[0.5,0.5]])
phi = np.array([[0.6,0.2,0.1,0.1],[0.1,0.2,0.3,0.4]])
def softmax(a):
a = np.asarray(a); e = np.exp(a-a.max(axis=-1,keepdims=True))
return e / e.sum(axis=-1,keepdims=True)
indicator=np.column_stack([np.eye(2)[[0,1,0,1]],np.eye(3)[[0,1,2,0]]])
joint=indicator/indicator.sum(); rows=joint.sum(1); cols=joint.sum(0)
res=(joint-rows[:,None]*cols)/np.sqrt(rows[:,None]*cols)
u,s,vt=np.linalg.svd(res,full_matrices=False)
scores=(u[:,:2]*s[:2])/np.sqrt(rows[:,None]); print(scores)
Complete calculation — Complete MCA standardized-residual SVD for a small complete indicator matrix.
Shared setup for this chapter
set.seed(17)
X <- matrix(c(1,2,0,1, 2,1,1,0, 3,4,1,2, 4,3,2,1,
5,6,2,3, 6,5,3,2, 7,8,3,4, 8,7,4,3), ncol=4, byrow=TRUE)
X <- scale(X, center=TRUE, scale=FALSE)
V <- sweep(X[,1:2], 2, c(0.3,-0.2), "+")
N <- abs(X)+0.2
D <- as.matrix(dist(X)); K <- exp(-D^2/20)
H <- diag(nrow(X))-matrix(1/nrow(X),nrow(X),nrow(X))
G <- matrix(c(0,1,1,0, 1,0,1,0, 1,1,0,1, 0,0,1,0),4,byrow=TRUE)
P <- G/rowSums(G)
E <- matrix(c(0,0,1,0,0,1,1,1),ncol=2,byrow=TRUE)
C <- matrix(c(8,1,0,2,1,7,2,0,2,0,8,1),ncol=4,byrow=TRUE)
theta <- matrix(c(.8,.2,.3,.7,.5,.5),ncol=2,byrow=TRUE)
phi <- matrix(c(.6,.2,.1,.1,.1,.2,.3,.4),ncol=4,byrow=TRUE)
softmax <- function(a) { z <- exp(a-max(a)); z/sum(z) }
pinv <- function(a) { s <- svd(a); keep <- s$d > max(s$d)*1e-10
s$v[,keep,drop=FALSE] %*% diag(1/s$d[keep],sum(keep)) %*% t(s$u[,keep,drop=FALSE]) }
indicator <- cbind(diag(2)[c(1,2,1,2),],diag(3)[c(1,2,3,1),])
joint <- indicator/sum(indicator); rows <- rowSums(joint); cols <- colSums(joint)
res <- (joint-outer(rows,cols))/sqrt(outer(rows,cols)); s <- svd(res)
scores <- sweep(sweep(s$u[,1:2],2,s$d[1:2],"*"),1,sqrt(rows),"/"); print(scores)
Sources
- Source 1cran.r-project.org
From the CodeSplash reference, entry REP014.