Using Stata’s margins Command for Adjusted Predictions and Marginal Effects
Learn how Stata’s margins command computes adjusted predictions and marginal effects after regression, with a concrete example using the auto dataset and guidance on limitations and verification.
26 Apr 2026, 23:31 UTC

Why margins matters
After fitting a regression model, the raw coefficients often do not answer the substantive question of interest: how does a change in a predictor affect the expected outcome, especially for non‑linear models? Stata’s margins command computes adjusted predictions and marginal effects (or elasticities) directly from the estimation results, handling factor variables and interactions automatically.
Basic syntax and options
The general form is:
margins, [options]
Key options include:
at()– set specific values for covariates (e.g.,at(age=30)).dydx(varlist)– marginal effect of each variable invarliston the expected outcome.eydx(varlist)– elasticity (percentage change in outcome for a 1% change in predictor).contrast()– compare predicted outcomes across groups or scenarios.
By default, margins reports standard errors, confidence intervals, and p‑values derived from the delta method.
Worked example with the auto dataset
Suppose we model the probability that a car’s price is above average using logistic regression.
sysuse auto, clear
generate highprice = (price > r(mean))
logistic highprice foreign mpg weight
To obtain the average marginal effect of foreign (whether the car is foreign‑made) on the probability of a high price, we evaluate the derivative at the sample means of the other covariates:
margins, dydx(foreign) atmeans
The command returns a table with the estimated marginal effect, its standard error, confidence interval, and a test statistic. No further algebra is required; Stata handles the derivative of the logistic function internally.
If we instead want the predicted probability for a domestic car with average mpg and weight, we can specify:
margins, at(foreign=0) atmeans
For a visual check, marginsplot draws the adjusted predictions across a range of a continuous covariate:
margins foreign, at(mpg=(10(5)40))
marginsplot
Limitations and practical checks
While margins is powerful, a few considerations help avoid misinterpretation:
- Estimation sample: Results are based on the sample used in the original model (
e(sample)). If the model excluded observations due to missing values, the margins will reflect that same restriction. - Choice of
at()values: For non‑linear models, marginal effects vary with covariate levels. Changing the values inat()can lead to substantively different numbers; always report the values you used. - Computational load: With very large datasets or models containing many interactions,
marginscan be slow. In such cases, consider estimating margins on a representative subsample or using thevce(bootstrap)option for faster inference.
To verify that the command behaved as expected, you can:
- Store the margins results:
margins, dydx(foreign) atmeans, save(margfx) - Reload and inspect:
marginsload margfx - Compare with a manual calculation using predicted probabilities from
predictfor a small subset of observations (this serves as a sanity check, not a proof).
Takeaway
The margins command bridges the gap between model coefficients and interpretable quantities such as adjusted predictions, average marginal effects, and elasticities. By specifying covariate values with at() and choosing the appropriate derivative option (dydx(), eydx(), or contrast()), you obtain statistically sound results without manual calculus. Always check the estimation sample, report the at() values used, and be aware of potential computational demands for large models.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.