When using at, margins() constructs modified datasets - using build_datalist() - containing the specified values and calculates marginal effects on each modified dataset, rbind-ing them back into a single “margins” object. It can be used by specifying a list of variable names and specified values for those variables at which to calculate marginal effects, such as margins(x, at = list(hp=150)). The at argument has been translated into margins() in a very similar manner. over: calculate marginal effects (including MEMs and/or AMEs at observed or specified values) on subsets of the original data (e.g., the marginal effect of a treatment separately for men and women).atmeans: calculate marginal effects at the mean (MEMs) of a dataset rather than the default behavior of calculating average marginal effects (AMEs).at: calculate marginal effects at (potentially representative) specified values (i.e., replacing observed values with specified replacement values before calculating marginal effects).(The functionality of Stata’s command to produce predictive margins is not ported, as this is easily obtained from the prediction package.) In particular, Stata provides the following options: Margins is intended as a port of (some of) the features of Stata’s margins command, which includes numerous options for calculating marginal effects at the mean values of a dataset (i.e., the marginal effects at the mean), an average of the marginal effects at each value of a dataset (i.e., the average marginal effect), marginal effects at representative values, and any of those operations on various subsets of a dataset. Using the plot() method also yields an aesthetically similar result to Stata’s marginsplot: plot(m)
![margins stata margins stata](https://i.ytimg.com/vi/uYV1yfHL0RQ/maxresdefault.jpg)
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 stata margins stata](https://www.stata.com/stata12/margins-plots/i/marginsplot1.png)
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.