Residual Calculator

Find the residuals of a regression line: enter X and Y data to fit the least-squares line, or enter a line you already have, or a list of observed and predicted values. The calculator returns every residual (observed − predicted) with the working and a residual plot, and for a fitted line also the standardized residuals, leverage and Cook's distance.

To see the fitted line and its tests first, use the linear regression calculator; for the error measures of the same residuals see the mean squared error calculator. If the residual plot curves, try the quadratic or exponential regression calculator. Related guide: linear regression.

Enter numbers separated by commas, spaces or new lines

One Y for every X, in the same order

What a residual is

A residual is the part of an observed value that a model does not explain: the observed value minus the value the model predicted for it, e = y − ŷ. It is measured in the units of Y and has a sign. A positive residual means the point lies above the line (the model predicted too low); a negative residual means it lies below (the model predicted too high); a residual of 0 means the line passes through the point.

Residuals are how a fitted line is checked. If a straight line describes the data, the residuals look like random noise around 0. If they show a pattern, a curve, a funnel or a lone extreme point, the line is not the whole story. Residuals are not the same as the errors ε of the true, unknown model: residuals are measured from the line that was estimated, the errors from the true one.

How to calculate a residual

Predict the value from the line, then subtract the prediction from the observed value. Suppose a line predicts ŷ = 2.5 + 0.8x and, at x = 4, the observed value is y = 7. The prediction is 2.5 + 0.8 × 4 = 5.7, so the residual is 7 − 5.7 = 1.3: the point lies 1.3 above the line. To do this for many points, choose Use a line I already have, enter the intercept and the slope, and paste the X and Y values. If you already have the predictions, choose Observed and predicted values.

To fit the line first, choose Fit a line to my x and y data. The calculator then finds the least-squares line, whose residuals always add up to 0 and are not correlated with x, and adds the standardized residual, leverage and Cook's distance of every pair.

Formulas

Residual: e = y − ŷ (observed − predicted)

Least-squares line: ŷ = b₀ + b₁x, b₁ = Sxy / Sxx, b₀ = ȳ − b₁x̄

Residual standard error: s = √(SSE / (n − 2)), SSE = Σ e²

Leverage: h = 1/n + (x − x̄)² / Sxx

Standardized residual: r = e / (s · √(1 − h))

Cook's distance: D = r² · h / (2 · (1 − h))

Here Sxx = Σ(x − x̄)² and Sxy = Σ(x − x̄)(y − ȳ). The standardized residual divides a residual by its own standard deviation, s·√(1 − h), which is smaller for a point far from the other X values because such a point pulls the line towards itself. Cook's distance is the summed squared change of all fitted values when the pair is left out, divided by 2s²; the closed form above is the same quantity written with the residual and the leverage. The n leverages add up to 2, the number of coefficients of a straight line.

How to read the results

OutputWhat it tells you
Regression EquationThe least-squares line the residuals are measured from.
Sum of Squared Residuals (SSE)Σ e²: the total squared miss of the line. The least-squares line makes it as small as any straight line can.
Residual Standard Error (s)√(SSE / (n − 2)): the typical size of a residual, in the units of Y. Needs at least 3 pairs.
Largest Absolute ResidualThe biggest miss in the units of Y; find its pair in the table.
Largest Absolute Standardized ResidualThe most extreme residual in standard deviations; beyond 2 is unusual.
Highest LeverageThe most isolated X value, between 1/n and 1. Depends on x only.
Largest Cook's DistanceThe biggest change in the fitted values from leaving one pair out. Above 1 is likely influential.
Sum of ResidualsFor a line you supply: 0 only if the line passes through the point (x̄, ȳ), as the least-squares line does.
Mean Absolute ResidualThe average size of a residual, ignoring its sign.

Worked example: five points

The data are x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5. Load example fills them in.

  1. The line. x̄ = 3, ȳ = 4, Sxx = 10 and Sxy = 6, so the slope is 6 ÷ 10 = 0.6 and the intercept is 4 − 0.6 × 3 = 2.2: ŷ = 2.2 + 0.6x.
  2. Residuals. The fitted values are 2.8, 3.4, 4, 4.6 and 5.2, so the residuals y − ŷ are −0.8, 0.6, 1, −0.6 and −0.2. They add up to 0, as they must for a least-squares line.
  3. SSE and s. The squares are 0.64, 0.36, 1, 0.36 and 0.04, so SSE = 2.4 and s = √(2.4 ÷ 3) = 0.8944.
  4. Leverages. h = 1/5 + (x − 3)² ÷ 10 gives 0.6, 0.3, 0.2, 0.3 and 0.6, which add up to 2. The outer pairs are the most isolated.
  5. Standardized residual of pair 1. r = −0.8 ÷ (0.8944 × √0.4) = −1.4142. No pair is beyond ±2.
  6. Cook's distance of pair 1. D = 1.4142² × 0.6 ÷ (2 × 0.4) = 1.5, above 1. Leaving pair 1 out drops the slope from 0.6 to 0.2, so with only five pairs the first one carries a lot of weight.

A second example, with a line that is given: the line ŷ = 52 + 4x and the pairs (2, 58), (4, 63), (6, 81), (8, 84), (10, 97) predict 60, 68, 76, 84 and 92, so the residuals are −2, −5, 5, 0 and 5. They add up to 3, so this is not the least-squares line for these data, and SSE = 4 + 25 + 25 + 0 + 25 = 79.

How to read a residual plot

The plot puts x (or the predicted value) on the horizontal axis and the residual on the vertical axis, with a line at 0. Look at the overall shape before looking at any number.

Pattern in the plotWhat it suggestsWhat to try
Random scatter around 0, about the same spread everywhereA straight line fits, and the spread of the errors looks constant.Nothing to fix.
A curve, such as a U or a humpThe relationship is not linear.A quadratic or exponential fit, or a transformation of x or y.
A funnel: the spread grows or shrinks along the x-axisThe variance of the errors is not constant.Transform y (a log often helps) or use weighted least squares.
One point far from the rest, up or downAn outlier in y.Check for a recording error; compare the fit with and without it.
One point far from the rest, left or rightA high-leverage point that can tilt the line.Check its Cook's distance and report the fit with and without it.

Rules of thumb for standardized residuals, leverage and Cook's distance

DiagnosticWhat it measuresCommon rule of thumb
Standardized residualHow many standard deviations a residual is from 0, allowing for the point's own variance.Beyond ±2 is unusual (some software flags it); beyond ±3 some call an outlier.
LeverageHow far a point's X lies from the other X values. Depends on x only.More than 3 times the mean leverage 2/n, that is above 6/n; some authors use 2 × 2/n.
Cook's distanceHow much all fitted values change when the pair is left out.Above 0.5 deserves a look; above 1 is quite likely influential.

These are guidelines, not tests. The notes under the results apply |r| > 2, leverage above 6/n and Cook's distance above 1. With fewer than about ten pairs they are blunt: leverage never exceeds 1, so with 5 pairs nothing can pass 6/5 = 1.2, and in the worked example only Cook's distance flags a pair. A flag is a reason to look at the point, not to delete it. Fit the line with and without it and report both.

Assumptions and pitfalls

  • Residuals belong to a model. A different line gives different residuals. A residual plot judges whether the line is adequate; it cannot tell you the line is the truth.
  • Residuals adding up to 0 proves nothing. Every least-squares line has that property, however badly it fits. Look at the plot, not at the sum.
  • The diagnostics need a line that misses. Standardized residuals, leverage and Cook's distance are withheld, with the reason, for 2 pairs, for equal Y values and for points that lie exactly on a line, where s = 0 and they would be 0/0. When the line passes exactly through one pair, that pair's standardized residual and Cook's distance are reported as undefined instead of a rounding artifact.
  • One predictor only. The leverage and Cook's distance here are those of simple linear regression, with 2 coefficients. Multiple regression uses the hat matrix of all predictors.
  • Stay inside the data. Residuals describe the fit where there is data. Predictions beyond the range of x can miss by far more.
  • Pairs must line up. The first Y belongs to the first X. The calculator refuses lists of different lengths rather than trimming one.

Residuals in other software

ToolCommand
Rmodel <- lm(y ~ x); resid(model) for residuals, rstandard(model) for standardized residuals, hatvalues(model) for leverage, cooks.distance(model) for Cook's distance
Python (statsmodels)results = sm.OLS(y, sm.add_constant(x)).fit(); results.resid; influence = results.get_influence(); influence.resid_studentized_internal, influence.hat_matrix_diag, influence.cooks_distance[0]
Python (NumPy)y - numpy.polyval(numpy.polyfit(x, y, 1), x)
Excel / Google Sheets=B2 - FORECAST.LINEAR(A2, $B$2:$B$6, $A$2:$A$6) for one residual (FORECAST in Google Sheets), filled down the column
Excel Analysis ToolPakData > Data Analysis > Regression, tick Residuals, Standardized Residuals and Residual Plots. Its Standard Residuals are the residuals divided by their sample standard deviation (STDEV.S), which ignores leverage, so they differ from the standardized residuals here and in R
TI-83 / TI-84After LinReg(ax+b) the residuals are stored in the list RESID; use it as the Y list of a scatter plot to draw the residual plot
SPSSAnalyze > Regression > Linear > Save. Its Studentized residual is the standardized residual computed here; its Standardized residual is e / s, without the leverage

R and statsmodels agree with this calculator to rounding error, because they use the same definitions. Check which scaling a program means by "standardized" before comparing numbers.

Frequently Asked Questions

What is a residual in statistics?

A residual is the difference between an observed value and the value a model predicted for it: residual = observed − predicted, or e = y − ŷ. It shows how far a point is from the fitted line, in the units of Y. A positive residual means the point lies above the line and a negative one below it.

How do I calculate a residual?

Work out the predicted value from the regression equation, then subtract it from the observed value. For the line ŷ = 2.5 + 0.8x and an observed y = 7 at x = 4, the prediction is 5.7 and the residual is 7 − 5.7 = 1.3. This calculator does it for a whole list: choose 'Use a line I already have', or let it fit the least-squares line for you.

What does a positive or negative residual mean?

A positive residual means the observed value is above the prediction, so the model under-predicted. A negative residual means the observed value is below the prediction, so the model over-predicted. A residual of 0 means the line passes exactly through the point.

Why do the residuals of a regression line add up to zero?

Least squares chooses the intercept and the slope so that the sum of squared residuals is as small as possible. Setting the derivative with respect to the intercept to zero forces the residuals to add up to 0, and the derivative with respect to the slope forces them to be uncorrelated with x. A line that is not the least-squares line, such as one you are given, need not have residuals that add up to 0.

How do I read a residual plot?

A good plot is a formless band of points around 0 with about the same height everywhere. A curve means the relationship is not linear, a funnel means the spread of the errors changes with x, and a single point far from the others is an outlier or a high-leverage point. Decide from the shape first, then check the standardized residuals and Cook's distances of any point that stands out.

What is a standardized residual, and what counts as large?

A standardized residual divides a residual by its estimated standard deviation, e / (s·√(1 − h)), so it counts standard deviations and can be compared across data sets. This is the internally studentized residual that R's rstandard() returns. As a rule of thumb, values beyond ±2 are unusual and beyond ±3 are extreme, but with many points a few beyond ±2 are expected.

What is the difference between a residual and an error?

An error is the deviation of an observed value from the true, unknown regression line, and cannot be observed. A residual is the deviation from the line that was estimated from the data, so it can be calculated. Residuals are used to judge whether the assumptions about the errors are reasonable.

What are leverage and Cook's distance?

Leverage measures how far a point's X lies from the other X values; a high-leverage point has the potential to tilt the line. Cook's distance measures how much all the fitted values change when one pair is left out, so it combines the size of the residual with the leverage. Cook's distance above 1 is commonly taken as a sign of an influential pair.

Why are my standardized residuals different in Excel?

The Excel Analysis ToolPak labels residuals divided by the sample standard deviation of the residuals (STDEV.S) as Standard Residuals. That scaling ignores the leverage of each point, so its values differ from the standardized residuals in R, statsmodels and this calculator, which divide by s·√(1 − h).

Embed This Calculator

Add this free calculator to your course page or LMS.

Adjust the height value to fit your page.