Command reference

predict — create fitted values and residuals

Use the most recently fitted model to add a new variable containing predictions, residuals, or the standard error of the linear prediction. The default prediction scale depends on the model you fitted.

Syntax

predict new_variable [, statistic]

Run an estimation command first, then give predict one new variable name. Replace statistic with one of the options below, or omit the comma and option to use the model's default. Choose one statistic per command and a different output name for each result.

After regress, the default is the fitted outcome. After logit or probit, it is the probability of the positive outcome. After poisson or nbreg, it is the expected count; after glm, it is the fitted mean.

Prediction statistics

xb
Create the linear prediction: the intercept plus each predictor multiplied by its fitted coefficient. After linear regression this is the fitted mean; after logit it is on the log-odds scale. For glm, stored offsets and exposures are included in the linear index.
residuals / e
Create the observed outcome minus the model's fitted value on the outcome scale. A negative residual means that the observed value is below its prediction. After a binary or count model, these are raw response residuals.
stdp
Create the standard error of the linear prediction, using the model's stored coefficient covariance matrix. After regress, this quantifies uncertainty in the fitted mean; it is not the standard deviation of individual outcomes or a prediction interval.
pr
Create fitted probabilities after logit or probit. This makes their default prediction explicit and is not accepted after linear regression.
n
Create expected counts after poisson or nbreg, by exponentiating the linear index. This is their default prediction.
mu
Create the fitted mean after glm, using its family and inverse link. With grouped binomial data, the mean is scaled by the number of trials. This is the default after glm.

Example

Fit math to read and female in the bundled synthetic student data. Then save fitted math scores, residuals, and standard errors of the fitted mean, and inspect the first six observations.

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
Open example in browser

Sample data included. Opens in a new tab; click Run.

What to look for

Observation 1 has an observed math score of 45 and a fitted score of about 47.82. Its residual is therefore 45 − 47.82 = −2.82: the model predicts a higher score than was observed. Its fitted_se of about 0.48 describes uncertainty in that fitted mean, not how far an individual score may fall from it.

Example result
idmathfitted_mathresidualfitted_se
14547.82−2.820.48
25151.22−0.220.66
34342.720.280.64
45658.86−2.860.43
54850.37−2.370.42
66062.68−2.680.41