Using Stata’s margins command to compute age‑varying marginal effects after logistic regression
Compute age‑varying marginal effects after a logistic regression in Stata, verify with manual calculation or bootstrap, and watch for speed and SE limitations.
19 Aug 2025, 10:22 UTC

Problem and Takeaway
When you fit a logistic regression in Stata and want to know how the effect of a treatment varies with age, a single number is often insufficient. You need to see the marginal effect of treatment at several ages, with confidence intervals, to decide if the effect is meaningful across the age range.
Why `margins` is the practical tool
`margins` computes marginal effects, adjusted predictions, and contrasts by applying the delta method to the estimated coefficients of any regression model (regress, logit, poisson, mixed, etc.). In practice this means you tell Stata which predictor changes (e.g., treatment) and at which values of other covariates (e.g., age), and it returns the change in the predicted probability together with standard errors and confidence intervals.
Worked Example
Suppose you have already run:
logit outcome age treatment
Now you want the marginal effect of `treatment` when `age` equals 20, 40, and 60 years.
- Run the margins command with the interaction of interest and the ages you care about:
- Use the `dydx()` option to request the marginal effect of the treatment variable.
- Supply the `at()` option to set `age` to the three values.
margins, dydx(treatment) at(age=(20 40 60))
The output will show a row for each age, the estimated change in the outcome probability when `treatment` goes from 0 to 1, the standard error, and the 95 % confidence interval.
To verify the results manually, compute predicted probabilities for each age under treatment=0 and treatment=1, subtract the two probabilities, and then average the differences across the sample. Compare those hand‑calculated values with the `margins` output; they should be close.
For a more robust check, bootstrap the marginal effects:
margins, dydx(treatment) at(age=(20 40 60))
bstat, reps(1000)
The bootstrap standard errors should be similar to the delta‑method SEs reported by `margins`. If they differ substantially, consider using a simulation‑based approach or refit the model with a more stable estimator.
Trade‑off and Limitation
The delta‑method standard errors that `margins` relies on can be unreliable when the underlying model is highly non‑linear or when the data set is very large. In such cases the command may run slowly, and the reported confidence intervals might be too narrow. A practical mitigation is to run a bootstrap (`bstat`) as shown above, or to use simulation‑based predictions for sensitive analyses.
Another limitation appears with interaction terms. The marginal effect of `treatment` varies with the values of any covariates it interacts with, so reporting a single effect for a fixed age may hide heterogeneity across the age distribution. Exploring the effect over a realistic range of the interacting variable is advisable.
Actionable Closing
To get reliable marginal effects in Stata, follow these steps:
- Fit your regression model and ensure it converges and passes basic diagnostics (e.g., residual plots, pseudo‑R²).
- Use `margins, dydx() at(=)` to obtain the effect of interest at the covariate values you care about. The command works after any estimation command that produces coefficients.
- Check the output for sensible magnitudes and confidence intervals; note that the standard errors are based on the delta method.
- Validate the results with a manual calculation or a bootstrap (`bstat`) to confirm that the delta‑method SEs are appropriate.
- Remember that `margins` works identically across Stata versions ≥ 11; newer releases add features such as `contrast` and `nlcom` but the core syntax remains unchanged.
- If the dataset is large or the model is very non‑linear, consider using `vce(cluster id)` for cluster‑robust SEs or run a simulation‑based check to guard against unreliable inference.
By combining the convenience of `margins` with a quick bootstrap check, you can confidently interpret how treatment effects change with age and make more informed decisions.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.