3
votes

I have a regression model with a bunch of indicator variables interacting with regressors. In this case P-values may work well for model selection. I know Stata has a command called margins, and it really helps in this case. Example:

margins, dydx(*)

From the relevant Stata manual

Description

Margins are statistics calculated from predictions of a previously fit model at fixed values of some covariates and averaging or otherwise integrating over the remaining covariates. The margins command estimates margins of responses for specified values of covariates and presents the results as a table. Capabilities include estimated marginal means, least-squares means, average and conditional marginal and partial effects (which may be reported as derivatives or as elasticities), average and conditional adjusted predictions, and predictive margins.

Is there any similar function in R?

1
There is no reason not to cite the latest manual edition (14, as I write).Nick Cox
start by checking out the effects and lsmeans packagesBen Bolker

1 Answers

8
votes

I have produced an R port of Stata's margins command called - intuitively - margins. Behavior is what you would probably expect:

library("margins")
x <- lm(mpg ~ cyl * hp + wt, data = mtcars)
summary(margins(x))
##  factor     AME     SE       z      p   lower   upper
##     cyl  0.0381 0.5999  0.0636 0.9493 -1.1376  1.2139
##      hp -0.0463 0.0145 -3.1909 0.0014 -0.0748 -0.0179
##      wt -3.1198 0.6613 -4.7176 0.0000 -4.4160 -1.8236

Compared to Stata:

quietly reg mpg c.cyl##c.hp wt
margins, dydx(*)
------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         cyl |   .0381376   .5998897     0.06   0.950    -1.192735     1.26901
          hp |  -.0463187    .014516    -3.19   0.004     -.076103   -.0165343
          wt |  -3.119815    .661322    -4.72   0.000    -4.476736   -1.762894
------------------------------------------------------------------------------