Reading residual plots and choosing a transformation… | StemPy Lab
Free previewCourse: Standard
Reading residual plots and choosing a transformation
A high r can sit on top of a line with the wrong shape. This lesson reads residual plots honestly: what a pattern means, and how often pure chance draws one. Then it chooses a transformation from the plot, and reads a power law on log-log axes.
This is the first lesson of Residuals, Leverage and Over-fitting: Advanced Problem Workshop, free for everyone to read.
What you will be able to do
read a residual plot for its shape, its spread and its single points, and say what each one means;
judge whether a pattern in a small residual plot is real or could be chance;
choose a power, a square root or a logarithm from the shape of the bend, and read the slope of a log-log line as a power.
Ten fish that look straight
In the free course on the least-squares line you met the residual plot. Here we go further. A made-up fish farm weighs ten fish.
Length (cm)
Mass (g)
12
21
15
37
18
71
21
121
24
154
27
237
30
291
33
455
36
534
40
786
A straight line gives r=0.952. Many people would stop there. But the residuals, from the shortest fish to the longest, have the signs ++−−−−−−−+. The two ends lie above the line and the middle lies below it. That is a U, so a straight line is the wrong shape. The misses at the right end are also larger than at the left.
Three graphs side by side for ten made-up fish. The left graph plots mass in grams against length in centimetres, from 12 to 40 cm, with a straight grey line through the points. The middle graph shows the residuals of that line against length: they are positive at both ends and negative in the middle, and the two end residuals are the largest. The right graph shows the residuals of a line fitted to the logarithms of both variables, against log length: they are small and scattered above and below zero with no pattern.
Four questions for every residual plot
Is there a bend? A U or an upside-down U says the form is wrong.
Does the spread change? A fan that opens to the right says the size of a miss grows with x. Errors that grow in proportion to the value often come from a quantity that multiplies.
Do neighbours agree? If the data were taken in time order, long runs of the same sign say that one week affects the next. The points are then not independent.
Is one point far away? A single big residual is an outlier. The next lesson shows that a dangerous point can hide with a small residual.
Reading honestly: chance draws patterns too
A residual plot of eight points is a small sample. Chance alone can draw a gentle U. The program below makes 2000 data sets of eight points from a perfectly straight model with random noise. It gives each residual plot a bend score: the correlation of the residuals with (x−xˉ)2. A score above 0.6 or below −0.6 looks clearly bent to the eye.
About 300 of the 2000 straight data sets look bent. That is roughly one in seven. So a mild bend in a few points is weak evidence. Trust a pattern when it is strong, when it has a reason (a square, a growth by factors), and when it stays as you add data. The ten fish pass all three tests: the U is strong, and a mass should grow like a volume.
Four residual plots in a two by two grid, labelled sample A to sample D, each with eight points against x from 1 to 8 and a line at zero. Every sample came from a perfectly straight model with random noise. Samples A and B have bend scores of minus 0.67 and minus 0.91 and look like upside-down U shapes. Samples C and D have bend scores of 0.01 and minus 0.21 and look patternless.
Choosing a transformation from the bend
The ladder of powers lists the usual changes of scale: y2, y, y, 3y, logy. Going down the ladder pulls in the large values more and more. The rule of thumb reads the shape of the bend:
The points bend upwards (a U in the residuals): take y down the ladder (a root or a log), or take x up.
The points bend downwards (an upside-down U): take x down the ladder (x or logx), or take y up.
A fan that opens to the right: a log of y often evens the spread, because it turns a multiplied error into an added one.
Try it on the fish. A mass grows like length times width times depth, so like a cube. The simulation raises the mass to a power p.
Near p=0.3 the U disappears. The cube root is p=31, as the volume idea predicts. Go all the way to p=0, the logarithm of the mass alone, and the plot bends the other way. One step too far down the ladder over-corrects.
A power law on log-log axes
For a law y=cxb there is a better route. Take logs of both sides:
log10y=log10c+blog10x.
So log10y against log10x is a straight line, and its slope is the power b. For the fish the log-log line is
log10m=−1.931+3.003log10L,
with r=0.9985 and residual signs that jump about with no pattern. The slope 3.003 is almost exactly 3.
Reading the slope as a power. A fish 10% longer is predicted to be 1.13.003≈1.331 times as heavy, so about 33% heavier, not 30%. A fish twice as long is about 23=8 times as heavy.
Predicting. For a fish 25 cm long, the log-log line gives 10−1.931+3.003log1025≈185 g. The straight line through the raw data said 255 g, far too much, because the middle of the U lies below that line.
One caution. The log-log line predicts the mean of logm. Undone, that is a typical mass (a geometric mean), a little below the average mass of such fish. For small scatter on the log scale, the gap is small.
A curve that bends down: a pendulum
A made-up school class times one swing, there and back, of a pendulum of six lengths.
Length (m)
Time of a swing (s)
0.2
0.91
0.4
1.26
0.6
1.56
0.8
1.79
1.0
2.00
1.2
2.21
A straight line gives r=0.9938, but the residual signs are −+++−−: an upside-down U. The points bend down, so take x down the ladder. Against L the fit is
T^=0.006+2.002L,
with r=0.9998 and an intercept close to 0. Physics says T=2πL/g, so the slope should be g2π. Then g≈(2.0022π)2≈9.85m/s2, close to the standard 9.81m/s2. The right transformation turned a curve into a measurement.
Practise with new numbers
Try it in Python
The program fits a straight line to the fish, prints the signs of the residuals, then does the same on log-log axes. Last, it counts how often straight data of eight points give a residual plot that looks bent.
The usual mistakes
Stopping at a high r. The fish have r=0.952 and the wrong shape.
Seeing a pattern in every small plot. About one in seven straight data sets of eight points looks bent. Ask for a reason and for more data.
Logging too much. The log of the mass alone over-corrects the fish. Climb down the ladder one step at a time.
Reading a log-log slope as a percentage per percentage for big changes. A 10% longer fish is 33% heavier, not 30%. Raise the factor to the power.
Forgetting to undo the transformation. A prediction of log10m is not a mass.
Remember these
A U or an upside-down U in the residuals: the form is wrong. A fan: the spread grows. Long runs in time order: the points are not independent.
Points bend up: take y down the ladder. Points bend down: take x down the ladder. A fan: try logy.
On log-log axes a power law y=cxb is a straight line with slope b. Multiplying x by k multiplies y by kb.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
A residual plot has an upside-down U and an even spread. Which variable should you transform, and which way on the ladder?
A log-log line has slope 2.5. By what factor does the prediction of y change when x doubles?
A friend fits a line to seven points and sees a gentle U in the residuals, with a bend score of 0.65. He says the line is wrong. What do you tell him?
In the pendulum fit, suppose the slope of T against L had come out as 2.010 s per m. What value of g would that give?
Why does the logarithm of the mass alone over-correct the fish, while log-log axes give a straight line?
References
OpenStax, Introductory Statistics, Chapter 12 (Linear Regression and Correlation), the sections on residuals and outliers.
J. W. Tukey, Exploratory Data Analysis (1977), the chapter on straightening curves with the ladder of powers.
Liked this lesson? There is more
Residuals, Leverage and Over-fitting: Advanced Problem Workshop is a Standard course. It adds 2 more lessons, practice and tests on top of the free chapter.
12Problems2Python Labs1Question Set1Mock Test
Outliers, leverage and influence: One unusual point can leave the line alone, raise r
, or drag the slope a long way. This lesson tells the three apart with leverage, shows how a dangerous point hides with a small residual, and ends with groups that reverse a slope.
Regression to the mean, uncertainty and over-fitting: A line from a sample is uncertain, and luck can make a pattern of its own. This lesson builds regression to the mean from pure luck and measures the wobble of a slope by resampling. Then it tells two kinds of interval apart, and catches a curve that fits too well.