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.
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
- The default and explicit
xbcreate the linear prediction. residuals, or its supportedealias, creates observed minus fitted values after a compatible model.stdpcreates the standard error of the linear prediction.predictuses the most recent estimation result and requires a new variable name.- Official postestimation commands offer many model-specific statistics. Do not assume options beyond
xb,residuals/e, andstdpare implemented here.
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