<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-to-perform-pca-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://francoishusson.wordpress.com/2017/02/17/how-to-perform-pca-with-r/"> François Husson</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This post shows how to perform PCA with R and the package FactoMineR.</p>
<p>If you want to learn more on methods such as PCA, you can enroll in this MOOC (everyting is free): <a target="_blank" rel="nofollow" href="https://www.fun-mooc.fr/courses/agrocampusouest/40001S03EN/session03/about">MOOC on Exploratory Multivariate Data Analysis</a></p>
<h2><strong>Dataset</strong></h2>
<p>Here is a <a target="_blank" rel="nofollow" href="http://factominer.free.fr/docs/Expert_wine.csv">wine dataset</a>, with 10 wines and 27 sensory attributes (like sweetness, bitterness, fruity odor, and so on), 2 preference variables, and a qualitative variable corresponding to the wine labels (there are 2 labels, Sauvignon and Vouvray). The values in the data table correspond to the average score given by several judges for the same wine and descriptive variable. The aim of doing PCA here is to characterize the wines according to their sensory characteristics.</p>
<h2><strong>Performing PCA … with additional information</strong></h2>
<p>Here are the lines of code used. Note that we use the information given by the qualitative variable.</p>
<p><span>### Read data</span><br>
<span>wine </span></p>
<p><span>### Loading FactoMineR</span><br>
<span>library(FactoMineR)</span></p>
<p><span>### PCA with supplementary variables</span><br>
<span>res </span></p>
<p><span>### Print the main results</span><br>
<span>summary(res)</span></p>
<p>Two graphs are given by default, one for the individuals, one for the quantitative variables.</p>
<p>But is is interesting to consider the qualitative variable to better understand the differences between wines. Wines are colored according to their label.</p>
<pre><span>## Drawing wines according to the label</span>
<span> plot(res,habillage=&quot;Label&quot;)</span></pre>
<p><img sizes="(max-width: 634px) 100vw, 634px" data-image-description="" data-orig-file="https://i0.wp.com/francoishusson.files.wordpress.com/2017/02/pca_wine.png?w=456&amp;ssl=1" data-orig-size="1912,875" data-image-title="pca_wine" class="wp-image-32" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="32" data-permalink="https://francoishusson.wordpress.com/pca_wine/" alt="pca_wine" data-medium-file="https://i0.wp.com/francoishusson.files.wordpress.com/2017/02/pca_wine.png?w=456&amp;ssl=1?w=300" data-large-file="https://i0.wp.com/francoishusson.files.wordpress.com/2017/02/pca_wine.png?w=456&amp;ssl=1?w=634" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/francoishusson.files.wordpress.com/2017/02/pca_wine.png?w=456&ssl=1"></p>
<h2><strong>Interpretation</strong></h2>
<p>The graph of the individuals shows, for instance, that S Michaud and S Trotignon are very “close”. <span></span>It means that the scores for S Michaud and S Trotignon are approximately the same, whatever the variable. In the same way, Aub Marigny and Font Coteaux are wines with similar sensory scores for the 27 attributes. On the other hand, Font Brulés and S Trotignon have very different sensory profiles, because the first principal component, representing the main axis of variability between wines, separates them strongly.</p>
<p>The variables astringency, visual intensity, mushroom odor and candied fruit odor, found to the right, have correlations close to 1 with the first dimension. Since the correlation with the 1st dimension is close to 1, the values of these variables move in the same direction as the coordinates in the 1<sup>st</sup> dimensions. Wines with a small value in the 1st dimension have low values for these variables, and wines with large values in the 1st dimension have high values for these variables. Thus, the wines that are to the right of the plot have high (and positive) values in the 1st dimension and thus have high values for these variables. With the same logic, wines that are to the left have a small value in the 1st dimension, and thus low values for these variables.</p>
<p>For the variables passionfruit odor, citrus odor and freshness, everything is the other way around. The correlation with the 1st dimension is close to -1, and thus the values move in the opposite direction. Wines with a low value in the 1st dimension have low coordinate values, and thus have high values for these variables, and wines with large values in the 1st dimension have small values for these variables.</p>
<p>Overall, we see that the first dimension splits apart wines that are considered fruity and flowery (on the left) from wines that are woody or with vegetal odors. And this is the main source of variability.</p>
<p>So then, how can we interpret the 2nd dimension, the vertical axis? At the top, wines have large values on the vertical axis. Since the correlation coefficients between the 2nd dimension and variables such acidity or bitterness are close to 1, it means that wines at the top take large values for these variables. And wines at the bottom have small values in the 2nd dimension, and thus small values for these variables. For sweetness, the correlation coefficient is close to -1, so wines that have a small value in the 2nd dimension are sweet, while wines that have large values are not.</p>
<p>Overall, the 2nd dimension separates the wines at the top, acidic and bitter, from sweet wines at the bottom.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/francoishusson.wordpress.com/94/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/francoishusson.wordpress.com/94/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://francoishusson.wordpress.com/2017/02/17/how-to-perform-pca-with-r/"> François Husson</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/pC8T7q7Ie54"></a></div>