Correspondence Analysis in R: Million Ways
This article describes the multiple ways to compute correspondence analysis in R (CA). Recall that, correspondence analysis is used to study the association between two categorical variables by analyzing the contingency table formed by these two variables.
There are many R functions/packages for computing CA. No matter what functions you decide to use, in the list hereafter, the factoextra package can handle the output.
Contents:
Related Book:
Practical Guide to Principal Component Methods in R
Data format
- Demo data:
housetasks
[in factoextra package] - Data contents: housetasks repartition in the couple.
- rows are the different tasks
- values are the frequencies of the tasks done by the i) wife only, ii) alternatively, iii) husband only, iv) jointly.
- Data illustration:
Load the data:
library(factoextra)
data(housetasks)
# head(housetasks)
Compute
- Using
CA()
[FactoMineR]
library(FactoMineR)
res.ca <- CA(housetasks, graph = FALSE)
- Using
dudi.coa()
[ade4]
library("ade4")
res.ca <- dudi.coa(housetasks, scannf = FALSE, nf = 5)
- Using
ca()
[ca]
library(ca)
res.ca <- ca(housetasks)
- Using
corresp()
[MASS]
library(MASS)
res.ca <- corresp(housetasks, nf = 3)
- Using
epCA()
[ExPosition]
library("ExPosition")
res.ca <- epCA(housetasks, graph = FALSE)
Visualize
- Required packages
library(factoextra)
- Scree plot
fviz_eig(res.ca)
- Biplot of rows and columns:
fviz_ca_biplot(res.ca, repel = TRUE)
Results
You can access to the results as follow:
library(factoextra)
# Eigenvalues
eig.val <- get_eigenvalue(res.ca)
eig.val
# Results for row variables
res.row <- get_ca_row(res.ca)
res.row$coord # Coordinates
res.row$contrib # Contributions to the PCs
res.row$cos2 # Quality of representation
# Results for column variables
res.col <- get_ca_col(res.ca)
res.col$coord # Coordinates
res.col$contrib # Contributions to the PCs
res.col$cos2 # Quality of representation
Read more
- Correspondence Analysis Essentials: Comprehensive guide to analyze, visualize and interpret correspondence analysis
- Correspondence Analysis: Theory and Practice