🔍 Module 3: Checking Assumptions and Diagnostics

Making sure your regression is valid

Working guidelines

Learning Objectives

The Four Key Assumptions of Linear Regression

Assumption What It Means How to Check
1. Linearity The relationship between X and Y is linear Residual plot (should show no pattern)
2. Independence Observations are independent of each other Study design (can't test with plots)
3. Normality Residuals are normally distributed Q-Q plot, histogram of residuals
4. Equal Variance Variance of residuals is constant (homoscedasticity) Residual plot (spread should be even)
Remember LINE:
  • Linearity
  • Independence
  • Normality (of residuals)
  • Equal variance

What Are Residuals?

Residual = Observed Y - Predicted Y

Residuals are the errors in our predictions. Good regression means small, random residuals with no patterns.

Why residuals matter:
  • They tell us how wrong our predictions are
  • Patterns in residuals reveal assumption violations
  • We check assumptions using residuals, not raw data

Demo 1: Understanding Residuals

Open Posit Cloud. This whole module runs on one file: 60 cases, one predictor x, and five different outcomes. Each outcome breaks exactly one assumption, so you can see one fault at a time.

Run this:

d <- read.csv("https://jkoxford-a11y.github.io/awesomesaucestats/data/regression_diagnostics.csv") str(d) good <- lm(y_good ~ x, data = d) summary(good) # a residual is one observed value minus what the line predicted for it head(round(residuals(good), 3))

What R gives for the reference model:

Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 20.8702 1.7927 11.64 <2e-16 *** x 2.9451 0.1718 17.14 <2e-16 *** Residual standard error: 6.837 on 58 degrees of freedom Multiple R-squared: 0.8351, Adjusted R-squared: 0.8323 F-statistic: 293.8 on 1 and 58 DF, p-value: < 2.2e-16

y_good is the reference case — it meets all four assumptions. Everything below is measured against it.

Question 1: In your own words, what is a residual?

Assumption 1: Linearity

What it means: The relationship between X and Y is actually linear (a straight line fits well).

How to check: Plot residuals vs. fitted values. Should see random scatter with no pattern.

Good (linear): Residuals randomly scattered around zero
Bad (non-linear): Residuals show a curve or U-shape pattern

Demo 2: Checking Linearity

The four-panel diagnostic plot from Chapter 22 is the workhorse. Its top-left panel — Residuals vs Fitted — is the linearity check.

Run this:

curved <- lm(y_curved ~ x, data = d) par(mfrow = c(2, 2)) plot(good) # the reference: no pattern in the top-left panel plot(curved) # compare par(mfrow = c(1, 1)) summary(curved)

What R gives for y_curved:

Estimate Std. Error t value Pr(>|t|) (Intercept) 25.6281 3.5884 7.142 0.0000 x -0.1266 0.3439 -0.368 0.7141 Multiple R-squared: 0.0023

Look at what just happened. The slope is not significant (p = .71) and R² is 0.002 — by the numbers, x tells you nothing about y_curved. But the residual plot shows a clean U.

There is a strong relationship here. It is just not a straight one, and a straight-line model cannot see it. This is the single best argument for looking at the plot before you trust the p-value.

Question 2b: y_curved has a non-significant slope and R² = .002. Explain to a classmate why "there is no relationship between x and y_curved" would be the wrong conclusion.

Question 2: What does it mean if your residual plot shows a curved pattern?

Assumption 2: Independence

What it means: Each observation is independent - knowing one observation doesn't tell you about another.

Common violations:
  • Time series: Observations collected over time (today's value related to yesterday's)
  • Clustered data: Students within classrooms, patients within hospitals
  • Repeated measures: Multiple observations from the same person
How to check: Think about your study design
  • Are observations collected over time?
  • Are observations nested within groups?
  • Do you have repeated measurements?

If yes to any, you may need special methods (time series analysis, multilevel models, repeated measures ANOVA).

Assumption 3: Normality of Residuals

What it means: The residuals follow a normal distribution.

How to check: Q-Q plot or histogram of residuals.

Important: We check if RESIDUALS are normal, not if X or Y are normal!

Demo 3: Checking Normality of Residuals

The top-right panel of plot(model) is the Q-Q plot of the residuals — the same plot you read in the normality modules, applied to residuals instead of raw scores. Chapter 11's rule applies: check the residuals, not y.

Run this:

skew <- lm(y_skew ~ x, data = d) par(mfrow = c(1, 2)) qqnorm(residuals(good), main = "y_good residuals"); qqline(residuals(good), col = "red") qqnorm(residuals(skew), main = "y_skew residuals"); qqline(residuals(skew), col = "red") par(mfrow = c(1, 1)) shapiro.test(residuals(good)) shapiro.test(residuals(skew))

What R gives:

> shapiro.test(residuals(good)) Shapiro-Wilk normality test W = 0.99131, p-value = 0.947 > shapiro.test(residuals(skew)) Shapiro-Wilk normality test W = 0.88396, p-value = 3.597e-05

Question 3: Why do we check if residuals are normal rather than checking if Y is normal?

Assumption 4: Equal Variance (Homoscedasticity)

What it means: The spread of residuals is the same across all values of X.

How to check: Residual plot - spread should be consistent, not getting wider or narrower.

Good (homoscedastic): Equal spread of residuals across all X values
Bad (heteroscedastic): Residuals fan out (wider spread at high X) or fan in (narrower at high X)

Demo 4: Checking Equal Variance

The bottom-left panel (Scale-Location) is the equal-variance check. You want a flat band; a wedge means the spread grows with the fitted value.

Run this:

fan <- lm(y_fan ~ x, data = d) par(mfrow = c(2, 2)) plot(fan) par(mfrow = c(1, 1)) # compare the two residual-vs-fitted pictures directly par(mfrow = c(1, 2)) plot(fitted(good), residuals(good), main = "y_good"); abline(h = 0, col = "red") plot(fitted(fan), residuals(fan), main = "y_fan"); abline(h = 0, col = "red") par(mfrow = c(1, 1))

y_fan was built so its residual spread grows with x. Its slope estimate is still roughly unbiased — what heteroscedasticity damages is the standard errors, and therefore every p-value and confidence interval the model reports.

Question 4b: Unequal variance does not really bias the slope, but it does make the standard errors wrong. Why does that matter more than it might sound?

Outliers and Influential Points

Outlier: An observation that doesn't fit the pattern (large residual)

Influential point: An observation that strongly affects the regression line

Type Description Impact
Outlier in Y Far from regression line vertically Large residual, but may not affect slope much
Outlier in X Extreme X value (leverage point) Can strongly pull the regression line
Influential point Both extreme X AND doesn't fit pattern Changes slope and R² substantially

Demo 5: Outliers vs. Influential Points

y_out is identical to y_good except for a single changed value, at the largest x in the dataset. That is the difference between an outlier and an influential point: an unusual y out at the edge of the x range moves the line, one in the middle mostly does not.

Run this (Chapter 25's Cook's distance check):

out <- lm(y_out ~ x, data = d) cooks_d <- cooks.distance(out) plot(cooks_d, type = "h", main = "Cook's distance", ylab = "Cook's D") abline(h = 4 / length(cooks_d), col = "red", lty = 2) which(cooks_d > 4 / length(cooks_d)) round(max(cooks_d), 3) # refit without the influential case and compare the slope coef(out) coef(lm(y_out ~ x, data = d[-which.max(cooks_d), ]))

What R gives:

> which(cooks_d > 4 / length(cooks_d)) [1] 9 10 43 > round(max(cooks_d), 3) [1] 1.28 # case 9 slope with case 9 : 2.5216 slope without case 9: 2.8513

Three cases clear the 4/n screening threshold, but only case 9 exceeds the far stricter Cook's D > 1 rule — and dropping it moves the slope by 0.33, about two standard errors. The other two are flagged by a deliberately sensitive screen and turn out not to matter.

Question 5a: You find one point with Cook's D = 1.28. What do you do about it? (There is a wrong answer here: "delete it and re-run.")

Question 5b: You report a slope of 2.52. A reviewer asks what happens if you drop case 9. Write the sentence you would add to the paper.

What to Do When Assumptions Are Violated

Violation Solutions
Non-linearity - Transform X or Y (log, square root)
- Add polynomial terms (X²)
- Use non-linear regression
Non-independence - Use multilevel/mixed models
- Time series methods
- Repeated measures ANOVA
Non-normal residuals - Transform Y
- Use robust regression
- Note: With large n, less critical
Unequal variance - Transform Y (often log)
- Use weighted least squares
- Use robust standard errors
Outliers/Influential - Investigate: data error or real?
- Report with and without
- Use robust regression
- NEVER just delete without justification

Key Takeaways

Remember:
  • ALWAYS check assumptions before trusting your regression
  • Use residual plots - they reveal problems
  • Check RESIDUALS for normality, not raw Y values
  • Patterns in residuals = assumption violations
  • Investigate outliers - don't just delete them
  • Many violations can be fixed with transformations
  • With large samples, normality is less critical

Ready to Continue?

Make sure you can:

  1. Name the four key assumptions of regression
  2. Explain what residuals are
  3. Interpret residual plots for linearity and equal variance
  4. Check normality of residuals
  5. Distinguish outliers from influential points

Before you submit

Grading: Credit/no-credit, not scored for correctness — but credit requires a genuine attempt; blank or clearly-unattempted answers earn no credit. Work through it with your partner and say what you actually think; a wrong idea you reasoned your way to is worth more here than a right one you copied.

How to submit

  1. Click Save progress.
  2. Print: Ctrl+P (Windows) or Cmd+P (Mac).
  3. Choose "Save as PDF".
  4. Save as reg3_lastname1_lastname2.pdf and upload to the course site.