Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

In 1973 two biologists, Michael Johnson and Peter Raven, tallied the number of plant species on 30 islands of the Galapagos archipelago and recorded a handful of geographic facts about each island: its area, its highest elevation, how far it sits from the nearest island, how far from the central island of Santa Cruz, and the area of the closest neighbor. The natural question is whether geography predicts biological richness. Bigger, taller, more isolated islands might hold more species. A multiple regression of species count on those five predictors is the obvious first move, and it looks like a success: it explains about 77 percent of the variation in species counts.

Then you plot the residuals, and the model falls apart. Figure 1 shows the residuals against the fitted counts. Three problems jump out at once. The spread of the residuals grows as the fitted count grows, a funnel that breaks the constant-variance assumption. One island, Isabela, sits far from the crowd. And for several small islands the model predicts a negative number of species, which is impossible. A fit that reports a high R2R^2 can still be wrong in every way that matters.

Scatterplot of residuals against fitted species counts for the Galapagos regression. The points fan outward as the fitted count increases, forming a funnel. Isabela is labeled as a large negative residual. A shaded band on the left marks fitted values below zero, where the model predicts a negative number of species.

Figure 1:The Galapagos model earns a high R-squared yet fails on sight: the residual spread widens with the fitted count (nonconstant variance), the model predicts negative species counts for small islands (the shaded region), and Isabela is an extreme case.

This chapter is about the fourth stage of the modeling workflow, CHECK. You have learned to ASK a question, EXPLORE the data, and FIT a model in matrix form (7.1 The model and least squares in matrix form) with all its inference machinery (8.4 The general linear test). Fitting is now the easy part; any computer will do it, and so will any student. The scarce skill is judgment: deciding whether a fitted model can be trusted, finding the observations that are quietly running the show, and naming exactly which assumption a model breaks. That is what diagnostics do. By the end of the chapter you will have a toolkit that turns “the fit looks fine” into a checklist you can defend.

We work two real datasets side by side. The Galapagos species data (gala) is the model that fails every test. The cross-country savings data (savings), which you first met in the correlation chapter, is the model whose assumptions mostly hold but whose fit is steered by a single country. Along the way the Toluca production data returns one last time to answer a question we could not settle before: is a straight line really the right shape?

Everything in this chapter answers one of two plain questions. Are any single data points trouble, either sitting in a strange spot or quietly steering the fit? Or does the model break a promise it made about the errors taken as a whole? Figure 2 is the map. The left branch chases troublesome cases (Sections 9.1 to 9.3); the right branch checks the assumptions (Sections 9.4 to 9.6); every box names the tool and the section that answers it. Come back to this picture whenever you lose the thread.

A flowchart of the chapter. A top box reads "You fit a model. Can you trust it?" and splits into two columns. The left column, "Which cases are trouble?", lists unusual predictors (leverage values, Section 9.1), fit poorly (studentized residual and Bonferroni, Section 9.2), and changes the fit if removed (Cook's distance, DFFITS, DFBETAS, Section 9.3). The right column, "Which assumptions break?", lists constant variance (residual and scale-location plots, Breusch-Pagan and Brown-Forsythe, Sections 9.4 to 9.5), normal errors (Q-Q plot and Shapiro-Wilk, Sections 9.4 to 9.5), independent errors (Durbin-Watson, Section 9.5), and right mean shape (lack-of-fit F, Section 9.6). Both columns feed a final box: name the broken assumption and the influential case, then decide whether to investigate, report with and without the case, or repair the model in later chapters.

Figure 2:The whole CHECK stage on one page. Diagnostics split into two families: finding the individual cases that are trouble (left) and testing the assumptions the model made about the errors (right). Each box points to the tool and the section that does the job.

9.1 Leverage values and the hat matrix

Intuition

Before we can judge a residual we have to know how much freedom the model gave each point. Picture the fitted line as a see-saw. A point sitting near the middle of the predictor values can move up or down quite a bit without shifting the line much; the crowd around it holds the line in place. A point way out at the edge of the predictor range is different: because it is the only observation out there, the line has to follow it, so the fitted line at that point is pulled toward the point’s own response. That pull is measured by the leverage value hiih_{ii} (Definition 9.1). A high-leverage point is one whose predictor values are unusual, so the fit is forced to pay it special attention regardless of its response.

The leverage value is a statement about the predictors only. It says nothing yet about whether a point is fit well or badly. It measures potential, the capacity to influence the line, and we will spend the next two sections turning potential into a verdict.

Formula

Recall the hat matrix from 7.3 The hat matrix. Stacking the observations into the design matrix X\mathbf{X} (an n×pn \times p matrix whose first column is ones, with pp the number of parameters including the intercept), the fitted values are Y^=HY\hat{\mathbf{Y}} = \mathbf{H}\mathbf{Y} where

H=X(XX)1X.\mathbf{H} = \mathbf{X}(\mathbf{X}'\mathbf{X})^{-1}\mathbf{X}' .

Derivation (properties of the leverage values)

Proof. In 7.3 The hat matrix we proved that H\mathbf{H} is symmetric (H=H\mathbf{H}' = \mathbf{H}) and idempotent (HH=H\mathbf{H}\mathbf{H} = \mathbf{H}), with trace equal to pp. Three facts follow directly.

First, because H\mathbf{H} is symmetric and idempotent, H=HH\mathbf{H} = \mathbf{H}\mathbf{H} read on the diagonal gives

hii=j=1nhijhji=j=1nhij2=hii2+jihij2.h_{ii} = \sum_{j=1}^n h_{ij} h_{ji} = \sum_{j=1}^n h_{ij}^2 = h_{ii}^2 + \sum_{j \neq i} h_{ij}^2 .

Rearranging, hiihii2=jihij20h_{ii} - h_{ii}^2 = \sum_{j \neq i} h_{ij}^2 \ge 0, so hii(1hii)0h_{ii}(1 - h_{ii}) \ge 0. Since hii=jhij20h_{ii} = \sum_j h_{ij}^2 \ge 0 as well, we get

0hii1.0 \le h_{ii} \le 1 .

In words: a leverage value is trapped between 0 and 1, and a value near 1 means the fit at that point is determined almost entirely by that point’s own response.

Second, the leverage values add up to the trace of H\mathbf{H}:

i=1nhii=tr(H)=p.\sum_{i=1}^n h_{ii} = \operatorname{tr}(\mathbf{H}) = p .

Third, dividing by nn, the average leverage value is p/np/n. A point is not carrying its fair share of predictor space if its leverage value is well above that average, which motivates the common rule of thumb: examine any case with

hii>2pn.h_{ii} > \frac{2p}{n} .

The factor of 2 is a convention, not a law; it flags roughly the points worth a second look. \blacksquare

For simple regression the leverage value has a clean closed form that shows plainly what it measures.

Proof. With one predictor, xi=(1,Xi)\mathbf{x}_i' = (1, X_i) and, as computed in 7.3 The hat matrix,

(XX)1=1nSxx(Xi2XiXin),Sxx=(XiXˉ)2.(\mathbf{X}'\mathbf{X})^{-1} = \frac{1}{n S_{xx}} \begin{pmatrix} \sum X_i^2 & -\sum X_i \\ -\sum X_i & n \end{pmatrix}, \qquad S_{xx} = \sum (X_i - \bar X)^2 .

Multiplying out the quadratic form xi(XX)1xi\mathbf{x}_i'(\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_i and simplifying with Xi=nXˉ\sum X_i = n\bar X gives

hii=1n+(XiXˉ)2Sxx.h_{ii} = \frac{1}{n} + \frac{(X_i - \bar X)^2}{S_{xx}} .

In words: the leverage value in simple regression is a baseline 1/n1/n shared by everyone, plus a penalty that grows with the squared distance of XiX_i from the mean predictor value. Figure 3 draws this parabola on the Toluca lot sizes. \blacksquare

Leverage values plotted against lot size for the Toluca data. The leverage values trace an upward-opening parabola with its minimum at the mean lot size of 70, rising toward both ends of the lot-size range. A dashed vertical line marks the mean and a dotted horizontal line marks the two-p-over-n cutoff.

Figure 3:The leverage value in simple regression is a parabola in the predictor: smallest at the mean lot size and largest at the extremes. No Toluca run crosses the 2p/n line, so none has an unusual leverage value.

R and Python

An index plot makes the two standouts obvious. Figure 4 plots each island’s leverage value against its position in the data, with the 2p/n2p/n line drawn in: Isabela and Fernandina tower over the other 28 islands, and everything else sits in a harmless band.

Index plot of the leverage values for the 30 Galapagos islands. Most islands have small values well below the dotted cutoff line at 0.40. Two spikes, labeled Isabela and Fernandina, rise near 0.95, far above every other island.

Figure 4:Two islands, Isabela and Fernandina, have leverage values near 0.95, far above the 2p/n cutoff of 0.40; the other 28 islands are unremarkable. These two occupy their own corner of predictor space.

The closed form hii=1/n+(XiXˉ)2/Sxxh_{ii} = 1/n + (X_i - \bar X)^2/S_{xx} is worth checking against the software once, so you trust that hatvalues and the hat matrix agree.

toluca <- read.csv("data/toluca.csv")
tfit <- lm(hours ~ lotsize, data = toluca)
x <- toluca$lotsize
h_closed <- 1 / nrow(toluca) + (x - mean(x))^2 / sum((x - mean(x))^2)
max(abs(hatvalues(tfit) - h_closed))
[1] 2.775558e-17
toluca = pd.read_csv("data/toluca.csv")
tfit = smf.ols("hours ~ lotsize", data=toluca).fit()
x = toluca["lotsize"].to_numpy()
h_closed = 1 / len(toluca) + (x - x.mean()) ** 2 / np.sum((x - x.mean()) ** 2)
print(np.max(np.abs(tfit.get_influence().hat_matrix_diag - h_closed)))
4.163336342344337e-17

The two agree to floating-point dust, confirming the formula.

Theorem 9.2 says the leverage values are a fixed budget that always adds up to pp, and that claim is far easier to believe once you have watched the budget being spent.

Slide one new run along the lot-size axis and watch the parabola of leverage values for all 26 runs redraw itself.

What to notice. No response value enters this picture anywhere, because hiih_{ii} is built from the predictors alone. Try this. Push the new run out to a lot size of 200 and confirm that its own leverage value climbs past 0.47 while the leverage value of every original run falls, with the sum pinned at p=2p = 2 throughout (9.1 Leverage values and the hat matrix).

9.2 Four flavors of residual

Intuition

The leverage value measured which points had the power to distort the fit. The next question is which points the fit actually misses. For that we read the residual ei=YiY^ie_i = Y_i - \hat Y_i, the model’s leftover: the part of YiY_i the fit did not capture. It is tempting to hunt for outliers by scanning the raw residuals for big values. That is a trap, because raw residuals are not on a level playing field. A high-leverage point drags the line toward itself, which shrinks its own residual. So the very points most able to distort the fit tend to have deceptively small raw residuals. To compare residuals fairly we have to put them on a common scale, and that means dividing by the right standard deviation, which depends on the leverage value.

Formula

The scale factor 1hii\sqrt{1 - h_{ii}} is not decoration. It comes from an exact variance formula for the residuals.

Derivation (the variance of a residual)

Proof. Write the residual vector as a linear map of the responses. Since Y^=HY\hat{\mathbf{Y}} = \mathbf{H}\mathbf{Y},

e=YY^=(IH)Y.\mathbf{e} = \mathbf{Y} - \hat{\mathbf{Y}} = (\mathbf{I} - \mathbf{H})\mathbf{Y}.

Under the model Var{Y}=σ2I\operatorname{Var}\{\mathbf{Y}\} = \sigma^2 \mathbf{I} (the errors are uncorrelated with common variance, from 6.7 Random vectors, expectation, and covariance matrices). The matrix IH\mathbf{I} - \mathbf{H} is symmetric and idempotent, just like H\mathbf{H}. Applying the covariance rule for a linear transform,

Var{e}=(IH)σ2I(IH)=σ2(IH)(IH)=σ2(IH).\operatorname{Var}\{\mathbf{e}\} = (\mathbf{I} - \mathbf{H})\sigma^2\mathbf{I}(\mathbf{I} - \mathbf{H})' = \sigma^2 (\mathbf{I} - \mathbf{H})(\mathbf{I} - \mathbf{H}) = \sigma^2 (\mathbf{I} - \mathbf{H}).

Reading the ii-th diagonal entry,

Var{ei}=σ2(1hii),Cov{ei,ej}=σ2hij.\operatorname{Var}\{e_i\} = \sigma^2 (1 - h_{ii}), \qquad \operatorname{Cov}\{e_i, e_j\} = -\sigma^2 h_{ij}.

In words: the residual at a high-leverage point has smaller variance, because the fit bends to meet it. That is exactly why a raw residual understates trouble at high-leverage points, and why dividing by 1hii\sqrt{1 - h_{ii}} restores a common scale. Figure 6 shows the shrinkage and its correction. \blacksquare

Two curves against the leverage value on the horizontal axis. A solid line showing one minus h falls from 1 to 0 as the leverage value grows, shaded underneath, labeled as the variance of a raw residual over sigma squared. A dashed line showing one over the square root of one minus h rises steeply, labeled as the standardizing factor.

Figure 6:As the leverage value grows, a raw residual keeps less of the error variance (solid curve), so it looks artificially small. Standardizing multiplies by the rising dashed factor to undo the shrinkage and put every residual on one scale.

Derivation (the studentized deleted residual and the outlier test)

The standardized residual rir_i has a subtle flaw: if case ii really is an outlier, it inflates ss, the very quantity in its own denominator, masking itself. The fix is to estimate the error standard deviation from the other cases. Let s(i)s_{(i)} be the root mean square error of the fit with case ii deleted. A useful algebraic identity, which avoids refitting nn times, is

(np1)s(i)2=(np)MSEei21hii.(n - p - 1)\,s_{(i)}^2 = (n - p)\,\mathrm{MSE} - \frac{e_i^2}{1 - h_{ii}} .

In words: this builds the leave-one-out error variance from quantities the full fit already gives you, by subtracting case ii’s own squared-error contribution, so you never actually refit.

Proof. Consider the deleted residual di=YiY^i(i)d_i = Y_i - \hat Y_{i(i)}, the gap between YiY_i and the prediction at xi\mathbf{x}_i from a model that never saw case ii. Because that prediction does not use YiY_i, the two are independent. An identity we prove from the leave-one-out update in 9.3 Influence: which points actually change the fit gives di=ei/(1hii)d_i = e_i / (1 - h_{ii}), with variance

Var{di}=σ21hii.\operatorname{Var}\{d_i\} = \frac{\sigma^2}{1 - h_{ii}} .

Estimating σ2\sigma^2 by the independent s(i)2s_{(i)}^2 and dividing did_i by its estimated standard error yields

ti=dis(i)/1hii=eis(i)1hii    t(np1)t_i = \frac{d_i}{s_{(i)}/\sqrt{1 - h_{ii}}} = \frac{e_i}{s_{(i)}\sqrt{1 - h_{ii}}} \;\sim\; t(n - p - 1)

under the model, when case ii is not an outlier. Because numerator and denominator are independent, this is an exact tt ratio. \blacksquare

To test whether the most extreme case is a genuine outlier you look at maxiti\max_i |t_i|. Since you are implicitly running nn tests, use a Bonferroni cutoff: flag case ii when ti>t(1α/(2n);np1)|t_i| > t(1 - \alpha/(2n);\, n - p - 1). The correction keeps the chance of a single false alarm across all nn cases below α\alpha.

9.3 Influence: which points actually change the fit

Intuition

A high leverage value is potential and a large residual is a symptom; influence is the thing we actually care about, the amount the fit would change if a point were gone (Definition 9.7). A point can be high-leverage yet have little influence, if it happens to fall right on the trend the other points set. And a point with a large residual but an ordinary leverage value may tug the line only a little. Influence is the product of the two ingredients: an unusual predictor row and a response the model fits poorly.

Figure 7 makes the point with four small pictures. In each one we drop a single red point into the same cloud and refit. Push the point far out in XX but keep it on the trend, and the line does not budge: a high leverage value, no influence. Give it a big vertical miss but an ordinary XX, and the line barely tilts: a plain outlier. Only when the point is both far out in XX and off the trend does the line swing to chase it. That last panel is influence, and it takes both ingredients at once.

A two-by-two grid of scatterplots. Each panel shows the same base cloud of blue points with a fitted line, plus one added red point, and two lines, a solid fit that includes the red point and a dashed fit that excludes it. Top left, an ordinary red point on the trend: the two lines coincide, labeled harmless. Top right, a red point at an extreme predictor value but on the trend: the lines still coincide, labeled still harmless. Bottom left, a red point at an ordinary predictor value but far above the trend: the line tilts only slightly, labeled outlier with small tilt. Bottom right, a red point at an extreme predictor value and far below the trend: the solid line swings well away from the dashed line, labeled influential, the line bends.

Figure 7:Influence needs both ingredients. A point moves the fit (solid versus dashed line) only in the bottom-right panel, where the red point is both far out in the predictor and off the trend. An unusual X alone (top right) or a big miss alone (bottom left) leaves the line almost where it was.

Formula

The second form makes the recipe explicit: influence equals a residual part (ri2r_i^2) times a factor hii/(1hii)h_{ii}/(1 - h_{ii}) that grows with the leverage value. Both must be sizable for DiD_i to be large. A rough guide treats DiD_i near or above 1 as worth serious attention, and any case far above the rest as worth naming.

Derivation (the leave-one-out update and Cook’s distance)

Every influence measure rests on one algebraic fact: you can compute the fit without case ii from the full fit, no refitting required.

Proof. Deleting case ii removes the row xi\mathbf{x}_i' from X\mathbf{X} and the value YiY_i from Y\mathbf{Y}, so

X(i)X(i)=XXxixi,X(i)Y(i)=XYxiYi.\mathbf{X}_{(i)}'\mathbf{X}_{(i)} = \mathbf{X}'\mathbf{X} - \mathbf{x}_i\mathbf{x}_i', \qquad \mathbf{X}_{(i)}'\mathbf{Y}_{(i)} = \mathbf{X}'\mathbf{Y} - \mathbf{x}_i Y_i .

The Sherman-Morrison formula gives the inverse of a rank-one downdate: for an invertible A\mathbf{A} and vector u\mathbf{u} with uA1u1\mathbf{u}'\mathbf{A}^{-1}\mathbf{u} \neq 1,

(Auu)1=A1+A1uuA11uA1u,(\mathbf{A} - \mathbf{u}\mathbf{u}')^{-1} = \mathbf{A}^{-1} + \frac{\mathbf{A}^{-1}\mathbf{u}\mathbf{u}'\mathbf{A}^{-1}}{1 - \mathbf{u}'\mathbf{A}^{-1}\mathbf{u}} ,

which you can verify by multiplying the right side by Auu\mathbf{A} - \mathbf{u}\mathbf{u}' and collecting terms to get I\mathbf{I}. Set A=XX\mathbf{A} = \mathbf{X}'\mathbf{X} and u=xi\mathbf{u} = \mathbf{x}_i, so that uA1u=hii\mathbf{u}'\mathbf{A}^{-1}\mathbf{u} = h_{ii}. Multiplying the updated inverse by X(i)Y(i)\mathbf{X}_{(i)}'\mathbf{Y}_{(i)} and simplifying (using xib=Y^i\mathbf{x}_i'\mathbf{b} = \hat Y_i and ei=YiY^ie_i = Y_i - \hat Y_i) collapses to the compact result

bb(i)=(XX)1xiei1hii.\mathbf{b} - \mathbf{b}_{(i)} = \frac{(\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_i\, e_i}{1 - h_{ii}} .

In words: dropping case ii shifts the coefficient vector by a multiple of (XX)1xi(\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_i, scaled by the case’s residual and inflated by 1/(1hii)1/(1 - h_{ii}) when the leverage value is high. Setting the shift’s effect at xi\mathbf{x}_i itself gives Y^iY^i(i)=hiiei/(1hii)\hat Y_i - \hat Y_{i(i)} = h_{ii} e_i/(1 - h_{ii}), so the deleted residual is di=YiY^i(i)=ei+hiiei/(1hii)=ei/(1hii)d_i = Y_i - \hat Y_{i(i)} = e_i + h_{ii}e_i/(1-h_{ii}) = e_i/(1 - h_{ii}), the identity promised in 9.2 Four flavors of residual. \blacksquare

Proof. The numerator of DiD_i is a quadratic form in that shift, because Y^Y^(i)=X(bb(i))\hat{\mathbf{Y}} - \hat{\mathbf{Y}}_{(i)} = \mathbf{X}(\mathbf{b} - \mathbf{b}_{(i)}):

j(Y^jY^j(i))2=(bb(i))XX(bb(i)).\sum_j (\hat Y_j - \hat Y_{j(i)})^2 = (\mathbf{b} - \mathbf{b}_{(i)})'\mathbf{X}'\mathbf{X}(\mathbf{b} - \mathbf{b}_{(i)}).

Substitute the deletion formula. The middle XX\mathbf{X}'\mathbf{X} cancels one inverse on each side, leaving xi(XX)1xi=hii\mathbf{x}_i'(\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_i = h_{ii}:

(bb(i))XX(bb(i))=ei2(1hii)2xi(XX)1xi=ei2hii(1hii)2.(\mathbf{b} - \mathbf{b}_{(i)})'\mathbf{X}'\mathbf{X}(\mathbf{b} - \mathbf{b}_{(i)}) = \frac{e_i^2}{(1 - h_{ii})^2}\,\mathbf{x}_i'(\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_i = \frac{e_i^2\, h_{ii}}{(1 - h_{ii})^2} .

Divide by pMSEp\,\mathrm{MSE} and recognize ri2=ei2/(MSE(1hii))r_i^2 = e_i^2/(\mathrm{MSE}(1 - h_{ii})):

Di=ei2hiipMSE(1hii)2=ri2phii1hii.D_i = \frac{e_i^2\, h_{ii}}{p\,\mathrm{MSE}\,(1 - h_{ii})^2} = \frac{r_i^2}{p}\cdot\frac{h_{ii}}{1 - h_{ii}} .

This is the promised split of DiD_i into a squared standardized residual and a term in the leverage value hiih_{ii}. \blacksquare

R and Python

Index plot of Cook's distance for the 50 countries in the savings data. Most countries have tiny values near zero. Libya rises well above the rest, with Japan and Zambia the next largest but much smaller than Libya.

Figure 8:Cook’s distance flags Libya as the single most influential country in the savings fit, standing clearly above Japan and Zambia while the other 47 countries barely register.

The influence plot in Figure 9 combines the whole story: the leverage value on one axis, studentized deleted residual on the other, and Cook’s distance as bubble size. It separates the roles that a one-number summary blends together.

Influence plot for the savings model. The leverage value is on the horizontal axis and the studentized deleted residual on the vertical axis, with bubble area proportional to Cook's distance. Libya is far to the right as a high-leverage case with a moderate negative residual and the largest bubble. Zambia is high on the vertical axis at an ordinary leverage value. Japan sits at a moderate leverage value with a large bubble.

Figure 9:The influence plot pulls apart the two ingredients. Zambia is a large residual at an ordinary leverage value; Libya is a moderate residual at an extreme leverage value; their product, the bubble area, makes Libya the most influential case.

Naming an influential point is not the same as deleting it. The honest workflow is to refit without the case and report how much the answer moves, so a reader can judge. Figure 10 and the numbers behind it do exactly that.

Scatterplot of savings rate against income growth rate for 50 countries, with Libya drawn as a large diamond at a high growth rate. Two fitted lines are shown: one including Libya with a shallower slope and one excluding Libya with a steeper slope. Removing Libya tilts the line upward.

Figure 10:Deleting one country visibly tilts the growth relationship. Libya (the diamond) sits at an extreme growth rate, and the fit including it (solid) is pulled toward a shallower slope than the fit without it (dashed).

The four panels of Figure 7 are fixed pictures of that argument. In the widget below you move the lone case yourself and watch DiD_i answer.

Drag the lone case at X=11X = 11 and watch its leverage value, raw residual, studentized deleted residual and Cook’s distance respond together.

What to notice. The lone case starts with the largest leverage value in the data and a Cook’s distance of essentially zero, which is the top-right panel of Figure 7. Try this. Drag it down to about 6.5, then reset and drag the case at X=5X = 5 down the same distance: the same vertical miss, and almost none of the influence (9.3 Influence: which points actually change the fit).

9.4 Reading the diagnostic plots

Intuition

Formal numbers are sharp, but the fastest way to catch a broken model is to look at it. Three plots do most of the work, and each targets one assumption. The residual-versus-fitted plot checks the shape of the mean function and the constancy of variance. The normal quantile-quantile plot checks the shape of the error distribution. The scale-location plot checks constant variance again, more sensitively. You learned to draw the first of these back in 2.3 Fitted values and the properties of residuals; now you can read all three as a set.

Figure 12 is a field guide to the residual-versus-fitted plot. Four shapes recur: a formless horizontal band (healthy), a funnel (variance grows with the mean), a bend (the mean function is the wrong shape), and a lone stray point (an outlier). Train your eye on these and most diagnostics become pattern recognition.

Four small residual-versus-fitted plots. Top left, a healthy formless band of points scattered evenly around zero. Top right, a funnel where the spread widens to the right. Bottom left, a curved band that rises then falls, showing a bend. Bottom right, a formless band with one point far above the rest, an outlier.

Figure 12:The four residual patterns worth memorizing: a healthy formless band, a funnel of growing variance, a bend that signals the wrong mean structure, and a single outlier. Reading the plot is mostly matching it to one of these.

R and Python

You have already seen the Galapagos residual-versus-fitted plot in Figure 1: a textbook funnel. The normal quantile-quantile plot in Figure 13 tells the normality half of the story. In R it is one line, qqnorm(residuals(gfit)); qqline(residuals(gfit)), and Python’s statsmodels offers sm.qqplot(gfit.resid, line="s"). If the errors were normal the points would hug the reference line; here the upper tail bends away and Isabela plunges far below, the visual echo of its -5.33 studentized residual.

Normal quantile-quantile plot of the Galapagos residuals. The middle points follow the straight reference line, but the upper points curve above it and the lowest point, Isabela, falls far below the line, indicating heavy tails and a severe low outlier.

Figure 13:The Galapagos residuals depart from the normal reference line at both ends, and Isabela sits dramatically below it. The pattern says the normal-errors assumption is not credible for this model.

The scale-location plot in Figure 14 plots ri\sqrt{|r_i|} against the fitted value. A flat cloud means constant variance; a rising trend means the spread grows with the mean. The Galapagos cloud climbs steadily, confirming the funnel from a second angle.

Scale-location plot for the Galapagos model, showing the square root of the absolute standardized residual against the fitted value. The points and a fitted trend line rise from left to right, indicating that residual spread grows with the fitted count.

Figure 14:The scale-location plot rises from left to right, a clear signal that the error variance is not constant but grows with the fitted species count.

9.5 Formal tests for the assumptions

Intuition

Plots persuade; tests put a number on the suspicion. Use them together, and lean on the plots when the two disagree, because a test can miss a pattern a plot makes obvious, and a test can flag a wrinkle too small to matter. There is one test for each assumption: constant variance, normal errors, and independence.

For constant variance, two tests are standard. The Breusch-Pagan test regresses the squared residuals on the predictors and asks whether that regression explains anything; if the spread depends on the predictors, it does. The Brown-Forsythe test splits the data into groups, compares the spread of residuals across groups using medians (so it resists outliers), and is a good choice when you suspect variance changes with one particular predictor.

For normality, the Shapiro-Wilk test compares the ordered residuals to what normal data would produce; a small p-value says the residuals are not normal. A close relative is the correlation test for normality, which computes the correlation between the ordered residuals and their normal scores (the points in the quantile-quantile plot) and rejects normality when that correlation is too far below 1.

For independence in time-ordered data, the Durbin-Watson test checks whether consecutive errors are correlated.

Formula

The Durbin-Watson statistic is

D=t=2n(etet1)2t=1net22(1ρ^),D = \frac{\sum_{t=2}^n (e_t - e_{t-1})^2}{\sum_{t=1}^n e_t^2} \approx 2(1 - \hat\rho),

where ρ^\hat\rho is the estimated lag-1 autocorrelation of the residuals, the correlation between each residual and the one before it in time order.

In words: Durbin-Watson compares how much neighboring residuals differ to how big the residuals are overall; when neighbors are alike, the numerator is small and DD drops below 2. The exact null distribution depends on the design matrix, so classic tables give two bounds dLd_L and dUd_U; modern software returns an exact p-value instead. This diagnostic is the entry point for 15.2 Why time breaks ordinary least squares, where time-ordered errors are the whole subject and where models are built that fix the problem rather than just detect it.

R and Python

When normality fails, as it does for gala, one honest response is to stop relying on the normal-theory t and F machinery and switch to the resampling inference of 5.4 The bootstrap for regression, which does not assume a normal error shape. The deeper fix, changing the model so its errors behave, is the subject of Chapter 10 and, for count data like species totals, Chapter 14.

The Durbin-Watson test needs data in a meaningful order, so it does not really apply to the savings countries, which have no natural sequence. To see it work we simulate two time-ordered series: one with independent errors, one with errors that carry over from step to step.

Two residual-versus-order plots side by side. On the left, independent errors flip sign frequently around zero, with Durbin-Watson near 2. On the right, positively autocorrelated errors drift in long runs above and below zero, with Durbin-Watson well below 2.

Figure 15:Independent residuals (left) flip sign freely and give a Durbin-Watson statistic near 2; positively autocorrelated residuals (right) drift in long runs, and the statistic falls toward 0.

9.6 The lack-of-fit F test

Intuition

Every diagnostic so far asks whether the errors misbehave. The lack-of-fit test asks a different question: is the shape of the regression function right at all? Suppose the true mean of YY bends, but you fit a straight line. Then even with perfect data the line will miss the true means. The trouble is that ordinary SSE cannot tell “the model is wrong” from “the data are noisy,” because both inflate it. The way out is replication. If you have several observations at the same predictor value, the spread among them measures pure noise, with no help from any model. That gives a yardstick of irreducible error to compare the model’s misses against.

The Toluca data are built for this. The 25 runs used only 11 distinct lot sizes, so most lot sizes were repeated. Figure 16 shows the idea: at each lot size the runs scatter around their own group mean (pure error), and the group means scatter around the fitted line (lack of fit).

The Toluca scatterplot with the fitted line, the individual runs, and a diamond marking the mean of the runs at each lot size. Short vertical segments connect each group mean to the fitted line. The group means fall very close to the line, so the segments are short.

Figure 16:Lack of fit compares two spreads: the runs around their group means (pure error) and the group means around the line (lack of fit). The Toluca group means hug the line, so lack of fit is small.

Formula

Derivation (lack of fit as a general linear test)

Proof. This is the general linear test of 8.4 The general linear test with a specific pair of models. The full model puts a free mean μj\mu_j at each of the cc distinct predictor levels: Yjk=μj+εjkY_{jk} = \mu_j + \varepsilon_{jk}. Its least-squares fitted value at level jj is the group mean Yˉj\bar Y_j, so its error sum of squares is exactly SSPE=jk(YjkYˉj)2\mathrm{SSPE} = \sum_j \sum_k (Y_{jk} - \bar Y_j)^2, with ncn - c degrees of freedom because it estimates cc means. The reduced model is the straight line Yjk=β0+β1Xj+εjkY_{jk} = \beta_0 + \beta_1 X_j + \varepsilon_{jk}, whose error sum of squares is the ordinary SSE\mathrm{SSE} with n2n - 2 degrees of freedom.

The general linear test statistic is

F=[SSE(R)SSE(F)]/[dfRdfF]SSE(F)/dfF=[SSESSPE]/[(n2)(nc)]SSPE/(nc)=SSLF/(c2)SSPE/(nc).F^\ast = \frac{[\mathrm{SSE}(R) - \mathrm{SSE}(F)]/[df_R - df_F]}{\mathrm{SSE}(F)/df_F} = \frac{[\mathrm{SSE} - \mathrm{SSPE}]/[(n-2)-(n-c)]}{\mathrm{SSPE}/(n-c)} = \frac{\mathrm{SSLF}/(c-2)}{\mathrm{SSPE}/(n-c)} .

That the difference SSESSPE\mathrm{SSE} - \mathrm{SSPE} equals jnj(YˉjY^j)2\sum_j n_j(\bar Y_j - \hat Y_j)^2 follows from expanding YjkY^j=(YjkYˉj)+(YˉjY^j)Y_{jk} - \hat Y_j = (Y_{jk} - \bar Y_j) + (\bar Y_j - \hat Y_j), squaring, and summing: the cross term vanishes because k(YjkYˉj)=0\sum_k (Y_{jk} - \bar Y_j) = 0 within each group. Under the reduced model the two mean squares both estimate σ2\sigma^2, so FF^\ast is near 1; when the true means depart from a line, only MSLF\mathrm{MSLF} inflates, since E{MSPE}=σ2E\{\mathrm{MSPE}\} = \sigma^2 always. \blacksquare

R and Python

The split in Figure 16 is worth taking apart with your own hands, because the two sums of squares respond to completely different moves.

Five lot sizes with three runs each: drag the runs up and down and watch SSPE, SSLF and the lack-of-fit F test respond.

What to notice. Moving a whole group of repeats together leaves the pure error untouched and charges the entire move to lack of fit. Try this. Lift the three runs at X=6X = 6 by about 6 and watch the p-value fall from 1.0000 to 0.017, then lift only one of them and watch the test go quiet again (Figure 16).

9.7 Chapter summary

Diagnostics turn “the fit looks fine” into a defensible checklist. You can now derive the leverage values from the hat matrix, bound them between 0 and 1, show they sum to pp, and compute them by hand in simple regression; place a residual on four scales and explain why the raw residual misleads at high-leverage points; test a case for outlyingness with the studentized deleted residual and a Bonferroni cutoff; derive Cook’s distance from the leave-one-out coefficient change and read it, with DFFITS and DFBETAS, to find the points that actually move the fit; read the residual, quantile-quantile, and scale-location plots and run the Breusch-Pagan, Brown-Forsythe, Shapiro-Wilk, and Durbin-Watson tests; and derive and apply the lack-of-fit F test with pure error. The Galapagos fit failed on every count while the savings fit was steered by one country, and the Toluca line was vindicated at last.

Key results at a glance

ResultStatement or formulaValid when
Leverage value (Def 9.1)hii=xi(XX)1xih_{ii} = \mathbf{x}_i'(\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_iany least-squares fit
Leverage values: bounds, sum (Thm 9.2)0hii1,  ihii=p0 \le h_{ii} \le 1,\ \ \sum_i h_{ii} = pany least-squares fit
Leverage value, simple regression (Thm 9.3)hii=1/n+(XiXˉ)2/Sxxh_{ii} = 1/n + (X_i - \bar X)^2/S_{xx}one predictor
Residual variance (Thm 9.5)Var{ei}=σ2(1hii)\operatorname{Var}\{e_i\} = \sigma^2(1 - h_{ii})linear model, Var{Y}=σ2I\operatorname{Var}\{\mathbf{Y}\} = \sigma^2\mathbf{I}
Studentized deleted residual (Thm 9.6)ti=ei/(s(i)1hii)t(np1)t_i = e_i/(s_{(i)}\sqrt{1 - h_{ii}}) \sim t(n-p-1)case ii not an outlier
Leave-one-out update (Thm 9.10)bb(i)=(XX)1xiei/(1hii)\mathbf{b} - \mathbf{b}_{(i)} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_i e_i/(1 - h_{ii})hii<1h_{ii} < 1
Cook’s distance (Thm 9.11)Di=(ri2/p)hii/(1hii)D_i = (r_i^2/p)\,h_{ii}/(1 - h_{ii})any least-squares fit
DFFITS, DFBETAS (Def 9.9)tihii/(1hii)t_i\sqrt{h_{ii}/(1-h_{ii})}; (bkbk(i))/(s(i)(XX)kk1)(b_k - b_{k(i)})/(s_{(i)}\sqrt{(\mathbf{X}'\mathbf{X})^{-1}_{kk}})per-case influence
Durbin-WatsonD=(etet1)2/et22(1ρ^)D = \sum(e_t - e_{t-1})^2/\sum e_t^2 \approx 2(1-\hat\rho)time-ordered errors
Lack-of-fit F (Thm 9.13)F=MSLF/MSPEF(c2,nc)F^\ast = \mathrm{MSLF}/\mathrm{MSPE} \sim F(c-2,\, n-c)replicated XX, linear mean

Key terms. leverage value, high-leverage point, raw residual, semistudentized residual, standardized residual, studentized deleted residual, deleted residual, outlier, influential observation, Cook’s distance, DFFITS, DFBETAS, Breusch-Pagan test, Brown-Forsythe test, Shapiro-Wilk test, correlation test for normality, Durbin-Watson test, autocorrelation, pure error, lack of fit.

You should now be able to

Where this fits. This chapter is the CHECK stage of the workflow spine from The modeling workflow: after you FIT a model you must find out whether it can be trusted before you USE it to predict or decide. Diagnostics look backward to the matrix machinery of 7.3 The hat matrix, which built the hat matrix whose diagonal holds the leverage values and whose properties power every influence measure, and to the general linear test of 8.4 The general linear test, which is the lack-of-fit test in disguise. They look forward to Chapter 10, which takes the two broken fits of this chapter, the Galapagos funnel and its non-normal errors, and tries to repair them with transformations and weighted least squares, continuing the savings and gala case studies by name. The gala thread runs further still: Chapter 14 gives the honest resolution with Poisson regression, the right tool for a count response that can never go negative. One diagnostic here is only introduced, not fully used: the Durbin-Watson statistic becomes the whole subject of 15.2 Why time breaks ordinary least squares, where time-ordered errors are modeled and corrected rather than merely detected.

9.8 Frequently asked questions

Q1. Is a high-leverage point a bad thing? Not by itself. A high leverage value means an unusual predictor row, which is potential to influence the fit, not proof of harm. A high-leverage point that sits right on the trend set by the others barely moves the line. A high leverage value becomes a problem only when it pairs with a large residual, and that combination is what Cook’s distance measures.

Q2. Should I delete an influential point? Almost never automatically. An influential point is a signal to investigate, not garbage to discard. Check it for a data-entry error first. If it is real, the honest report shows the fit with and without it and explains the difference, as we did with Libya. Deleting real data to get a cleaner answer is how analyses go wrong.

Q3. What is the difference between an outlier and an influential point? An outlier has a large residual: the model fits it poorly. An influential point changes the fit when removed. They can occur separately. Zambia is close to an outlier at an ordinary leverage value, so it is fit poorly but does not run the model. Libya is influential without an extreme residual, because its leverage value is so high.

Q4. My residual plot looks fine but Shapiro-Wilk rejects normality. Who wins? Read the sample size. In large samples formal tests reject tiny, harmless departures from normality; in small samples they miss real ones (the Galapagos Breusch-Pagan p-value of 0.08 is an example of the miss). Use the plot to judge whether the departure is big enough to matter, and remember that the t and F tests are fairly forgiving of mild non-normality.

Q5. Why divide by s(i)s_{(i)} instead of ss for the outlier test? If case ii is truly an outlier, including it inflates the ordinary ss, which sits in the denominator of rir_i and hides the very case you are testing. Estimating the error standard deviation from the other cases, s(i)s_{(i)}, removes that self-masking and gives an exact tt distribution for tit_i.

Q6. Does Durbin-Watson apply to any regression? No. It tests correlation between consecutive errors, so it only means something when the rows have a real order, usually time. Running it on cross-sectional data like the savings countries is meaningless, because “consecutive” is just alphabetical order. Chapter 15 uses it where it belongs.

Q7. Cook’s distance has no p-value. How large is too large? Cook’s distance is a descriptive scale, not a formal test. Common guidance flags DiD_i near or above 1, but the most useful reading is relative: look for cases that stand far above the rest in an index plot. Libya at 0.27 is below 1 yet clearly separated from the pack, which is enough to warrant a closer look.

Q8. If a model fails a diagnostic, is it useless? No. A diagnostic tells you which assumption broke and therefore which conclusions to distrust. The Galapagos fit still describes a real association between geography and species richness; what it cannot support is a normal-theory prediction interval or a claim about a small island’s exact count. Knowing the limits of a model is what lets you use the part that holds.

9.9 Practice problems

  1. (A) In one sentence each, define the leverage value, outlier, and influential point, and say which two combine to make the third.

  2. (A) A leverage value comes out as hii=1.3h_{ii} = 1.3 in someone’s homework. Explain how you know it is wrong without seeing the data.

  3. (A) Why does a high-leverage point tend to have a small raw residual? Refer to the variance formula for eie_i.

  4. (A) State the 2p/n2p/n rule and compute the cutoff for the savings model. Which single fact about the hat matrix makes p/np/n the average leverage value?

  5. (A) Explain in plain words why the studentized deleted residual uses s(i)s_{(i)} rather than ss.

  6. (A) Give the residual-versus-fitted pattern you would expect for (i) nonconstant variance, (ii) a missing quadratic term, (iii) a single gross outlier.

  7. (A) A colleague deletes every point with ti>2|t_i| > 2 and reports the cleaned fit. Give two reasons this is bad practice.

  8. (A) The savings Durbin-Watson statistic is 1.93. Explain why this is not evidence that the savings errors are independent.

  9. (A) Interpret Libya’s DFBETAS of -1.02 for the ddpi coefficient for a reader who has never seen the statistic.

  10. (B) Using the symmetry and idempotence of H\mathbf{H}, prove that 0hii10 \le h_{ii} \le 1 and that ihii=p\sum_i h_{ii} = p (Theorem 9.2).

  11. (B) Derive the simple-regression leverage value formula hii=1/n+(XiXˉ)2/Sxxh_{ii} = 1/n + (X_i - \bar X)^2/S_{xx} from hii=xi(XX)1xih_{ii} = \mathbf{x}_i'(\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_i (Theorem 9.3).

  12. (B) Prove Var{e}=σ2(IH)\operatorname{Var}\{\mathbf{e}\} = \sigma^2(\mathbf{I} - \mathbf{H}), and deduce both Var{ei}\operatorname{Var}\{e_i\} and Cov{ei,ej}\operatorname{Cov}\{e_i, e_j\} (Theorem 9.5).

  13. (B) Verify the Sherman-Morrison formula for (Auu)1(\mathbf{A} - \mathbf{u}\mathbf{u}')^{-1} by multiplying it by Auu\mathbf{A} - \mathbf{u}\mathbf{u}' and simplifying to I\mathbf{I}.

  14. (B) Starting from the deletion formula bb(i)=(XX)1xiei/(1hii)\mathbf{b} - \mathbf{b}_{(i)} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{x}_i e_i/(1 - h_{ii}) (Theorem 9.10), derive the closed form Di=(ri2/p)hii/(1hii)D_i = (r_i^2/p)\,h_{ii}/(1 - h_{ii}) (Theorem 9.11).

  15. (B) Show that the deleted residual satisfies di=ei/(1hii)d_i = e_i/(1 - h_{ii}) and that Var{di}=σ2/(1hii)\operatorname{Var}\{d_i\} = \sigma^2/(1 - h_{ii}).

  16. (B) Derive the lack-of-fit decomposition SSE=SSPE+SSLF\mathrm{SSE} = \mathrm{SSPE} + \mathrm{SSLF} by expanding YjkY^jY_{jk} - \hat Y_j and showing the cross term vanishes (Theorem 9.13).

  17. (B) Explain why E{MSPE}=σ2E\{\mathrm{MSPE}\} = \sigma^2 whether or not the linear model is correct, but E{MSLF}>σ2E\{\mathrm{MSLF}\} > \sigma^2 when it is not. Identify the full and reduced models behind the lack-of-fit F test.

  18. (B) Show algebraically that DFFITSi=tihii/(1hii)\mathrm{DFFITS}_i = t_i\sqrt{h_{ii}/(1 - h_{ii})} starting from the change in the single fitted value Y^iY^i(i)\hat Y_i - \hat Y_{i(i)} and the deletion formula.

  19. (B) Using D=2(1ρ^)D = 2(1 - \hat\rho), find the value of ρ^\hat\rho implied by a Durbin-Watson statistic of 0.67, and explain what that autocorrelation means for the errors.

  20. (C) Fit the gala model. Report the leverage values above 2p/n2p/n, and confirm that the leverage values sum to pp.

  21. (C) For the savings model, compute all four residual scales for the United States and Japan, and comment on how the studentizing changes the picture for each.

  22. (C) Run the Bonferroni outlier test on the savings model. Report the most extreme studentized deleted residual, the cutoff, and your decision.

  23. (C) Compute Cook’s distance for the gala model, make an index plot, and confirm Isabela’s value with the (ri2/p)hii/(1hii)(r_i^2/p)\,h_{ii}/(1-h_{ii}) formula.

  24. (C) Refit the gala model without Isabela and report how the Elevation and Area coefficients change. Comment on whether the conclusions are fragile.

  25. (C) Make the residual-versus-fitted, normal quantile-quantile, and scale-location plots for the savings model, and argue from them that its assumptions are acceptable.

  26. (C) Run the Breusch-Pagan and Shapiro-Wilk tests on the gala model, then on a model that uses log(Species)\log(\text{Species}) as the response. Does the log help? (This previews Chapter 10.) Also run the Brown-Forsythe (modified Levene) test for constant variance on the raw gala model by splitting the cases at the median fitted value and running a two-sample tt test on the absolute deviations of the residuals from their group medians; does it agree with Breusch-Pagan?

  27. (C) Using toluca.csv, run the lack-of-fit F test by hand: build the group means, compute SSPE and SSLF, form FF^\ast, and match anova.

  28. (C) Simulate a regression with positively autocorrelated errors (seed 4210, ρ=0.8\rho = 0.8, n=40n = 40) and one with independent errors, and compare their Durbin-Watson statistics and p-values.

  29. (B) A point has hii=0.8h_{ii} = 0.8 and lies exactly on the fitted line (ei=0e_i = 0). Compute its Cook’s distance and DFFITS, and explain why a high-leverage point can have zero influence.

  30. (C) For the gala model, produce the influence plot (leverage value versus studentized deleted residual, bubble size Cook’s distance) and identify every island that is unusual on at least one axis.

9.10 Exam practice

These five questions are written in the style of the course exams. Each asks you to explain your reasoning in full sentences, not just to report a number. Where a question shows software output, it was produced on the course machine from the same data/ files you used all term; you read the numbers off the printout and interpret them. Work each one before opening the model answer.

EP 9.1. The savings model sr ~ pop15 + pop75 + dpi + ddpi (n=50n = 50, p=5p = 5) is fit, and the residual scales for Chile and Zambia, the two countries with the largest raw residuals, are printed below along with the Bonferroni outlier cutoff.

savings <- read.csv("data/savings.csv")
sfit <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = savings)
n <- nrow(savings); p <- length(coef(sfit))
i <- which(savings$country %in% c("Chile", "Zambia"))
data.frame(country = savings$country[i],
           hii = round(hatvalues(sfit)[i], 3),
           raw_e = round(residuals(sfit)[i], 3),
           standardized = round(rstandard(sfit)[i], 3),
           stud_deleted = round(rstudent(sfit)[i], 3))
qt(1 - 0.05 / (2 * n), n - p - 1)
 country   hii  raw_e standardized stud_deleted
   Chile 0.037 -8.242       -2.209       -2.313
  Zambia 0.064  9.751        2.651        2.854
[1] 3.5258

Explain why Zambia’s residual grows from 2.564 on the semistudentized scale (its raw residual 9.751 divided by s=3.803s = 3.803) to 2.854 once it is studentized and deleted, and say which feature of Zambia in the table drives that increase. Then state whether the outlier test flags Zambia as a genuine outlier, name the number you compare it against, and explain in one sentence what the Bonferroni correction protects you from.

EP 9.2. The same savings fit gives the influence summary below for its three most talked-about countries.

 country   hii rstudent cooksD dffits
   Japan 0.223    1.603  0.143  0.860
  Zambia 0.064    2.854  0.097  0.748
   Libya 0.531   -1.089  0.268 -1.160
   ( 2p/n = 0.200,   DFFITS cutoff 2*sqrt(p/n) = 0.632 )

A student writes: “Zambia has the largest studentized residual of any country, so Zambia is the single point most distorting the savings fit, and I would refit without it.” Evaluate this claim. State whether it is right, correct the reasoning using the numbers in the table, and name the country that actually most influences the fit, with the measure that settles it.

EP 9.3. For the Galapagos model Species ~ Area + Elevation + Nearest + Scruz + Adjacent (n=30n = 30, p=6p = 6), a grader runs the Breusch-Pagan and Shapiro-Wilk tests and inspects the fitted values.

gala <- read.csv("data/gala.csv")
gfit <- lm(Species ~ Area + Elevation + Nearest + Scruz + Adjacent, data = gala)
library(lmtest)
bptest(gfit)
shapiro.test(residuals(gfit))
range(fitted(gfit)); sum(fitted(gfit) < 0)
	studentized Breusch-Pagan test
data:  gfit
BP = 9.7959, df = 5, p-value = 0.08123

	Shapiro-Wilk normality test
data:  residuals(gfit)
W = 0.91351, p-value = 0.01826

[1] -36.38392 386.40356
[1] 5

A student concludes: “The Breusch-Pagan p-value is 0.081, which is above 0.05, so the constant-variance assumption holds and the Galapagos model is fine.” Evaluate this conclusion. Explain what the test does and does not show here, bring in the other evidence on the printout, and state the general rule about tests versus plots that this case illustrates.

EP 9.4. Continuing with the Galapagos fit, an analyst is worried that Isabela, the largest island, is running the whole model. The fit is refit with Isabela removed and the coefficients compared.

iso <- which(gala$island == "Isabela")
c(h = hatvalues(gfit)[iso], rstudent = rstudent(gfit)[iso],
  cooksD = cooks.distance(gfit)[iso])
gfit2 <- lm(Species ~ Area + Elevation + Nearest + Scruz + Adjacent,
            data = gala[-iso, ])
round(rbind(all = coef(gfit), no_Isabela = coef(gfit2)), 4)
c(R2_all = summary(gfit)$r.squared, R2_no_Isabela = summary(gfit2)$r.squared)
       h.16 rstudent.16   cooksD.16
     0.9685     -5.3337     68.0764
           (Intercept)    Area Elevation Nearest   Scruz Adjacent
all             7.0682 -0.0239    0.3195  0.0091 -0.2405  -0.0748
no_Isabela     22.5861  0.2957    0.1404 -0.2552 -0.0901  -0.0650
   R2_all R2_no_Isabela
    0.766         0.871

Interpret this output in context. Say what would change in the reported conclusions about Area and Elevation if Isabela were dropped, explain why one island can do this using its three diagnostic numbers, and state what the honest way to report a result this fragile is.

EP 9.5. The Toluca data record labor hours against lotsize for 25 production runs that used only 11 distinct lot sizes. The lack-of-fit F test compares the straight-line fit to a model with a free mean at each lot size.

toluca <- read.csv("data/toluca.csv")
reduced <- lm(hours ~ lotsize, data = toluca)
full    <- lm(hours ~ factor(lotsize), data = toluca)
anova(reduced, full)
Analysis of Variance Table

Model 1: hours ~ lotsize
Model 2: hours ~ factor(lotsize)
  Res.Df   RSS Df Sum of Sq      F Pr(>F)
1     23 54825
2     14 37581  9     17245 0.7138 0.6893

Interpret this output in context: identify which printed number is the pure-error sum of squares and which is the lack-of-fit sum of squares, state the conclusion of the test in plain language, and explain what the test is comparing to reach it. Then answer: what would change about your ability to run this test if every one of the 25 runs had used a different lot size?

Chapter game