Lesson 7 of 8

Multivariate EDA & PCA

In Lesson 6 you read the shape of one column at a time, and in Lesson 2 you compared two columns with a scatterplot and a correlation. But Maya's bakery has outgrown one-and-two-at-a-time. Her new logbook has six numeric columns recorded over 22 days: how many people walked in (foot_traffic), how many sales she rang up (transactions), her takings (revenue), the day's temperature, and how many iced_coffee and hot_cocoa cups she sold.

Six columns make \(15\) different pairs to inspect. Squint at them one by one and you drown. The skill in this lesson is to take in all the columns at once, find which ones really move together, and then squeeze them down to the handful of underlying stories actually driving the data. The heatmap below is the first hint: paint every pair's correlation as colour and two blocks jump out, before you compute anything by hand.

By the end of this lesson you will be able to:

  • See every pair of numeric columns at once with a scatterplot matrix and a correlation heatmap
  • Explain what a principal component is and why many correlated columns can be compressed into a few
  • Run PCA with prcomp() and use a scree plot to decide how many components to keep
  • Read a component's loadings to name what it measures, and read a biplot to place each day in component space

Prerequisites: you can run R and load a package with library(), and you have met the scatterplot, Pearson's cor(), and the correlation matrix in Lesson 2. Every new term is defined as it appears.

Every pair at once

The scatterplot matrix

The honest first move with many numeric columns is to look at all the pairs, not pick favourites. The tool for that is the scatterplot matrix (also called a pairs plot): a grid with your variables along both the rows and the columns, where each cell is the scatterplot of that row's variable against that column's variable. With four variables you get a 4-by-4 grid of little scatters, all on one screen.

Each lesson runs in a fresh R session, so let us build Maya's 22-day logbook right here (run this once). It is fixed, so every number below is exact.

RInteractive R
bakery <- data.frame( foot_traffic = c(170,196,146,199,228,113,170,153,131,90,128,181,163,98,187,90,153,155,141,166,130,155), transactions = c(132,157,110,157,184,90,138,114,107,70,99,147,128,85,156,70,122,118,108,134,108,122), revenue = c(510,600,435,588,659,377,526,456,429,300,426,552,522,338,576,300,477,478,435,523,426,476), temperature = c(71,70,68,48,65,74,75,75,66,62,68,71,59,69,78,69,84,62,69,73,49,69), iced_coffee = c(30,25,27,4,19,38,33,28,21,13,26,30,13,22,33,26,50,16,32,27,4,36), hot_cocoa = c(10,15,11,30,14,9,7,10,23,18,22,10,23,15,10,12,1,19,8,10,22,12) ) nrow(bakery) # one row per day #> [1] 22 head(bakery) #> foot_traffic transactions revenue temperature iced_coffee hot_cocoa #> 1 170 132 510 71 30 10 #> 2 196 157 600 70 25 15 #> 3 146 110 435 68 27 11 #> 4 199 157 588 48 4 30 #> 5 228 184 659 65 19 14 #> 6 113 90 377 74 38 9

  

The ggpairs() function from the GGally package draws the whole matrix in one line. Below the diagonal it puts the scatterplots, above it prints each pair's correlation, and down the diagonal it shows each variable's own distribution:

RInteractive R
library(GGally) # every pair of four columns at once ggpairs(bakery[, c("foot_traffic", "revenue", "temperature", "iced_coffee")])

  

Reading a pairs plot is just reading many small scatters at once. Here is one cell pulled out and enlarged, foot_traffic against revenue: the dots march tightly up and to the right, a near-perfect line, exactly the strong positive relationship the matrix would show you in miniature.