POSC 8410: Simple Linear Regression

Fitting a line to the CEF

Will Horne

Where We Are

  • Imagine we want to know the relationship between GDP Per Capita and Life Expectancy

  • The object we want is the CEF, \(E[Y \mid X]\)

  • With categorical \(X\), we just computed the group means

  • But with continuous \(X\), most values appear once or never

distinct GDP values appearing more than once
142 0

There are no groups to average within. So we assume a shape.

The Model

Estimating the CEF with a line

\[Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i\]

  • \(\beta_0\) — the intercept: predicted \(Y\) when \(X = 0\)

  • \(\beta_1\) — the slope: change in predicted \(Y\) per one-unit change in \(X\)

  • \(\varepsilon_i\) — the error: everything about case \(i\) we did not model

Equivalently: \(E[Y \mid X] = \beta_0 + \beta_1 X\). We are asserting the CEF is a straight line.

That Is an Assumption

  • The CEF is whatever it is. A line is our approximation of it.

  • If the true CEF curves, the line is the best straight summary of a curved thing

  • We have already seen the fix when it curves: transform, usually with logs or polynomials

We have that problem here! We model life expectancy on log GDP rather than GDP.

Fitted Values and Residuals

  • The fitted value is our prediction: \(\hat{y}_i = \hat{\beta}_0 + \hat{\beta}_1 x_i\)

  • The residual is what we missed: \(e_i = y_i - \hat{y}_i\)

Note the difference from \(\varepsilon_i\): the error is a feature of the world we never see; the residual is what our fitted line leaves over. \(\epsilon\) is unknowable, \(e_{i}\) is found in your data.

Least Squares

Choosing the Line

Which line? The one minimizing the sum of squared residuals:

\[\min_{\beta_0,\, \beta_1} \; \sum_{i=1}^{n}\big(y_i - \beta_0 - \beta_1 x_i\big)^2\]

  • Same criterion that gave us the mean, and then the CEF

  • Third time we have minimized squared error - now with two unknowns instead of one

Deriving It

Take the derivative with respect to \(\beta_0\) and set it to zero:

\[-2\sum_i (y_i - \beta_0 - \beta_1 x_i) = 0 \;\Longrightarrow\; \bar{y} = \beta_0 + \beta_1 \bar{x}\]

  • Rearranged: \(\hat{\beta}_0 = \bar{y} - \hat{\beta}_1\bar{x}\)

  • Which says something important: the fitted line passes through \((\bar{x}, \bar{y})\), always

Now the Slope

Differentiate with respect to \(\beta_1\), set to zero, and substitute the intercept condition:

\[\hat{\beta}_1 = \frac{\sum_i (x_i - \bar{x})(y_i - \bar{y})}{\sum_i (x_i - \bar{x})^2} = \frac{\widehat{\text{Cov}}(X,Y)}{\widehat{\text{Var}}(X)}\]

This is the formula we saw earlier.

Co-movement in the numerator, variation in the predictor in the denominator.

Check It

Code
with(g07, cov(lgdp, lifeExp) / var(lgdp))
[1] 7.202802
Code
coef(lm(lifeExp ~ lgdp, data = g07))[2]
    lgdp 
7.202802 

Identical. Everything lm does, you could do with cov and var.

What Least Squares Guarantees

Setting those two derivatives to zero forces two conditions on the residuals:

Code
m1 <- lm(lifeExp ~ lgdp, data = g07)
sum(resid(m1))                 # residuals sum to zero
[1] 6.483702e-14
Code
sum(g07$lgdp * resid(m1))      # and are uncorrelated with X
[1] 2.913225e-13
  • Both are exactly zero - not approximately.

  • So the fitted line has extracted every scrap of linear signal in \(X\): whatever is left over has no linear relationship to it

This is not evidence the model is right. It is true by construction, even for a badly misspecified model. This is not a diagnostic.

Why Squared and Not Absolute?

We could have minimized \(\sum|y_i - \hat{y}_i|\) instead. Why squares?

  • Squares are differentiable everywhere, so calculus gives a closed-form answer. Absolute values are not.

  • Squares give the conditional mean; absolute values give the conditional median (as with the mean and median earlier)

  • And squares punish large misses disproportionately

Minimizing absolute deviations is a real method - quantile regression - and it can be more robust to outliers.

Is It Unbiased?

Substitute the true model \(y_i = \beta_0 + \beta_1 x_i + \varepsilon_i\) into the slope formula. Writing \(d_i = x_i - \bar{x}\):

\[\hat{\beta}_1 = \frac{\sum_i d_i\,y_i}{\sum_i d_i^{2}} = \beta_1 + \frac{\sum_i d_i\,\varepsilon_i}{\sum_i d_i^{2}}\]

  • The estimate is the truth plus a weighted average of the errors

  • Everything hinges on whether that second term averages to zero

Taking Expectations

Treat the \(x_i\) as fixed, so the \(d_i\) are constants. By linearity of expectation:

\[E[\hat{\beta}_1] = \beta_1 + \frac{\sum_i d_i\, E[\varepsilon_i]}{\sum_i d_i^{2}}\]

If \(E[\varepsilon_i] = 0\) for every \(i\), the whole second term vanishes:

\[E[\hat{\beta}_1] = \beta_1 \;\;\blacksquare\]

  • That is the key assumption: the errors are unrelated to \(X\)

  • An omitted variable correlated with \(X\) breaks this assumption - we will see how later

Reading the Output

The Fit

Code
summary(lm(lifeExp ~ lgdp, data = g07))$coefficients
            Estimate Std. Error   t value     Pr(>|t|)
(Intercept) 4.949612  3.8576841  1.283053 2.015936e-01
lgdp        7.202802  0.4423393 16.283430 4.115370e-34
  • Slope 7.20: a one-unit increase in log GDP is associated with 7.2 more years of life expectancy

  • Intercept 4.95: predicted life expectancy at log GDP \(= 0\), meaning GDP per capita of $1. Meaningless in this case.

Intercepts Are Often Nonsense

  • The intercept is only interpretable if \(X = 0\) is a real, observed possibility

  • No country has $1 GDP per capita, so 4.95 years is extrapolation outside of the support of our data

  • Centering \(X\) (subtracting its mean) makes the intercept the predicted value at average \(X\)

Do not report an intercept as a finding unless zero means something.

Transformations

Four Ways to Write a Regression

The units on each side change what the coefficient means. These four are common in published work:

Model Name Interpretation of \(\beta_1\)
\(Y \sim X\) linear–linear a 1-unit rise in \(X\) → \(\beta_1\) units of \(Y\)
\(Y \sim \log X\) linear–log a 1% rise in \(X\) → \(\beta_1/100\) units of \(Y\)
\(\log Y \sim X\) log–linear a 1-unit rise in \(X\) → \(100\beta_1\) percent change in \(Y\)
\(\log Y \sim \log X\) log–log a 1% rise in \(X\) → \(\beta_1\) percent change in \(Y\) (elasticity)

Our Data

Model Slope
lifeExp ~ gdpPercap 0.0006370
lifeExp ~ log(gdpPercap) 7.2028000
log(lifeExp) ~ gdpPercap 0.0000098
log(lifeExp) ~ log(gdpPercap) 0.1134000
  • 0.00064 — each extra dollar buys 0.0006 years.

  • 7.20 — each unit of log GDP buys 7.2 years.

Reading the Log-Log

  • 0.113 is an elasticity: a 1% rise in income is associated with a 0.11% rise in life expectancy

  • Unit-free, so it is comparable across countries, currencies, and years

  • Common when looking at macro-economic trends

The Picture

Purple is the fitted line. Orange segments are the residuals — and least squares makes the sum of their squares as small as possible.

\(R^2\)

Code
summary(lm(lifeExp ~ lgdp, data = g07))$r.squared
[1] 0.654449
  • 65% of the variation in life expectancy is accounted for by log GDP

  • Same quantity we computed from group means earlier: a proportional reduction in squared error

  • It is not a measure of whether your model is correct, useful, or causal

A high \(R^2\) with a bad design is a well-fitted wrong answer. A low \(R^2\) in a clean experiment can still be an important finding.

Residual Standard Error

Code
sigma(lm(lifeExp ~ lgdp, data = g07))
[1] 7.122255
  • Typical prediction miss: about 7 years

  • In the units of \(Y\), so it is directly interpretable — unlike \(R^2\)

  • Often more useful than \(R^2\) for judging whether a model is practically good enough

Two Different Predictions

Asked to predict a country at $10,000 GDP per capita, R will give you two very different intervals:

Code
nd <- tibble(lgdp = log(10000))
predict(m1, nd, interval = "confidence")   # the average country
       fit      lwr      upr
1 71.28987 69.99887 72.58086
Code
predict(m1, nd, interval = "prediction")   # one particular country
       fit      lwr   upr
1 71.28987 57.14973 85.43

[70.0, 72.6] versus [57.1, 85.4] — more than ten times wider.

Why So Different?

  • A confidence interval covers the mean at that income — it only has to account for uncertainty in the fitted line

  • A prediction interval covers an individual country — so it must also carry the residual scatter around the line

\[\text{prediction} \;\approx\; \text{line uncertainty} \;+\; \sigma_\varepsilon^{2}\]

  • More data shrinks the first term toward zero. It never shrinks the second.

  • Even with infinite data you could not predict one country’s life expectancy to within a year

You Try

  1. Regress lifeExp on unlogged gdpPercap. Compare \(R^2\) to the logged version. Which fits better, and why?

  2. Compute the slope by hand with cov and var. Does it match lm?

  3. Verify the fitted line passes through \((\bar{x}, \bar{y})\).

  4. Center lgdp by subtracting its mean and refit. Which coefficient changed, and which did not?

For Next Time

  • Read: Gelman, Hill and Vehtari, Ch. 8; Blackwell, Ch. 5

  • Next class: inference for coefficients — where the standard errors come from, what assumptions they need, and when they lie to you