POSC 8410: Model Fit and Diagnostics

Should you trust your results?

Will Horne

Where We Are

  • We can fit regressions, interpret them, and attach uncertainty

  • Today: how to tell whether the model you fit is a reasonable description of your data

Not whether your finding is causal. That is next semester (and then forever and ever until you retire). This is the narrower question of whether the arithmetic is describing what you think it is.

\(R^2\) and Its Limits

What \(R^2\) Is

\[R^2 = 1 - \frac{RSS}{TSS}\]

Code
summary(m1)$r.squared
[1] 0.654449
Code
cor(g07$lifeExp, fitted(m1))^2
[1] 0.654449
  • The proportional reduction in squared error from using your model instead of the mean

  • Also the squared correlation between \(Y\) and the fitted values: how closely the predictions track the outcome

  • Bounded between 0 and 1 for models with an intercept

What It Is Not

  • Not evidence the model is correct. A curved relationship fitted with a line can still post a high \(R^2\).

  • Not a measure of whether your coefficients are unbiased

  • Not comparable across different outcomes or samples

  • Not an indicator of importance - experiments often have tiny \(R^2\) and crucial findings

A randomized trial with \(R^2 = 0.02\) can be the most credible piece in the literature. An observational model with \(R^2 = 0.85\) can be worthless.

Adjusted \(R^2\)

\[\bar{R}^2 = 1 - \frac{RSS/(n-k-1)}{TSS/(n-1)}\]

Code
summary(lm(lifeExp ~ lgdp, data = g07))$adj.r.squared
[1] 0.6519808
Code
summary(lm(lifeExp ~ lgdp + continent, data = g07))$adj.r.squared
[1] 0.7588141
  • Penalizes each added parameter, so it can go down when you add junk

  • Better than raw \(R^2\) for comparing nested models

  • Still not a model-selection procedure: in the noise simulation earlier, pure junk still pushed it up.

Residual Standard Error

Code
sigma(m1)
[1] 7.122255

Typical miss: about 7 years of life expectancy.

  • Expressed in the units of \(Y\)

  • Compare it to the outcome’s own spread (sd(g07$lifeExp) is about 12) to gauge improvement

  • More meaningful than \(R^2\)

Diagnostic Plots

Residuals vs Fitted

  • Left: a clear arc. The functional form is wrong.

  • Right: flatter, though the spread still narrows at the top

You want structureless noise. Any visible pattern is the model telling you what it missed.

Why Fitted and Not Observed?

  • Against \(Y\) there is always an upward trend, even when the model is right: the slope is exactly \(1 - R^2\), here 0.35

  • Against \(\hat{Y}\) the trend is exactly zero, by construction. With the hat matrix from earlier, \(\hat{\mathbf{y}} = \mathbf{H}\mathbf{y}\) and \(\mathbf{e} = (\mathbf{I} - \mathbf{H})\mathbf{y}\), and because \(\mathbf{HH} = \mathbf{H}\): \[\hat{\mathbf{y}}'\mathbf{e} = \mathbf{y}'(\mathbf{H} - \mathbf{HH})\mathbf{y} = 0\]

So any pattern against the fitted values is structure the model missed, not an artifact of the plot.

Which One Is Real?

Eight panels come from fake data that the fitted model describes perfectly. One is the real data.

Show the simulation
set.seed(8410)
real_pos <- sample(1:9, 1)   # hide the real residuals in a random panel
lineup <- map_dfr(1:9, function(p) {
  if (p == real_pos) return(tibble(panel = p, fit = fitted(m1), res = resid(m1)))
  y_fake <- fitted(m1) + rnorm(nrow(g07), 0, sigma(m1))   # data the model describes perfectly
  m_fake <- lm(y_fake ~ g07$lgdp)
  tibble(panel = p, fit = fitted(m_fake), res = resid(m_fake))
})

ggplot(lineup, aes(fit, res)) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey50") +
  geom_point(alpha = .6, color = "#522D80", size = 0.9) +
  facet_wrap(~panel, ncol = 3) +
  labs(x = "Fitted values", y = "Residuals") +
  theme_minimal(base_size = 12)

Real data: panel 5. The spread narrows at the top and a few countries sit far below. If you can pick out the real panel, the model is missing something (a lineup, Buja et al. 2009).

Color by What You Left Out

  • Income alone leaves African countries 4.1 years below the line on average, and the Americas 3.7 above

  • Add continent and every continent’s residuals average exactly zero: \(\mathbf{X}'\mathbf{e} = \mathbf{0}\) has one equation per dummy column

Normality of Residuals

  • Deviation in the lower tail - some countries do far worse than predicted

  • This matters least of all the assumptions. The CLT means inference is fine in large samples regardless.

Do not reject a model because a QQ plot is imperfect. Do look at which cases sit in the tails.

Influence

Not All Points Count Equally

  • Leverage — how unusual a case is in its \(X\) values

  • Residual — how badly the model predicts it

  • Influence — how much the fit would change if you dropped it

High influence requires both unusual \(X\) and a large residual. Cook’s distance combines them.

Leverage Is \(h_{ii}\)

Earlier we met the hat matrix \(\mathbf{H} = \mathbf{X}(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}'\), which projects \(\mathbf{y}\) onto the plane spanned by the columns of \(\mathbf{X}\). Its diagonal is leverage.

Code
X <- model.matrix(m1)
H <- X %*% solve(t(X) %*% X) %*% t(X)
all.equal(unname(diag(H)), unname(hatvalues(m1)))
[1] TRUE
Code
mean(hatvalues(m1))   # (k + 1) / n
[1] 0.01408451
  • Row \(i\) of \(\hat{\mathbf{y}} = \mathbf{H}\mathbf{y}\) says \(\hat{Y}_i = h_{ii}Y_i + \sum_{j \neq i} h_{ij}Y_j\), so \(h_{ii}\) is how much \(Y_i\) pulls its own fitted value

  • Leverages average \((k+1)/n\). A common rule flags anything above twice that: here 4 countries, the four poorest in the data

They Are Genuinely Different

Point size is Cook’s distance. The influential cases are high on the vertical axis, not the horizontal one.

Reading The Plot

country leverage resid cooksD
Congo, Dem. Rep. 0.042 1.0 0.000
Liberia 0.033 -2.7 0.002
Burundi 0.032 1.0 0.000
  • The highest-leverage countries are the very poorest: unusual in \(X\), sitting at the far left of the income range

  • But their residuals are tiny. The model predicts them well.

  • So they have almost no influence, despite being extreme in \(X\)

Compare that to Swaziland and Botswana: ordinary incomes with enormous residuals, and thus the largest Cook’s distances in the data.

Cook’s Distance

\[D_i = \underbrace{\frac{e_i^2}{(k+1)\,s^2}}_{\text{badly predicted}} \times \underbrace{\frac{h_{ii}}{(1-h_{ii})^2}}_{\text{unusual in } X}\]

Code
h <- hatvalues(m1); e <- resid(m1); s2 <- sigma(m1)^2; k <- 1
D <- e^2 / ((k + 1) * s2) * h / (1 - h)^2
all.equal(D, cooks.distance(m1))
[1] TRUE
  • A product: a big residual with little leverage, or big leverage with a small residual, stays small

  • A common rule flags \(D_i > 4/n\). Here that is 7 countries, every one in sub-Saharan Africa

Comparing

Unusual in \(X\)? Badly predicted? Moves the fit?
High leverage yes not necessarily only if also badly predicted
Outlier not necessarily yes only if also unusual in \(X\)
Influential yes yes yes

You need both to matter. A point far out in \(X\) that sits on the line just anchors it; a badly predicted point in the middle of the data has little leverage to pull with.

Who Is Driving This Fit?

country cooksD residual
Botswana 0.048 -22.2
Swaziland 0.048 -25.9
Equatorial Guinea 0.042 -21.1
Angola 0.039 -23.3
South Africa 0.037 -21.4
  • Swaziland, Botswana, Equatorial Guinea, Angola, South Africa - all 20+ years below what income predicts

  • These are not data errors. In 2007 they were among the countries hardest hit by HIV/AIDS.

An outlier is a finding! The model is telling you income does not explain these cases, which is substantively the interesting part.

What Not To Do

  • Do check flagged cases for data errors first: a turnout of 140% or an age of -3 gets fixed or dropped

  • Do not delete real influential points to improve fit

  • Do report whether your conclusions depend on them

Code
top5 <- c("Swaziland", "Botswana", "Equatorial Guinea", "Angola", "South Africa")
coef(m1)["lgdp"]
    lgdp 
7.202802 
Code
coef(lm(lifeExp ~ lgdp, data = g07 |> filter(!country %in% top5)))["lgdp"]
    lgdp 
7.354504 

A Sensible Workflow

  1. Plot the raw data before fitting anything

  2. Fit, then plot residuals against fitted values

  3. Check influence, and look at which cases are influential

  4. Refit under a reasonable alternative and see whether conclusions move

  5. Report what you found, including what did not change