Univariate EDA
You can now get data into R, tidy it, and join tables together. So you are holding a clean data frame. The very next question, before any model or fancy chart, is the simplest one there is: what is actually in it?
Meet Maya, who runs a small neighbourhood bakery. She has just handed you one column: her daily revenue for the 30 days of March, in dollars. Below is every one of those 30 days drawn as a histogram. By the end of this lesson you will read a picture like this at a glance, and back it up with numbers.
By the end you will be able to:
- Follow a repeatable 7-step framework for exploring any new dataset
- Read one variable's distribution from a histogram: its shape, its typical value, and any surprises
- Summarise that variable with the right numbers (mean, median, spread) and a boxplot
Prerequisites: you can run R and load a package with library(), and you have a tidy data frame in hand (the dplyr and joins courses got you here). Every other term is defined as it appears.
EDA is a disciplined first look
Exploratory Data Analysis (EDA) is the work of getting to know a dataset before you commit to any conclusion. Its goal is to describe and question, not to confirm. You are not trying to prove Maya's bakery is doing well; you are trying to see, honestly, what the numbers say, including the parts that surprise you.
It helps to follow the same path every time. Here is a 7-step framework you can run on any new dataset:
What is EDA for?
Maya hopes March was a good month. You have just imported and tidied her data. What is the first job of exploratory data analysis?
The shape of a single column
Univariate analysis just means looking at one variable at a time, one column on its own. Maya's revenue column is 30 numbers, so let us build it in R and take a first glance. Each lesson runs in a fresh R session, so we create the data right here (run this once):
Thirty numbers are hard to feel by reading them. So we draw them. A histogram chops the range of values into equal-width slices called bins, then draws a bar for each bin whose height is the count of days that fell in it. The shape of those bars is the variable's distribution.
Three things to read off this shape:
- Where the bulk sits: most days cluster between about $200 and $350. That is a typical day at Maya's bakery.
- Skew: the bars trail off to the right, a long thin tail. A distribution with a long right tail is called right-skewed (or positively skewed). Bakery revenue is skewed because a normal day has a floor near zero but a great day has no ceiling.
- A lone bar far to the right: one day sits near $900, all by itself, with empty space before it. Hold onto that day; it is going to matter for every number we compute next.
Draw the histogram in R
The widget above showed you the shape; now make it yourself. In ggplot2 you name the data, map revenue to the x-axis with aes(), then add a geom (a geometric layer) for the chart type you want. Add the histogram layer. Fill in the blank.
Show answer
library(ggplot2)
bakery <- data.frame(revenue = revenue)
ggplot(bakery, aes(x = revenue)) +
geom_histogram(bins = 8)The typical value: mean vs median
The first number anyone wants is the center: what is a typical day worth? There are two common answers, and the gap between them is the whole story.
The mean (the everyday "average") adds up every value and divides by how many there are. For \(n\) days with revenues \(x_1, x_2, \ldots, x_n\),
\[ \bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i \]
where \(\bar{x}\) is the mean, \(n\) is the number of days (30 here), and \(x_i\) is the revenue on day \(i\).
The median is the middle value: sort the days from smallest to largest and take the one in the middle (with an even count, the average of the two middle ones). Half the days fall below the median, half above.
The mean ($298) sits well above the median ($273). That gap is the right skew talking: the one $905 festival day drags the average upward, while the middle day barely notices it. To prove it, drop that single day and recompute:
Which number should Maya use?
Maya wants one number for a typical day's revenue, to plan how much dough to mix each morning. The $905 day was a one-off street-festival order she will not see again. Which summary should she rely on, and why?
How much days vary
A center alone can mislead: two bakeries can both average $298 a day, one steady and one wild. Spread measures how much the values bounce around the center. Three measures, from crudest to most useful:
The range is just the largest value minus the smallest. The standard deviation is the typical distance of a day from the mean. It is the square root of the variance, the average squared distance from the mean:
\[ s^2 = \frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})^2 \qquad s = \sqrt{s^2} \]
where \(s^2\) is the variance, \(s\) is the standard deviation, and \(x_i - \bar{x}\) is how far each day sits from the mean. (We divide by \(n-1\), not \(n\), so a sample gives an unbiased estimate; R does this for you.) The IQR (interquartile range) is the width of the middle 50% of the data. Sort the days and split them into four equal quarters; the three cut points are the quartiles. So \(Q_1\) (the 25th percentile) is the value a quarter of the days fall below, \(Q_3\) (the 75th percentile) the value three-quarters fall below, and the IQR is the gap between them, \(\text{IQR} = Q_3 - Q_1\).
The boxplot and the five-number summary
Mean, median, spread: a boxplot shows them all at once. It is built from the five-number summary, the minimum, the three quartiles, and the maximum:
In the boxplot below, the box spans Q1 to Q3 (that middle-50% IQR), the line inside the box is the median, and the whiskers reach out toward the extremes:
A short box low down with a long reach upward is the same right skew you saw in the histogram, said a second way. There is also a precise rule for "surprise": a value is flagged as an outlier when it sits more than \(1.5 \times \text{IQR}\) beyond Q3 (or below Q1). R's own boxplot() draws any such value as a separate point past the whisker, and for Maya's data that is exactly the $905 festival day. You will pin it down in code next.
Flag the outlier with the 1.5xIQR rule
The upper outlier fence is \(Q_3 + 1.5 \times \text{IQR}\). Compute it for Maya's revenue, then count how many days exceed it. Fill in the blank with the function that returns the interquartile range.
Show answer
q3 <- as.numeric(quantile(revenue, 0.75))
fence <- q3 + 1.5 * IQR(revenue)
fence
#> [1] 442.5
sum(revenue > fence)
#> [1] 1References
A few authoritative places to take this further:
- R for Data Science (2e), Exploratory Data Analysis - the canonical free chapter on EDA in R: variation, distributions, and the questions to ask.
- NIST/SEMATECH e-Handbook of Statistical Methods, EDA - the authoritative reference on EDA techniques and the philosophy behind looking before testing.
- OpenIntro Statistics (free) - chapter 2 explains center, spread, histograms and boxplots from scratch, with worked examples.
- ggplot2 reference: geom_histogram() - every option for the histogram you built, including binning and
binwidth.
Lesson 1 complete
You have a repeatable way to meet any dataset, and you can now describe a single variable properly. You ran the 7-step EDA framework, read a histogram for shape and skew, chose the median over the mean when an outlier was lurking, measured spread with the robust IQR, and flagged the $905 festival day with the 1.5xIQR rule.
Next, Lesson 2: Two variables and correlation. Maya does not just want to know how revenue behaves on its own, she wants to know what moves with it, foot traffic, weather, the day of the week. You will plot two variables against each other, read the relationship, and measure it with correlation.