Stock against days

R output
Residuals against fitted values

Residuals in row order

Normal QQ plot

500 repeated samples

Each row summarizes 500 new samples drawn from the true curve on the same days.

The fix
Worked question

Can lm() fit \(y = \beta_0 e^{\beta_1 x}\) as written? What would you run instead, and what does \(\beta_1\) mean afterward?

Not as written: \(\beta_1\) is used as a power. Take logs of both sides, \(\log y = \log\beta_0 + \beta_1 x + \log\varepsilon\), and the \(\beta\)'s add. Run lm(log(y) ~ x). Then \(\beta_1\) is a rate: each day multiplies the stock by \(e^{\beta_1}\), about \(100\beta_1\) percent per day.

◆ The Blueprint
What it says
\(y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \varepsilon\)

Each \(\beta\) multiplies one term and the terms add. The \(x\)'s may be squared or logged, and the \(\beta\)'s enter as plain multipliers.

What it does for you

Least squares solves a set of linear equations for the \(\beta\)'s.

What fails

With a \(\beta\) used as a power, or a \(\beta\) you divide by, there are no linear equations to solve, and lm() fits a different model from the one you wrote.

Where to look

Read the equation, since this assumption has no test. On this tab the residual plot bends and RESET starts to reject as the curve steepens, because the straight line that lm() fits in place of the officer's model leaves out the curve, which breaks assumption 2.

The fix

Take logs of both sides when that makes the \(\beta\)'s add, and the slope then means a percentage change. Otherwise the model needs nonlinear least squares, which is beyond this course.

In Wooldridge

MLR.1, Linear in Parameters. It is the first of the five assumptions behind the Gauss-Markov theorem (Theorem 3.4).

The dial

The dial is \(\beta_1\), the growth rate per day, in \(y = 100\,e^{\beta_1 x}\varepsilon\). At the low end \(e^{\beta_1 x}\) is close to a line over 60 days, and both fits forecast day 60 well.

Finish time against load

R output
Residuals against fitted values

Residuals in row order

Normal QQ plot

500 repeated samples

Each row summarizes 500 new samples of soldiers drawn from the true model. The printed standard error is the one summary() reports, and the true spread is the standard deviation of the slope across the 500 samples.

The fix

Worked question

A model of ride fare on miles driven fails RESET. Name two causes of the failure, and say which one RESET can see.

A curve (fare rises faster, or slower, than a straight line in miles), or a left-out variable such as the market the ride was in, which moves with miles and biases the miles slope. RESET sees the first directly. It only catches the second when the left-out variable happens to bend the fitted relationship; otherwise nothing in the residuals shows it.

◆ The Blueprint
What it says
\(\mathrm{E}[\varepsilon \mid x] = 0\)

At every \(x\) the errors average zero: there is no curve the line missed and no left-out variable that moves with \(x\).

What it does for you

An unbiased slope, which averages to the truth over repeated samples.

What fails

The slope is the wrong number, and its standard error, \(p\)-value, and interval all describe that wrong number. More data makes you surer of it: at \(n = 1280\) the interval is narrow and misses the true slope at 40 pounds in every sample.

Where to look

Residuals against fitted values, where a bend means a missed curve. For a left-out variable, look at the columns the data have that the model leaves out, and at how the data were collected.

The test

resettest(model), Ramsey's RESET, adds the squared fitted values and tests whether they help. \(H_0\): the squared fitted values add nothing. \(F\) on 1 and \(n-p-2\) degrees of freedom, where \(p\) is the number of predictors. A left-out variable has no test.

The fix

Add the curve (\(x^2\), \(\log x\)) or the missing variable, and refit.

In Wooldridge

MLR.4, Zero Conditional Mean (numbered 2 on these tabs), the assumption behind unbiasedness (Theorem 3.1). Wooldridge's discussion of omitted-variable bias (Section 3.3) is the left-out-variable case.

The dials

The curvature dial is \(c\) in \(\text{time} = 95 + 0.10(\text{load}-35) + c(\text{load}-35)^2 + \varepsilon\). Its middle notch is the memo's data, \(c = 0.028\), and at zero the line is the right shape. The true slope at a load is \(d\,\text{time}/d\,\text{load} = 0.10 + 2c(\text{load} - 35)\).

The left-out variable dial is \(\rho\). Each soldier's unrecorded slowness \(z\) is the standardized error, and each load is round(35 + 40 * pnorm(-ρ z + sqrt(1 - ρ²) q)) with \(q\) the standard normal value implied by the memo's own load, so at \(\rho = 0\) loads are random and as \(\rho\) grows the faster soldiers get heavier loads. The readout's \(r\) is the correlation between load and \(z\). The fix card's fitness column is \(z\) measured with a little error, so higher means slower. Every notch reuses the same 80 draws, so only the curvature or the load assignment changes.

Distance against flight hours

R output
Fuel against flight hours

Residuals against fitted values

Residuals in row order

Normal QQ plot

500 repeated samples

Each row summarizes 500 new samples drawn from the true model on the same sorties' hours and distances. The printed standard error is the one summary() reports, and the true spread is the standard deviation of the slope across the 500 samples.

The fix
Worked question

Meridian Rides regresses fare on miles, minutes, and surge pricing for 200 rides, fare ~ miles + minutes + surge. Miles and minutes have a correlation of 0.989, and the variance inflation factors are 46.7 for miles, 46.7 for minutes, and 1.0 for surge. The miles slope is \(-1.24\) (standard error 0.73), and after dropping minutes it is 1.83 (standard error 0.11). Which slopes in the full model can you read, and which can you not? What does the sign of the miles slope mean?

Surge can be read: its VIF is 1.0, so no other predictor in the model is correlated with it. Miles and minutes cannot: the data have almost no rides where one moves without the other, so least squares divides one real effect between the two slopes arbitrarily. The negative sign comes from that split, and the slope is less than twice its standard error, so it says nothing about fares.

◆ The Blueprint
What it says
no constants with \(x_j = a_0 + \sum_{k \ne j} a_k x_k\)

No predictor is an exact combination of the others.

What it does for you

Each predictor gets its own slope: the effect of \(x_1\) with \(x_2\) held fixed.

What fails

With perfect collinearity there is no unique solution, and lm() drops a predictor. With near collinearity the slopes exist, but their standard errors grow large and their signs can reverse, while predictions stay accurate. Over 500 samples the hours slope averages 1400 at every notch and its spread grows, so this failure is about precision, and the 95% interval still covers the truth about 95% of the time.

Where to look

With two or more predictors, plot them against each other. Points on a straight line are the warning sign.

The test

vif(model) from car gives one variance inflation factor (VIF) per predictor: \(\text{VIF}_j = 1/(1-R^2_j)\), where \(R^2_j\) is from regressing \(x_j\) on the other predictors. Above 5 is worth a look, and above 10 is a problem. There is no \(p\)-value.

The fix

Drop one of the pair or combine them. Ridge and lasso keep both and shrink the slopes.

In Wooldridge

MLR.3, No Perfect Collinearity. Wooldridge's variance formula (Section 3.4) has \(1 - R^2_j\) in the denominator, and the VIF is that term inverted.

The dial

The dial is the scatter of distance around \(380 \times\) hours, in nautical miles. Wide scatter means the two predictors move separately. The second-to-last notch is the planner's data, \(r = 0.996\) and VIF 136. The true model is \(\text{fuel} = 900 + 1400 \times \text{hours} + \varepsilon\) at every notch.

Cost against odometer miles

R output
Residuals against fitted values

Residuals in row order

Normal QQ plot

500 repeated samples

Each row summarizes 500 new samples drawn from the true model on the same 90 odometer readings. The printed standard error is the one summary() reports, and the true spread is the standard deviation of the slope across the 500 samples.

The fix
Worked question

Breusch-Pagan regresses the squared residuals on \(x\). Why not the residuals themselves?

Least squares makes the residuals uncorrelated with \(x\) by construction, so regressing them on \(x\) gives a slope of exactly zero whatever the spread does. The squared residual at \(x_i\) estimates the error variance there, so if that variance changes with \(x\), the squared residuals trend with \(x\).

◆ The Blueprint
What it says
\(\mathrm{Var}(\varepsilon \mid x) = \sigma^2\)

The spread of the errors is the same at every \(x\). Its name is homoskedasticity, and its failure is heteroskedasticity.

What it does for you

Correct standard errors and prediction intervals, because least squares uses one spread for the whole data set.

What fails

The slope is still unbiased. Its standard error can be wrong when the spread changes with \(x\), but on this tab it stays close: at the memo's data the printed standard error is 0.0061 against a true spread of 0.0065, and the slope's 95% interval covers 0.05 in 95% of samples. The prediction interval for one truck uses one spread at every mileage, so it covers 84% of trucks over 45,000 miles and 100% of those under 20,000.

Where to look

Residuals against fitted values, for a fan that widens or narrows as the fitted value grows.

The test

bptest(model), Breusch-Pagan, regresses the squared residuals on the predictors, and \(H_0\) is that the spread does not depend on them. The statistic is \(n\) times that regression's \(R^2\), against a \(\chi^2\) with one degree of freedom per predictor.

The fix

Robust (sandwich) standard errors keep the slope and replace its standard error: coeftest(model, vcov = vcovHC(model)). Here they barely change the slope's interval (1.02 times the printed standard error), and they do not repair the prediction interval. That needs an interval that widens with mileage, from a model of the spread, or a log of the response, which often evens the spread.

In Wooldridge

MLR.5, Homoskedasticity (numbered 4 on these tabs), the last of the five Gauss-Markov assumptions. Chapter 8 covers heteroskedasticity, with robust standard errors in Section 8.2 and the Breusch-Pagan test in Section 8.3.

The dial

The dial is \(f\) in the error's standard deviation \(60 + f \times \text{miles}\). At zero every truck misses the line by about the same amount, and the middle notch is the memo's data, \(f = 0.022\).

Sick calls against daily high

R output
Residuals against fitted values

Residuals in collection order

Normal QQ plot

500 repeated samples

Each row summarizes 500 new 120-day samples drawn from the true model on the same daily highs. The printed standard error is the one summary() reports, and the true spread is the standard deviation of the slope across the 500 samples.

The fix
Worked question

Your data are 200 soldiers chosen at random from the brigade roster, one row each. Should you run Durbin-Watson? What does independence mean for these data?

No. The row order is the order of the roster draw, which means nothing, so a streak or the lack of one says nothing. Independence here is about how the sample was drawn: random selection makes the errors independent, and no residual plot can check that. A sample of 200 soldiers from one platoon would not be independent, and no test would show it.

◆ The Blueprint
What it says
\(\mathrm{Cov}(\varepsilon_i, \varepsilon_j) = 0\) for \(i \ne j\)

One observation's error says nothing about the next one's. Independence usually fails when the rows have a time order, and it also fails when the rows were drawn in clusters, such as soldiers from one platoon.

What it does for you

Correct standard errors. Each observation adds new information, so the standard error uses the full \(n\).

What fails

The slope is still unbiased, but correlated observations contain less information than the same number of independent ones. The standard error comes out too small, and the \(p\)-values overstate the evidence. At the memo's data the printed \(p\) is below 0.001 in 31% of samples, and the Newey-West \(p\) is below 0.05 in 39%.

Where to look

Residuals in the order collected, for long runs above and then below zero. The plot shows carry-over only when the rows are in collection order.

The test

dwtest(model), Durbin-Watson, tests \(H_0\) that consecutive residuals are uncorrelated. DW runs from 0 to 4: near 2 means none, and well below 2 means each residual resembles the last.

The fix

Newey-West standard errors, coeftest(model, vcov = NeweyWest(model)). Or add yesterday's \(y\) as a predictor, so the model includes the day-to-day dependence.

In Wooldridge

Wooldridge splits the assumption numbered 5 on these tabs in two. For a cross section it is MLR.2, Random Sampling: independence comes from how the rows were drawn. For rows in time order it is TS.5, No Serial Correlation, in Chapter 10, with the Durbin-Watson test and Newey-West standard errors in Chapter 12.

The dial

The dial is \(\varphi\) in \(\varepsilon_t = \varphi\,\varepsilon_{t-1} + u_t\), the share of yesterday's error repeated in today's. The fresh draws \(u_t\) behind the memo's data were recovered and rescaled so every notch has the same fresh draws and the same overall standard deviation of the error, and only \(\varphi\) changes. The memo's data use \(\varphi = 0.8\).

Minutes against complexity

R output
Residuals against fitted values

Residuals in row order

Normal QQ plot

500 repeated samples

Each row summarizes 500 new samples drawn from the true model on the same 60 complexity ratings.

The fix
Worked question

\(n = 200\) and Shapiro-Wilk rejects. Which of these still works: the \(p\)-value on the slope, or a 95% prediction interval for one new observation? Why?

The \(p\)-value on the slope works: at \(n = 200\) the slope is close to normal whatever the errors look like. The prediction interval does not: it is built from one error, and that error is skewed, so the interval misses on the wrong side. On this tab's tickets a 95% interval still covers about 94%, but the misses are 5.6% above the upper end and 0.0% below the lower end, against 2.5% on each side.

◆ The Blueprint
What it says
\(\varepsilon \sim N(0, \sigma^2)\)

Most misses are small, big misses are rare, and misses above and below the line are equally likely.

What it does for you

Exact \(t\) and \(F\) tests and intervals at any sample size. Least squares is BLUE without it.

What fails

At large \(n\) the slope's \(t\)-test and confidence interval still work, because the slope averages many errors and averages come out close to normal. The prediction interval for one new observation is wrong, because it depends on the shape of a single error. At the memo's data the slope's interval still covers 94%, but the interval for one ticket misses 5.6% above and 0.0% below.

Where to look

A QQ plot of the residuals. Points that bend away from the line at one end mean a heavy or skewed tail on that side.

The test

shapiro.test(resid(model)), with \(H_0\) that the residuals are normal. At large \(n\) it rejects small departures that do not matter, so read it with the QQ plot.

The fix

Log the response when the tail is on the high side, or bootstrap the prediction interval.

In Wooldridge

MLR.6, Normality. Added to the Gauss-Markov five, it gives the classical linear model assumptions (Section 4.1). Wooldridge's Section 5.2 is the large-sample argument for why the slope's tests work without it.

The dial

The error is \(9(a z + b(E - 1))\) with \(z\) standard normal, \(E\) standard exponential, and \(a^2 + b^2 = 1\), so the spread is the same at every notch and only the shape changes. The dial is \(b\), and skewness is \(2b^3\). At the far right the error is a shifted exponential, the memo's data.

Data and fitted line

R output
Residuals against fitted values

Residuals in collection order

Normal QQ plot

500 repeated samples

Each row summarizes 500 new samples drawn from the true model with the same \(x\) values. The printed standard error is the one summary() reports, and the true spread is the standard deviation of the slope across the 500 samples.

◆ The Blueprint
The Anvil

Every case here was computed once, in R, with a seed: 81 combinations of the four dials, each fitted, tested, and then repeated 500 times with the design held fixed. Every case was fitted once in R, and moving a dial shows that case's results.

The dials

Curve adds \(c(x-5)^2\) to the truth. The single fitted slope then averages about 2, while the true slope differs at the two ends. This breaks zero conditional mean.

Unequal variance scales the error's spread by \(1 + f\,x/5\), a fan. This breaks equal variance.

Autocorrelation makes each row's error \(\varphi\) times the last row's plus a fresh piece. This breaks the no-autocorrelation assumption. Here \(x\) varies at random from row to row, so autocorrelation alone barely changes the slope's standard error, and its cost is a prediction interval that is too narrow (91% coverage at strong).

Skew mixes an exponential into the error, \(a z + b(E-1)\) with \(a^2 + b^2 = 1\). This breaks normal errors. The slope's interval still works, and the prediction interval's misses move to the high side.

Two violations together

Unequal variance and autocorrelation together make the printed standard error too small: at strong, the 95% interval covers the slope 80% of the time. Unequal variance and skew together make Breusch-Pagan reject in 53% of samples, against 96% for unequal variance alone. Autocorrelation never shows in the residuals against fitted values; only the order plot and Durbin-Watson detect it.

The mystery cases

Six fixed cases, in order, chosen so that each planted violation's own test rejects in that sample. In the fourth, Shapiro-Wilk also rejects although no skew is planted, because unequal variance makes the residuals look heavy-tailed.

Wooldridge's theorem

Under MLR.1 to MLR.5 the least squares estimators are the best linear unbiased estimators, BLUE (Theorem 3.4). The Anvil with every dial off is that case. Each dial breaks one of the five, or MLR.6.

Credits

Jeffrey M. Wooldridge, Introductory Econometrics: A Modern Approach, Chapters 3, 4, 8, 10, and 12, for the assumption list, the BLUE argument, and the plain reading of what each assumption does. The stories and worked questions come from the MA376 regression diagnostics lesson, and the data are simulated from the seeds in The Script.

Other Forge tools: OLS Basics, Regression & Regularization, Bias-Variance.

The diagnostics, function by function
CallWhat it returns
lm(y ~ x, data = d)The least squares fit. summary() prints the estimates, standard errors, \(p\)-values, and \(R^2\).
plot(fitted(model), resid(model))Residuals against fitted values: a bend for assumption 2, a fan for assumption 4.
plot(resid(model), type = "b")Residuals in row order: long runs for assumption 5, when the rows have an order.
qqnorm(resid(model)); qqline(resid(model))The normal QQ plot for assumption 6.
resettest(model, power = 2, type = "fitted")Ramsey's RESET from lmtest, for assumption 2.
vif(model)One variance inflation factor per predictor, from car, for assumption 3.
bptest(model)Breusch-Pagan from lmtest, for assumption 4.
dwtest(model)Durbin-Watson from lmtest, for assumption 5.
shapiro.test(resid(model))Shapiro-Wilk, for assumption 6.
coeftest(model, vcov = vcovHC(model))The same slopes with robust standard errors from the sandwich package, the fix for assumption 4.
coeftest(model, vcov = NeweyWest(model))The same slopes with Newey-West standard errors from the sandwich package, the fix for assumption 5.
gm_in_R.R