I have been looking around for a way to calculate marginal effects/predictions at specific values in R, while the remaining variables are kept as observed. I believe this is the default in Stata (for example, when using margins, at(age=(30 35 40 45 50))
(see Stata manual).
From what I can tell, marginaleffects
and ggeffects
automatically sets the other variables to the mean. Is there a good way to achieve this (preferably using the above packages)?
You can do this easily with the
marginaleffects
package. Please refer to the documentation for details. In particular, note that theat
notation inStata
replicates the full dataset several times for each value of the specified variable, and then averages. This is equivalent to using thedatagridcf()
function inmarginaleffects
to create “counter-factual” datasets, and theby
argument to marginalize. You may also find those two vignettes useful:marginaleffects
andStata