Chalk−1

Math · Introductory statistics · Concept

Linear regression, correlation and residuals

Linear regression fits the line of best fit, the least-squares line y^=a+b⁢x that makes the squared vertical misses from the points, the residuals, as small as possible. The slope b estimates how much y changes per unit of x. The correlation r, between −1 and 1, measures the strength and direction of a linear pattern, and r2 gives the fraction of the variation in y that the line accounts for.

Try one — Chalk checks each line

A concrete case

Iced coffee and the heat

A café counts the iced coffees it sells on four days. At 15 °⁢C it sells 22, at 20 °⁢C 31, at 25 °⁢C 38 and at 30 °⁢C 49. Plotted against temperature, the points climb almost in a line. Which line describes them best?

One guess runs through the first and last points. It rises 27 cups over 15 degrees, 1.8 cups per degree, and predicts 22, 31, 40 and 49. It hits three points exactly and misses the 25 °⁢C day by 2 cups. Its vertical misses, sold minus predicted, are 0, 0, −2 and 0.

Another line predicts 21.8, 30.6, 39.4 and 48.2. It hits no point exactly, and its misses are 0.2, 0.4, −1.4 and 0.8. Square the misses and add them. The first line gives 4, and the second only 2.8. Judged by squared misses, the second line fits better, and no line does better than 2.8. It is the least-squares line, cups ≈−4.6+1.76×temperature.

0.22+0.42+(−1.4)2+0.82=2.8
0.22+0.42+(−1.4)2+0.82=2.8

The rule

The least-squares line

Write y^, read “y-hat”, for the value a line predicts. The least-squares line y^=a+b⁢x, with slope b and intercept a, makes the sum of squared residuals as small as possible. With x¯ and y¯ the averages of x and y, the slope is the sum of the products of the deviations divided by the sum of the squared x-deviations. The intercept puts the line through the point (x¯,y¯).

b=∑(xi−x¯)(yi−y¯)∑(xi−x¯)2,a=y¯−bx¯
b=∑(xi−x¯)(yi−y¯)∑(xi−x¯)2a=y¯−bx¯

Slope from summary statistics

The slope can also come from the correlation r and the two standard deviations sx and sy. A report that gives only the averages, the standard deviations and r is enough to write the line.

b=rsysx,a=y¯−bx¯
b=rsysxa=y¯−bx¯

Reading the slope and intercept

The slope b is the predicted change in y for a one-unit increase in x, in y’s units per unit of x. For the café, b=1.76 cups per degree. The intercept a is the prediction at x=0. It means something only when x=0 lies in or near the data. At 0 °⁢C the café line predicts −4.6 cups, which is impossible, because 0 °⁢C is far below any day in the data.

Residuals

A residual is observed minus predicted, e=y−y^. A point above the line has a positive residual, and a point below it a negative one. The residuals of a least-squares line always add to 0. A residual plot, residuals against x, should look like a patternless band around 0.

ei=yi−y^i

Correlation

The correlation r measures how closely the points follow a straight line. It runs from −1 to 1, and its sign matches the slope. Values near ±1 mean the points lie close to a line, and values near 0 mean no linear pattern. r has no units, and swapping x and y leaves it unchanged.

r=∑(xi−x¯)(yi−y¯)∑(xi−x¯)2∑(yi−y¯)2
r=Sx⁢ySx⁢xSy⁢ySx⁢y=∑(xi−x¯)(yi−y¯)

r squared

r2 is the fraction of the variation in y that the line explains. SST, the total sum of squares Σ⁢(yi−y¯)2, measures how much y varies around its average. SSE, the sum of squared residuals, is the part the line leaves unexplained. For the café, SST=390 and SSE=2.8, so r2=1−2.8⁢/390≈0.993. An r2 of 0.993 says the line accounts for 99.3⁢% of the variation, not that predictions are 99.3⁢% accurate.

r2=1−SSESST

Why it works

Why least squares gives this line

Start with the intercept. For any slope, shifting the line up or down by the average residual lowers the sum of squared residuals. The best line therefore has residuals that average 0, and that puts it through (x¯,y¯).

Now measure every point from the averages. Write u=x−x¯ and v=y−y¯. A line through (x¯,y¯) with slope b predicts b⁢u, so its squared residuals add to Σ⁢(v−b⁢u)2. Expanding gives Sy⁢y−2⁢bSx⁢y+b2Sx⁢x, where Sx⁢x=Σu2, Sx⁢y=Σ⁢u⁢v and Sy⁢y=Σv2.

That expression is a parabola in b that opens upward. Its lowest point is at b=Sx⁢y/Sx⁢x, which is the slope formula. Putting that b back in leaves SSE=Sy⁢y−Sx⁢y2/Sx⁢x, and SSE⁢/⁢SST=1−r2, so r2 is exactly the share of variation the line explains. For the café, Sx⁢y=220, Sx⁢x=125 and Sy⁢y=390, which give b=1.76 and SSE=390−2202/125 =2.8.

∑(v−b⁢u)2=Sy⁢y−2⁢bSx⁢y+b2Sx⁢x
∑(v−b⁢u)2=Sy⁢y−2⁢bSx⁢y+b2Sx⁢x

The method

Fitting a line in six steps

The table fits the study-hours data from the first worked example, five students’ hours and scores (1,55), (2,62), (3,64), (4,71) and (5,78). A deviation table, with columns for x−x¯, y−y¯, their product and their squares, keeps the sums straight.

Fitting study hours and exam scores
StepWhat to doFor the study data
1Plot the points, and check that the pattern is straight with no point far from the rest.Five points rising in a nearly straight band.
2Find x¯ and y¯.3 hours and 66 points.
3Find Sx⁢y, Sx⁢x and Sy⁢y from the deviations.55, 10 and 310.
4Find b =Sx⁢y/Sx⁢x, then a =y¯−bx¯.b=5.5 and a=49.5.
5Find r =Sx⁢y/Sx⁢x Sy⁢y and r2.r ≈0.988 and r2 ≈0.976.
6Find the residuals, check that they add to 0, and interpret the slope.0, 1.5, −2, −0.5 and 1. Each hour goes with 5.5 more points.

Where it breaks

Where it breaks

A straight line is the wrong model for curved data. A curve shows up as a pattern in the residual plot, such as positive residuals at both ends and negative ones in the middle. Bacterial counts that double each hour need an exponential model, not a line.

r measures only straight-line association. The points (−2,4), (−1,1), (0,0), (1,1) and (2,4) lie exactly on y=x2, yet r=0. A correlation near 0 does not mean the variables are unrelated.

One point far from the rest can move the line a great deal. A point with an extreme x pulls hard on the line, and when it also falls off the pattern it can tilt the slope by itself. Fit the line with and without such a point, and report both if they differ.

The least-squares line can be fit to any data. Inference from it, such as a confidence interval for the slope, also needs residuals with roughly constant spread (no fan shape in the residual plot), residuals without big outliers, and independent observations. Data collected over time, such as a stock price each day, often fail the last.

Extrapolation, predicting outside the range of the data, trusts the pattern where there is no evidence for it. The study line predicts 115.5 points for 12 hours of study, more than a perfect score.

Correlation is not causation. Ice cream sales and drownings rise together because both rise in hot weather, a lurking variable. Only a designed experiment supports a causal claim.

Predicting x from y gives a different line. Take the used cars of the second example, worth 20, 17, 15, 11 and 7 thousand dollars at ages 0 to 4 years. The slope of value on age is −32⁢/10=−3.2. The slope of age on value divides the same −32 by the value deviations’ squares, which add to 104, so it is −32⁢/104≈−0.308, not 1⁢/⁢(−3.2)≈−0.3125. The two lines agree only when r=±1.

Check your answer

Check your answer

A fitted line has to agree with the data that made it, in three places you can test.

The line passes through (x¯,y¯). Put x¯ into the equation, and the prediction must be y¯. For the study data, 49.5+5.5×3=66.

The residuals add to 0, up to rounding. For the study data they are 0, 1.5, −2, −0.5 and 1. Their squares add to SSE=7.5, and 1−7.5⁢/310≈0.976 matches r2.

The signs agree. b and r always have the same sign, and r2 lies between 0 and 1. Regression in Statistics fits the line and shows r, R2 and the residuals.

To have Chalk check the work, write each step as arithmetic in a Math box, such as 55⁢/10=5.5 for the slope and 66−5.5⋅3=49.5 for the intercept. The Checker marks each row ✓, or ✗ when a number is wrong. A row that names its quantity, b=55⁢/10=5.5, has its arithmetic checked but shows “?”, because Chalk has no formula for b to check.

Write a prediction as plain arithmetic too, 49.5+5.5⋅3.5=68.75. Under the line y^=49.5+5.5⁢x, the row y^=49.5+5.5⋅3.5 =68.75 reads ✓, because it puts x=3.5 into the line. A wrong value there reads ✗.

The x-values are heights in inches, and you convert them to centimeters before fitting. What happens to r and to the slope b?

r stays the same and b changes. r has no units, and rescaling x stretches the scatterplot sideways without changing how closely the points follow a line. The slope is in y’s units per unit of x, and an inch is 2.54 cm, so the new slope is the old one divided by 2.54. The predictions for each person stay the same.

Regression to the mean, and beyond one line

In standard units the line is simple. A point z standard deviations from the average of x is predicted to sit r⋅z standard deviations from the average of y. With ∣⁢r⁢∣<1, the prediction is always closer to average than the starting value. Francis Galton saw this in 1886 in the heights of parents and their children and called it regression, which gave the method its name.

The slope from one sample is an estimate. Inference for regression treats it like a sample mean, with a standard error and a t distribution on n−2 degrees of freedom. A test of slope 0 asks whether the association could be chance.

With several explanatory variables, multiple regression fits y^=a+b1x1+b2x2+… by the same least-squares rule. Each slope then estimates the change in y when one variable moves and the others are held fixed.

Common mistakes

  • Reading r2=0.976 as “the line is right 97.6% of the time”. r2 is the share of the variation in y that the line explains. Single predictions still miss, as the −2 residual at 3 hours shows.
  • Extrapolating far outside the data. The study line gives 115.5 points at 12 hours, beyond a perfect score. A line describes the pattern only where there are data, here 1 to 5 hours.
  • Treating a strong correlation as proof of cause and effect. Ice cream sales and drownings are correlated because hot weather drives both. Only an experiment that sets x can show that x causes y.
  • Computing a residual as predicted minus observed. At 4 hours the line predicts 71.5 and the score was 71, so the residual is 71−71.5=−0.5, a point just below the line.
  • Taking r=0 to mean no relationship. Points on y=x2 from −2 to 2 have r=0 but follow a perfect curve. r measures only how well a straight line fits.
  • Inverting the slope to predict x from y. The slope of age on value is −0.308, not 1⁢/⁢(−3.2). Decide which variable responds, put it on the y-axis and fit that line.

Key terms

Linear regression
Fitting a straight line y=a+b⁢x to paired data, usually by least squares, where a is the intercept and b the slope. In the broader statistical sense any model that is linear in its fitted coefficients is a linear model, so a polynomial fit counts too.
Least squares
Choosing the fit that makes the sum of the squared residuals as small as possible. Squaring stops positive and negative misses from canceling and weights big misses more.
Residual
Observed value minus predicted value, y−y^. A positive residual means the point lies above the fitted line.
Residual plot
A plot of the residuals against x or the predicted values. A random scatter suits the model. A curve or a fan shape shows the model is missing something.
Pearson correlation
r, a number from −1 to 1 measuring how closely paired data follow a straight line: positive when y tends to rise with x, negative when it tends to fall. r=0 doesn’t rule out a curved relationship, and correlation doesn’t prove causation.
Coefficient of determination
R2, the share of the variation in y that a model explains: R2=1−SSE⁢/⁢SST, where SSE is the sum of squared residuals and SST=∑⁢(y−y¯)2 is the total variation about the mean. For a least-squares line with an intercept it equals r2. For other models it can even be negative.
Extrapolation
Using a fitted model outside the range of the data. The formula still gives numbers, but the real relationship there may be different.
Exponential regression
Fitting a curve y=A⋅eb⁢x to data, where each equal step in x multiplies y by the same factor. This app fits the curve to y directly, which can differ from fitting a line to lny.

Work through an example

Five students report hours studied and exam scores: (1,55), (2,62), (3,64), (4,71) and (5,78). Find the least-squares line, r and r2, and predict the score after 3.5 hours.

Fit a least-squares line and interpret r2 →

Interpret a negative correlation →

Fit an exponential model to growth data →

Sources and scope

Authored study material. Tool results depend on the stated inputs and model assumptions.

After reading

Try it, then study it

Open the example in its tool to check steps or change values, then review the key terms as flashcards.

Fit the line in Statistics Check the line in Math Study the 8 key terms Open worked example on a board Statistics formulas in Math Reference

Your existing work stays on this device. Examples open as editable copies.