Introduction
When you fit a statistical model—be it a simple linear regression, a more complex mixed‑effects model, or even a generalized linear model—the residuals are the differences between the observed outcomes and the values predicted by the model. Plotting these residuals is one of the most straightforward yet powerful diagnostic tools you can use to assess whether the model’s assumptions hold up in practice. In the R programming language, visualising residuals is both simple and flexible, offering a suite of built‑in functions that let you inspect patterns, spot outliers, and verify homoscedasticity or normality. This article walks you through the entire workflow of how to plot residuals in R, from fitting a basic model to interpreting the resulting diagnostic graphics, so you can confidently evaluate and improve your statistical analyses.
Detailed Explanation
Residuals are defined as
[ e_i = y_i - \hat{y}_i ]
where (y_i) is the observed response and (\hat{y}_i) is the model‑generated prediction for the i‑th observation. Plotting residuals against fitted values, against each predictor, or against themselves helps you answer three core questions:
- Linearity & Additivity – Do residuals scatter randomly around zero, or do they display systematic curvature?
- Homoscedasticity – Does the spread of residuals stay constant across the range of fitted values, or does it fan out/in?
- Normality – Are the residuals approximately normally distributed, a prerequisite for many inferential tests?
By examining these plots, you can decide whether to transform the response variable, add interaction terms, or even reconsider the model family altogether. In R, the plot() method for objects of class "lm" automatically produces a 2×2 panel of residual diagnostics, while functions like qqnorm() and qqline() let you assess normality more formally. Understanding the underlying logic behind each plot empowers you to move beyond rote code and into genuine model evaluation Worth knowing..
Step‑by‑Step or Concept Breakdown
Below is a practical, step‑by‑step guide that you can follow for any linear model in R.
-
Fit the model
model <- lm(y ~ x1 + x2, data = mydata)Replace
y,x1,x2with your actual variable names That's the part that actually makes a difference.. -
Extract residuals and fitted values
resid_vec <- residuals(model) # raw residuals fit_vec <- fitted(model) # predicted means -
Create a basic residual‑vs‑fitted plot
plot(fit_vec, resid_vec, xlab = "Fitted values", ylab = "Residuals", main = "Residuals vs Fitted") abline(h = 0, lty = 2, col = "red") # reference line at zero -
Check normality with a Q‑Q plot
qqnorm(resid_vec) qqline(resid_vec, col = "red", lwd = 2) -
Inspect constant variance using a Scale‑Location plot
plot(model, which = 3) # automatically draws Scale‑Location -
Look for influential points
plot(model, which = 4) # Cook's distance plot
Each of these commands corresponds to one of the standard diagnostic panels that R produces when you call plot(model). By breaking the process into discrete steps, you can selectively focus on the aspect you wish to investigate without being overwhelmed by the full suite of default plots Worth keeping that in mind. Took long enough..
Real Examples
Let’s illustrate the workflow with a concrete dataset, the classic mtcars data frame, where we model miles per gallon (mpg) as a function of weight (wt) and horsepower (hp).
# Fit a linear model
fit <- lm(mpg ~ wt + hp, data = mtcars)
# 1. Residuals vs Fitted
plot(fitted(fit), residuals(fit),
xlab = "Fitted mpg",
ylab = "Residuals",
main = "Residuals vs Fitted (mtcars)")
abline(h = 0, lty = 2, col = "blue")
The resulting scatter should hug a horizontal band centred at zero. Any systematic curvature would hint that a quadratic term or a different functional form might be needed.
# 2. Normal Q‑Q plot
qqnorm(residuals(fit))
qqline(residuals(fit), col = "red")
If the points fall close to the red line, the residuals are approximately normal. Deviations at the tails could indicate heavy‑tailed errors, suggesting a strong regression might be warranted.
# 3. Scale‑Location plot (checks homoscedasticity)
plot(fit, which = 3)
A flat trend line in this plot confirms that variance is stable across the range of fitted values. A funnel shape would signal heteroscedasticity, prompting a possible variance‑stabilising transformation Worth knowing..
These examples demonstrate how each diagnostic plot provides a distinct lens on model fit, and together they form a comprehensive assessment toolkit And that's really what it comes down to..
Scientific or Theoretical Perspective
From a statistical theory standpoint, the Gauss‑Markov theorem asserts that under the classical linear model assumptions—linearity, independence, homoscedasticity, and normality of errors—the ordinary least squares (OLS) estimator is the best linear unbiased estimator (BLUE). Residual analysis is essentially a practical embodiment of these assumptions Turns out it matters..
- Linearity is examined via residual‑vs‑fitted plots; systematic patterns indicate model misspecification.
- Homoscedasticity is probed by Scale‑Location and Breusch‑Pagan tests (the latter can be implemented with
bptest()from thelmtestpackage). - Normality is crucial for confidence intervals and hypothesis tests; the Q‑Q plot visualises departures from the normal distribution.
Mathematically, if the error term ( \varepsilon ) follows ( \varepsilon \sim N(0, \sigma^2) ), then the vector of residuals ( \mathbf{e} ) follows a multivariate normal distribution with
Completing the statement, the residual vector e is distributed as
[ \mathbf{e};\sim; N!\bigl(\mathbf{0},;\sigma^{2}\mathbf{I}_{n}\bigr), ]
so each residual has the same variance σ² and they are mutually uncorrelated (though they are not independent of the design matrix X). That said, this multivariate normal form underpins the finite‑sample properties that the Gauss‑Markov theorem relies on: the OLS coefficients are linear unbiased estimators, and their covariance matrix is σ² (XᵀX)⁻¹. When any of the classical assumptions break down, the distribution of e deviates from this ideal, and the diagnostic plots become the primary tools for detecting those deviations Easy to understand, harder to ignore..
Extending the diagnostic toolbox
Beyond the three basic plots already illustrated, several complementary diagnostics are routinely employed:
| Diagnostic | What it assesses | Typical R command |
|---|---|---|
| use / hat values | Influence of individual observations on the fitted values | hatvalues <- hatvalues(fit) |
| Cook’s distance | Overall influence of each case on the coefficient estimates | cooks.Which means distance(fit) |
| DFBETAS | Change in each coefficient when a case is omitted | dfbetas(fit) |
| Residual‑by‑apply plot | Joint view of apply and residual magnitude, highlighting high‑put to work points with large residuals | plot(fit, which = 5) |
| Non‑linearity tests (e. Also, g. , component‑plus‑residual) | Detect misspecification that a simple residual‑vs‑fitted plot may miss | crplot(fit) (requires car package) |
| Breusch‑Pagan / White test | Formal test of heteroscedasticity | bptest(fit) (from lmtest) |
| Shapiro‑Wilk test | Formal test of normality for residuals | `shapiro. |
These tools complement the visual checks. To give you an idea, a residual‑vs‑fitted plot may appear innocuous, yet a high‑take advantage of point with an outlying residual can exert disproportionate pull on the coefficient estimates. Cook’s distance quantifies that pull, while DFBETAS tells you which coefficients are most affected. Together they provide a more nuanced picture than any single plot Turns out it matters..
Practical workflow in R
A concise workflow that incorporates both graphical and formal diagnostics might look like this:
# Fit the model
fit <- lm(mpg ~ wt + hp, data = mtcars)
# 1. Classic residual diagnostics
par(mfrow = c(2, 2)) # 2×2 layout for quick visual scan
plot(fit, which = 1:4) # residuals vs fitted, Q‑Q, Scale‑Location, QQ‑residuals
# 2. take advantage of and influence
use <- hatvalues(fit)
plot(use, residuals(fit),
xlab = "take advantage of", ylab = "Residual",
main = "Residuals vs make use of (mtcars)")
abline(h = 0, lty = 2, col = "gray")
# 3. Formal tests
library(lmtest)
bptest(fit) # heteroscedasticity
shapiro.test(residuals(fit)) # normality
# 4. Influence measures
infl <- influence.measures(fit)
plot(infl$diagnostics) # combined take advantage of‑residual plot
print(cooks.distance(fit)) # Cook's distance values
The sequence above moves from quick visual inspection to quantitative assessment, ensuring that no single aspect is overlooked Less friction, more output..
Interpreting the results
- Systematic curvature in the residuals‑vs‑fitted plot signals a missed non‑linear component; adding polynomial terms or transforming the response often resolves the issue.
- Funnel shapes in the Scale‑Location or heteroscedasticity test indicate non‑constant variance; a variance‑stabilising log or Box‑Cox transformation, or a weighted least‑squares approach, may be appropriate.
- Heavy tails in the Q‑Q plot or a significant Shapiro‑Wilk p‑value suggest non‑normal errors; strong regression (e.g.,
rlmfrom theMASSpackage) or bootstrapping confidence intervals can mitigate the problem. - High make use of combined with large residuals yields elevated Cook’s distance, indicating that the observation is influential. In such cases, re‑fitting the model with the observation omitted can reveal whether the conclusions are driven by that point.
Concluding remarks
Residual analysis is not a peripheral step but an integral component of the linear modelling pipeline. That said, by systematically examining the residuals for patterns, variance heterogeneity, and non‑normality—and by supplementing visual inspection with apply, influence, and formal hypothesis tests—analysts can confidently ascertain whether the classical OLS assumptions hold or require adjustment. When the diagnostics reveal departures, the analyst has a clear set of remedies: model re‑specification, transformation, strong techniques, or, in extreme cases, a shift to a different modelling framework altogether. In this way, the suite of residual diagnostics serves as both a diagnostic microscope and a decision‑making compass, ensuring that the final inference drawn from the mtcars example—or any applied dataset—rests on a statistically sound foundation Most people skip this — try not to..