Extraordinary Least Squares
Best-Fit Lines, Statistical Control, and Uncertainty
Regression Is About Relationships
We want to know if X causes Y. But first, we need to describe the relationship between them.
- Does Y change as X changes?
- Is the relationship positive or negative?
- How big is the relationship?
- Is the relationship real or just noise?
Do Taller People Earn More Money?
Do Taller People Earn More Money?
Do Taller People Earn More Money?
Linear Regression
y = mx + b y_i = \beta_0 + \beta_1 x_i + \epsilon_i
Where…
- y_i is the dependent/outcome variable.
- x_i is the independent/predictor/explanatory variable.
- \beta_0 is the intercept.
- \beta_1 is the slope.
- \epsilon_i is the error (everything about y we don’t capture with x).
Reading Regression Output
summary(lm(earnk ~ male, data = earnings))
| (Intercept) |
15.85 |
0.64 |
24.9 |
< .001 |
| male |
14.26 |
1.04 |
13.7 |
< .001 |
Why We Need Regression
With a binary variable, computing averages is easy. With a continuous variable, it isn’t.
- Sex has two values → two group means, one difference.
- Height has many values → averaging at every height gets unwieldy.
- Regression uses one fitted line to approximate the averages.
- Scales to many variables at once.
Modeling Height and Earnings
Regression as Prediction
Plug a height into the equation to predict earnings.
- Equation: earnings = -85.03 + 1.59 × height
- Plug in 60: -85.03 + 1.59 × 60 = 10.37
- A 60-inch person is predicted to earn about $10K.
| 58 |
7.2 |
| 60 |
10.4 |
| 65 |
18.3 |
| 70 |
26.3 |
| 72 |
29.5 |
Where Does the Line Come From?
Some lines miss the points by less than others. Which line is best?
- A residual is the vertical miss: y_i - \hat{y}_i
- Bad lines have big residuals, good lines have small ones.
- We need one number that summarizes how “bad” a line is. Choose the least bad.
- Choose \beta_0 and \beta_1 to minimize the sum of squared residuals:
\sum (y_i - \hat{y}_i)^2 = \sum (y_i - \beta_0 - \beta_1 x_i)^2
Grid Search
Try many lines, keep the lowest sum of squared residuals.
- Build a grid of candidate intercepts (\beta_0) and slopes (\beta_1).
- Compute \sum (y_i - \beta_0 - \beta_1 x_i)^2 at every combination. For example,
sum((earnings - b0 - b1 * male)^2).
- Keep the pair with the smallest SSR, the cool end of the heatmap.
The Conditional Expectation Function
E[Y \mid X = x], the average y at a chosen value of x.
- With a binary predictor, there are two conditional means.
- They match the regression line.
- That isn’t an accident: for a single binary predictor, regression is the conditional mean.
| male = 0 |
15.8 |
| male = 1 |
30.1 |
The CEF and Regression for Height
- The dashed line (with triangle markers) is the the CEF: average earnings at each height.
- The solid line is the regression of earnings on height.
- Regression is the best linear approximation of the CEF.
But Wait: Does Height Really Matter?
- Men are taller than women on average.
- Men also earn more, on average.
- So the height coefficient may be partly a disguised gender difference.
- Regression lets us ask whether height still matters after holding sex constant.
Controlling for Sex
Adding sex to the model shrinks the height coefficient from 1.60 to 0.65.
Another Visualization: A Plane Through the Data
Interactions
An interaction lets the height slope differ for women and men.
Adding Additional Predictors
- Each coefficient compares people who match on every other variable in the model.
- The height coef now holds sex, age, education, and race constant.
- More controls means fewer real matches in the data backing each comparison.
| (Intercept) |
-60.74 |
11.88 |
< .001 |
| male |
11.40 |
1.39 |
< .001 |
| height |
0.48 |
0.18 |
0.007 |
| education |
2.70 |
0.19 |
< .001 |
| age |
0.19 |
0.03 |
< .001 |
| ethnicityHispanic |
-0.93 |
2.50 |
0.71 |
| ethnicityOther |
1.22 |
3.61 |
0.736 |
| ethnicityWhite |
2.22 |
1.60 |
0.166 |
How Certain Are We About These Estimates?
- We estimated people earn about $1.6K per inch.
- But…another sample of 1,800 people could give a slightly different slope.
- That sample-to-sample movement is sampling uncertainty.
- Standard errors tell us how much the estimate would move across samples.
Sampling Variation (The Bootstrap)
- Draw 1,800 people from the data, with replacement.
- Refit regression model on new data and save the slope.
- Repeat 1,000 times.
- The standard deviation of the slopes approximates the standard error.
Where Standard Errors Come From
The standard error captures how much the slope shifts across repeated samples.
se_{\hat{\beta}_1} = \sqrt{\frac{\sum \hat \epsilon_i^2}{(n - k)\sum (x_i - \bar{x})^2}}
- Numerator (squared residuals): a worse-fitting line means a bigger SE.
- Denominator: bigger n or more spread in x means a smaller SE.
What About p-values and Statistical Significance?
- Probability of observing a test statistic as extreme as observed under the assumption that the true coefficient is 0.
- Divide the coefficient by the SE. If the coefficient is at least twice its standard error, the estimate is “hard to explain” as noise.
- The “2” comes from the normal distribution, where two standard deviations capture 95% of the data.
Why Regression?
Regression turns messy data into interpretable numbers.
- A cloud of points becomes a line.
- A line becomes a coefficient.
- A coefficient becomes an interpretation.
- Uncertainty tells us how much to trust it.