Advanced level

Correlation and linear regression

Two quantities measured on the same objects: price and floor area, speed and fuel consumption. The correlation coefficient measures how strongly they move together, the regression line describes how, and the coefficient of determination says how much of it was really explained. Plus the most famous sentence in statistics: correlation is not causation.

Before you start

This topic builds on earlier ideas. Before you start, it's worth working through the lessons below — they'll make everything click:

Where this is used

Real situations where you count exactly the way this lesson teaches:

  • Flat price against floor area
    Sixty transactions in one district give the line ŷ = 9.2x + 45, where x is the floor area in square metres and ŷ the price in thousands. The model values a 50-square-metre flat at 9.2 · 50 + 45 = 505 thousand. The coefficient of determination is 0.78, so floor area explains 78 per cent of the variation in price — the other 22 per cent is the floor, the condition, the aspect, and how much of a hurry the seller was in.
  • Fuel consumption against speed
    Measurements on a route at speeds from 70 to 130 km/h give ŷ = 0.08v + 1.6 litres per 100 km. At 110 km/h the model predicts 10.4 litres and that is credible. Substituting 200 km/h gives 17.6 litres and is worthless: air resistance grows with the square of speed, so outside the measured range the line stops being a model of anything.
  • A laboratory calibration curve
    A spectrophotometer is calibrated with five standards at concentrations from 0.2 to 2.0 mg/l. The fitted line has r² = 0.999, so absorbance predicts concentration to within a thousandth of the variation. A sample whose absorbance falls outside the range of the standards must be diluted and measured again — the standard forbids reading it off by extrapolation, because beyond that range the detector stops being linear.
  • Forecasting demand
    A café plots daily ice-cream sales against temperature and gets a = 42 servings per degree with r = 0.86. For a day forecast at 28 degrees, 6 degrees above the summer average, the model predicts 252 servings more than usual. The coefficient of determination of 0.74 says, however, that a quarter of the variation is unexplained — the day of the week and the rain are separate factors that temperature does not stand in for.

All formulas

  • Sum of products of deviations

    Sxy=i=1n(xixˉ)(yiyˉ)S_{xy} = \sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})

    positive when the deviations lean the same way

  • Pearson correlation coefficient

    r=SxySxxSyyr = \frac{S_{xy}}{\sqrt{S_{xx} \cdot S_{yy}}}

    always between −1 and 1; no units

  • Least-squares line

    a=SxySxx,b=yˉaxˉa = \frac{S_{xy}}{S_{xx}}, \qquad b = \bar{y} - a\bar{x}

    the line always passes through the point (x̄, ȳ)

  • Fitted value

    y^=ax+b\hat{y} = ax + b

    the model’s prediction at a given x

  • Residuals

    ei=yiy^i,i=1nei=0e_i = y_i - \hat{y}_i, \qquad \sum_{i=1}^{n} e_i = 0

    it is their squares the method minimises

  • Coefficient of determination

    r2r^{2}

    the fraction of the variation in y the model explains

  • Significance test for a correlation

    t=rn21r2t = \frac{r\sqrt{n-2}}{\sqrt{1-r^{2}}}

    t distribution with n − 2 degrees of freedom

Everything else in this branch spoke about one quantity: its mean, its spread, its distribution. This lesson is about two, measured on the same objects — floor area and price, speed and fuel consumption, temperature and ice-cream sales.

There are two questions. Whether the quantities move together — that is what the correlation coefficient answers. How — that is what the regression line answers.

The scatter plot

As usual in this branch, it starts with a drawing. Each object contributes one point (xi,yi)(x_i, y_i):

012345605101520xydata
Five objects, five points. The cloud clearly rises to the right, but the points do not lie on a line — and the question is how to turn that 'clearly' into a number.

The scatter plot is the first step, not an ornament. Every number below can be computed without looking at the picture, and every one of them can then lie — which is what the section on abuses is about.

Pearson's correlation coefficient

The starting point is the sum of products of deviations from the two means:

Sxy=i=1n(xixˉ)(yiyˉ)S_{xy} = \sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})

A product is positive when both coordinates deviate the same way (both above their mean or both below) and negative when they deviate oppositely. The sum therefore says whether agreement outweighs disagreement — but it depends on the units, so it has to be normalised:

r=SxySxxSyy,Sxx=(xixˉ)2,Syy=(yiyˉ)2r = \frac{S_{xy}}{\sqrt{S_{xx} \cdot S_{yy}}}, \qquad S_{xx} = \sum (x_i - \bar{x})^2, \quad S_{yy} = \sum (y_i - \bar{y})^2

Built this way, the coefficient has three properties worth remembering: it always lies in [1;1][-1; 1], it has no units, and it does not change when the data is rescaled (converting a price from pounds to euros does not move rr at all).

r\lvert r \rvertreading
0.00.30.0 - 0.3weak or none
0.30.50.3 - 0.5moderate
0.50.70.5 - 0.7marked
0.70.90.7 - 0.9strong
0.91.00.9 - 1.0very strong
Data: (1; 10), (2; 8), (3; 12), (4; 14), (5; 16). Compute r.

The regression line

Correlation says "strongly" but does not say "by how much". That is what the regression line answers — one chosen line y^=ax+b\hat{y} = ax + b, picked so that the sum of the squared vertical distances of the points from it is smallest. Hence the name: the method of least squares.

a=SxySxx,b=yˉaxˉa = \frac{S_{xy}}{S_{xx}}, \qquad b = \bar{y} - a\bar{x}

The second formula says more than "how to compute bb": rearranged it reads yˉ=axˉ+b\bar{y} = a\bar{x} + b, i.e. the regression line always passes through the point (xˉ,yˉ)(\bar{x}, \bar{y}). That makes a convenient check on the arithmetic.

012345605101520xydataŷ = 1.8x + 6.6
The same cloud with its fitted line. A slope of 1.8 means: a one-unit rise in x corresponds to a rise in y of 1.8 on average.
For the same data, find the regression line.

A warning about reading the slope. "A one-unit rise in xx corresponds to a rise in yy of aa on average" is a correct sentence. "A one-unit rise in xx causes a rise in yy of aa" is a statement about causation, and regression does not license it — as we shall see.

Residuals

The points do not lie on the line and are not meant to. The difference between an observation and a prediction is called a residual:

ei=yiy^ie_i = y_i - \hat{y}_i
012345605101520xydataŷ = 1.8x + 6.6
The vertical segments from the points to the line are the residuals. Least squares picks the line for which the sum of their squares is smallest. The point (3; 12) sits exactly on the line, so its residual is zero and it has no segment.
xix_iyiy_iy^i\hat{y}_ieie_i
1110108.48.41.61.6
228810.210.22.2-2.2
33121212.012.000
44141413.813.80.20.2
55161615.615.60.40.4
total00

The residuals always sum to zero — that follows directly from the formula for bb — so the sum is not the point. The pattern is: residuals scattered without structure say the linear model fits; residuals bending into an arc say the relationship is curved; a fan widening to the right says the spread grows with xx. Those are faults that no single number will show.

The coefficient of determination

The square of the correlation coefficient has its own, very practical reading:

r2=the fraction of the variation in y the model explainsr^{2} = \text{the fraction of the variation in } y \text{ the model explains}

For our data r=0.9r = 0.9, so r2=0.81r^2 = 0.81: floor area, speed or whatever plays the part of xx accounts for 81 per cent of the variation in yy, and 19 per cent stays in the residuals.

It is worth seeing how fast that falls:

rrr2r^{2}explained
0.950.950.900.9090%90\%
0.90.90.810.8181%81\%
0.70.70.490.4949%49\%
0.50.50.250.2525%25\%
0.30.30.090.099%9\%

A correlation of 0.70.7 sounds solid and explains less than half the variation. A correlation of 0.30.3, which the table above calls "moderate", explains nine per cent — practically nothing.

Prediction and the limits of extrapolation

Any xx can be substituted into a regression line to get a number. Not every such number means anything.

  • Interpolation — predicting for an xx inside the range of the data. There the model has support in observations and is credible.
  • Extrapolation — predicting outside it. The line continues there only because lines always continue, not because the relationship stays linear.
Fuel consumption was measured at speeds from 70 to 130 km/h, giving ŷ = 0.08v + 1.6 l/100 km. What does the model give at 110 km/h, and at 200?

Is the correlation significant

A coefficient computed from a sample is — like every statistic in this branch — a random number. The question "is there any correlation in the population at all" is a hypothesis test from the previous lesson, with H0:ρ=0H_0: \rho = 0:

t=rn21r2t = \frac{r\sqrt{n-2}}{\sqrt{1-r^{2}}}

The statistic follows a Student t distribution with n2n - 2 degrees of freedom. In practice it is handier to read off the critical r|r| at α=0.05\alpha = 0.05:

nnr\lvert r \rvert significant from
550.880.88
10100.630.63
30300.360.36
1001000.200.20

The table says two things at once and both matter. A high correlation from a handful of points means almost nothing — our r=0.9r = 0.9 at n=5n = 5 barely clears the boundary of 0.880.88. And conversely: a low correlation from a thousand points can be significant and entirely unimportant — significance says "this is not noise", while r2=0.04r^2 = 0.04 says the model explains four per cent.

Where this mathematics gets abused

This is the lesson after which most people draw the wrong conclusion, so there are five abuses.

1. Correlation read as causation

The most famous sentence in statistics and still the most often broken. The same correlation between XX and YY is explained by four different things:

  • XX affects YY — what we usually assume;
  • YY affects XX — reverse causation ("hospitals are dangerous, more people die in them");
  • both depend on a third variable;
  • the coincidence is chance, especially when many pairs were compared at once.

The correction: the arithmetic cannot tell those four apart, because all it sees is a pair of numbers per object. What can settle it is a randomised experiment or domain knowledge — never the coefficient itself, even at 0.990.99.

2. The lurking variable

A special and very common case of the above. Ice-cream sales correlate with drownings because both rise in hot weather. The number of firefighters at a fire correlates with the damage because both depend on the size of the fire. A lurking variable manufactures a correlation that no direct link accounts for.

The correction: before believing in a relationship, list the candidates for a third variable. The formal tool is a partial correlation or a model that controls for it; the informal one is the question "what else changes along with both".

3. Extrapolating beyond the data

A model fitted between 7070 and 130130 knows nothing about 200200. The classic examples are dramatic: linearly extrapolating 100-metre sprint records predicts a negative time, and a child's growth predicts a two-metre five-year-old.

The correction: the range of the data is part of the model and is quoted together with the equation of the line. A high r2r^2 does not license extrapolation — it speaks only to the quality of the fit where the points were.

4. The coefficient read without the plot

rr measures linear dependence only. Points lying exactly on a parabola can give r=0r = 0, and a single outlier can lift rr from zero to 0.80.8 or drop it from 0.90.9 to zero.

−4−3−2−1012340246810xydataŷ = 4, r = 0
A perfect relationship: y is exactly the square of x. The correlation coefficient is zero and the regression line is flat — because both measure linear dependence only, and there is none here.

The correction: the scatter plot is compulsory, not optional. Four data sets with identical xˉ\bar{x}, yˉ\bar{y}, rr and regression line but entirely different shapes are the classic Anscombe quartet — built precisely to make this visible.

5. Significance mistaken for strength

On a large sample any non-zero rr becomes significant: at n=100n = 100 the boundary is 0.200.20, at n=10000n = 10\,000 it is 0.020.02. A headline reading "researchers found a significant association" therefore says nothing about how strong that association is.

The correction: read r2r^2, not the p-value. A significant correlation of 0.050.05 explains one four-hundredth of the variation and predicts nothing.

Exercises

The first set works on a five-point table, exactly like the examples above. The r prompt gives five pairs and asks for the correlation coefficient — compute it through the deviations from the means, since xˉ\bar{x} always lands on the middle point. The "regression line" prompt gives the same data and asks for both coefficients at once: write them as a = …, b = …; the parts are named, so the order does not matter.

Practice

Work through a set of exercises — they get harder as you go. At the end you'll see your score and the mistakes worth reviewing.

Exercise 1 of 8Score: 0
r: (3; 11), (4; 13), (5; 15), (6; 17), (7; 19)

The second set is authored, because it asks about quantities the generator deliberately does not compute: r2r^2 is rr squared, and a prediction y^\hat{y} with aa and bb known is a substitution into a linear function — both already have generators in other branches. They stay here as questions about interpretation, alongside residuals and the arithmetic of extrapolation.

Practice

Work through a set of exercises — they get harder as you go. At the end you'll see your score and the mistakes worth reviewing.

Exercise 1 of 7Score: 0
r = 0.9. What percentage of the variation in y does the model explain? → % =

The last question is a trap and is meant to be one: the arithmetic gives 17.617.6 and is correct, while the result is worthless, because 200200 lies outside the range of the fit. Judging "may this be extrapolated" has no number for an answer and stays content — it is the third point in the section on abuses.

Common mistakes

  • Inferring a cause from a correlation alone — the same number is explained by four different causal arrangements, and the arithmetic does not tell them apart.
  • Forgetting the lurking variable — ice-cream sales and drownings correlate through the heat; look for a third quantity before believing in a link.
  • Reading rr without a scatter plot — the coefficient measures only linear dependence, and a single outlier can invert it entirely.
  • Confusing rr with r2r^2 — a correlation of 0.70.7 explains 4949 per cent of the variation, not 7070; that is the difference between "a strong link" and "less than half".
  • Extrapolating beyond the data — lines always continue, data ends where it ends, and a high r2r^2 does not change that.
  • Measuring distances perpendicular to the line rather than vertically — the method minimises deviations in yy, because yy is what is being predicted; the perpendicular gives a different line and answers a different question.
  • Treating significance as strength — at n=10000n = 10\,000 an rr of 0.020.02 is significant, and it explains four ten-thousandths of the variation.
  • Swapping the roles of xx and yy — the regression of yy on xx and of xx on yy are two different lines; they coincide only when r=1|r| = 1.

Formula card

Topic: Correlation and regression

  • Sum of products of deviations

    Sxy=i=1n(xixˉ)(yiyˉ)S_{xy} = \sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})

    positive when the deviations lean the same way

  • Pearson correlation coefficient

    r=SxySxxSyyr = \frac{S_{xy}}{\sqrt{S_{xx} \cdot S_{yy}}}

    always between −1 and 1; no units

  • Least-squares line

    a=SxySxx,b=yˉaxˉa = \frac{S_{xy}}{S_{xx}}, \qquad b = \bar{y} - a\bar{x}

    the line always passes through the point (x̄, ȳ)

  • Fitted value

    y^=ax+b\hat{y} = ax + b

    the model’s prediction at a given x

  • Residuals

    ei=yiy^i,i=1nei=0e_i = y_i - \hat{y}_i, \qquad \sum_{i=1}^{n} e_i = 0

    it is their squares the method minimises

  • Coefficient of determination

    r2r^{2}

    the fraction of the variation in y the model explains

  • Significance test for a correlation

    t=rn21r2t = \frac{r\sqrt{n-2}}{\sqrt{1-r^{2}}}

    t distribution with n − 2 degrees of freedom

012345605101520xydataŷ = 1.8x + 6.6
Five points and the least-squares line fitted to them. The points do not lie on the line and are not meant to — the line is the one for which the sum of squared vertical distances is smallest.
−4−3−2−1012340246810xydataŷ = 4, r = 0
Seven points lying exactly on a parabola. The relationship is perfect and the correlation coefficient is zero — because it measures linear dependence only, and there is none here.

Frequently asked questions

Related articles