Two Variables

Study Sheet

Two Variables

Scatter plots, correlation and the least-squares line

Scatter Plots and Correlation

Concept
Correlation rr

A scatter plot shows pairs (x,y)(x, y). The correlation coefficient rr measures the strength and direction of a LINEAR relationship: −1≤r≤1-1 \le r \le 1, with rr near ±1\pm1 for tight lines and near 00 for no linear pattern. rr has no units and does not change if either variable is rescaled.

Correlation is the average product of the two z-scores: r=1n−1∑zxzyr = \dfrac{1}{n-1}\sum z_xz_y. When xx is above its mean, is yy usually above its mean too? If so, the products are mostly positive and rr is positive. That formula also explains what rr misses: a perfect U-shaped relationship has r≈0r \approx 0, because above-average xx pairs with above-average yy on one side and below-average yy on the other.

Example
Compute rr
xy12345123456
Five points rising together: most lie up-right or down-left of the centre (3, 4), so r is positive.

Data: (1,2),(2,3),(3,5),(4,4),(5,6)(1, 2), (2, 3), (3, 5), (4, 4), (5, 6). Means xˉ=3\bar x = 3, yˉ=4\bar y = 4. Deviations: x−xˉ=−2,−1,0,1,2x - \bar x = -2, -1, 0, 1, 2 and y−yˉ=−2,−1,1,0,2y - \bar y = -2, -1, 1, 0, 2. Products sum to 4+1+0+0+4=94 + 1 + 0 + 0 + 4 = 9; squares sum to 1010 for xx and 1010 for yy. So r=910⋅10=0.9r = \dfrac{9}{\sqrt{10\cdot10}} = 0.9: a strong positive linear pattern. (Dividing each deviation by its SD and averaging the products with n−1n - 1 gives the same number.)

Example
Estimate rr

Points tightly along a line rising to the right: r≈0.9r \approx 0.9. A loose cloud falling to the right: r≈−0.4r \approx -0.4. A circle of points: r≈0r \approx 0. The sign is the direction; the size is how closely the points hug a line — not the steepness of the line.

Example
Strong pattern, zero correlation

Data: x=−2,−1,0,1,2x = -2, -1, 0, 1, 2 with y=x2=4,1,0,1,4y = x^2 = 4, 1, 0, 1, 4. Here xˉ=0\bar x = 0 and yˉ=2\bar y = 2, and ∑(x−xˉ)(y−yˉ)=(−2)(2)+(−1)(−1)+0+(1)(−1)+(2)(2)=0\sum (x - \bar x)(y - \bar y) = (-2)(2) + (-1)(-1) + 0 + (1)(-1) + (2)(2) = 0. So r=0r = 0 although yy is completely determined by xx. Correlation measures LINEAR association only.

Tip
Correlation is not causation

A strong rr says the variables move together. It does not say one causes the other: a lurking variable may drive both (ice-cream sales and drownings both rise in summer).

Side note
Where it goes wrong

Reading the sign as strength. r=−0.8r = -0.8 is a STRONGER linear relationship than r=0.5r = 0.5; it simply runs downhill. And a single outlier can create or destroy a large rr in a small data set, so always look at the scatter plot.

Try it
Try it: what happens to rr?

Heights (cm) and weights (kg) have r=0.6r = 0.6. If heights are converted to inches, what is rr?

Show the answer

Unchanged: 0.6\mathbf{0.6}. Converting units is a linear rescaling, which leaves every z-score, and so the average product of z-scores, the same.

The Least-Squares Line

Concept
Regression line

The least-squares line y^=a+bx\hat y = a + bx minimizes the sum of squared vertical residuals y−y^y - \hat y. Its slope is b=rsysxb = r\dfrac{s_y}{s_x} and it always passes through (xˉ,yˉ)(\bar x, \bar y), so a=yˉ−bxˉa = \bar y - b\bar x. The slope means: for each unit increase in xx, y^\hat y changes by bb on average.

The line predicts, it does not explain. Its slope is rr times the ratio of spreads, which says something surprising: for r<1r < 1, the predicted yy is always fewer standard deviations from its mean than xx is from its mean. Tall parents have children who are tall, but on average less tall than the parents — "regression toward the mean", which gave the whole method its name.

Example
Build the line from summaries

xˉ=10\bar x = 10, sx=2s_x = 2, yˉ=50\bar y = 50, sy=8s_y = 8, r=0.75r = 0.75. Slope b=0.75⋅8/2=3b = 0.75\cdot8/2 = 3. The line passes through the point of means, so a=50−3⋅10=20a = 50 - 3\cdot10 = 20. Line: y^=20+3x\hat y = 20 + 3x. Predicted yy at x=12x = 12: 20+36=5620 + 36 = 56.

Example
Fit the five points
xy12345123456ŷ = 1.3 + 0.9x
The dashed segments are the residuals; the line makes the sum of their squares as small as possible.

For (1,2),(2,3),(3,5),(4,4),(5,6)(1, 2), (2, 3), (3, 5), (4, 4), (5, 6): b=∑(x−xˉ)(y−yˉ)∑(x−xˉ)2=910=0.9b = \dfrac{\sum(x - \bar x)(y - \bar y)}{\sum(x - \bar x)^2} = \dfrac{9}{10} = 0.9 (the same as r sy/sxr\,s_y/s_x, since sx=sys_x = s_y here). Then a=4−0.9⋅3=1.3a = 4 - 0.9\cdot3 = 1.3: y^=1.3+0.9x\hat y = 1.3 + 0.9x. The predictions 2.2,3.1,4.0,4.9,5.82.2, 3.1, 4.0, 4.9, 5.8 leave residuals −0.2,−0.1,1.0,−0.9,0.2-0.2, -0.1, 1.0, -0.9, 0.2, which sum to 00 — the line balances the points above it against those below.

Example
Prediction and its limits

With y^=1.3+0.9x\hat y = 1.3 + 0.9x, the prediction at x=6x = 6 is 1.3+5.4=6.71.3 + 5.4 = 6.7 — a mild extrapolation just past the data. At x=50x = 50 the line says 46.346.3, a number with no support at all: nothing in five points between x=1x = 1 and x=5x = 5 says the pattern continues that far.

Side note
Where it goes wrong

Extrapolating far outside the range of the data, and swapping the roles of the variables: the line that predicts yy from xx is NOT the line that predicts xx from yy (unless r=±1r = \pm1), because each minimizes errors in a different direction.

Try it
Try it: residual

Using y^=20+3x\hat y = 20 + 3x, a data point is (12,60)(12, 60). Find the residual.

Show the answer

Predicted y^=20+36=56\hat y = 20 + 36 = 56; residual =y−y^=60−56=4= y - \hat y = 60 - 56 = \mathbf{4} (the point is above the line).

Residuals and r2r^2

Concept
How good is the line?

r2r^2 is the fraction of the variation in yy explained by the line: r2=1−∑(y−y^)2∑(y−yˉ)2r^2 = 1 - \dfrac{\sum(y - \hat y)^2}{\sum(y - \bar y)^2}. A residual plot graphs residuals against xx; a patternless band means a line is a reasonable model, while a curve or a funnel means it is not.

The total variation ∑(y−yˉ)2\sum(y - \bar y)^2 is how much yy varies with no model at all; the residual sum of squares is what is left after the line. Their ratio is the unexplained fraction, and r2r^2 is the rest. But r2r^2 measures how close the points are to the line, not whether a line is the right shape — a residual plot answers that question, because a good model leaves only noise behind.

Example
r2r^2 for the five points

Residuals −0.2,−0.1,1.0,−0.9,0.2-0.2, -0.1, 1.0, -0.9, 0.2 have squares summing to 0.04+0.01+1+0.81+0.04=1.90.04 + 0.01 + 1 + 0.81 + 0.04 = 1.9. The total variation of yy is ∑(y−4)2=10\sum(y - 4)^2 = 10. So r2=1−1.9/10=0.81r^2 = 1 - 1.9/10 = 0.81, which is indeed 0.920.9^2. The line explains 81%81\% of the variation in yy.

Example
A high r2r^2 and the wrong model
xresidual12345−3−2−112
Residuals 2, −1, −2, −1, 2 trace a U: the data curve, and a line is the wrong model.

Fit a line to (1,1),(2,4),(3,9),(4,16),(5,25)(1, 1), (2, 4), (3, 9), (4, 16), (5, 25): b=60/10=6b = 60/10 = 6, a=11−18=−7a = 11 - 18 = -7, y^=−7+6x\hat y = -7 + 6x. The residuals are 2,−1,−2,−1,22, -1, -2, -1, 2: positive, negative, positive — a U. And r2=1−14/374≈0.96r^2 = 1 - 14/374 \approx 0.96. The fit looks excellent by r2r^2, yet the residual plot shows the points follow a curve; predictions from the line will be systematically wrong at both ends.

Side note
Where it goes wrong

Reading r2=0.64r^2 = 0.64 as "64%64\% of the points are on the line", or as "the prediction is right 64%64\% of the time". It is a fraction of VARIATION: 64%64\% of the spread in yy is accounted for by its linear relationship with xx.

Try it
Try it: from rr to r2r^2

A study reports r=−0.7r = -0.7 between hours of television and test score. What fraction of the variation in test scores does the line explain?

Show the answer

r2=(−0.7)2=0.49r^2 = (-0.7)^2 = \mathbf{0.49}: about 49%49\%. The negative sign disappears on squaring — it gives the direction, not the strength.

Why It Works

For any fixed slope bb, the sum ∑(yi−a−bxi)2\sum(y_i - a - bx_i)^2 is a quadratic in aa, smallest when its derivative −2∑(yi−a−bxi)-2\sum(y_i - a - bx_i) is zero, i.e. when the residuals sum to zero. That gives a=yˉ−bxˉa = \bar y - b\bar x: the line passes through (xˉ,yˉ)(\bar x, \bar y). Substituting and minimizing over bb the same way gives b=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2b = \dfrac{\sum(x_i - \bar x)(y_i - \bar y)}{\sum(x_i - \bar x)^2}, and dividing top and bottom by n−1n - 1 turns this into r sy/sxr\,s_y/s_x. Since ∣r∣≤1|r| \le 1, a one-SD change in xx predicts at most a one-SD change in yy: regression toward the mean is built into the formula.