


A slightly more concise expression relies on the syntactic sugar provided by margins_summary(): margins_summary(x) # factor AME SE z p lower upper With the exception of differences in rounding, the above results match identically what Stata’s margins command produces.
(m <- margins(x)) # Average marginal effects # lm(formula = mpg ~ cyl * hp + wt, data = mtcars) # cyl hp wt X <- lm(mpg ~ cyl * hp + wt, data = mtcars) With margins in R, replicating Stata’s results is incredibly simple using just the margins() method to obtain average marginal effects and its summary() method to obtain Stata-like output: library("margins") Stata’s margins command is very simple and intuitive to use. Users interested in generating predicted (fitted) values, such as the “predictive margins” generated by Stata’s margins command, should consider using prediction() from the sibling project, prediction. This is an S3 generic method for calculating the marginal effects of covariates included in model objects (like those of classes “lm” and “glm”). The major functionality of Stata’s margins command - namely the estimation of marginal (or partial) effects - is provided here through a single function, margins(). margins therefore provides ways of calculating the marginal effects of variables to make these models more interpretable. In other cases and for generalized linear models, the coefficients are not marginal effects at least not on the scale of the response variable. In ordinary least squares regression with no interactions or higher-order term, the estimated slope coefficients are marginal effects. Marginal effects are partial derivatives of the regression equation with respect to each variable in the model for each unit in the data average marginal effects are simply the mean of these unit-specific partial derivatives over some sample. margins provides “marginal effects” summaries of models and prediction provides unit-specific and sample average predictions from models. These tools provide ways of obtaining common quantities of interest from regression-type models. The margins and prediction packages are a combined effort to port the functionality of Stata’s (closed source) margins command to (open source) R.
