FactoMineR2
was designed to return same result as
FactoMineR
but with a more modern and tidyverse-friendly
syntax. This document aims to show the differences between the two
packages.
library(FactoMineR)
library(FactoMineR2)
data(decathlon)
Eigs
# Get eigvalues and eigvectors with FactoMineR
X <- decathlon[, -c(11:13)]
res_pca <- PCA(X, quanti.sup = 10, ind.sup = 1, graph = FALSE)
head(res_pca$eig)
#> eigenvalue percentage of variance cumulative percentage of variance
#> comp 1 3.2817473 36.463859 36.46386
#> comp 2 1.6893625 18.770694 55.23455
#> comp 3 1.0648909 11.832121 67.06667
#> comp 4 0.8572889 9.525432 76.59211
#> comp 5 0.6349252 7.054725 83.64683
#> comp 6 0.4677351 5.197057 88.84389
# Get eigvalues and eigvectors with FactoMineR2
X_active <- X[-1, -10]
X_active_scaled <- pca_standardize(X_active, scale = TRUE)
eigs <- pca_weighted_eigen(X_active_scaled)
df_eigs <- data.frame(
eigenvalue = eigs$values,
`percentage of variance` = eigs$values / sum(eigs$values) * 100,
`cumulative percentage of variance` = cumsum(eigs$values / sum(eigs$values)) * 100
)
rownames(df_eigs) <- paste0("comp ", 1:nrow(df_eigs))
df_eigs |> head()
#> eigenvalue percentage.of.variance cumulative.percentage.of.variance
#> comp 1 3.2817473 36.463859 36.46386
#> comp 2 1.6893625 18.770694 55.23455
#> comp 3 1.0648909 11.832121 67.06667
#> comp 4 0.8572889 9.525432 76.59211
#> comp 5 0.6349252 7.054725 83.64683
#> comp 6 0.4677351 5.197057 88.84389
PCA
Individuals
# Get principal components with FactoMineR
head(res_pca$ind$coord)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> CLAY 1.3100544 -0.4159492 0.5208056 0.369856466 -2.17766124
#> KARPOV 1.4371854 -0.4206194 -0.9485451 2.055098128 -0.08739926
#> BERNARD -0.6048025 -1.1039234 2.2461329 -0.308551094 0.52147527
#> YURKOV -0.5701719 2.4598460 0.6145443 0.002631539 1.26970612
#> WARNERS 0.3739047 -1.8369980 -0.4725537 0.728066088 0.69493980
#> ZSIVOCZKY 0.2692284 -0.4794763 -1.9969562 -0.258190426 -0.13749027
# Get principal components with FactoMineR2
ind_coords <- pca_ind_coords(eigs)
head(ind_coords[,1:5])
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> [1,] 1.3100544 -0.4159492 0.5208056 0.369856466 -2.17766124
#> [2,] 1.4371854 -0.4206194 -0.9485451 2.055098128 -0.08739926
#> [3,] -0.6048025 -1.1039234 2.2461329 -0.308551094 0.52147527
#> [4,] -0.5701719 2.4598460 0.6145443 0.002631539 1.26970612
#> [5,] 0.3739047 -1.8369980 -0.4725537 0.728066088 0.69493980
#> [6,] 0.2692284 -0.4794763 -1.9969562 -0.258190426 -0.13749027
# Get individual cos2 with FactoMineR
head(res_pca$ind$cos2)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> CLAY 0.20619906 0.02078684 0.03258812 1.643518e-02 0.569755684
#> KARPOV 0.25723191 0.02203321 0.11205086 5.259741e-01 0.000951294
#> BERNARD 0.04764941 0.15874795 0.65720548 1.240179e-02 0.035424009
#> YURKOV 0.03581693 0.66664184 0.04160860 7.629506e-07 0.177616436
#> WARNERS 0.02392091 0.57739535 0.03820834 9.069796e-02 0.082632386
#> ZSIVOCZKY 0.01324414 0.04200650 0.72865053 1.218043e-02 0.003454030
# Get individual cos2 with FactoMineR2
ind_cos2 <- pca_ind_cos2(ind_coords)
head(ind_cos2[,1:5])
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> [1,] 0.20619906 0.02078684 0.03258812 1.643518e-02 0.569755684
#> [2,] 0.25723191 0.02203321 0.11205086 5.259741e-01 0.000951294
#> [3,] 0.04764941 0.15874795 0.65720548 1.240179e-02 0.035424009
#> [4,] 0.03581693 0.66664184 0.04160860 7.629506e-07 0.177616436
#> [5,] 0.02392091 0.57739535 0.03820834 9.069796e-02 0.082632386
#> [6,] 0.01324414 0.04200650 0.72865053 1.218043e-02 0.003454030
# Get individual contributions with FactoMineR
head(res_pca$ind$contrib)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> CLAY 1.30741518 0.2560340 0.6367752 3.989140e-01 18.67231141
#> KARPOV 1.57347720 0.2618157 2.1122769 1.231623e+01 0.03007689
#> BERNARD 0.27865195 1.8034123 11.8442024 2.776304e-01 1.07074211
#> YURKOV 0.24765466 8.9543282 0.8866277 2.019447e-05 6.34780926
#> WARNERS 0.10650172 4.9938388 0.5242485 1.545804e+00 1.90156779
#> ZSIVOCZKY 0.05521748 0.3402134 9.3620727 1.943986e-01 0.07443229
# Get individual contributions with FactoMineR2
ind_contrib <- pca_ind_contrib(ind_coords, eigs)
head(ind_contrib[,1:5])
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> [1,] 1.30741518 0.2560340 0.6367752 3.989140e-01 18.67231141
#> [2,] 1.57347720 0.2618157 2.1122769 1.231623e+01 0.03007689
#> [3,] 0.27865195 1.8034123 11.8442024 2.776304e-01 1.07074211
#> [4,] 0.24765466 8.9543282 0.8866277 2.019447e-05 6.34780926
#> [5,] 0.10650172 4.9938388 0.5242485 1.545804e+00 1.90156779
#> [6,] 0.05521748 0.3402134 9.3620727 1.943986e-01 0.07443229
Supplementary individuals
# Get supplementary individuals with FactoMineR
res_pca$ind.sup$coord
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> SEBRLE 0.8344973 0.3186264 1.214558 -0.201484 1.281883
# Get supplementary individuals with FactoMineR2
weights <- rep(1, nrow(X_active)) / nrow(X_active)
center <- colMeans(X_active)
std <- sqrt(as.vector(crossprod(weights, as.matrix(X_active^2)) - center^2))
X_sup <- X[1, -10]
X_sup_scaled <- (X_sup - center) / std
ind_sup_coords <- as.matrix(X_sup_scaled) %*% eigs$vectors
ind_sup_coords[,1:5]
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 0.8344973 0.3186264 1.2145584 -0.2014840 1.2818827
# Get supplementary individuals cos2 with FactoMineR
res_pca$ind.sup$cos2
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> SEBRLE 0.1505617 0.02194968 0.3189345 0.008776992 0.3552722
# Get supplementary individuals cos2 with FactoMineR2
ind_sup_cos2 <- pca_ind_cos2(ind_sup_coords)
ind_sup_cos2[,1:5]
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 0.150561716 0.021949682 0.318934499 0.008776992 0.355272243
Variables
# Get variable coordinates with FactoMineR
head(res_pca$var$coord)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 100m -0.7802087 0.2474540 -0.07802294 0.15739065 0.10348090
#> Long.jump 0.7418925 -0.4073126 0.05696965 -0.10200465 0.12853454
#> Shot.put 0.6230146 0.5713999 0.28623659 0.22239225 -0.02547125
#> High.jump 0.5704197 0.4191676 -0.24210464 0.09178538 0.63728214
#> 400m -0.6756524 0.4720254 0.12379407 0.12444012 -0.02216888
#> 110m.hurdle -0.7510101 0.2498147 0.23834558 -0.06295776 0.22508937
# Get variable coordinates with FactoMineR2
var_coords <- pca_var_coords(eigs)
head(var_coords[,1:5])
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 100m -0.7802087 0.2474540 -0.07802294 0.15739065 0.10348090
#> Long.jump 0.7418925 -0.4073126 0.05696965 -0.10200465 0.12853454
#> Shot.put 0.6230146 0.5713999 0.28623659 0.22239225 -0.02547125
#> High.jump 0.5704197 0.4191676 -0.24210464 0.09178538 0.63728214
#> 400m -0.6756524 0.4720254 0.12379407 0.12444012 -0.02216888
#> 110m.hurdle -0.7510101 0.2498147 0.23834558 -0.06295776 0.22508937
# Get variable cos2 with FactoMineR
head(res_pca$var$cos2)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 100m 0.6087256 0.06123346 0.006087579 0.024771818 0.0107082970
#> Long.jump 0.5504045 0.16590352 0.003245541 0.010404949 0.0165211277
#> Shot.put 0.3881471 0.32649781 0.081931386 0.049458311 0.0006487846
#> High.jump 0.3253787 0.17570146 0.058614658 0.008424556 0.4061285259
#> 400m 0.4565061 0.22280802 0.015324971 0.015485343 0.0004914592
#> 110m.hurdle 0.5640162 0.06240739 0.056808617 0.003963679 0.0506652236
# Get variable cos2 with FactoMineR2
var_cos2 <- pca_var_cos2(var_coords)
head(var_cos2[,1:5])
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 100m 0.6087256 0.06123346 0.006087579 0.024771818 0.0107082970
#> Long.jump 0.5504045 0.16590352 0.003245541 0.010404949 0.0165211277
#> Shot.put 0.3881471 0.32649781 0.081931386 0.049458311 0.0006487846
#> High.jump 0.3253787 0.17570146 0.058614658 0.008424556 0.4061285259
#> 400m 0.4565061 0.22280802 0.015324971 0.015485343 0.0004914592
#> 110m.hurdle 0.5640162 0.06240739 0.056808617 0.003963679 0.0506652236
# Get variable contributions with FactoMineR
head(res_pca$var$contrib)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 100m 18.54882 3.624649 0.5716623 2.8895531 1.68654464
#> Long.jump 16.77169 9.820481 0.3047769 1.2137039 2.60205888
#> Shot.put 11.82745 19.326688 7.6938764 5.7691535 0.10218284
#> High.jump 9.91480 10.400459 5.5042879 0.9826975 63.96478233
#> 400m 13.91046 13.188882 1.4391119 1.8063156 0.07740427
#> 110m.hurdle 17.18646 3.694139 5.3346892 0.4623504 7.97971527
# Get variable contributions with FactoMineR2
var_contrib <- pca_var_contrib(var_cos2, eigs)
head(var_contrib[,1:5])
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 100m 18.54882 3.624649 0.5716623 2.8895531 1.68654464
#> Long.jump 16.77169 9.820481 0.3047769 1.2137039 2.60205888
#> Shot.put 11.82745 19.326688 7.6938764 5.7691535 0.10218284
#> High.jump 9.91480 10.400459 5.5042879 0.9826975 63.96478233
#> 400m 13.91046 13.188882 1.4391119 1.8063156 0.07740427
#> 110m.hurdle 17.18646 3.694139 5.3346892 0.4623504 7.97971527
Supplementary variables
# Get supplementary variables coordinates with FactoMineR
res_pca$quanti.sup$coord
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 1500m -0.05231146 0.1142921 0.1396336 0.4621371 -0.1964609
# Get supplementary CONTINUOUS variables coordinates with FactoMineR2
X_sup <- X[-1, 10, drop = FALSE]
X_sup_scaled <- pca_standardize(X_sup, scale = TRUE)
var_sup_coords <- t(X_sup_scaled * weights) %*% eigs$U
var_sup_coords[,1:5]
#> [1] -0.05231146 0.11429210 0.13963364 0.46213713 -0.19646090
# Get supplementary variables cos2 with FactoMineR
res_pca$quanti.sup$cos2
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> 1500m 0.002736489 0.01306268 0.01949755 0.2135707 0.03859689
# Get supplementary variables cos2 with FactoMineR2
var_sup_cos2 <- pca_var_cos2(var_sup_coords)
var_sup_cos2[,1:5]
#> [1] 0.002736489 0.013062684 0.019497553 0.213570730 0.038596885