[Stata] Calculating marginal effects: margins, marginsplot, and mchange commands
Regression Coefficients, Odds Ratios, and Marginal Effects
Feature | Regression Coefficients | Odds Ratios | Marginal Effects |
---|---|---|---|
Definition | Represents 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 unit | Measures how the probability of the outcome changes when the independent variable increases by one unit |
Applicable Models | Mostly in linear regression (e.g., OLS) but also appears in nonlinear models | Used in logistic/probit regressions (binary outcomes) | Used in logistic/probit and other nonlinear models to translate effects into probability changes |
Interpretation | Direct in linear models: A one-unit increase in X changes Y by β units | A 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 |
Units | Same as the dependent variable (e.g., dollars, years, scores) | Unit-free (a ratio) but applies to odds, not probability | Expressed as a probability change (0 to 1) |
Nonlinear Models | Difficult to interpret meaningfully | Can be misleading because odds are not the same as probabilities | Preferred for nonlinear models because they directly express probability changes |
Interaction Effects | Interaction terms are straightforward | Interaction terms affect odds multiplicatively, making interpretation difficult | Interaction 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?
- 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.
- 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.
- 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).
- 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 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:
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.
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 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.
// 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.
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:
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.
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
1 Response
[…] [Stata] Calculating marginal effects: margins, marginsplot, and mchange commands […]