Bivariate EDA & Correlation
In Lesson 1 you learned to read one variable at a time: you took Maya's daily bakery revenue and found its shape, its typical value, and the one festival day hiding in the tail. But a single column can only tell you so much. The questions Maya really loses sleep over are about pairs: does a busier day actually bring in more money? Do hot days sell more iced coffee? Does anything she does explain anything else?
This fortnight Maya kept a richer logbook: for each of 14 days she wrote down the foot traffic (people who walked in), the revenue (dollars), the day's temperature, and how many iced coffees and hot cocoas she sold. Below is the first pair, foot traffic across the bottom and revenue up the side. Each dot is one day, and the pattern jumps out before you compute a single number.
By the end of this lesson you will be able to:
- Read a scatterplot of two numeric variables: its direction, its form, and its strength
- Measure that relationship with Pearson's correlation (
cor()) and read a correlation matrix - Explain, with a real example, why correlation is not causation
Prerequisites: you can run R and load a package with library(), and you have met one-variable EDA in Lesson 1 (histogram, mean vs median, spread). Every new term is defined as it appears.
The scatterplot: one point per day
When you have two numeric columns and want to know how they relate, the first tool is always the scatterplot: put one variable on the horizontal axis, the other on the vertical axis, and draw one point for every row. Maya's foot_traffic goes across, revenue goes up, and each of the 14 dots is a single day, placed at that day's (people, dollars).
Each lesson runs in a fresh R session, so let us build the fortnight's logbook right here (run this once):
In ggplot2 you name the data, map one column to x and one to y with aes(), then add geom_point() to draw the dots. That is the whole scatterplot:
A scatter is read on three dimensions, and you can see all three above:
- Direction: the cloud climbs from lower-left to upper-right, so as foot traffic rises, revenue rises too. That is a positive relationship. (A cloud sloping the other way, down to the right, is negative.)
- Form: the points roughly follow a straight line, not a curve. A straight-line pattern is called linear.
- Strength: the dots hug that line in a tight, narrow band rather than scattering loosely. Tight band, strong relationship.