# Stata Margins tool

How does Stata create their margins dy/dx output? I would like to recreate some Stata output in SAS. The results of a logistic regression give a coefficient of 1.4921 for my dummy variable, and an odds ratio of 4.447 (or the exponentiation of 1.4921). Stata has a margins dy/dx tool that gives a value of 0.1309 for this variable. I interpret this to mean there is a 13.9% difference between the probability of each outcome for this 0 1 indicator.

How can I generate this 13.9% difference outside of Stata?

• I might be able to have a go at answering if you put your whole output here. Commented Jan 28, 2015 at 18:12
• I'd recommend checking Stata manuals. They are freely available online as pdf files. Commented Jan 28, 2015 at 18:32

For a continuous variable, the marginal effect of $x_k$ in a logit is

$$\Lambda(X'\beta)\cdot \left[1-\Lambda(X'\beta)\right]\cdot\beta_k,$$ where $$\Lambda(z)=\frac{\exp{z}}{1+\exp{z}}.$$

By default, Stata actually calculates the average of this over the estimation sample, but I will use the mean value of x in what follows (marginal effect at the mean rather than average marginal effect) to get the point across:

. sysuse auto, clear
(1978 Automobile Data)

. logit foreign mpg

Iteration 0:   log likelihood =  -45.03321
Iteration 1:   log likelihood = -39.380959
Iteration 2:   log likelihood = -39.288802
Iteration 3:   log likelihood =  -39.28864
Iteration 4:   log likelihood =  -39.28864

Logistic regression                               Number of obs   =         74
LR chi2(1)      =      11.49
Prob > chi2     =     0.0007
Log likelihood =  -39.28864                       Pseudo R2       =     0.1276

------------------------------------------------------------------------------
foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg |   .1597621   .0525876     3.04   0.002     .0566922     .262832
_cons |  -4.378866   1.211295    -3.62   0.000    -6.752961   -2.004771
------------------------------------------------------------------------------

. di _b[mpg] * exp(_b[mpg]*21.2973+_b[_cons])/(1+exp(_b[mpg]*21.2973+_b[_cons]))* (1-exp(_b[mpg]*21.2973+_b[_cons])/(1+exp(_b[mpg]*21.2973+_b[_cons])))
.03175262

. margins, dydx(*) atmean

Conditional marginal effects                      Number of obs   =         74
Model VCE    : OIM

Expression   : Pr(foreign), predict()
dy/dx w.r.t. : mpg
at           : mpg             =     21.2973 (mean)

------------------------------------------------------------------------------
|            Delta-method
|      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg |   .0317526   .0103945     3.05   0.002     .0113798    .0521254
------------------------------------------------------------------------------


For dummy variables, Stata uses a finite difference method (average of predicted probabilities with $x_i$ set to 1 minus predicted probability with $x_i$ set to zero).

Standard errors are a bit more complicated.

• Useful answer, although I'm not sure about the clarity of the link between the opening equations and the formula actually used in the di part of the Stata code. It took me 10 good minutes to decompose the code to reconstitute the equation :)
– Fr.
Commented Mar 19, 2019 at 9:22
• @Fr. What was confusing about it? Is it the means? I suppose the index function coefficient in the code comes first rather than last, but that is the only difference. Please feel free to propose an edit that you think works better. Commented Mar 19, 2019 at 14:15
• I think one way to make the code easier to parse is (1) to define $z$: sca de z = _b[mpg] * mu_x + _b[_cons], (2) then $\lambda_z$: sca de lambda_z = exp(z) / (1 + exp(z)), and finally (3) di lambda_z * (1 - lambda_z) * _b[mpg] to display the estimated effect.
– Fr.
Commented Mar 20, 2019 at 16:46