[Stata] Calculating marginal effects: margins, marginsplot, and mchange commands

Regression Coefficients, Odds Ratios, and Marginal Effects

FeatureRegression CoefficientsOdds RatiosMarginal Effects
DefinitionRepresents the effect of a one-unit increase in an independent variable on the dependent variable (linear models)Measures how the odds of the outcome change when the independent variable increases by one unitMeasures how the probability of the outcome changes when the independent variable increases by one unit
Applicable ModelsMostly in linear regression (e.g., OLS) but also appears in nonlinear modelsUsed in logistic/probit regressions (binary outcomes)Used in logistic/probit and other nonlinear models to translate effects into probability changes
InterpretationDirect in linear models: A one-unit increase in X changes Y by β unitsA one-unit increase in X multiplies the odds of the outcome by e^\beta A one-unit increase in X changes the probability of the outcome by a certain percentage
UnitsSame as the dependent variable (e.g., dollars, years, scores)Unit-free (a ratio) but applies to odds, not probabilityExpressed as a probability change (0 to 1)
Nonlinear ModelsDifficult to interpret meaningfullyCan be misleading because odds are not the same as probabilitiesPreferred for nonlinear models because they directly express probability changes
Interaction EffectsInteraction terms are straightforwardInteraction terms affect odds multiplicatively, making interpretation difficultInteraction effects can be easily understood in terms of probability changes, making them more intuitive

Why do we use Marginal Effects instead of coefficients or odds ratio?

  1. More Intuitive in Nonlinear Models
    • Logistic and probit regression models predict probabilities, but their coefficients and odds ratios describe changes in log-odds or odds—which are hard to interpret.
    • Marginal effects directly express probability changes, which are more meaningful for decision-making.
  2. Better for Interaction Effects
    • In nonlinear models, interaction effects affect probabilities in complex and non-constant ways.
    • Marginal effects allow us to evaluate how an interaction changes actual probabilities, while odds ratios can distort the magnitude of an effect.
  3. Easier to Compare Across Models
    • Odds ratios are unit-free but can vary dramatically based on sample composition and model specification.
    • Marginal effects provide a consistent way to compare probabilities across different models and groups.

For example,

  • If the odds ratio for insurance is 2.5, insured individuals have 2.5 times higher odds of using services than uninsured individuals.
    • This does not mean they are 2.5 times more likely in probability terms.
  • If the marginal effect of insurance is 0.30, it means having insurance increases the probability of using health services by 30 percentage points—e.g., from 50% to 80%.
    • This is easier to interpret and directly relevant for policy discussions.

Marginal Effects in Stata

The margins command in Stata offers an approach to interpreting the results of regression models. Marginal effects quantify how a change in an independent variable affects the dependent variable while holding other variables constant.

The marginal effects have three types: average marginal effects (AMEs) and marginal effects at the means (MEMs) or at representative values (MERs).

  1. Average marginal effect (AME): an average of the marginal effects at each value of a dataset
  2. Marginal effects at the mean (MEM): marginal effects at the mean values of a dataset
  3. Marginal effects at representative values (MER): marginal effects at representative values

Here, in this example, we will use logistic regression results to calculate marginal effects using the sample dataset nhanes2. The dependent variable is whether the participant has high blood pressure or not (binary). The independent variables are age, bmi, sex, and race. The following example and interpretation are for logistic regression results (binary outcome).

Stata
webuse nhanes2
logit highbp age bmi i.sex i.race

Approach 1: Using margins command

Average Marginal Effect (AME)

AMEs tell us how a one-unit change in an IV affects the probability of the DV being 1, on average across the sample.

Stata
margins, dydx(bmi)

We can assess the impact of all variables in the model on the probability of the outcome. For instance, a one-unit increase in bmi might increase the likelihood of high blood pressure by 2.6% on average.

  • If a variable is continuous: When you use margins, dydx(variable), Stata estimates the first partial derivative of the probability with respect to the continuous predictor.
    • In other words, it calculates how a small change in the continuous variable affects the predicted outcome (e.g., probability, mean, or count), while holding other variables constant.
    • This distinction applies only if your original regression model (e.g., probit, logit, or Poisson) used factor variable notation to distinguish discrete variables from continuous ones.
    • Suppose you have a continuous independent variable, such as bmi. Running margins, dydx(bmi) will give you the expected change in the dependent variable (DV) associated with a one-unit increase in BMI.
    • If your model is a probit or logit, this represents the change in the probability of the outcome.
    • For count models (e.g., Poisson), it indicates the change in the expected count.
  • If a variable is nominal (binary/categorical): When you run margins, dydx(variable), you obtain the expected difference in the dependent variable (DV) between the specified level of the variable and the reference level.
    • For example, if your variable is gender (coded as 0 for male and 1 for female), margins, dydx(gender) will give you the difference in the DV between females (gender = 1) and males (gender = 0).

Marginal Effects at the Means (MEMs)

MEMs show how changes in the IVs influence the probability of the DV being 1, with all other variables held at their mean. This is particularly insightful for understanding the effect of specific variables in a “typical” scenario:

atmeans:

  • The atmeans option calculates marginal effects at the mean values of the independent variables.
  • When you use margins, atmeans, Stata sets all covariates to their respective means and then computes the marginal effect.
  • Essentially, it provides the average effect of the independent variables on the dependent variable.
  • Use atmeans when you want to understand how changes in the predictors impact the average outcome.

For example, after running a logistic regression (logit) model, you might use:

Stata
margins i.race, atmeans

This command calculates the predicted probability of the outcome variable being 1 for different levels of the variable race, holding all other variables at their mean values. For discrete variables (such as binary indicators), the marginal effect represents the difference in the dependent variable between two distinct levels of the independent variable. The interpretation reveals that Black and Other individuals have a higher probability of high blood pressure compared to White individuals.

Stata
margins, dydx(bmi) atmeans

This command provides an estimate of the effect of bmi on the probability of high blood pressure, assuming average values for all other characteristics — one unit increase in bmi might increase the likelihood of high blood pressure by 3.3%, assuming average values for all other variables .

Marginal Effects at Representative Values (MERs)

MERs allow for the exploration of how the effects of IVs vary across different levels of another variable (which you need to specify).

at:

  • The at option allows you to specify specific values for the independent variables.
  • When you use margins, at(x=(0(0.1)5)), Stata evaluates the marginal effect at different levels of the continuous variable x.
  • For example, it calculates the effect of a 0.1-unit increase in x from 0 to 5.
  • You can customize the range of values for x using the (0(0.1)5) syntax.
  • Use at when you want to explore the effect of specific values of the predictors.

For example:

Stata
margins, dydx(age) at (race==(1 2 3))

This could illustrate how the impact of age on high blood pressure changes based on their race.

Visualizing Results with marginsplot

Stata
marginsplot

The marginsplot command is a tool for visualizing the results obtained from margins. It can graphically display the differences in predicted probabilities, AMEs, MEMs, or MERs, making it easier to interpret and present the results. You can simply put this command right after your margins commands 🙂

Approach 2: Using mchange command

The m* commands, namely mgen, mtable, and mchange, serve as convenient wrappers for the margins- command, developed by Long & Freese. These commands simplify the process by automatically generating the necessary -margins- commands and presenting the output in a more concise format.

Stata
// install package
net install spost13_ado, from(https://jslsoc.sitehost.iu.edu/stata) replace

To calculate the marginal change in the predicted probability of the outcome variable for a change in one or more explanatory variables, holding other variables constant, we will use the mchange command.

Average Marginal Effect (AME)

Without atmeans or at options, it automatically returns the average marginal effects. You don’t have to specify the variables of your interest since it returns the marginal effects of ALL variables in your model by default.

Stata
mchange, stat(change from to pvalue)

The output of the mchange command shows the marginal change in the predicted probability of having high blood pressure for a change in each variable from 0 to its mean value, holding other variables at their mean values. The output also shows the predicted probabilities before and after the change, and the confidence intervals for the marginal change and the predicted probabilities.

This mchange command is especially useful for health disparities research, like an example below by Cuevas et al. (2020) published in Ethnicity & Health:

Adolfo G. Cuevas , Kasim Ortiz , Nancy Lopez & David R. Williams (2020)
Assessing racial differences in lifetime and current smoking status & menthol consumption among Latinos in a nationally representative sample, Ethnicity & Health, 25:5, 759-775.

Marginal Effects at the Means (MEMs)

You can also compute the marginal effects at the means (MEM) using atmeans option.

For each additional year of age while holding other variables at means, the probabilities of health status change as follows:

  • Poor: +0.002
  • Fair: +0.004
  • Good: +0.003
  • Very good: -0.003
  • Excellent: -0.006

All changes are statistically significant (p < 0.05).

When age increases by one standard deviation (SD) while holding other variables at means, the probabilities change as follows:

  • Poor: +0.048
  • Fair: +0.083
  • Good: +0.027
  • Very good: -0.070
  • Excellent: -0.088

All changes are statistically significant (p < 0.05).

Marginal effect at representative values (MER)

Further, you can also calculate the marginal effect at representative values (MER) using mchange with at option.

Stata
mchange age, at(race==2)

Reference

  • March 7, 2024