kassambara / factoextra

Extract and Visualize the Results of Multivariate Data Analyses
http://www.sthda.com/english/rpkgs/factoextra
355 stars 103 forks source link

Issue with ellipses in MFA #9

Closed kassambara closed 7 years ago

kassambara commented 8 years ago

Hi @inventionate,

I received by e-mail the following message:

Dear Alboukadel,
I am currently using factoextra to plot results I have obtained with factominer (using MFA).
The ellipses drawn by factoextra using the function fviz_mfa_ind() are drawn around the individuals, and not the barycenter/centroid as in factominer.
Is it possible to draw ellipses around barycenter/centroids in factoextra?
Thank you in advance,
Anne-Marie

Do you have any solution?

Best, AK

inventionate commented 8 years ago

Hi Albou,

I think Anne-Marie wants to plot confidence ellipses and not concentration ellipses. We may could implement it (also for MCA). It should be not too difficult, we can use FactoMineRs coord.ellipse(). What do you think?

By the way, I've played around with the new ggplot2 stat_ellipse(). In the attached document you can check my results. Unfortunately it is in german, but luckily the graphs are the most interesting part. Maybe we should refactor the code?

https://www.dropbox.com/s/1y1cpsahkg3ivs7/soziale_topologie_ws_1516.pdf?dl=0

Here are my functions:

https://github.com/inventionate/TimeSpaceAnalysis

And last but not least, some interesting news. The upcoming FactoMineR 1.33 will support specific MCA and specific MFA. It will be possible to exclude categories (make them passive) of active variables. So someone can exclude junk categories.

Best Regards, Fabian

Am 09.04.2016 um 19:59 schrieb Alboukadel KASSAMBARA notifications@github.com:

Hi @inventionate https://github.com/inventionate,

I received by e-mail the following message:

Dear Alboukadel, I am currently using factoextra to plot results I have obtained with factominer (using MFA). The ellipses drawn by factoextra using the function fviz_mfa_ind() are drawn around the individuals, and not the barycenter/centroid as in factominer. Is it possible to draw ellipses around barycenter/centroids in factoextra? Thank you in advance, Anne-Marie Do you have any solution?

Best, AK

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/kassambara/factoextra/issues/9

kassambara commented 8 years ago

Waoooooh! I found that all your graphs are beautiful, very well presented and well arranged.

I think also that its about confidence ellipses. After a quick research on google, I found that a similar question have been already asked on this french forum (http://forums.cirad.fr/logiciel-R/viewtopic.php?t=7661 http://forums.cirad.fr/logiciel-R/viewtopic.php?t=7661) about ellipse in factoextra PCA. So I think that we should add the implementation of coord.ellipse() in our todo list.

For drawing ellipses, we should definitively go for stat_ellipse().

Very interesting news about the new features of FactoMiner. I look forward to install this new version.

Best regards Albou

Le 9 avr. 2016 à 20:44, Fabian Mundt notifications@github.com a écrit :

Hi Albou,

I think Anne-Marie wants to plot confidence ellipses and not concentration ellipses. We may could implement it (also for MCA). It should be not too difficult, we can use FactoMineRs coord.ellipse(). What do you think?

By the way, I've played around with the new ggplot2 stat_ellipse(). In the attached document you can check my results. Unfortunately it is in german, but luckily the graphs are the most interesting part. Maybe we should refactor the code?

https://www.dropbox.com/s/1y1cpsahkg3ivs7/soziale_topologie_ws_1516.pdf?dl=0

Here are my functions (in :

https://github.com/inventionate/TimeSpaceAnalysis

And last but not least, some interesting news. The upcoming FactoMineR 1.33 will support specific MCA and specific MFA. It will be possible to exclude categories (make them passive) of active variables. So someone can exclude junk categories.

Best Regards, Fabian

Am 09.04.2016 um 19:59 schrieb Alboukadel KASSAMBARA notifications@github.com:

Hi @inventionate https://github.com/inventionate,

I received by e-mail the following message:

Dear Alboukadel, I am currently using factoextra to plot results I have obtained with factominer (using MFA). The ellipses drawn by factoextra using the function fviz_mfa_ind() are drawn around the individuals, and not the barycenter/centroid as in factominer. Is it possible to draw ellipses around barycenter/centroids in factoextra? Thank you in advance, Anne-Marie Do you have any solution?

Best, AK

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/kassambara/factoextra/issues/9

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/kassambara/factoextra/issues/9#issuecomment-207829605

inventionate commented 8 years ago

Thank you! If you want I can merge my stat_ellipses() implementations. I really like the new ggplot2 solution. I can also integrate the so called "concentration ellipses" (grey ellipse) to compare different group ellipses. I've seen this solution in B. Le Roux and H. Rouanets GDA book and prefer it. Thanks to the GDAtools package it's also possible to calculate variance values (within-variance, between-variance, eta^2).

I'm with you in case of confidence ellipses. Maybe I can start some coding in the next weeks.

Glad to hear that you like the new features. Unfortunately the new FactoMineR version hasn't published on CRAN yet. But it should arrive soon.

Best Regards, Fabian

Am 09.04.2016 um 21:15 schrieb Alboukadel KASSAMBARA notifications@github.com:

Waoooooh! I found that all your graphs are beautiful, very well presented and well arranged.

I think also that its about confidence ellipses. After a quick research on google, I found that a similar question have been already asked on this french forum (http://forums.cirad.fr/logiciel-R/viewtopic.php?t=7661 http://forums.cirad.fr/logiciel-R/viewtopic.php?t=7661) about ellipse in factoextra PCA. So I think that we should add the implementation of coord.ellipse() in our todo list.

For drawing ellipses, we should definitively go for stat_ellipse().

Very interesting news about the new features of FactoMiner. I look forward to install this new version.

Best regards Albou

Le 9 avr. 2016 à 20:44, Fabian Mundt notifications@github.com a écrit :

Hi Albou,

I think Anne-Marie wants to plot confidence ellipses and not concentration ellipses. We may could implement it (also for MCA). It should be not too difficult, we can use FactoMineRs coord.ellipse(). What do you think?

By the way, I've played around with the new ggplot2 stat_ellipse(). In the attached document you can check my results. Unfortunately it is in german, but luckily the graphs are the most interesting part. Maybe we should refactor the code?

https://www.dropbox.com/s/1y1cpsahkg3ivs7/soziale_topologie_ws_1516.pdf?dl=0

Here are my functions (in :

https://github.com/inventionate/TimeSpaceAnalysis

And last but not least, some interesting news. The upcoming FactoMineR 1.33 will support specific MCA and specific MFA. It will be possible to exclude categories (make them passive) of active variables. So someone can exclude junk categories.

Best Regards, Fabian

Am 09.04.2016 um 19:59 schrieb Alboukadel KASSAMBARA notifications@github.com:

Hi @inventionate https://github.com/inventionate,

I received by e-mail the following message:

Dear Alboukadel, I am currently using factoextra to plot results I have obtained with factominer (using MFA). The ellipses drawn by factoextra using the function fviz_mfa_ind() are drawn around the individuals, and not the barycenter/centroid as in factominer. Is it possible to draw ellipses around barycenter/centroids in factoextra? Thank you in advance, Anne-Marie Do you have any solution?

Best, AK

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/kassambara/factoextra/issues/9

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/kassambara/factoextra/issues/9#issuecomment-207829605

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/kassambara/factoextra/issues/9#issuecomment-207836278

kassambara commented 8 years ago

Yep, it would be nice if you can merge your stat_ellipse() implementations and the concentration ellipses comparing groups.

Best, Albou

inventionate commented 8 years ago

Here is a first implementation based on coord.ellipse(). If it's OK, I can try to integrate it.

confidence-ellipses.R.zip

Best, Fabian

inventionate commented 8 years ago

Maybe Anne-Marie can check the results and confirm that this is what she wants?

kassambara commented 8 years ago

It works!!

As Anne-Marie asked about MFA, I derived the following simplified script from the file you sent to me:

# Compute mfa with FactoMineR
library("FactoMineR")
data(poison)
res.mfa <- MFA(poison, group=c(2,2,5,6), type=c("s","n","n","n"),
               name.group=c("desc","desc2","symptom","eat"),
               num.group.sup=1:2, graph=FALSE)

# 1. Confidence ellipse with Factominer
FactoMineR::plotellipses(res.mfa, keepvar = "Vomiting")

# 2. Concentration ellipse with factoextra
library("factoextra")
grp <- as.factor(poison[, "Vomiting"])
fviz_mfa_ind(res.mfa, label="none", habillage=grp,
              addEllipses=TRUE)

# 3. Confidence ellipse with factoextra
grp <- as.factor(poison[, "Vomiting"])
axes <- c(1, 2)
ind_coord <- cbind.data.frame(grp, res.mfa$ind$coord[, axes])
ellipse_coord <- FactoMineR::coord.ellipse(ind_coord, bary = TRUE, axes = axes )$res
ellipse_center <- stats::aggregate(ind_coord[, 2:3], by = list(grp = grp), mean)

fviz_mfa_ind(res.mfa, label = "none", habillage = grp)+
  geom_path(data = ellipse_coord, aes(Dim.1, Dim.2, colour = grp), 
            show.legend = FALSE) # add confidence ellipse

The output is as follow:

rplot

rplot09

rplot10

I'll share this link with her so that she can check if it answers to her question.

Best Regards, Albou

inventionate commented 8 years ago

Great! Thank you for your MFA solution.

Best, Fabian

Am 10.04.2016 um 12:24 schrieb Alboukadel KASSAMBARA notifications@github.com:

It works!!

As Anne-Marie asked about MFA, I derived the following simplified script from the file you sent to me:

Compute mfa with FactoMineR

library("FactoMineR") data(poison) res.mfa <- MFA(poison, group=c(2,2,5,6), type=c("s","n","n","n"), name.group=c("desc","desc2","symptom","eat"), num.group.sup=1:2, graph=FALSE)

1. Confidence ellipse with Factominer

FactoMineR::plotellipses(res.mfa, keepvar = "Vomiting")

2. Concentration ellipse with factoextra

library("factoextra") grp <- as.factor(poison[, "Vomiting"]) fviz_mfa_ind(res.mfa, label="none", habillage=grp, addEllipses=TRUE)

3. Confidence ellipse with factoextra

grp <- as.factor(poison[, "Vomiting"]) axes <- c(1, 2) ind_coord <- cbind.data.frame(grp, res.mfa$ind$coord[, axes]) ellipse_coord <- FactoMineR::coord.ellipse(ind_coord, bary = TRUE, axes = axes )$res ellipse_center <- stats::aggregate(ind_coord[, 2:3], by = list(grp = grp), mean)

fviz_mfa_ind(res.mfa, label = "none", habillage = grp)+ geom_path(data = ellipse_coord, aes(Dim.1, Dim.2, colour = grp), show.legend = FALSE) # add confidence ellipse The output is as follow:

Confidence ellipse with Factominer

Concentration ellipse with factoextra

Confidence ellipse with factoextra

I'll share this link with her so that she can check if it answers to her question.

Best Regards, Albou

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub

kassambara commented 7 years ago

Confidence ellipse can be added now using the argument ellipse.type = "confidence"

library("FactoMineR")
data(poison)
res.mfa <- MFA(poison, group=c(2,2,5,6), type=c("s","n","n","n"),
               name.group=c("desc","desc2","symptom","eat"),
               num.group.sup=1:2, graph=FALSE)

library("factoextra")
grp <- as.factor(poison[, "Vomiting"])
fviz_mfa_ind(res.mfa, label="none", habillage=grp,
             addEllipses=TRUE, ellipse.type = "confidence")

rplot

korypostma commented 7 years ago

For those coming here and trying to get this line to work when changing the axes value: ellipse_coord <- FactoMineR::coord.ellipse(ind_coord, bary = TRUE, axes = axes )$res

Just completely remove the axes parameter because ind_coord only has 3 columns and it selects them correctly.

So your line should really be: ellipse_coord <- FactoMineR::coord.ellipse(ind_coord, bary = TRUE)$res

Also, I could not get the ellipse.type = "confidence" parameter to work. It would draw the plot but there were no ellipses. I'm using FactoMineR 1.33 and factoextra 1.0.3

Thanks!

kassambara commented 7 years ago

To get the ellipse.type = "confidence to work, please install the latest developmental version of factoextra (v = 1.0.4.999).

You can install it as follow:

if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/factoextra")