Quantile Regression
Every regression you have fit so far draws one line through the middle of the data. lm() finds the average outcome at each x, and even the robust methods from the last two lessons still aim at a single typical value. Often the middle is exactly what you want. Sometimes it hides the thing you actually need to know.
Picture a compensation analyst with a 300-person salary survey: annual income against years of experience. Among people two years in, salaries sit tightly around $57k. Among 18-year veterans, they scatter from about $70k to over $150k. She has three real questions, and one average line cannot answer any of them: what is a fair floor for a senior hire, what is a competitive top offer, and how much does the pay range widen as people gain experience?
Quantile regression answers all three, by fitting a line to the low end, the middle, and the high end separately. Drag the percentile control below and watch the lines fan apart as experience grows, the exact shape a single average line can never show.
By the end of this lesson you will be able to:
- Say why one average line hides the story when the spread grows with x
- Read a percentile off your data, and picture what a "line fit to the 90th percentile" even means
- Understand the one small twist (the check loss) that turns a mean-fitter into a percentile-fitter
- Fit every percentile at once in R with
rq(), read the fanning slopes, and turn them into an offer band
Prerequisites: you can fit a line with lm() and you know a residual is the gap between the actual value and the line's prediction. That is all. Every new idea is built up from there.
One line cannot show a widening spread
Let us build the analyst's survey so the problem is in front of us. Income rises with experience, but here is the key twist we build in on purpose: the spread of income rises too. Junior salaries are packed close together; senior salaries are all over the map.
Now fit the ordinary average line with lm(), and separately measure how far apart salaries are for juniors versus seniors, using the standard deviation (a plain measure of spread: bigger means the numbers are more scattered).
The average line tells one tidy story: pay starts near $47k and climbs about $3.3k per year of experience. Useful. But look at the two spreads. Junior salaries sit within about $9.6k of each other; senior salaries scatter by nearly $36k, almost four times as wide. The average line has a single slope and a single position, so it simply cannot express "the range gets wider on the right." That widening is exactly what the analyst needs, and it is invisible to lm().