# Why margins and mfx yield different results in R?

When I run a simple logit regression between a binary variable $$y$$ over another binary variable $$x$$, the average marginal effects obtained by the function logitmfx (from mfx package) are different from those obtained by the function margins (from the eponymous package).

Because there is only one covariate ($$x$$), this discrepancy cannot be linked to a difference in concepts between average marginal effects (AME) and marginal effects at mean (MEM), as both coincide in this case.

Hence, what explains the gap?

The reason lies in the types of the variables: mfx is insensitive to type, and treats a binary variable as a boolean, even if it is numeric; whereas margins treats differently factors or boolean from numerical variables. In particular, the marginal effect computed for binary numerical variables is the predicted output $$y$$ when the variable $$x$$ equals 1 minus the predicted $$y$$ when $$x$$ equals its average, instead of subtracting the predicted output at $$x=0$$, as in mfx or when $$x$$ is a factor or a boolean. Hence, if you change the types of $$x$$ and $$y$$ to logical (or factor), you should retrieve the results of mfx.
To conclude, the most intuitive and consistent definition is probably the one of mfx.