x <- c(3, 5, 5, 7, 20) # note the outlier
c(mean = mean(x), median = median(x)) mean median
8 5
# R has no built-in mode; here is the quick one:
mode_val <- as.numeric(names(which.max(table(x))))
c(mode = mode_val)mode
5
Measures of central tendency are a way to summarize a distribution of values with a single value. This single value is the “best guess” for future, unknown values that may be observed. In this section, you will learn about the most common measures of central tendency and how to use them to make sense of your data.
Back in the introduction we asked you to guess a family member’s weight, and claimed your best guess was the arithmetic mean. Here we make good on that claim. There are three classic summaries of “the middle,” and each is the best guess under a different definition of “best.”
x <- c(3, 5, 5, 7, 20) # note the outlier
c(mean = mean(x), median = median(x)) mean median
8 5
# R has no built-in mode; here is the quick one:
mode_val <- as.numeric(names(which.max(table(x))))
c(mode = mode_val)mode
5
FREQUENCIES VARIABLES=x /STATISTICS=MEAN MEDIAN MODE.
using Statistics, StatsBase
mean(df.x)
median(df.x)
mode(df.x)Here is the deep idea that ties this chapter to the entire rest of the book. Suppose you must pick a single number \(c\) to guess every value, and you will be penalized by the squared error of each guess. Which \(c\) minimizes your total penalty? Let’s just try every candidate and see:
guesses <- seq(0, 20, by = 0.1)
sse <- sapply(guesses, function(c) sum((x - c)^2)) # sum of squared errors
best <- guesses[which.min(sse)]
plot(guesses, sse, type = "l", lwd = 2, col = "steelblue",
xlab = "guess (c)", ylab = "sum of squared errors",
main = "The mean minimizes squared error")
abline(v = mean(x), col = "red", lwd = 2, lty = 2)
c(best_guess = best, the_mean = mean(x))best_guess the_mean
8 8
The valley of that curve sits exactly at the mean. That is not a coincidence - it is the reason the mean matters. When we do regression later and “minimize the sum of squared residuals,” we are doing this same thing in more dimensions. The mean is least-squares estimation with no predictors.
The median has its own claim to fame: it minimizes the sum of absolute errors, \(\sum |x - c|\). Different penalty, different winner. And the mode simply asks “what shows up most?” - the only one of the three that makes sense for a categorical variable like ice-cream flavor.
Look again at that outlier (20). Squaring errors makes the mean very sensitive to extreme values; one billionaire walks into the room and the “average” net worth becomes absurd. The median shrugs it off:
income <- c(30, 35, 40, 42, 45, 48, 5000) # thousands; one very rich person
c(mean = mean(income), median = median(income)) mean median
748.5714 42.0000
FREQUENCIES VARIABLES=income /STATISTICS=MEAN MEDIAN.
using Statistics
mean(df.income)
median(df.income)The median (about 42) describes a typical person; the mean (about 749) describes nobody in the room. This is why you should reach for the median whenever a distribution is skewed or outlier-ridden, and the mean when it is roughly symmetric.
Median or mean isn’t the only choice. Robust estimators - the trimmed mean (drop a fixed fraction of each tail, average the rest) and the MAD (a median-based cousin of the SD) - keep most of the mean’s efficiency while resisting the outliers that wreck it. The mean’s breakdown point is essentially 0% (one bad value can move it anywhere); the median’s is 50%. We develop this fully in Beyond the Normal Curve.
There is no universally “correct” measure of center. There is only the one that answers your question honestly.
y <- rexp(200). Compute its mean and median. Which is larger, and why?y, then make a second plot of \(\sum|y - c|\) and confirm its minimum sits at the median.y (say, c(y, 500)) and recompute both. Which summary barely moved? State the lesson in one sentence.A center is only half the story. Two distributions can share the same mean and be wildly different - one tightly packed, one spread all over. That spread is our uncertainty, and it is the single most important quantity in statistics. On to dispersion.