5.09a–5.09e - Least squares regression lines

5.09a–5.09e - Least squares regression lines

A regression line turns a scatter diagram into a prediction, but the direction of prediction matters. Learn how least squares chooses the line, calculate it from raw or summarised data, translate it under linear coding and decide how much confidence a prediction deserves.

Variables and vertical residuals

Can the best line for predicting yy from xx simply be rearranged to give the best prediction of xx from yy? Start with just three observed pairs: (1,1)(1, 1), (2,3)(2, 3) and (3,2)(3, 2). A sensible rising line is y=1+0.5xy = 1 + 0.5x. Later you will find that the line for predicting in the opposite direction is different. “Best” needs a direction before it has a meaning.

You already know

A scatter diagram displays paired observations. Pearson's coefficient r=Sxy/SxxSyyr = S_{xy}/\sqrt{S_{xx}S_{yy}} measures their linear correlation; swapping the variables leaves rr unchanged. Correlation alone does not establish cause and effect.

An experiment may choose that direction for you. If you set a water bath's temperature and record how long a reaction takes, temperature is the controlled input and reaction time is the response. If you measure people's hand lengths and foot lengths, neither measurement is controlled. You can still nominate one as a predictor, but the choice does not make it a cause of the other.

Independent and dependent variables

An independent, or controlled, variable is the variable whose values are set in an experiment. The dependent, or response, variable is the measured response. In observational data neither variable need be independent in this sense. This use of “independent” does not mean independence of random variables.

For prediction of yy, compare the observed yy-value with the line's prediction at the same xx. The signed difference is positive above the line and negative below it.

Residual and least squares

For a proposed line y=a+bxy = a + bx, the residual at (xi,yi)(x_i, y_i) is ei=yi−(a+bxi)e_i = y_i - (a + bx_i). The least squares regression line of yy on xx is the line that minimises the sum of the squared vertical residuals, ∑ei2\sum e_i^2.

Squaring prevents positive and negative residuals from cancelling and gives greater weight to a large miss. The distances are vertical: perpendicular distances to the line solve a different problem.

[DIAGRAM: asset_name: Comparing squared vertical residuals; asset_slug: h245_5_09_vertical_residuals; caption: Both lines have residuals summing to zero, but line A has the smaller sum of squared vertical residuals; description: Two scatter diagrams display the points (1,1), (2,3), (3,2). Left line y=1+0.5x has residuals -0.5,1,-0.5 and squared sum 1.5. Right line y=2 has residuals -1,1,0 and squared sum 2. Sage segments show vertical residuals.]
Both lines have residuals summing to zero, but line A has the smaller sum of squared vertical residuals

For line A, the squared sum is (−0.5)2+12+(−0.5)2=1.5(-0.5)^2 + 1^2 + (-0.5)^2 = 1.5. For line B, it is (−1)2+12+02=2(-1)^2 + 1^2 + 0^2 = 2. Line A wins this comparison. Its residuals still need not be small for every individual observation.

Key idea

Predicting yy means minimising squared errors in yy, measured vertically. A small signed total is not the least squares criterion.

This distinction is what the following calculation overlooks.

Calculating the regression line

The least squares calculation uses the same centred sums as correlation, but a regression coefficient is a gradient with units, rather than a unit-free measure of strength.

You already know

For nn pairs, xˉ=∑xi/n\bar{x} = \sum x_i/n, yˉ=∑yi/n\bar{y} = \sum y_i/n, Sxx=∑xi2−(∑xi)2/nS_{xx} = \sum x_i^2 - (\sum x_i)^2/n and Sxy=∑xiyi−(∑xi)(∑yi)/nS_{xy} = \sum x_i y_i - (\sum x_i)(\sum y_i)/n. These centred-sum formulae are in the formula booklet.

When Sxx>0S_{xx} > 0, the predictor takes more than one value, so its variation can identify a gradient. If all the xx-values are equal, there is no uniquely determined regression gradient of yy on xx.

Regression of y on x

For Sxx>0S_{xx} > 0, the least squares line is

y=a+bxb=SxySxxa=yˉ−bxˉ\begin{aligned} y &= a + bx \\ b &= \frac{S_{xy}}{S_{xx}} \\ a &= \bar{y} - b\bar{x} \end{aligned}

In the formula booklet.

Choose the denominator by naming the predictor: “yy on xx” puts SxxS_{xx} underneath. The intercept then makes the line pass through the mean point (xˉ,yˉ)(\bar{x}, \bar{y}), because a+bxˉ=yˉa + b\bar{x} = \bar{y}. This mean-point property is not printed separately in the formula booklet: learn it. Remarkably, it holds even when the scatter is curved and a straight-line model would be poor.

Worked example 1: fitting raw data

Question. Calculate the regression line of yy on xx for these observations.

xx1122334455
yy2244554477

Plan. Obtain the sums, calculate the two centred sums, then find the gradient and intercept.

n=5,∑xi=15,∑yi=22count and total∑xi2=55,∑xiyi=76square and multiply pairsxˉ=3,yˉ=4.4divide totals by 5Sxx=55−1525=10centre the predictorSxy=76−15×225=10centre the productsb=1010=1divide by Sxxa=4.4−1×3=1.4use the mean point\begin{aligned} n &= 5,\quad \sum x_i = 15,\quad \sum y_i = 22 && \text{count and total} \\ \sum x_i^2 &= 55,\quad \sum x_i y_i = 76 && \text{square and multiply pairs} \\ \bar{x} &= 3,\quad \bar{y} = 4.4 && \text{divide totals by } 5 \\ S_{xx} &= 55 - \frac{15^2}{5} = 10 && \text{centre the predictor} \\ S_{xy} &= 76 - \frac{15 \times 22}{5} = 10 && \text{centre the products} \\ b &= \frac{10}{10} = 1 && \text{divide by } S_{xx} \\ a &= 4.4 - 1 \times 3 = 1.4 && \text{use the mean point} \end{aligned}

Why this step? The products must pair each xix_i with its own yiy_i; multiplying the two totals would discard the association between the measurements.

Answer. y=1.4+xy = 1.4 + x

Check. At x=xˉ=3x = \bar{x} = 3, the line gives y=4.4=yˉy = 4.4 = \bar{y}. Its residuals are −0.4-0.4, 0.60.6, 0.60.6, −1.4-1.4, 0.60.6, whose sum is zero.

Worked example 2 changes how the data are supplied: summaries replace the list of pairs. The calculation is identical, but you must distinguish the sum of the squares from the square of the sum.

Worked example 2: fitting summarised data

Question. Six paired observations have

∑xi=30,∑yi=54,∑xi2=170,∑xiyi=308\sum x_i = 30,\quad \sum y_i = 54,\quad \sum x_i^2 = 170,\quad \sum x_i y_i = 308

Calculate the regression line of yy on xx.

Plan. Recover the means and centred sums, then substitute into the regression formulae.

xˉ=306=5,yˉ=546=9calculate meansSxx=170−3026=20subtract the mean correctionSxy=308−30×546=38centre the productsb=3820=1.9regress y on xa=9−1.9×5=−0.5use the mean point\begin{aligned} \bar{x} &= \frac{30}{6} = 5,\quad \bar{y} = \frac{54}{6} = 9 && \text{calculate means} \\ S_{xx} &= 170 - \frac{30^2}{6} = 20 && \text{subtract the mean correction} \\ S_{xy} &= 308 - \frac{30 \times 54}{6} = 38 && \text{centre the products} \\ b &= \frac{38}{20} = 1.9 && \text{regress } y \text{ on } x \\ a &= 9 - 1.9 \times 5 = -0.5 && \text{use the mean point} \end{aligned}

Why this step? ∑xi2=170\sum x_i^2 = 170 is already supplied; it is not (∑xi)2=900(\sum x_i)^2 = 900.

Answer. y=−0.5+1.9xy = -0.5 + 1.9x

Check. Substitution of the mean predictor x=5x = 5 gives the mean response y=9y = 9.

A correlation coefficient and a regression gradient share a numerator but have different denominators and different jobs.

Watch out

Wrong: b=rb = r. Correlation divides by SxxSyy\sqrt{S_{xx}S_{yy}}; the gradient for yy on xx divides by SxxS_{xx}. Right: b=Sxy/Sxxb = S_{xy}/S_{xx}. A regression gradient can exceed 11, and its size depends on the measurement units.

For a routine “Calculate the equation” question, show enough working to identify your gradient and intercept; these are the two numerical results the equation needs.

The two prediction directions

When neither variable is controlled, you may want to predict either one from the other, and each direction has its own least squares line.

For xx on yy, the response is xx, so residuals run horizontally on the usual scatter diagram. Relabel the formula in the booklet: the predictor's centred sum is now SyyS_{yy}.

Regression of x on y

For Syy>0S_{yy} > 0, with neither variable controlled, the least squares line for predicting xx is

x−xˉ=SxySyy(y−yˉ)x - \bar{x} = \frac{S_{xy}}{S_{yy}}(y - \bar{y})

This is the booklet's regression rule with the variables interchanged.
Not in the formula booklet: learn it.

Both centred equations are satisfied by (xˉ,yˉ)(\bar{x}, \bar{y}). If the lines are distinct, that is their intersection; if the observations lie exactly on a non-horizontal, non-vertical line, the two lines coincide. In particular, the original three points have mean point (2,2)(2, 2), and the two fitted lines meet there.

[DIAGRAM: asset_name: Two regression directions through the mean point; asset_slug: h245_5_09_two_regression_lines; caption: For the same three points, the two least squares lines meet at the mean point but give different predictions away from it; description: The points (1,1), (2,3), (3,2) appear on a scatter diagram. The blue y-on-x line y=1+0.5x and clay x-on-y line x=1+0.5y cross at the sage mean point (2,2). Labels connect each line to its vertical or horizontal squared-residual criterion.]
For the same three points, the two least squares lines meet at the mean point but give different predictions away from it

For non-constant variables, write the gradients in their native directions as byx=Sxy/Sxxb_{yx} = S_{xy}/S_{xx} and bxy=Sxy/Syyb_{xy} = S_{xy}/S_{yy}. Their product is

byxbxy=Sxy2SxxSyy=r2b_{yx}b_{xy} = \frac{S_{xy}^2}{S_{xx}S_{yy}} = r^2

This derived relationship is not in the formula booklet: learn it. It explains why the gradients are reciprocals only when ∣r∣=1\lvert r\rvert = 1. When r=0r = 0, the lines are y=yˉy = \bar{y} and x=xˉx = \bar{x}: a horizontal line and a vertical line through the mean point.

Worked example 3: calculating both lines

Question. For observational paired data, neither variable is controlled. Given xˉ=10\bar{x} = 10, yˉ=20\bar{y} = 20, Sxx=40S_{xx} = 40, Syy=90S_{yy} = 90 and Sxy=48S_{xy} = 48, calculate both regression lines.

Plan. Use the same numerator with the appropriate predictor's denominator, and write each line through the mean point.

byx=4840=65x is the predictory−20=65(x−10)use the mean pointy=8+65xexpandbxy=4890=815y is the predictorx−10=815(y−20)use the same mean pointx=−23+815yexpand\begin{aligned} b_{yx} &= \frac{48}{40} = \frac65 && x \text{ is the predictor} \\ y - 20 &= \frac65(x - 10) && \text{use the mean point} \\ y &= 8 + \frac65x && \text{expand} \\ b_{xy} &= \frac{48}{90} = \frac8{15} && y \text{ is the predictor} \\ x - 10 &= \frac8{15}(y - 20) && \text{use the same mean point} \\ x &= -\frac23 + \frac8{15}y && \text{expand} \end{aligned}

Why this step? Changing the response changes which residuals are minimised, so you must calculate the second gradient rather than invert the first.

Answer. For yy on xx, y=8+65xy = 8 + \frac65x. For xx on yy, x=−23+815yx = -\frac23 + \frac8{15}y.

Check. Both contain (10,20)(10, 20), and 65×815=1625\frac65 \times \frac8{15} = \frac{16}{25}, consistent with r=0.8r = 0.8.

Worked example 4 changes only the sign of SxyS_{xy}. The scatter now slopes downwards; both prediction coefficients change sign, but the mean point stays fixed.

Worked example 4: negative association

Question. Observational paired data have xˉ=10\bar{x} = 10, yˉ=20\bar{y} = 20, Sxx=40S_{xx} = 40, Syy=90S_{yy} = 90 and Sxy=−48S_{xy} = -48. Neither variable is controlled. Calculate both regression lines.

Plan. Calculate each negative coefficient and use the centred equations.

byx=−4840=−65predict yy−20=−65(x−10)centre at the meansy=32−65xexpandbxy=−4890=−815predict xx−10=−815(y−20)centre at the meansx=623−815yexpand\begin{aligned} b_{yx} &= -\frac{48}{40} = -\frac65 && \text{predict } y \\ y - 20 &= -\frac65(x - 10) && \text{centre at the means} \\ y &= 32 - \frac65x && \text{expand} \\ b_{xy} &= -\frac{48}{90} = -\frac8{15} && \text{predict } x \\ x - 10 &= -\frac8{15}(y - 20) && \text{centre at the means} \\ x &= \frac{62}{3} - \frac8{15}y && \text{expand} \end{aligned}

Why this step? Negative correlation makes both native prediction gradients negative; it does not change which centred sum belongs underneath.

Answer. y=32−65xy = 32 - \frac65x and x=623−815yx = \frac{62}{3} - \frac8{15}y.

Check. At the mean point, 32−12=2032 - 12 = 20 and 623−323=10\frac{62}{3} - \frac{32}{3} = 10. The gradient product is still 16/2516/25, now corresponding to r=−0.8r = -0.8.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account