Multiple Correspondence Analysis with FactoMineR

In this tutorial, the presenter demonstrates how to perform Multiple Correspondence Analysis (MCA) using the FactoMineR package in R on a tea‑consumption questionnaire dataset comprising 300 respondents and 19 variables (18 categorical consumption items, image‑related categorical items, personal categorical items, and one quantitative age variable). The walkthrough covers importing the data, configuring the MCA function with supplementary quantitative (age) and qualitative (image and personal) variables, and interpreting the output from summary.MCA (eigenvalues, coordinates, contributions, squared cosines, and v‑tests). The speaker then explains how to describe dimensions with dimdesc, and provides an extensive guide to customizing MCA visualizations via plot.MCA—adjusting labels, visibility, selection criteria, axis ranges, and adding confidence ellipses with plot_ellipses—to make plots readable when many individuals or categories are present. Practical advice on handling overloaded plots and highlighting the most representative categories or individuals is also given, concluding with encouragement for viewers to apply these techniques themselves.
Chapters
We are now going to see how to do MCA using FactoMineR. The dataset we're going to use deals with tea consumption. A questionnaire was answered by 300 people, and there were 3 types of variables in it. 18 variables dealt with how tea is consumed, for instance, do you take tea with breakfast? Yes or no. At afternoon tea time? Yes or no. In what form? In a tea bag, as loose tea. So that's the first 18 variables. Then there were the questions about the image people had about tea, and lastly, questions about the people themselves. These are the questions in green here. For example, sex, socioprofessional category, whether they do sport or not, age coded as a qualitative variable age_q, in age brackets. As for their image of tea, there were questions like, to you, is tea good for health? Is it a diuretic? And so on. Then, one last quantitative variable, actual age. That's the 19th column in the dataset. Now, let's get into the practical details of running an MCA using FactoMineR. You should now go and open the script file we've provided for this task. The script is called script_MCA_facto.R. So, with this, you'll be able to run the code at the same time as us, and see the results instantly. First, let's import the data. The first line of code lets me say in which folder my data is located, using the command setwd. Next, we import the dataset by giving the name of the file. We then signal that the variable names are included by putting header=True, and that the columns are separated by semicolons. Once the data is imported, we can ask for its summary to make sure that it all arrived OK. Check: all of the variables are qualitative except for age, which is indeed quantitative. Next, let's load the FactoMineR library, and do an MCA. Before running the code, let's have a look at the help file for the MCA function. See how we have to provide the dataset, and that after, the results are given for the first 5 components. See how we can add supplementary individuals, quantitative supplementary variables, qualitative supplementary variables. By default, the plots show the first and second axes. There are no weights on the individuals, row.w NULL, and the method that will be run is the standard one, with indicator matrices. If we want, we can use birth method to get the correspondence analysis results on birth table. So, let's run the MCA using the MCA function. We enter the name of the dataset, tea. The quantitative variable age has to be supplementary, because it's quantitative. We signal this by putting quanti.sup= 19. That is, the 19th variable is quantitative and supplementary. Also, for now, let's Let's make the image of T on the personal data supplementary. We put quali.sup equal twenty to thirty-six. Here are the results of MCA. Some plots are output. One shows the supplementary quantitative variables. Here, there's only one on the correlation circle. Another plot shows the active and supplementary qualitative variables. Giving the squared of the correlation ratio between each of them on the axes. We also have the age variable, here. We have the R-squared of the regression of the age variable in terms of each axis. The plot is really quite overloaded for the moment. We'll see later how to make this better. Next up, we also have a plot showing just the individuals, and one showing all the categories, with the active ones in red, and the supplementary ones in green. Once we have the results from the MCA, we can visualize them using the summary.MCA function, which can be called using summary. The output of this function first reminds us of the line of code we used to run it, followed by a table with the eigenvalues and percentages of inertia associated with each axis, and then the results on the individuals. The fact that we wrote nb_elements equal inf means we get the result for all individuals. So, for the individuals, we have their coordinate values on the first axis, their contributions to the construction of the axes, which are quite small, but don't forget there are 300 individuals, followed by the quality of representation, measured by the squared cosine, we have these results for the first, second, and third dimensions. Next, we have the results for the categories. This means, all categories, their coordinate values, their contributions, their quality of representation, as defined by squared cosine, and the test statistic. The test statistics here follow a Gaussian distribution, so those below -2 or above 2 can be considered significant. This means that the category in question has a coordinate significantly different to 0. This is useful for knowing which categories have large positive or negative values in each dimension. For example, here, breakfast has a large positive value in the first dimension. We have the result for the first, 2nd, and 3rd dimensions. We also get the results on the variables, that is, the correlation ratio of each variable for each of the first 3 dimensions. This is simply the correlation ratio between the axis and the variable. These are the coordinate values that help us to build the plot of the squared relationships. Next, if we have any supplementary categories, we obtain results on them, including coordinate values, representation quality, and the v-test. However, no contributions because they did not contribute to axes construction. Also, we get a table showing the correlation ratios for the supplementary qualitative variables. Next up, we have results for the supplementary quantitative variables. Here, there's only one of them, So, we have its coordinate value in each dimension, that is, the correlation coefficient between each axis and the quantitative variable. We can also give a description of the axes, which can be quite useful when we have a lot of variables, using the dimdesc function. By default, the first 3 dimensions are described. We can see, for example, that the first dimension isn't characterized by quantitative variables, None of them. Instead, it is significantly linked to various qualitative variables. The strongest link is with where do you buy your tea, then do you drink it in a tea room, etc. The variables are ranked from most to least related, and only the ones with a correlation ratio significantly different to 0 are shown in the results. Next, we have tables for the categories, showing each category and its coordinate values on the axes, along with the test: is this coordinate value significantly different to 0 or not? We can see that we have both the active and supplementary variables used to describe the axes, as well as the active and supplementary categories. So, we have the results, in the first dimension, then the second dimension, where we can see, for example, that the age variable is significantly correlated with it. And we also have the result for the third dimension. We went a bit fast on these default plots, because they are quite useful when we don't have many individuals or categories, but if we do have many of either, we have some work to do to make them more easily interpretable. There are lots of options available in the plot.MCA function, in FactoMineR, to help us to do this. Let's have a look at some of them. First, we can put labels only on active and supplementary categories, and not on the individuals. This gives us the biplot with the active individuals. There are no supplementary individuals, but if there were, we could show them, the active categories, the supplementary categories, and as we said, labels only on the active and supplementary categories. Another thing we can do to the plot is to make certain things invisible. For example, we can make the active and supplementary categories invisible, like this. This gives a plot that only shows the individuals, As another example, here, we make the individuals and the supplementary categories invisible. This plot therefore shows only the active categories. We can then, for instance, indicate that we want the font size to be a little less than 0.7 for the points, that is, a bit smaller than the default. We can add a title, Active Categories, on autolab=y, which means that we ask the software to place labels so that they overlap as little as possible. This always happens by default when there are not many labels required on the plot, when there are less than 50. But when there are more than that, this could take a long time, so we have to force the software using the command autolab=y to place the labels as we would like. And lastly, we can make the active categories invisible, as well as the individuals, leaving us with a plot with only the supplementary categories on it. We can then select certain categories to highlight. Here, we select the ones we want using the select mode option, only putting labels on the 10 best represented categories. Here is that plot, showing the 10 active categories with the highest quality of representation, and the 10 supplementary categories with the highest quality of representation, too. Instead of using the quality of representation, we could instead use contribution. contrib 20 signals that we want to label the 20 categories with the largest contributions. Clearly, only the active categories are used for this. If we want to do a similar kind of thing but for the individuals, for instance, and put names only on the 20 which have the highest quality projection onto the plane of the first 2 axes, we first make the categories invisible, and then provide the option select rather than selectMode. If we write select=square cosine 20, this means, label the 20 individuals with the best quality of representation. If we want to mix things up a bit more, we could do a biplot with the 20 best represented individuals, using the select option, along with the 10 best represented categories, using selectMode=square cosine 20. Square cosine 10. This means showing both the 10 best represented active categories and the 10 best represented supplementary ones. Another interesting plot is the one showing the squared relationships. We already got this plot by default, but it was really overloaded with information. One thing we can do is to make the plot a bit larger, but change the range of the axes. By default, it's between 0 and 1. What we can do is change this to between 0 and 0.6 for both axes. But the plot is still overloaded with information. We can, for instance, make the qualitative and quantitative supplementary variables invisible. All that remains are the active variables. Another possibility instead is to make the active variables and the supplementary quantitative variables invisible. If we want to see the 3rd and 4th axes, not just the 1st and 2nd, we can type axes , which gives us the result for the 3rd and 4th axes. Here is such a plot, with the active and supplementary categories made invisible. We can do the same plot, but with the individuals made invisible, and only the active and supplementary categories visible, that is, the ones that are best projected onto the 3rd and 4th axes. Lastly, we can output plots with confidence ellipses around the categories of some variables. The function we need to use here is called plot_ellipses. This function will give one plot per variable, and draw a confidence ellipse around each category of that variable. So, there will be the same number of plots as there are variables. Here, again, as we have a huge number of qualitative variables, we've chosen just to output these plots for the 14th to 17th ones, that is, only 4 of them. These 4 variables are, where do you buy your tea, sugar, how do you drink your tea, and in what form. So here we have the confidence ellipses. For buying location, we have tea shop, chain store, and both in tea shop and chain store. We see that the confidence ellipses are quite small. In effect, we have a lot of individuals here. 300 of them. The subpopulations are quite distinct and far apart, so the ellipses don't overlap. All of these plots show that the subpopulations are significantly separated. So, there you have it, the most important functions needed to run MCA in FactoMineR, and the main options that are useful for creating plots and improving their interpretability. Now, it's your turn to use these functions to do MCA with FactoMineR. Good luck!
More from Yamossoun

Algorithme de calcule de la racine carrée par dichotomie #dichotomy #squareroot #algorithm









