Correlation coefficient and residuals: Formulas and a worked example

Correlation summarizes the direction and strength of a linear relationship, while a residual describes one point’s vertical gap from the fitted line. We’ll calculate both from the same seven points and use the fitted equation to see how the residuals balance.

Read the correlation coefficient

Pearson’s correlation coefficient r measures linear association and lies between -1 and 1. Values close to 1 indicate strong positive linear association, and values close to -1 indicate strong negative linear association. A value near 0 indicates weak linear association; a curved relationship can still be present.

The sign gives the direction, and |r| gives the strength of the linear association. A correlation of -1 means the points lie on a line with negative slope; it does not specify a slope of -1. Values such as 0.3 and 0.7 are sometimes used as rough descriptive cutoffs, with interpretation depending on the context.

Use consistent sample standard deviations

For n≥2 paired observations, with positive sample standard deviations sx and sy, use

r=1n-1∑i=1n(xi-x¯sx)(yi-ȳsy)

Each parenthesized quantity is a standardized deviation, or z-score. The sample standard deviations use n-1 in their variance denominators. Correlation is undefined if either variable has zero standard deviation.

Example: Calculate correlation for seven points

Original paired observations
xy
00.8
21.0
40.2
60.2
82.0
100.8
120.6

The means are x¯=6 and ȳ=0.8. Add squared deviations and products of matching deviations to get the three sums below.

Sxx=∑i=17(xi-x¯)2=112Syy=∑i=17(yi-ȳ)2=2.24Sxy=∑i=17(xi-x¯)(yi-ȳ)=1.6

The two sample standard deviations and the equivalent centered-sums formula give

sx=1126≈4.3205sy=2.246≈0.6110
r=SxySxxSyyr=1.6112(2.24)≈0.1010

This is a weak positive linear relationship. The centered-sums formula gives the same correlation as the standardized-deviation formula when the standard deviations are used consistently.

Find the fitted line without rounding its coefficients

The ordinary least-squares slope is b=SxySxx. The intercept is a=ȳ-bx¯.

b=1.6112=170a=0.8-670=57ŷ=x70+57

The approximate line is ŷ≈0.0143x+0.7143. We’ll use the exact fractions for the remaining calculations.

Observed points, a shallow fitted line and vertical residual segmentsThe seven original points range from x zero to twelve. Their fitted line has slope one seventieth and intercept five sevenths. Dashed vertical segments connect each observed point to the fitted line; the point eight, two has the largest positive residual.
The shallow line fits these seven points; dashed vertical gaps show the signed residuals.

A residual is actual minus predicted

ei=yi-ŷi

A point above the fitted line has a positive residual. A point below it has a negative residual. The residual is a signed vertical difference; its absolute value is the vertical distance.

At (8,2), the predicted value is ŷ=870+57=2935. Subtracting gives a relatively large positive residual.

e=2-2935=4135≈1.1714

At (10,0.8), the residual is 0.8-67=-235≈-0.0571, so that point lies just below the fitted line.

Predictions and residuals, rounded to four decimals
xActual yPredicted ŷResidual e
00.80000.71430.0857
21.00000.74290.2571
40.20000.7714-0.5714
60.20000.8000-0.6000
82.00000.82861.1714
100.80000.8571-0.0571
120.60000.8857-0.2857

Inspect the residual plot

Residuals plotted above and below a horizontal zero referenceThe seven residuals are about zero point zero eight six, zero point two five seven, negative zero point five seven one, negative zero point six, one point one seven one, negative zero point zero five seven, and negative zero point two eight six. The horizontal dashed reference is e equals zero.
Positive residuals are above the zero reference; negative residuals are below it.

Plotting residuals against x helps us look for systematic curvature or changing spread. A curved pattern suggests that a linear equation is missing part of the relationship. In this example, the residuals balance around zero, while the correlation still shows a weak linear association.

Why least squares uses squared residuals

Ordinary least squares chooses the slope and intercept that minimize ∑i=1nei2. Squaring makes each contribution nonnegative, so positive and negative residuals contribute to the total instead of canceling.

For an ordinary least-squares fit with an intercept, the residuals on the fitted observations sum to zero, using the exact coefficients. Their mean is therefore zero as well. For these data, ∑ei=0 and ē=0. That balance alone does not measure how tightly the points follow the line.