PCA in R
Welcome to the Unsupervised Learning track. Meet Rosa, a botanist. She has measured 150 iris flowers, and for each one she wrote down four numbers with a ruler: the length and width of a petal, and the length and width of a sepal (the small green leaf under the petal), all in centimetres. Four numbers per flower does not sound like much, until you try to eyeball all 150 flowers at once and see which ones resemble each other. Rosa wishes she could place every flower as a single dot on a flat map, where similar flowers sit close together and different ones sit far apart.
Principal Component Analysis, or PCA, is exactly that machine. It takes many overlapping measurements and boils them down to a couple of new numbers you can actually plot and read. The panel below previews the map PCA builds for Rosa's flowers, and lets you build the real one in R.
By the end of this lesson you will be able to:
- Say what a principal component is, in plain words
- Run PCA in R the right way (scaling first), with one function
- Read how much each component is worth, choose how many to keep, and read a biplot
Prerequisites: you can run R and read its output, and you know what a variable, a mean, and a scatter plot are. No linear algebra is assumed. Every term is defined as it appears. (The iris measurements are real: they were collected by the botanist Edgar Anderson in 1935.)
Each dot stands for a flower and the three colours are the three iris species. Notice how the species fall into clean, separate clumps, even though the map keeps just two new numbers per flower instead of the original four. Press Run to build that same map from the real measurements. That compression is what PCA is for.
Four rulers, one story
Here is the thing Rosa notices when she looks at her four columns: they overlap. A flower with a long petal almost always has a wide petal too. A flower with a long petal also tends to have a long sepal. The measurements are not four independent facts, they partly repeat each other. That repetition has a name: correlation, a number from -1 to +1 that says how tightly two columns move together (near +1 = rise together, near 0 = unrelated, near -1 = one rises as the other falls).
The grid below is the correlation between every pair of Rosa's four measurements, computed from the real data.
Petal length and petal width move together almost perfectly (r = 0.96). You can confirm every number yourself: