
The tutorial demonstrates how to perform Principal Component Analysis (PCA) in R with the FactoMineR package using the publicly available 'decathlon' dataset (41 athletes, 13 variables). It begins by describing the dataset structure—10 active quantitative event variables, two supplementary quantitative variables (ranking and total points), and one supplementary qualitative variable (competition: Decastar vs. 2004 Olympic Games). The speaker shows how to import the CSV file, preserving original variable names and row identifiers. A basic PCA is run on the ten active variables, with default standardization and extraction of five components, and the resulting eigenvalues, inertia percentages, and individual/variable tables are examined (coordinates, contributions, squared cosines). The tutorial then adds the supplementary variables, illustrating how they appear in the variable and individual plots (active variables in black, supplementary in blue, categories as barycenters). Results for all individuals are retrieved (nb_elements = Inf), highlighting that supplementary variables have no contribution values but can be evaluated via V‑test and cos2. The dimdesc function is introduced to characterize axes by correlating variables (including qualitative) with each dimension, with adjustable p‑value cut‑offs. Finally, extensive customization of the plot.PCA function is covered: adjusting font size (cex), hiding qualitative categories, adding titles, coloring by qualitative groups, drawing confidence ellipses (plotEllipses), selecting specific axes, auto‑labeling, highlighting individuals or variables based on contribution or cos2, and other aesthetic tweaks (shadow, autolab). The tutorial concludes by encouraging users to explore the many plotting options available in FactoMineR.
Chapters
We are now going to see how to put PCA into practice using FactoMineR. We're going to use a dataset called decathlon in this tutorial. The data has 41 rows, corresponding to 41 athletes, on 13 variables. Among these, these 13, we have 10 variables corresponding to athletes' performance in each decathlon event. Events like the 100 meters, long jump, shot put, high jump, the 400 meters, etc. Then, we have 2 quantitative variables: the athletes' ranking on the scoreboard, and their number of points. Basically, in decathlon, each event leads to a number of points given to each athlete, so each athlete ends up with a total number of points after the 10 events. Lastly, there is a competition variable with 2 categories: Decastar, and 2004 Olympic Games. These were 2 important competitions in 2004. We're going to do a PCA on this dataset using the 10 event variables as the active ones, and the ranking and points variables as supplementary ones, as well as the competition variable. Thus, we're going to compare athletes uniquely in terms of their performance in the 10 events. Moving now to R, let's get this analysis started. First, we have to import the data. I'm going to import the data and keep it in an object called Decathlon. The data can be obtained at this web address. In fact, we can either extract the data directly from the website, or download it to our own computer and extract it from the folder we put it in. Note that here, the variable names are contained in the dataset, And the column separation is a semicolon, which is usual when dealing with CSV files. Also, note that the individuals' names are found in the first column of the dataset, so we put row.names 1. We can also add check.names FALSE. This means that the variable names will be imported exactly as they are written in the dataset. The data has now been imported. Let's check that it worked okay. We can see that the quantitative variables, 100 meters, long jump, shot put, etc., all the way up to the rankings and number of points, are indeed quantitative, as we can see the minimum, mean, maximum, etc. As for the last variable, the competition one, it's indeed qualitative. We can see the number of times the Decastar category was found, 13, and the Olympic Games category, 28. Now, let's load the FactoMineR library. We can start by looking at the help file for the PCA function, for instance. In this help file, we can see that the only compulsory input is the dataset itself. All the other inputs have default values provided. So by default, the PCA will be standardized because scale.unit equals true. Also, five principal components will be returned. If we want, we can state the indexes of supplementary individuals, the indexes of supplementary quantitative variables, and the indexes of supplementary qualitative ones. There are also values for row weights. Row.w. By default, there are no row weights, and also no column weights, as col.w equals null. Also, by default, the plots will show the first and second axes. Remember that it's always useful to have a look at the examples for using a function correctly, found at the bottom of the help file. Back to our example. First, We're going to do a PCA considering only the active variables, so only the first 10 variables in the dataset. See how we just have to provide the data, and the normalized PCA is performed. By default, we get 2 plots, one for the variables, and one for the individuals. We can access the main results using the summary function: summary.PCA. This summary.PCA is a generic function that can be called using summary. This function reminds us of the line of code we ran: PCA of decathlon columns 1 to 10. Next, it gives us a little table with the eigenvalues and the percentages of inertia associated with each axis. The first axis represents 32% of the total inertia, the second, 17%. Therefore, the first plane gives about 50% of the total inertia. The third axis has 14%, the fourth 10%, etc. Next, we have a table showing the results for the individuals. By default, this shows the results for the first 10 individuals. If we want to see the results for all individuals, we have to include the argument nbelements=inf, meaning infinity. So, in the results for the individuals, we have their name, then their distance from the cloud center of gravity, then results for the first component, including the individual's coordinate value, and its quality of representation on this axis, given by the square cosine. For the first individual, for example, this is equal to 0.695. As well as for the first axis, we also have the same result for the second and third axes. So, if we want to get the quality of representation of the first individual on the plane, we can just sum 0.695 and 0.080. So, that's all for the individuals. Next, we have the results for the variables. The variables' names, then their coordinate value in the first dimension, then their contribution to the construction of the first axis, and their quality of representation, measured by the squared cosine. All of that for the first axis, then the second, then the third. As you can see, it's easy to do PCA when all variables are active. Now, let's look at what to do when we have supplementary variables, either quantitative, qualitative, or both. Here, remember that for our dataset, the 11th and the 12th variables are supplementary quantitative, the ranking variable, and the number of points. And the 13th variable, the competition one, is supplementary qualitative. If we run this, 2 plots appear again. The variables plot, this time, with the active variables in black, with solid lines, and the supplementary variables in blue, with dotted lines. Then, we have the individuals plot, showing individuals, and the categories for the qualitative variable. Thus, the Decastar category is at the barycenter of all individuals that that participated in that competition, and the Olympic Games category is at the barycenter of the individuals that participated in the Olympics. So, let's summarize the main results. Here, we're going to ask for the results for the whole set of individuals, by typing nb_elements=inf. The output gives us a reminder of our previous command, results for the eigenvalues and percentages of inertia, which haven't changed because we have the same active variables as before. However, the results this time are for all individuals, both the active variables and the supplementary ones. So, we now have the coordinate of each individual for the first axis, its contribution, and its quality of representation. It is the same for the active variables. But for the supplementary variables, there is no value for contribution, because they did not contribute to the construction of the axes. For the categories, we have the distance to the barycenter as well as their coordinate values on the first axis, their quality of representation on a V-test, which measures if coordinate values are significantly different to 0. The V-test follows a normal distribution, So, extreme values indicates that the values are significantly different to 0 on the axis. And we also have the results for the second and third axes. After we have run this PCA, it can be interesting to characterize the axes using the dimdesc function. This is especially useful when there are lots of variables. You just have to type dimdesc. And get the correlation between each variable on the first dimension. Only correlations significantly different to 0 are shown. So, the variables are sorted, from the most significantly positively correlated to most significantly negatively correlated. By default, we get these results for the first 3 dimensions. We can modify the cutoff for keeping variables, for instance, and keep variables with a p-value less than 0.2, rather than 0.05. Here, we're going to keep a few more variables than the default. We're doing this just to show that we can see links with quantitative variables, and also with qualitative ones. For example, here, for the first dimension, we see that the competition variable is linked to the first dimension, and significant at 20%. So, the R-squared corresponds to the correlation ratio. There is 5% of the variation in the coordinates, which is provided by the competition variable, which is significant at 20%, but not at the cutoff of 5%. Then, for the categories, if we take a close look, we can see that the Olympic Games and Decastar categories are related to each other, if we use a cutoff of 20%. Just remember that we are using the 20% cutoff to illustrate this example, to show we can characterize axes using qualitative variables. But in general, we use a 5% cutoff. It's often quite useful to work on these plots in order to show the important information in them better. The plot.PCA function in FactoMineR has lots of options that can help us improve plots. Let's take a look at a few of them. To start, let's decrease the font size, cex= 0.8. The font size stays the same as long as cex 1, and shrinks for values less than 1. If we add invisible=quali, this hides the categories of qualitative variables. This isn't particularly useful in our example, but when there are many qualitative variables, it avoids overloading the graph. Next, we can give the individuals plot a title. Here's the plot, with a slightly smaller font size. We can also color individuals in terms of the category they belong to, for a given qualitative variable. Let's color the individuals in terms of the competition variable. Here, we have the Decastar individuals in black, and the Olympic Games individuals in red. Instead of putting habillage equal competition, instead of putting the variable name, we could simply put the variable's number, habillage equal 13, without the quote marks. We can also create confidence ellipses with the function plotEllipses with an S. We type plotEllipses, and we get the same plot as before, but with confidence ellipses around the DecaStar and Olympic Games barycenters. This can be seen as confidence regions for the barycenters. What does this mean? It means that if we had some more athletes who'd participated in Decastar, their mean would be located, with 95% confidence, in this ellipse. If we had several qualitative variables, we'd get as many plots as there are variables, each giving the relevant confidence ellipses around the categories of that particular qualitative variable. If for some reason the third and fourth axes are of interest, it may be useful to output the individuals and variables plot for the third versus the fourth axis. Let's see how to do this. We have to type axes equal three four to plot the third axis against the fourth. Here it is for the individuals, and here it is for the variables using the argument choix equal. When we have lots of individuals, when the plots are overloaded with information, we can also improve them by highlighting certain things. Here are a few interesting plot options. Let's focus on individuals which are projected better than 0.7 on the plane. That is, we want these individuals to be labeled, and the others to have transparent labels. So, we can't see all of the individuals in the center of the plot. Their labels will often be overlapping when there are lots of individuals. Rather than selecting individuals in terms of their squared cosine, we can also select them in terms of their contribution, for example. Here, let's select only the 5 individuals with the largest contribution to the first 2 axes. There are even more options. For instance, we can select individuals by name. Here, as an example, we choose only Clay and Karpov. The same things can be done for the variables. We can highlight the ones with the largest contributions. Here are the 5 with the longest arrows, that is, the ones that most contributed to the plane. Evidently, the supplementary variables haven't contributed anything, and the 5 variables in grey have not contributed much. To finish, let's look at a plot that shows that there are many, many options available in the plot.PCA function. Feel free to go and have a look at the plot.PCA function to further improve your plots. Here's one where we have changed the font size of the title, the font size of the axes, and added shadow TRUE, meaning that we add a little shadow under the names of the individuals, which makes the labels easier to read. If we type autolab YES, or Y, the plot will try to separate the labels so they don't overlap. This happens by default when there are not many variables, but not when there are more than 50. So, that's it for the most common ways to use the PCA function in FactoMineR. Now, it's your turn!
More from Yamossoun

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









