James's Math Hub
Statistics & Probability

Lesson 13 / 13 · 11 min read

Linear Regression & Correlation

Best-fit lines, the correlation coefficient, and the causation trap.

Scatter plots and association

A scatter plot shows pairs (x,y)(x, y) as points. The shape hints at a relationship:

  • Up-and-right pattern → positive association.
  • Down-and-right → negative association.
  • No pattern → no linear association.

Correlation: how strong is the line?

The correlation coefficient rr measures the strength and direction of a linear relationship.

  • rr ranges from 1-1 to +1+1.
  • r=1r = 1: perfect positive line. r=1r = -1: perfect negative line.
  • r=0r = 0: no linear relationship (a non-linear one could still exist).
  • r>0.7|r| > 0.7 is usually "strong"; r<0.3|r| < 0.3 is "weak."

Important: rr only measures linear association. A perfect quadratic relationship can have rr near zero.

The line of best fit

If a linear pattern exists, the least-squares regression line minimizes the sum of squared vertical distances from the data points:

y^=a+bx\hat{y} = a + bx

  • y^\hat{y} is the predicted yy.
  • bb is the slope: predicted change in yy per unit increase in xx.
  • aa is the intercept: predicted yy when x=0x = 0.

The slope satisfies:

b=rsysxb = r \cdot \frac{s_y}{s_x}

where sxs_x and sys_y are the standard deviations. The line always passes through the point (xˉ,yˉ)(\bar{x}, \bar{y}).

Predicting with the line

Example. A regression line for hours-studied vs. test-score is y^=50+4x\hat{y} = 50 + 4x.

For a student who studies 6 hours: y^=50+4(6)=74\hat{y} = 50 + 4(6) = 74. Predicted score: 74.

r2r^2 — the coefficient of determination

Squaring rr gives the proportion of variation in yy explained by the line.

  • r=0.8    r2=0.64r = 0.8 \implies r^2 = 0.64: the line explains 64% of the variation in yy.
  • r2r^2 is always between 0 and 1.

Correlation is not causation

A strong rr tells you two variables move together. It does not tell you one causes the other. Ice cream sales and shark attacks both rise in summer — ice cream doesn't cause sharks.

Always ask: could a third variable explain the link?

Key takeaways

  • rr measures the strength of a linear relationship; 1r1-1 \leq r \leq 1.
  • Best-fit line: y^=a+bx\hat{y} = a + bx; it passes through (xˉ,yˉ)(\bar{x}, \bar{y}).
  • r2r^2 is the fraction of variation in yy explained by the line.
  • Correlation \neq causation. Watch for lurking variables.