This function uses numeric differentiation (grad
) to extract marginal effects from an estimated model with respect to all numeric variables specified in data
and returns a data.frame containing the unit-specific marginal effects with respect to each variable included (or not included) in the model. (Note that this is not each coefficient.) For factor variables (or character variables, which are implicitly coerced to factors by modelling functions), discrete differences in predicted outcomes are reported instead (i.e., change in predicted outcome when factor is set to a given level minus the predicted outcome when the factor is set to its baseline level). If you want to use numerical differentiation for factor variables (which you probably do not want to do), enter them into the original modelling function as numeric values rather than factors.
Variable class coercion (other than factor(x)
) inside a formula passed to, for example, lm
may cause weird behavior, or errors.