Guide
Exporting Average Marginal Effects from Logistic Regression in Stata
Step‑by‑step guide to fit a logistic model in Stata, compute average marginal effects with margins, save them to a dataset, and export to Excel.
Published by Tasadduq Burney
21 Mar 2026, 02:05 UTC
2 min80.9K views0

Desired Outcome
Obtain the average marginal effects (AME) for each predictor in a logistic regression model and export those estimates to an Excel workbook for further reporting or sharing.
Prerequisites
- Stata version 13 or later (the
dydx(*)syntax formarginswas introduced in version 13). - Write permission to the directory where you intend to save the Stata dataset and the Excel file.
- A dataset loaded in memory with a binary dependent variable and at least one independent variable (continuous or categorical).
Procedure
- Fit the logistic regression model. Use
logit(orlogistic) with factor notation for any categorical predictors. Example using the built‑inautodataset:
Heresysuse auto, clear logit foreign i.foreign mpg weightforeignis the dependent variable (0 = Domestic, 1 = Foreign) andmpgandweightare continuous predictors. - Compute the average marginal effects. Run
marginswith thedydx(*)option to obtain the AME for every predictor. Store the results in a temporary Stata dataset:
After this command, Stata creates a dataset namedmargins, dydx(*) save(margins_eff)margins_effin memory that contains the estimates inr(b)and the variance‑covariance matrix inr(V). - Export the results to Excel. Use
export excelto write the dataset to a workbook. Specify that the first row should contain variable names:
The fileexport excel using margins_eff.xlsx, first(variable names) replacemargins_eff.xlsxwill appear in the current working directory.
Expected Checks
- After
margins, inspect the output table to ensure that standard errors are present and not excessively large (which could indicate convergence problems). - Run
return listto verify that the numbers shown in the table match the contents ofr(b)and that the squared standard errors match the diagonal ofr(V):return list - Optionally, perform a manual check for a continuous predictor (e.g.,
mpg) by computing the difference in predicted probabilities atxandx+1usingpredictand comparing it to the AME reported bymargins. The two values should agree within rounding error. - Assess model adequacy before trusting the margins:
estat goforlinktestcan be run after the logistic regression to detect major misspecification.
Recovery Options
- If the
marginscommand fails because of missing factor notation consistency, re‑estimate the model using the exact same factor specification (e.g.,i.foreign) and then rerunmargins. - If the Excel export fails due to lack of write permission, choose a different directory where you have rights, or run Stata with elevated privileges.
- If the AME estimates appear unstable, repeat the procedure with robust or bootstrap standard errors:
logit foreign i.foreign mpg weight, vce(bootstrap)followed bymargins, dydx(*).
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.