predict residuals and fitted values after regression

Use predict after a fitted model to save observation-level residuals, fitted values, and standard errors as new variables.

Tested 2026-08-16 with the current stats.camp development build · View the do-file on GitHub

Run the residuals example View supported commands
Independent software. Not affiliated with, sponsored by, or endorsed by StataCorp LLC.

How to predict residuals after a regression

regress math read female
predict residual, residuals

Run predict after the estimation command. The new residual variable contains the observed outcome minus its fitted value for each observation used by the compatible model.

Complete runnable example

clear
import delimited "data/student_scores.csv"
regress math read female
predict fitted_math
predict residual, residuals
predict fitted_se, stdp
list id math fitted_math residual fitted_se in 1/6

The first predict uses the default linear prediction. The next two request residuals and the standard error of the fitted mean.

Output from stats.camp

+--------------------------------------------------------+
|  #    id    math   fitted_math   residual   fitted_se  |
|--------------------------------------------------------|
|   1     1     45         47.82      -2.82        0.48  |
|   2     2     51         51.22      -0.22        0.66  |
|   3     3     43         42.72       0.28        0.64  |
|   4     4     56         58.86      -2.86        0.43  |
|   5     5     48         50.37      -2.37        0.42  |
|   6     6     60         62.68      -2.68        0.41  |
+--------------------------------------------------------+

What each generated variable means

fitted_math is the model's conditional mean estimate for each covariate pattern. residual is observed math minus fitted math, so observation 1 has 45 − 47.82 ≈ −2.82. fitted_se quantifies uncertainty in the fitted mean, not the spread of a future individual outcome.

Common options and current limits

See the current predict entry and Stata's regress postestimation manual for an external reference.

Related guides

Try the residuals workflow

Clone the example repository in stats.camp and open the exact do-file shown on this page. No stats.camp account is required.

Run the residuals example