Simple linear regression: the least-squares line, residuals and R²
Ten problems on fitting a straight line by least squares: the slope and intercept from sums of squares, residuals that sum to zero, the split of the total sum of squares and R² as the squared correlation, the slope from a correlation and two standard deviations, the standard error of the slope with a t test and confidence interval, what changes with the units, one point of high leverage, the normal equations from calculus, intervals for a mean response and for a new observation, and a negative R² on test data, with worked solutions and the mistakes of inverting the slope to predict x, dividing by n instead of n − 2, reading r as the fraction of variance explained, extrapolating far past the data and trusting training R² as a test score.
Before you start
Simple linear regression fits the line y^=b0+b1x that makes the squared vertical distances from the data to the line as small as possible. It is the one-feature case of the least-squares problems elsewhere on this site, but in statistics it comes with its own vocabulary: the slope as a ratio of sums of squares, the residuals, R2 as the share of the variation in y that the line explains, and a standard error that says how far the slope could move with new data. These ten problems compute all of them by hand on small data sets, connect the slope to the correlation, and end with the number every machine-learning library reports as a score, R2 on held-out data, which can be negative.
Data and sums of squares. Pairs (xi,yi) for ,i=1,…,n, with means xˉ and .yˉ.,Sxx=∑(xi−xˉ)2,Sxy=∑(xi−xˉ)(yi−yˉ) and .Syy=∑(yi−yˉ)2.
The least-squares line.b1=Sxy/Sxx and .b0=yˉ−b1xˉ. The line passes through .(xˉ,yˉ). With the correlation r and the standard deviations ,sx,,sy, the slope is also .b1=rsy/sx.
Residuals.,ei=yi−y^i, with .y^i=b0+b1xi. For a line with an intercept, ∑ei=0 and .∑xiei=0.
Sums of squares.,SST=Syy,SSE=∑ei2 and ,SSR=∑(y^i−yˉ)2=b12Sxx, with .SST=SSR+SSE.
.R2.,R2=SSR/SST=1−SSE/SST, and for simple regression .R2=r2. On data the line was not fitted to, R2=1−SSE/SST can be negative.
Inference.s=SSE/(n−2) estimates the noise standard deviation; two degrees of freedom go to b0 and .b1.,SE(b1)=s/Sxx, and t=b1/SE(b1) has a t distribution with n−2 degrees of freedom when the true slope is .0. Two-sided 5% cutoffs: ,t3=3.182,,t4=2.776,.t8=2.306.
Intervals at a new .x0. For the mean response, .SE=s1/n+(x0−xˉ)2/Sxx. For a single new observation, add 1 under the root: .s1+1/n+(x0−xˉ)2/Sxx.
Five students report hours studied and test scores:
x (hours)y (score)152255361464568
Find the least-squares line.
·
For the line of Problem 1, find the five residuals. Check that ∑ei=0 and ,∑xiei=0, and predict the score for 6 hours.
··
For the data of Problem 1, find ,SST,SSR and ,SSE, then .R2. Check that .R2=r2.
··
Adult heights have mean 170 cm and sd 10 cm, weights mean 70 kg and sd 12 kg, with correlation .r=0.5. Find the line predicting weight from height and the prediction at 180 cm. Then find the line predicting height from weight, and the predicted height for 82 kg.
··
For the data of Problem 1, find ,s, the standard error of ,b1, the t statistic for H0:β1=0 and a 95% confidence interval for the slope.
··
In Problem 1, the hours are re-recorded in minutes. What are the new slope, intercept and ?R2? What are the slope and intercept if instead the hours are centered, ?x′=x−3?
···
Add a sixth student to the data of Problem 1, with 10 hours and a score of .60. Find the new line and ,R2, and explain what the one point did.
···
Minimize SSE(b0,b1)=∑(yi−b0−b1xi)2 with calculus. Derive the two normal equations, and show they give b1=Sxy/Sxx and .b0=yˉ−b1xˉ.
···
For the data of Problem 1 at x0=6 hours, find a 95% confidence interval for the mean score and a 95% prediction interval for one new student's score.
···
The line of Problem 1 is tested on two new students, with (6,66) and .(7,70). Find R2=1−SSE/SST on these two points, where SST uses their own mean.
Answers
y^=47.7+4.1x
Residuals ,0.2,−0.9,1,−0.1,−0.2, summing to 0 with ;∑xiei=0; at 6 hours, y^=72.3
,SST=170,,SSR=168.1,,SSE=1.9,;R2≈0.989;r=41/10⋅170≈0.9944 and r2≈0.989
Weight ,=−32+0.6⋅height,76 kg at 180 cm; height ,=170+0.417(weight−70),175 cm at 82 kg
Mean score: ;72.3±2.66≈(69.6,75.0); one new student: 72.3±3.67≈(68.6,76.0)
R2≈−9.08
Worked solutions
Problem 1
Five students report hours studied and test scores:
x (hours)y (score)152255361464568
Find the least-squares line.
,xˉ=3,yˉ=300/5=60The two means.
;x−xˉ=−2,−1,0,1,2;y−yˉ=−8,−5,1,4,8Center both variables.
;Sxy=16+5+0+4+16=41;Sxx=10Products of the centered values, and squares of the centered .x.
;b1=41/10=4.1;b0=60−4.1⋅3=47.7Slope, then the intercept from the point .(xˉ,yˉ).
y^=47.7+4.1xEach extra hour goes with 4.1 more points, on average, in these data.
Problem 2
For the line of Problem 1, find the five residuals. Check that ∑ei=0 and ,∑xiei=0, and predict the score for 6 hours.
y^=51.8,55.9,60,64.1,68.247.7+4.1x at .x=1,…,5.
e=0.2,−0.9,1,−0.1,−0.2Observed minus fitted.
∑ei=0.2−0.9+1−0.1−0.2=0The intercept makes the residuals balance.
∑xiei=0.2−1.8+3−0.4−1=0The slope makes the residuals uncorrelated with .x.
Residuals ,0.2,−0.9,1,−0.1,−0.2, summing to 0 with ;∑xiei=0; at 6 hours, y^=72.3The two zero sums are the normal equations (Problem 8), so they hold for every least-squares line with an intercept.
Problem 3
For the data of Problem 1, find ,SST,SSR and ,SSE, then .R2. Check that .R2=r2.
SST=64+25+1+16+64=170The squared centered scores from Problem 1.
SSE=0.04+0.81+1+0.01+0.04=1.9The squared residuals from Problem 2.
SSR=b12Sxx=16.81⋅10=168.1And .168.1+1.9=170.
R2=168.1/170≈0.9888The share of the variation in y explained by the line.
,SST=170,,SSR=168.1,,SSE=1.9,;R2≈0.989;r=41/10⋅170≈0.9944 and r2≈0.989.r2=Sxy2/(SxxSyy)=b12Sxx/Syy=SSR/SST.
Problem 4
Adult heights have mean 170 cm and sd 10 cm, weights mean 70 kg and sd 12 kg, with correlation .r=0.5. Find the line predicting weight from height and the prediction at 180 cm. Then find the line predicting height from weight, and the predicted height for 82 kg.
b1=rsy/sx=0.5⋅12/10=0.6 kg per cmThe slope from the correlation.
b0=70−0.6⋅170=−32The line passes through the means.
At 180 cm: −32+0.6⋅180=76 kgOne sd above the mean in height predicts only 0.5 sd above in weight: regression toward the mean.
Height on weight: slope rsx/sy=0.5⋅10/12≈0.417 cm per kgSwap the roles of x and ;y; the correlation is the same.
Weight ,=−32+0.6⋅height,76 kg at 180 cm; height ,=170+0.417(weight−70),175 cm at 82 kg82 kg is one sd above the mean, so the prediction is half an sd, 5 cm, above. Inverting the first line gives a slope of 1/0.6≈1.67 instead (Mistake 1).
Problem 5
For the data of Problem 1, find ,s, the standard error of ,b1, the t statistic for H0:β1=0 and a 95% confidence interval for the slope.
s2=SSE/(n−2)=1.9/3≈0.6333Two degrees of freedom are used by b0 and .b1.
s≈0.7958The typical size of a residual.
SE(b1)=0.7958/10≈0.2517.s/Sxx.
,t=4.1/0.2517≈16.29, with 3 degrees of freedomFar past the cutoff ;3.182; two-sided .p≈0.0005.
,s≈0.796,,SE(b1)≈0.252,;t≈16.3;95% interval 4.1±3.182⋅0.2517≈(3.30,4.90)With only 5 points the t cutoff is ,3.182, not .1.96. Dividing by n instead of n−2 gives an interval that is too narrow (Mistake 2).
Problem 6
In Problem 1, the hours are re-recorded in minutes. What are the new slope, intercept and ?R2? What are the slope and intercept if instead the hours are centered, ?x′=x−3?
,xmin=60x, so Sxy and Sxx scale by 60 and 3600Every centered x is multiplied by .60.
b1=4.1/60≈0.0683 points per minuteThe slope is per unit of ,x, so it shrinks by the factor .60.
;b0=60−0.0683⋅180=47.7;R2 unchangedThe fitted values are the same, so the residuals and R2 are too.
Minutes: ,b1≈0.0683,,b0=47.7,;R2≈0.989; centered: ,b1=4.1,b0=60A slope's size depends on the units, so it is not by itself a measure of how strong the relationship is. R2 and r do not depend on units.
Problem 7
Add a sixth student to the data of Problem 1, with 10 hours and a score of .60. Find the new line and ,R2, and explain what the one point did.
,xˉ=25/6≈4.167,yˉ=360/6=60The mean score does not move, because the new score is .60.
Sxy=∑xi(yi−60)=41The new point has ,y−yˉ=0, so it adds nothing; and ∑(yi−yˉ)=0 lets xˉ drop out.
Sxx=155−6xˉ2≈50.83;∑xi2=55+100=155; the new point is far from the others in .x.
;b1=41/50.83≈0.807;b0=60−0.807⋅4.167≈56.64Slope, then intercept.
,y^≈56.64+0.807x,,R2≈0.195, down from 0.989SSR=0.8072⋅50.83≈33.07 of .SST=170. One point with an extreme ,x, a point of high leverage, cut the slope by a factor of .5. Plot the data before trusting a line.
Problem 8
Minimize SSE(b0,b1)=∑(yi−b0−b1xi)2 with calculus. Derive the two normal equations, and show they give b1=Sxy/Sxx and .b0=yˉ−b1xˉ.
∂SSE/∂b0=−2∑(yi−b0−b1xi)=0That is, .∑ei=0.
∂SSE/∂b1=−2∑xi(yi−b0−b1xi)=0That is, .∑xiei=0.
First equation over :n:yˉ−b0−b1xˉ=0So ,b0=yˉ−b1xˉ, and the line goes through .(xˉ,yˉ).
,b1=∑xi(xi−xˉ)∑xi(yi−yˉ)=SxxSxy,b0=yˉ−b1xˉReplacing xi by xi−xˉ in front changes neither sum, because ∑(yi−yˉ)=0 and .∑(xi−xˉ)=0. The Hessian has determinant ,4nSxx>0, so this is a minimum.
Problem 9
For the data of Problem 1 at x0=6 hours, find a 95% confidence interval for the mean score and a 95% prediction interval for one new student's score.
y^0=72.3 and (x0−xˉ)2/Sxx=9/10=0.9Problem 2, and the distance of x0 from the center of the data.
Mean: SE=0.79580.2+0.9≈0.8347.1/n=0.2.
New student: SE=0.79581+0.2+0.9≈1.1533One more s2 for the student's own scatter around the line.
Margins 3.182⋅0.8347≈2.66 and 3.182⋅1.1533≈3.67The t cutoff with 3 degrees of freedom.
Mean score: ;72.3±2.66≈(69.6,75.0); one new student: 72.3±3.67≈(68.6,76.0)The prediction interval is always wider. More data shrinks the first toward ,0, but the second never shrinks below about .±1.96σ.
Problem 10
The line of Problem 1 is tested on two new students, with (6,66) and .(7,70). Find R2=1−SSE/SST on these two points, where SST uses their own mean.
Predictions 72.3 and 76.447.7+4.1⋅6 and .47.7+4.1⋅7.
Errors −6.3 and ,−6.4, so SSE=39.69+40.96=80.65Both students score well below the line.
Test mean ,68, so SST=4+4=8The spread of the test scores around their own mean.
R2=1−80.65/8SSE is ten times .SST.
R2≈−9.08A negative R2 means the line predicts worse than the constant ,68, the test set's own mean. On training data with an intercept R2≥0 is guaranteed; on new data it is not (Mistake 5).
Where this goes wrong
1. Inverting the slope to predict x from y
Problem 4 has a line for weight from height, and solving it for height looks like the line for height from weight.
Weight =−32+0.6⋅heightRight so far.
“Solve for height: height .=(weight+32)/0.6.”That line minimizes the vertical distances to weight, not the distances in height. Least squares in the other direction is a different problem, with slope .rsx/sy.
At 82 kg: height =114/0.6=190 cmThe right prediction is 175 cm. The inverted slope 1/0.6≈1.67 cm per kg is four times ,0.417, since the two slopes multiply to ,r2=0.25, not to .1.
2. Dividing SSE by n instead of n − 2
Problem 5 needs an estimate of the noise variance, and an average of squared residuals looks like dividing by .n.
,SSE=1.9,n=5Right so far.
“.s2=1.9/5=0.38.”The residuals are forced to satisfy two equations, ∑ei=0 and ,∑xiei=0, so only n−2 of them are free, and SSE/n underestimates σ2 on average.
,SE(b1)≈0.195, interval ≈(3.48,4.72)With :n−2=3:SE(b1)≈0.252 and interval .(3.30,4.90). The wrong interval is about 23% too narrow, and it covers the true slope only about 91% of the time instead of .95%.
3. Reading r as the fraction of variance explained
The correlation of Problem 4 is ,0.5, and a half sounds like a share.
r=0.5 between height and weightRight so far.
“Height explains 50% of the variation in weight.”The fraction explained is ,R2=r2, not .r.
50% of the variance in weight is explainedIt is .r2=25%. A correlation of 0.5 leaves three quarters of the variance unexplained, and even r=0.7 explains less than half.
4. Extrapolating far past the data
The line of Problem 1 has a clean ,R2≈0.989, so it seems safe to use anywhere.
,y^=47.7+4.1x, fitted for 1≤x≤5Right so far.
“A student who studies 40 hours scores .47.7+4.1⋅40.”The data say nothing about the relationship outside 1 to 5 hours, and the prediction interval of Problem 9 already widens with .(x0−xˉ)2.
y^=211.7 at 40 hoursOn a 100-point test. A high R2 says the line fits the range it was fitted on; it says nothing about where the line stops being a line.
5. Trusting training R² as a test score
The line of Problem 1 has R2≈0.989 on its own data, and that number is reported as how good the model is.
R2≈0.989 on the five training pointsRight so far.
“So the model will explain about 99% of the variation in new scores.”Training R2 measures the fit to the points that chose the line. With few points and a flexible model it is optimistic, and it says nothing about new data from a different group.
Expected test R2≈0.99On the two test students of Problem 10, .R2≈−9.08. Report R2 on held-out data, and remember it is not bounded below by .0.