[Stata] Calculating marginal effects: margins, marginsplot, and mchange commands
The margins
command in Stata offers a versatile 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. 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 marginal effects have three types: average marginal effects (AMEs) and marginal effects at the means (MEMs) or at representative values (MERs).
- Average marginal effect (AME): an average of the marginal effects at each value of a dataset
- Marginal effects at the mean (MEM): marginal effects at the mean values of a dataset
- 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).
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.
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
. Runningmargins, 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).
- For example, if your variable is gender (coded as 0 for male and 1 for female),
Marginal Effects at the Means (MEMs)
MEMs offer a snapshot of 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:
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. The interpretation might reveal that Black and Other individuals have a higher probability of high blood pressure compared to White individuals.
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 variablex
. - 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:
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
marginsplot
The marginsplot
command is a powerful 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 official Stata -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.
// install package
net describe spost13_ado, from(https://jslsoc.sitehost.iu.edu/stata)
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.
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:
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.
mchange age, at(race==2)
Reference
- Using Stata’s Margins Command to Estimate and Interpret Adjusted Predictions and Marginal Effects
- An Introduction to ‘margins’ (r-project.org)
- Interpreting Model Estimates: Marginal Effects (slides)
- Marginal Effects for Continuous Variables
- Introduction to the Stata -margins- Command
- Using Stata’s Margins Command to Estimate and Interpret Adjusted Predictions and Marginal Effects
- Predicted Probabilities and Marginal Effects After (Ordered) Logit/ Probit models using margins in Stata
- Using the spost13 commands for adjusted predictions and marginal effects with binary dependent variables
- New methods of interpretation using marginal effects for nonlinear models