These functions provide a simple interface to the calculation of marginal effects for specific variables used in a model, and are the workhorse functions called internally by marginal_effects
.
dydx
is an S3 generic with classes implemented for specific variable types. S3 method dispatch, somewhat atypically, is based upon the class of data[[variable]]
.
For numeric (and integer) variables, the method calculates an instantaneous marginal effect using a simple “central difference” numerical differentiation:
$$\frac{f(x + \frac{1}{2}h) - f(x - \frac{1}{2}h)}{dh}$$, where (\(h = \max(|x|, 1) \sqrt{\epsilon}\) and the value of \(\epsilon\) is given by argument eps
. This procedure is subject to change in the future.
For factor variables (or character variables, which are implicitly coerced to factors by modelling functions), discrete first-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). These are sometimes called “partial effects”. 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.
For ordered factor variables, the same approach as factors is used. This may contradict the output of modelling function summaries, which rely on options("contrasts")
to determine the contrasts to use (the default being contr.poly
rather than contr.treatment
, the latter being used normally for unordered factors).
For logical variables, the same approach as factors is used, but always moving from FALSE
to TRUE
.