10  Linear Models in Practice

10.1 Prerequisites

Answer the following questions to see if you can bypass this chapter. You can find the answers at the end of the chapter in Section 10.19.

  1. Name three assumptions of the classical linear regression model.
  2. What does \(R^2\) represent, and what does an \(R^2\) of 0.7 tell you about a fitted model?
  3. What is the primary purpose of residual diagnostic plots in linear regression?

10.2 Learning objectives

By the end of this chapter you should be able to:

  • Fit a linear model in R with lm() and interpret every column of summary().
  • Diagnose a fitted model using the four default plots: residuals, Q-Q, scale-location, Cook’s distance.
  • Build a model matrix X for a mix of continuous and categorical predictors, including interactions and polynomial terms.
  • Compute the coefficient vector by hand using the QR decomposition and reproduce the output of lm() (point estimates, standard errors, \(R^2\), residual degrees of freedom) from scratch.
  • Recognize and report violations of model assumptions: heteroscedasticity, non-linearity, non-normality, influential observations, multicollinearity.
  • Compute robust (sandwich) standard errors with sandwich::vcovHC() when homoscedasticity fails.
  • Work with broom::tidy(), glance(), and augment() for reproducible reporting of linear models.

10.3 Orientation

lm() is the workhorse of applied statistics. It is also the first real test of everything we have built so far: matrix algebra, QR decomposition, vectorized R, and the grammar of data frames. In this chapter we treat lm() as a white box. We shall not invoke it until we have reimplemented most of what it does.

The linear model assumes

\[ \mathbf{y} = \mathbf{X}\boldsymbol\beta + \boldsymbol\varepsilon, \quad \boldsymbol\varepsilon \sim N(0, \sigma^2 I). \]

Recall from chapter 5 that the least-squares estimator is \(\hat\beta = (X^T X)^{-1} X^T y\). Chapter 6 explained why lm() uses QR in place of the normal equations. This chapter adds the inferential machinery (standard errors, t-statistics, p-values, \(R^2\)), the diagnostics, and the working craft of fitting and interpreting linear models on real data.

We begin with the decisions that belong to the statistician and then dissect the lm() object and its model matrix. We next reimplement lm() in a few lines, and then turn to diagnostics, sandwich standard errors, multicollinearity, and model comparison. Lastly, we shall consider reproducible reporting with broom, collaboration with an LLM, and a worked example.

10.4 The statistician’s contribution

lm() fits a model. The interesting questions are which model, on which variables, on which transformations, and with which subset. A further question is how to interpret the result. These are the statistician’s contribution, and they are not automatable.

Which variables to include. The naive answer (‘whichever predicts’) is wrong for inference. In a randomized trial, the adjustment covariates go in because the analysis plan pre-specified them (typically the randomization stratification factors and strong prognostic variables). Adjusting for them gives the pre-registered estimand. Their contribution to prediction is not the reason for including them. In an observational study, the covariate set is the set that blocks non-causal paths between exposure and outcome. That set is determined by a causal diagram drawn before the model is fit. Stepwise selection and similar algorithmic procedures are problematic. They produce standard errors that understate uncertainty, and coefficients that do not mean what they appear to mean.

Which scale. A coefficient on age is interpretable if age is in years. It is less so if age is centered at 50 but unscaled, and less still if it is transformed to a z-score. The choice depends on the audience. For a clinical paper, a one-year increment is natural; for a methods paper, a z-score may read more cleanly. The transformation on the outcome (log, square root, Box-Cox) is a more consequential decision. In the case of the log, it changes the estimand from ‘additive effect on \(y\)’ to ‘multiplicative effect on \(y\)’.

Whether the model’s claims are defensible. A linear model’s p-values and confidence intervals are valid under its assumptions. When the assumptions fail, the numbers are still computed and printed. They are simply no longer meaningful. Detecting the failure is the purpose of diagnostics. Choosing a remedy (a transformation, robust standard errors, a different model entirely, or a reported caveat) is the statistician’s judgment.

Statistical vs. clinical significance. Consider a \(p < 0.05\) treatment effect of 0.2 mmHg on blood pressure. It is not clinically meaningful even if it is statistically certain. Conversely, a \(p = 0.07\) effect of 8 mmHg may be clinically very meaningful but imprecisely estimated. The statistician reports both. Claiming importance because of \(p\) alone, or dismissing importance for the same reason, is not defensible.

An LLM will fit any model we describe without telling us that it is the wrong model for our question. These decisions determine whether the resulting analysis is honest or misleading.

10.5 Anatomy of lm()

R’s formula interface abstracts away the model matrix. The expression lm(mpg ~ wt + hp, data = mtcars) specifies that the outcome is mpg and the predictors are wt and hp. It also specifies that an intercept is included by default and that the data are in mtcars. Internally, R proceeds as follows:

  1. Parses the formula into a list of terms.
  2. Builds the model matrix \(X\) via model.matrix(), converting factors to indicator columns and applying any transformations mentioned in the formula.
  3. Computes the QR decomposition of \(X\).
  4. Solves for \(\hat\beta\) via qr.coef(qrX, y).
  5. Computes residuals, fitted values, residual variance \(\hat\sigma^2 = \|y - X\hat\beta\|^2 / (n - p)\), and the covariance matrix \(\widehat{\mathrm{Var}}(\hat\beta) = \hat\sigma^2 (X^T X)^{-1}\). The last is computed by back-substitution on \(R\); R does not form \((X^T X)^{-1}\) explicitly.
  6. Returns all of this as an lm object.
fit <- lm(mpg ~ wt + hp, data = mtcars)
class(fit)                  # "lm"
names(fit)                  # what's in the object
#>  [1] "coefficients"  "residuals"     "effects"       "rank"
#>  [5] "fitted.values" "assign"        "qr"            "df.residual"
#>  [9] "xlevels"       "call"          "terms"         "model"

summary(fit)

The summary() output has four parts:

  1. Residuals. Min, 1Q, median, 3Q, max. These serve as a quick sanity check: the median should be near zero and the quantiles approximately symmetric.
  2. Coefficients. Estimate, standard error, \(t\)-value, and \(\Pr(>|t|)\), for each coefficient.
  3. Residual standard error. \(\hat\sigma\) and residual degrees of freedom \(n - p\).
  4. F-statistic. Tests whether the model as a whole explains variation beyond the intercept.

The coefficients table is the most important part. Its columns are as follows.

  • Estimate. \(\hat\beta_j\), the fitted coefficient.
  • Std. Error. \(\widehat{\mathrm{SE}}(\hat\beta_j) = \sqrt{\hat\sigma^2 [(X^T X)^{-1}]_{jj}}\).
  • t value. Estimate divided by Std. Error.
  • Pr(>|t|). Two-sided p-value from a \(t\)-distribution with \(n - p\) degrees of freedom.

We extract specific components with accessor functions:

coef(fit)              # coefficient vector
confint(fit)           # 95% CIs for coefficients
fitted(fit)            # fitted values
resid(fit)             # residuals
vcov(fit)              # coefficient covariance matrix
sigma(fit)             # residual standard error
AIC(fit); BIC(fit)     # information criteria

We prefer these to fit$coefficients, since accessor functions are stable across lm, glm, and many other model classes.

10.6 Building the model matrix

model.matrix() converts a formula into the design matrix \(X\) that lm() actually works with. An understanding of this step makes interactions, contrasts, and coefficient interpretation far clearer.

X <- model.matrix(mpg ~ wt + hp, data = mtcars)
head(X)
#>                   (Intercept)    wt  hp
#> Mazda RX4                   1 2.620 110
#> Mazda RX4 Wag               1 2.875 110
#> Datsun 710                  1 2.320  93

The reader will note the intercept column of 1s, which is included by default. To suppress it, we write lm(mpg ~ 0 + wt + hp) or lm(mpg ~ wt + hp - 1).

10.6.1 Categorical predictors and contrasts

When a factor with \(k\) levels appears in a formula, R creates \(k - 1\) indicator columns for it:

fit <- lm(mpg ~ wt + factor(cyl), data = mtcars)
head(model.matrix(fit))
#>                   (Intercept)    wt factor(cyl)6 factor(cyl)8
#> Mazda RX4                   1 2.620            1            0
#> Datsun 710                  1 2.320            0            0

The factor’s first level (‘4’ here) is the reference, and the other two levels have their own indicator columns. The intercept represents the mean of the reference level (at \(wt = 0\)). The factor coefficients represent differences from the reference.

To change the reference level:

mtcars$cyl_f <- relevel(factor(mtcars$cyl), ref = "8")

Alternatively, we may set the factor levels directly: factor(mtcars$cyl, levels = c("8", "4", "6")).

Some contrast codings give coefficients with a different interpretation. Sum-to-zero coding, for example, gives coefficients interpretable as deviations from the grand mean. For such a non-default coding, we set contrasts() on the factor:

contrasts(mtcars$cyl_f) <- contr.sum

Default treatment contrasts are clinically interpretable and almost always the right choice. Sum contrasts are useful in some ANOVA presentations. Unfortunately, they confuse readers who expect reference-level interpretation.

10.6.2 Interactions

Interactions are specified with : (interaction only) or * (both main effects and interaction):

lm(mpg ~ wt * cyl, data = mtcars)    # wt, cyl, wt:cyl
lm(mpg ~ wt + cyl + wt:cyl, data = mtcars)   # same thing

Consider an interaction between continuous wt and factor cyl. The model matrix gets an extra column for every non-reference level of cyl: wt:cyl6, wt:cyl8. The interpretation is then as follows. The slope on wt for the reference level is the main-effect coefficient. The slope for a non-reference level is the main effect plus the interaction coefficient.

10.6.3 Polynomial and transformed terms

I() insulates an expression from formula parsing:

lm(mpg ~ wt + I(wt^2), data = mtcars)
lm(mpg ~ log(hp) + wt, data = mtcars)

poly() creates orthogonal polynomial terms. These are better-conditioned than raw powers, although their coefficients are not on the scale of the original predictor and are therefore harder to interpret:

lm(mpg ~ poly(wt, 2), data = mtcars)

Orthogonal polynomials have the property that the coefficient on the linear term is not affected by whether we include the quadratic term.

Question. You fit lm(outcome ~ treatment, data = trial) where treatment is a factor with levels ‘placebo’, ‘low-dose’, and ‘high-dose’. The Intercept coefficient in the output estimates 18.2, and the treatmentlow-dose coefficient is -4.1. What are the mean outcomes in the placebo and low-dose groups?

Answer.

The default reference level is alphabetical, here ‘high-dose’. The intercept therefore estimates the mean outcome in the high-dose group (18.2). The low-dose coefficient gives the difference between low-dose and high-dose (-4.1), so the low-dose mean is 14.1. In a trial you usually want placebo as the reference. To obtain it, use factor(treatment, levels = c("placebo", "low-dose", "high-dose")) or relevel(treatment, ref = "placebo"). This is a classic source of misreporting. The intercept does not mean ‘baseline’ in any natural sense; it means ‘the reference level that R picked’. Always set the reference level deliberately.

10.7 Reimplementing lm() in 10 lines

set.seed(1)
n <- 100; p <- 3
X <- cbind(1, matrix(rnorm(n * p), n, p))
y <- X %*% c(2, 1, -0.5, 0.3) + rnorm(n)

qrX <- qr(X)
beta <- qr.coef(qrX, y)
resids <- qr.resid(qrX, y)
rss <- sum(resids^2)
df_resid <- n - ncol(X)
sigma2 <- rss / df_resid

# coefficient covariance: sigma^2 * (X'X)^-1 = sigma^2 * solve(R) %*% t(solve(R))
R <- qr.R(qrX)
Rinv <- backsolve(R, diag(ncol(R)))
vcov_beta <- sigma2 * tcrossprod(Rinv)
se_beta <- sqrt(diag(vcov_beta))

# R-squared
tss <- sum((y - mean(y))^2)
r2 <- 1 - rss / tss

# compare to lm()
lm_fit <- lm(y ~ X - 1)
all.equal(as.vector(beta), unname(coef(lm_fit)))
#> [1] TRUE
all.equal(as.vector(se_beta),
          unname(summary(lm_fit)$coefficients[, "Std. Error"]))
#> [1] TRUE

Everything lm() reports (coefficients, SEs, \(R^2\), residual SD, df) comes from the QR decomposition, the residuals, and \((X^T X)^{-1}\). There is no prior and no hidden regularization.

10.8 Diagnostics

plot(fit) produces four diagnostic plots. Together they cover the main assumption checks.

Plot 1: Residuals vs. Fitted. This plot checks linearity and homoscedasticity. We look for no systematic trend (the linear relationship holds) and for consistent spread (the variance is constant). A curve indicates non-linearity; a funnel indicates heteroscedasticity.

Plot 2: Q-Q plot of standardized residuals. This plot checks approximate normality, and the points should lie near the 45° line. Heavy tails show up as an ‘S’ shape. Skewness shows up as a curved deviation from the line.

Plot 3: Scale-Location. This plot shows the square root of the absolute standardized residual against the fitted values. It is another view of homoscedasticity, more sensitive than plot 1 to trends in spread. A monotonic increasing trend means that the variance increases with the mean. This is a classical pattern for count or proportion data.

Plot 4: Residuals vs. Leverage. This plot identifies influential observations. Cook’s distance contours (dashed lines) mark the regions of concern. Points far to the right (high-leverage: unusual covariate values) with large residuals warrant scrutiny.

par(mfrow = c(2, 2))
plot(fit)
par(mfrow = c(1, 1))

None of these plots checks independence. For time-series or clustered data, we plot the residuals against time or against the cluster ID and look for structure. Independence violations usually require a different model (mixed-effects, GEE, or an explicit correlation structure).

10.8.1 Leverage, influence, Cook’s distance

Leverage (\(h_i\), the \(i\)th diagonal of the hat matrix \(H = X(X^T X)^{-1} X^T\)) measures how unusual observation \(i\)’s predictor values are. High-leverage observations pull the fit toward themselves.

h <- hatvalues(fit)

Cook’s distance combines leverage and residual size into a single measure. It records how much the regression would change if observation \(i\) were removed. In symbols,

\[ D_i = \frac{e_i^2}{p \hat\sigma^2} \cdot \frac{h_i}{(1 - h_i)^2}. \]

cd <- cooks.distance(fit)

As a rule of thumb, \(D_i > 4/n\) warrants investigation and \(D_i > 1\) is a red flag. We should not blindly delete influential observations. Instead, we check whether the observation is correctly coded and whether it belongs to a class of points the model is not designed for. We also check whether re-fitting without it changes the substantive conclusions.

DFBETA (or DFBETAS, its studentized version) measures the change in each coefficient when each observation is removed:

db <- dfbetas(fit)

It is useful when we want to know which coefficients an influential point is moving.

Question. The residuals-vs-fitted plot shows a clear fan shape: residuals small for small fitted values and large for large fitted values. What does this violate, and what are your remedies?

Answer.

The assumption violated is constant error variance (homoscedasticity). The point estimates are still unbiased, but the standard errors and their derived quantities (CIs, p-values) are wrong. There are three remedies. The first, (a), is a variance-stabilizing transformation of the outcome (log for variance proportional to mean^2, square-root for variance proportional to mean). The second, (b), is weighted least squares with weights inversely proportional to the estimated variance. The third, (c), is robust (sandwich / HC) standard errors, which stay consistent under heteroscedasticity without re-fitting the model; sandwich::vcovHC(fit, type = "HC3") is the default recommendation. The choice depends on whether you also care about efficient estimation (use WLS or transform) or simply want honest SEs with the existing fit (use sandwich).

10.9 Robust (sandwich) standard errors

Suppose homoscedasticity is implausible but the model is otherwise defensible. The sandwich estimator then gives heteroscedasticity-consistent SEs without changing the point estimates:

library(sandwich)
library(lmtest)

fit <- lm(mpg ~ wt + hp, data = mtcars)
coeftest(fit, vcov. = vcovHC(fit, type = "HC3"))

HC3 is the default recommendation for moderate sample sizes. HC0 through HC2 are alternatives that differ in how they weight by leverage. We should not use robust SEs as a substitute for diagnosing why homoscedasticity fails. They correct the SEs, but they do not address a mis-specified mean function.

10.10 Multicollinearity

Highly correlated predictors produce unstable coefficient estimates. The variance inflation factor (VIF) quantifies the inflation:

\[ \mathrm{VIF}_j = \frac{1}{1 - R_j^2} \]

where \(R_j^2\) is from regressing \(x_j\) on all other predictors. A VIF above 5 is a warning, and a VIF above 10 is usually a problem.

library(car)
vif(fit)

The remedies are as follows.

  • Drop one of a near-redundant pair of predictors.
  • Combine them (mean, principal component).
  • Use ridge regression, which introduces a small bias in exchange for large variance reduction.

Multicollinearity does not bias the coefficient estimates; it only makes them imprecise. If the goal is prediction, some degree of multicollinearity is tolerable. If the goal is inference on individual coefficients, it is not.

10.11 Model comparison

Nested models can be compared with an F-test via anova():

fit_small <- lm(mpg ~ wt, data = mtcars)
fit_large <- lm(mpg ~ wt + hp + disp, data = mtcars)
anova(fit_small, fit_large)

Non-nested models can be compared by AIC or BIC:

AIC(fit_small, fit_large)
BIC(fit_small, fit_large)

Lower is better for both. AIC estimates expected prediction error, whereas BIC approximates the model posterior probability under a unit-information prior. BIC penalizes complexity more heavily than AIC at large \(n\), so BIC tends to prefer smaller models.

Neither criterion is a substitute for cross-validation when the goal is genuine prediction. For inference, neither is a substitute for careful thought, grounded in subject-matter knowledge, about which predictors belong in the model.

10.12 Reproducible reporting with broom

broom converts model objects to tidy data frames, which are suitable for further manipulation and for rendering in reports:

library(broom)

tidy(fit)        # per-coefficient: estimate, SE, t, p
glance(fit)      # per-model: R2, adj R2, sigma, AIC, BIC, df, ...
augment(fit)     # per-observation: fitted, residuals, hat values, cooks

We pair these with gt or kableExtra for publication-quality tables. We pair them with ggplot2 for coefficient forest plots, fitted-value overlays, and diagnostic plots customized beyond plot(fit).

Question. You want to produce a coefficient plot with 95% CIs for a linear model fit to 12 predictors. Which broom function gives you the right data shape with minimal further work?

Answer.

broom::tidy(fit, conf.int = TRUE) returns one row per coefficient. Its columns hold the estimate, standard error, statistic, p-value, and (with conf.int = TRUE) the lower and upper CI bounds. That is exactly the shape ggplot2::geom_pointrange() expects. glance() gives per-model summaries, and augment() gives per-observation data. For the coefficient plot, tidy() with conf.int = TRUE is the one-line answer.

10.13 Collaborating with an LLM on linear models

LLMs can produce correct linear-model code almost instantly. They are less reliable with the judgment that surrounds the code: what to include, how to interpret, and when to worry about assumptions.

Prompt 1: drafting a model specification. We describe the study and the question, and ask: ‘write a lm() call that addresses this question, explain why each predictor is included, and flag any assumptions that may be questionable.’

What to watch for. LLMs tend to assemble kitchen-sink models (lots of predictors) in place of models matched to the question. We check whether each predictor is there for causal blocking, for adjustment, for prediction, or merely because it was in the dataset. If the justification is ‘it was in the data’, we push back.

Verification. We compare the LLM’s specification to what the study’s analysis plan (or a similar published paper) would have pre-specified. Each discrepancy is a question to resolve. By itself, a discrepancy does not show that the LLM is wrong.

Prompt 2: interpreting coefficients. We paste the summary(fit) output and ask: ‘interpret each coefficient in the context of the scientific question, being explicit about the reference level for categorical predictors.’

What to watch for. LLMs occasionally paraphrase column names as if they were interpretations (‘the estimate is 0.42, the standard error is 0.12’). A real interpretation includes the direction of the effect and its magnitude on the outcome scale. It also includes the scientific meaning of a one-unit change in the predictor. If the interpretation does not read like something a clinical collaborator would write, we rewrite it.

Verification. We double-check any mention of the intercept and any categorical coefficient. Reference-level confusion is the single most common LLM error in regression interpretation.

Prompt 3: diagnosing a failed fit. We describe the symptoms (weird coefficients, huge SEs, non-convergence) and ask: ‘what is likely going wrong, and what diagnostics would clarify?’

What to watch for. LLMs are reasonable at proposing diagnostics (VIF, residual plots, Cook’s distance) but less reliable at prioritizing them. Under an analysis deadline, we start with the cheapest diagnostics (check for linear combinations among predictors, plot residuals). The expensive ones (Cook’s distance on every observation, bootstrap SEs) come afterward.

Verification. We run the suggested diagnostics and compare the results across them. If all point the same way, we may trust the conclusion. If they disagree, the problem is more subtle than any one diagnostic is detecting.

10.14 Worked example: Arthritis trial

data("Arthritis", package = "vcd")
Arthritis$Improved_n <- as.numeric(Arthritis$Improved)   # 1/2/3
# technically ordinal; a proportional-odds model is the proper choice

fit <- lm(Improved_n ~ Treatment + Age + Sex, data = Arthritis)
summary(fit)
broom::tidy(fit, conf.int = TRUE)

# diagnostics
par(mfrow = c(2, 2))
plot(fit)
par(mfrow = c(1, 1))

The Treatment effect (Treated vs. Placebo, adjusting for age and sex) appears in the coefficient table. The interpretation needs to be cautious. The outcome is ordinal, and the 1-2-3 coding imposes equal spacing that may not be clinically valid. A proportional-odds (ordinal logistic) model is a better fit. This example demonstrates the workflow only, since linear regression is not the right model for an ordinal outcome.

10.15 Principle in use

Three habits define defensible linear-model practice:

  1. Pre-specify the model. Predictors, transformations, and reference levels should be chosen before looking at the data. At the least, they should be chosen before looking at any outcome-by-predictor relationship. Ex post data-driven model choice destroys the nominal error rates.
  2. Inspect diagnostics before interpreting. A pathological residual pattern means that the p-values on the table are not what they appear to be. We fix or acknowledge the violation before writing the results paragraph.
  3. Interpret on the scale of the outcome and the audience. A clinical reader wants ‘mmHg per 10-year increase in age’, while a methods reader may want a standardized coefficient. We write for the reader who will actually use the result.

10.16 Exercises

  1. Using palmerpenguins::penguins, fit body_mass_g ~ flipper_length_mm + species. Extract the coefficient vector with coef() and reproduce it with qr.coef(qr(X), y) where X is the model matrix.
  2. For the same fit, reproduce the residual standard error, the \(R^2\), and the standard errors of the coefficients without calling summary(). Verify every number agrees with summary(fit).
  3. Find the observation with the largest Cook’s distance and re-fit the model without it. Report the change in coefficients and the change in \(\hat\sigma\).
  4. Fit a model with interactions between species and flipper length. Use emmeans::emmeans() or a hand-computed contrast to estimate the species-specific slope on flipper length. Compare to a simple pooled slope.
  5. Apply sandwich::vcovHC() to a linear model where the residuals-vs-fitted plot shows heteroscedasticity. Compare the robust and classical standard errors. How much do the p-values change?

10.17 Further reading

10.18 Practice test

The following multiple-choice questions exercise the chapter’s content. Attempt each question before expanding the answer.

10.18.1 Question 1

Which of the following is an assumption of linear regression models?

    1. The predictor variables must follow a normal distribution
    1. The response variable must be categorical
    1. The error terms have constant variance (homoscedasticity)
    1. The relationship between predictors must be linear

C. Homoscedasticity is one of the Gauss-Markov assumptions. The predictors need not be normal, and the response need not be categorical. The linearity assumption concerns the mean function, not the relationship among the predictors.

10.18.2 Question 2

In a linear regression model, what does \(R^2\) represent?

    1. The probability that the null hypothesis is true
    1. The proportion of variance in the response variable explained by the predictors
    1. The regression coefficient for the main predictor variable
    1. The expected change in the response for a one-unit change in a predictor

B. \(R^2\) is the fraction of the total response variance accounted for by the fitted model.

10.18.3 Question 3

What is the primary purpose of residual diagnostic plots in linear regression?

    1. To determine the statistical significance of coefficient estimates
    1. To calculate confidence intervals for predictions
    1. To check whether model assumptions are satisfied
    1. To identify which predictors should be included in the model

C. Residual plots check linearity, homoscedasticity, normality, and independence.

10.18.4 Question 4

A fitted linear model has one predictor with \(\mathrm{VIF} = 12\) and another with \(\mathrm{VIF} = 3\). What does this indicate?

    1. The first predictor is a better predictor than the second.
    1. The first predictor is highly collinear with other predictors; its coefficient will have inflated standard error.
    1. The second predictor should be removed.
    1. The model is over-fit.

B. A VIF above 10 is usually a problem. The first predictor’s coefficient is imprecisely estimated because of its collinearity with other predictors. Consider dropping it, combining it with a correlated predictor, or using ridge regression.

10.18.5 Question 5

Your residual plot shows a clear curve. Which diagnosis is most appropriate?

    1. Heteroscedasticity; use robust standard errors.
    1. Non-linearity in the mean function; add a polynomial or transformation, or consider a non-linear model.
    1. Non-normal residuals; transform the outcome.
    1. Influential observations; remove outliers.

B. A curved residual pattern indicates that the linear mean function is misspecified. The remedies are a polynomial term, a log or Box-Cox transformation of the outcome, or a generalized additive model (mgcv::gam). Robust SEs do not fix a wrong mean function.

10.19 Prerequisites answers

  1. The assumptions are linearity of the mean function, independence of observations, and constant error variance (homoscedasticity). For inference, approximate normality of the errors is also assumed. Point estimates are unbiased under linearity and independence alone. CIs and p-values additionally require homoscedasticity and normality (or a large enough sample that the CLT rescues normality).
  2. \(R^2\) is the proportion of variance in the response explained by the predictors. \(R^2 = 0.7\) means that the model accounts for 70% of the total variation in the response. It does not measure goodness of fit in any absolute sense. A high \(R^2\) does not rule out bias, non-linearity, or other misspecifications. Likewise, a low \(R^2\) does not rule out a correctly specified model in a high-noise setting.
  3. Residual diagnostics check whether the model’s assumptions (linearity, homoscedasticity, normality, independence) are reasonable. Systematic patterns in the residuals signal model misspecification. A curve signals non-linearity, and a funnel signals heteroscedasticity. Heavy tails on the Q-Q plot signal non-normality. Autocorrelation in the residuals (not shown by plot(fit)) signals a violation of independence.