Lesson 6 of 8

Distributions & Transformations

In Lesson 5 you explored the categories in Maya's bakery data, what sells and how it is ordered. Now we come back to her numbers, but we ask a deeper question than "what is a typical value?" We ask: what is the shape of the whole distribution?

Below is what each of 50 customers spent on a single visit to Maya's bakery, in dollars. Most spend a few dollars on a coffee and a pastry; a handful spend far more. That lopsided picture, a tall stack on the left and a long thin tail reaching right, is the most common shape in real data, and reading it is today's skill.

Shape matters for a practical reason: almost every method you meet later, the t-test, linear regression, control charts, quietly assumes your data is roughly symmetric. Feed them a lopsided variable and they mislead. So before you model, you read the shape, and when it is badly skewed, you fix it.

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

  • Read a distribution's shape from a histogram: its peaks, its symmetry, and the weight of its tails
  • Tell right skew from left skew, and measure it with a number
  • Check whether a variable is normal using a Q-Q plot
  • Transform a skewed variable with log, square-root or Box-Cox so later methods behave

Prerequisites: you can run R and load a package with library(), and you have met the histogram, the mean, the median and the boxplot in Lesson 1. Every new term is defined as it appears.

The three questions

What "shape" really means

A distribution's shape is the silhouette its histogram traces out, and you read it by asking three questions in order:

  1. How many peaks? One hump, two, or more. This is the distribution's modality.
  2. Is it symmetric or skewed? A symmetric shape looks the same flipped left-to-right (a bell). A skewed shape leans, with one tail longer than the other.
  3. How heavy are the tails? Do extreme values trail off quickly, or is there a long thin reach toward one side?

Let us build Maya's spend and look. Each lesson runs in a fresh R session, so we create the data right here (run this once); it is fixed, so every number below is exact.

RInteractive R
# What 50 customers each spent on one visit to Maya's bakery, in dollars spend <- c(28, 25, 37, 8, 13, 10, 3, 27, 13, 12, 14, 16, 4, 41, 34, 26, 9, 12, 6, 78, 24, 6, 35, 18, 15, 3, 25, 30, 18, 10, 3, 6, 21, 9, 6, 14, 3, 7, 9, 7, 7, 44, 18, 15, 5, 12, 12, 9, 44, 7) spend_df <- data.frame(spend = spend) # a tidy frame for ggplot later length(spend) #> [1] 50 summary(spend) #> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 3.00 7.00 12.50 17.16 24.75 78.00

  

Now read the histogram against the three questions: one peak (unimodal), clearly skewed to the right (the bulk piles up on the low side, the tail leans right), and a heavy right tail, that lone $78 catering order sits far from everyone else. That last point is the same $78 the summary reports as the maximum, more than four times the median.