alexym1 / FactoMineR2

Multivariate exploratory data analysis in R
https://alexym1.github.io/FactoMineR2/
Other
0 stars 0 forks source link

Returning different eigenvalues using FactoMineR and FactoMineR2 with `scale.unit = FALSE` #1

Closed alexym1 closed 4 months ago

alexym1 commented 5 months ago

R : version 4.3.2 FactoMineR2 : > 0.1.0

Hello,

FactoMineR and FactoMineR2 return different eigenvalues in absence of scaling.

library(FactoMineR)
library(FactoMineR2)
data(decathlon)
# Get eigvalues using FactoMineR
res.pca <- PCA(decathlon, quanti.sup = 11:12, quali.sup=13, graph = FALSE, scale.unit = FALSE)
head(res.pca$eig)

        eigenvalue percentage of variance cumulative percentage of variance
comp 1 134.8072870            79.65958964                          79.65959
comp 2  22.9355599            13.55295646                          93.21255
comp 3   9.7472631             5.75979978                          98.97235
comp 4   1.1172154             0.66017883                          99.63252
comp 5   0.3477705             0.20550264                          99.83803
comp 6   0.1326819             0.07840365                          99.91643
# Get eigvalues with FactoMineR2
eigs <- decathlon[,-c(11:13)] |>
  scale(center = FALSE, scale = FALSE) |>
  get_eigen()

df_eigs <- eigs |>
  extract(1) |>
  as.data.frame() |>
  rename(eigenvalue = values) |>
  mutate(
    `percentage of variance` = eigenvalue / sum(eigenvalue) * 100,
    `cumulative percentage of variance` = cumsum(`percentage of variance`)
  ) |>
  set_rownames(paste0("comp", 1:nrow(eigs$vectors)))

head(df_eigs)

        eigenvalue percentage of variance cumulative percentage of variance
comp 1 138.1774692            79.65958964                          79.65959
comp 2  23.5089489            13.55295646                          93.21255
comp 3   9.9909447             5.75979978                          98.97235
comp 4   1.1451457             0.66017883                          99.63252
comp 5   0.3564647             0.20550264                          99.83803
comp 6   0.1359989             0.07840365                          99.91643
alexym1 commented 4 months ago

R : 4.3.2 FactoMineR2 : development version

get_eigen() gained a new argument called weigths allowing to return same results as FactoMineR

library(FactoMineR)
data("decathlon")
row.w <- rep(1, nrow(decathlon))
weights <- row.w / sum(row.w)
ex <- FactoMineR2::standardize(decathlon[,-c(11:13)], scale = FALSE)
head(FactoMineR2::get_eigen(ex, weights)[[1]])
#> [1] 134.8072870  22.9355599   9.7472631   1.1172154   0.3477705   0.1326819