Written and maintained by CASRAI Editorial Board
Last updated
To calculate a residual, subtract the value your model predicted from the value you actually observed: residual = observed y − predicted ŷ. You repeat that subtraction once for every observation. This guide shows the formula with every symbol explained, a six-point worked example with all the arithmetic, how to read the results, and one short snippet each for Excel, R and Python.
What is a residual?
A residual is the difference between an observed response and the response the fitted model predicts for the same observation. The NIST/SEMATECH e-Handbook of Statistical Methods describes the residuals of a fitted model as the differences between the responses observed at each combination of explanatory-variable values and the corresponding prediction computed using the regression function. A positive residual means the observation sits above the fitted line or curve; a negative residual means it sits below. A residual of zero means the model reproduced that observation exactly.
Residuals are not the same thing as the true random errors in the data. The true errors are never observed, because the true model is unknown. NIST puts it this way: if the model fit to the data were correct, the residuals would approximate the random errors. That is why residuals are the main raw material for checking whether a model is adequate.
Residual formula and what each symbol means
The NIST handbook (section 4.4, Model validation) writes the residual for observation i as:
ei = yi − f(xi; β̂)
- ei: the residual for the ith observation.
- yi: the observed response for the ith observation.
- xi: the explanatory (predictor) values for the ith observation.
- β̂: the estimated parameters of the model.
- f(xi; β̂): the predicted response, often written ŷi.
Because f can be any regression function, the same subtraction works for a straight line, a polynomial or a nonlinear curve. For simple linear regression it becomes ei = yi − (a + b·xi), where a is the intercept and b is the slope. The slope is covered in what a regression coefficient is.
How do you calculate a residual? A worked example
Take six observations: x = 1, 2, 3, 4, 5, 6 and y = 3, 5, 4, 7, 8, 9. The numbers are illustrative, chosen so the arithmetic is easy to follow.
Step 1: fit the line
- Means: x̄ = (1+2+3+4+5+6)/6 = 3.5 and ȳ = (3+5+4+7+8+9)/6 = 36/6 = 6.
- Sum of cross-products: Σ(x − x̄)(y − ȳ) = (−2.5)(−3) + (−1.5)(−1) + (−0.5)(−2) + (0.5)(1) + (1.5)(2) + (2.5)(3) = 7.5 + 1.5 + 1 + 0.5 + 3 + 7.5 = 21.
- Sum of squares of x: Σ(x − x̄)² = 6.25 + 2.25 + 0.25 + 0.25 + 2.25 + 6.25 = 17.5.
- Slope: b = 21 / 17.5 = 1.2. Intercept: a = ȳ − b·x̄ = 6 − 1.2 × 3.5 = 1.8.
- Fitted line: ŷ = 1.8 + 1.2x.
Step 2: predict, then subtract
| x | Observed y | Predicted ŷ = 1.8 + 1.2x | Residual e = y − ŷ | e² |
|---|---|---|---|---|
| 1 | 3 | 3.0 | 0.0 | 0.00 |
| 2 | 5 | 4.2 | +0.8 | 0.64 |
| 3 | 4 | 5.4 | −1.4 | 1.96 |
| 4 | 7 | 6.6 | +0.4 | 0.16 |
| 5 | 8 | 7.8 | +0.2 | 0.04 |
| 6 | 9 | 9.0 | 0.0 | 0.00 |
Step 3: check your arithmetic
- The residuals add to zero: 0 + 0.8 − 1.4 + 0.4 + 0.2 + 0 = 0. For a least-squares line that includes an intercept this is expected, so a non-zero sum signals a slip in the predictions.
- The sum of squared residuals is 0 + 0.64 + 1.96 + 0.16 + 0.04 + 0 = 2.80. This is the residual sum of squares, explained in the sum of squares formula guide.
- The residual standard deviation, using n − 2 = 4 degrees of freedom for a two-parameter line, is √(2.80 / 4) = √0.70 ≈ 0.84.
The largest residual is at x = 3: the observed value 4 is 1.4 below the predicted 5.4.
What do positive and negative residuals mean?
| What you see | Reading |
|---|---|
| Positive residual (e > 0) | The observation is above the model’s prediction (observed > predicted). |
| Negative residual (e < 0) | The observation is below the model’s prediction (observed < predicted). |
| Residual near 0 | The model predicts that observation closely. |
| Residuals scatter randomly around zero | Consistent with an adequate model, since the residuals then look like random errors (NIST). |
| Visible non-random structure in the residuals | NIST calls non-random structure in the residuals a clear sign that the model fits the data poorly. |
| High R² but structured residuals | Do not trust the fit. NIST states that a high R² does not guarantee that the model fits the data well. |
NIST calls graphical residual analysis the primary tool for most process modelling applications. Plot the residuals against the fitted values or the predictor, then read the plot using the residual plot and homoscedasticity guide. To check whether the residuals look normally distributed, see the Q-Q plot guide, and for residuals that follow each other in time see the Durbin-Watson test. A high R² on its own is discussed in how to interpret R-squared.
Assumptions and common mistakes
NIST’s design-of-experiments chapter states the usual assumption: the residuals are approximately independently distributed with a mean of 0 and some constant variance. Residual checks exist to test exactly that.
- Reversing the sign. The convention here is observed minus predicted. If you compute predicted minus observed, every sign flips, so state which one you used.
- Mixing models. Each residual must use the predicted value from the same fitted model. Subtracting predictions from a different model, or from a line fitted to different data, gives numbers that mean nothing.
- Calling residuals “errors” without qualification. Residuals approximate the random errors only if the model is correct; they are computed from estimated parameters.
- Judging fit from R² alone. Look at the residuals as well.
- Rounding too early. Round the slope and intercept before predicting and the residuals will no longer sum to zero. Keep full precision until the end.
How to report residuals
Residuals are usually summarised rather than listed. Report the model, the number of observations, the residual standard deviation (or the residual sum of squares), and what the residual plot showed. For the worked example: “A linear model fitted to six observations gave ŷ = 1.8 + 1.2x. The residuals summed to zero, the residual standard deviation was 0.84 (4 degrees of freedom), and a plot of residuals against fitted values showed no systematic pattern.” If you are following a journal or style manual, take its rules for formatting statistics from the manual itself.
How to calculate residuals in Excel, R or Python
Excel
With x in column A and y in column B, put the predicted value in C2 and the residual in D2, then fill both down:
C2: =FORECAST(A2, $B$2:$B$7, $A$2:$A$7)
D2: =B2-C2
R
The R documentation for lm describes residuals as “response minus fitted values”, and the fitted model object gives them through an accessor function:
x <- c(1, 2, 3, 4, 5, 6)
y <- c(3, 5, 4, 7, 8, 9)
fit <- lm(y ~ x)
residuals(fit)
# 0.0 0.8 -1.4 0.4 0.2 0.0 (up to floating-point rounding)
Python
In statsmodels, the fitted results object exposes the residuals of the model as resid:
import numpy as np
import statsmodels.api as sm
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([3, 5, 4, 7, 8, 9])
results = sm.OLS(y, sm.add_constant(x)).fit()
print(results.resid) # approx. [0. 0.8 -1.4 0.4 0.2 0.]
All three should reproduce the table above. Note that sm.add_constant is needed, otherwise statsmodels fits a line through the origin and the residuals differ.
Frequently asked questions
How do you calculate residuals?
Subtract each predicted value from the matching observed value: e = y − ŷ. Fit the model first, compute ŷ for every observation, then subtract. The worked example above shows all six steps.
What is the formula for a residual?
ei = yi − f(xi; β̂), where yi is the observed response and f(xi; β̂) is the response predicted by the fitted model (NIST/SEMATECH e-Handbook, section 4.4).
Should residuals add up to zero?
For an ordinary least-squares line fitted with an intercept, they do, apart from rounding, as the worked example shows. A model without an intercept does not guarantee it.
Is a residual the same as an error?
No. An error is the unobserved deviation from the true model; a residual is the deviation from the fitted model. Residuals approximate the errors when the model is correct.
Sources
- NIST/SEMATECH e-Handbook of Statistical Methods, section 4.4 “Model validation”: itl.nist.gov/div898/handbook/pmd/section4/pmd44.htm (checked 2026-10-29).
- NIST/SEMATECH e-Handbook of Statistical Methods, design of experiments chapter (assumptions on residuals): itl.nist.gov/div898/handbook/pri/section2/pri24.htm (checked 2026-10-29).
- R Documentation,
lm(stats package): stat.ethz.ch/R-manual/R-devel/library/stats/html/lm.html (checked 2026-10-29). - statsmodels documentation,
RegressionResults.resid: statsmodels.org (checked 2026-10-29).








