Marginal effects/predictions at representative values (with other values at observed)

156 views Asked by At

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)?

1

There are 1 answers

0
Vincent On BEST ANSWER

You can do this easily with the marginaleffects package. Please refer to the documentation for details. In particular, note that the at notation in Stata replicates the full dataset several times for each value of the specified variable, and then averages. This is equivalent to using the datagridcf() function in marginaleffects to create “counter-factual” datasets, and the by argument to marginalize. You may also find those two vignettes useful:

  1. Marginal Effects Vignette
  2. Detailed comparison between marginaleffects and Stata
library(marginaleffects)
mod <- lm(mpg ~ hp + am, data = mtcars)

predictions(mod, newdata = datagridcf(hp = c(110, 120, 130)), by = "hp")

          type  hp predicted std.error statistic p.value conf.low conf.high
    1 response 110  22.25107 0.5895479  37.74260       0 21.09558  23.40656
    2 response 120  21.66219 0.5553795  39.00431       0 20.57367  22.75072
    3 response 130  21.07332 0.5307275  39.70647       0 20.03311  22.11352

marginaleffects(mod, newdata = datagridcf(hp = c(110, 120, 130)), by = "hp")

          type term    contrast       dydx   std.error statistic      p.value
    1 response   hp mean(dY/dX) -0.0588878 0.007856747 -7.495189 6.620280e-14
    2 response   hp mean(dY/dX) -0.0588878 0.007856747 -7.495189 6.620280e-14
    3 response   hp mean(dY/dX) -0.0588878 0.007856745 -7.495191 6.620172e-14
    4 response   am mean(dY/dX)  5.2770853 1.079540509  4.888270 1.017260e-06
    5 response   am mean(dY/dX)  5.2770853 1.079540509  4.888270 1.017260e-06
    6 response   am mean(dY/dX)  5.2770853 1.079540509  4.888270 1.017260e-06
         conf.low   conf.high predicted predicted_hi predicted_lo  hp
    1 -0.07428674 -0.04348886  25.38434     25.38267     25.38434 110
    2 -0.07428674 -0.04348886  24.79546     24.79380     24.79546 120
    3 -0.07428674 -0.04348887  24.20658     24.20492     24.20658 130
    4  3.16122479  7.39294583  25.38434     25.38487     25.38434 110
    5  3.16122479  7.39294583  24.79546     24.79599     24.79546 120
    6  3.16122479  7.39294583  24.20658     24.20711     24.20658 130