Skip to content

Beta release

Compare
Choose a tag to compare
@leeper leeper released this 05 Aug 14:56
· 239 commits to main since this release

This is a beta release of margins. Earlier versions (available in the git commit history on GitHub) were largely a "proof of concept" effort. The current release (v0.2.0) could benefit from improvements, particularly with regard to speed of variance estimation, but represents a first draft that is for the most part complete and represents a first full draft of the package API.

Some notes about this release (compared to previous "alpha" versions):

Plotting

  • Added plot.margins() method for mimicking Stata's marginsplot behavior.
  • Added persp() methods for "lm" and "glm" class objects to display 3-dimensional representations of predicted values and marginal effects.
  • Added cplot() generic and methods for "lm" and "glm" class objects to display conditional predictions and conditional marginal effects in the style of the interplot and plotMElm packages.

Estimation and statistical details

  • Implemented estimation of "discrete change" representations of marginal effects of factor variables in models, ala Stata's default settings. It is currently not possible to do - as in Stata - "continuous" estimation (i.e., partial derivatives) of factors. If you want this, enter a factor variable into the model as a numeric instead of as a factor.
  • Added various variance estimation procedures for marginal effects: delta method (the default), bootstrap, and simulation (ala Clarify).
  • Fixed estimation of marginal effect variances for generalized linear models, so that they are correct on both "link" and "response" scales.
  • Exposed two internal marginal effect estimation functions:
    • build_margins() is called by margins() methods (perhaps repeatedly) and actually assembles a "margins" object from a model and data. It is never necessary to call this directly, but may be useful for very simple marginal effect estimation procedures (i.e., using original data with no at specification).
    • marginal_effects() is the very low level function that differentiates a model with respect to some input data (or calculate discrete changes in the outcome with respect to factor variables). This is the fastest way to obtain marginal effects without the overhead of creating a "margins" object (for which variance estimation is fairly time-consuming).

Computational details

  • Re-implemented marginal effects estimation using numeric derivatives provided by numDeriv::grad() and numDeriv::jacobian() rather than symbolic differentiation (via D() and deriv()). This allows margins() to handle almost any model that can be specified in R, including models that cannot be specified in Stata (e.g., y ~ x + I(log(x))).
  • Used the compiler package to byte compile the internal prediction and gradient functions used by build_margins(), thereby improving estimation speed.
  • The internal build_datalist() now checks for specification of illegal factor levels in at and errors when these are encountered, as well as issues warnings when requesting values outside of the observed range of numeric variables.

Thanks and inspiration

Some thanks are due to Martin Bisgaard, Justin Esarey and Jane Lawrence Sumner, Christopher Gandrud, Matt Golder, William Greene, Frederik Hjorth, Yue Hu and Frederick Solt, Jay Kahn, Sharyn O'Halloran, Carlisle Rainey, Måns Söderbom, Kim Sønderskov, and StataCorp for statistical help, inspiration, helpful discussions, and/or previous programming efforts.