Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display all levels of factor (including intercept) #120

Closed
kaiemjoy opened this Issue Mar 12, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@kaiemjoy
Copy link

kaiemjoy commented Mar 12, 2019

Hi @leeper, Thank you for your terrific work developing this package.

A question: Is there a way to display the marginal predictions and SEs for all levels of a factor? This is the default in Stata. Example using mtcars below:

In Stata:

use mtcars.dta

logit am i.cyl

margins cyl

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         cyl |
          4  |   .7272727   .1342816     5.42   0.000     .4640856    .9904599
          6  |   .4285714   .1870439     2.29   0.022     .0619721    .7951707
          8  |   .1428571    .093522     1.53   0.127    -.0404425    .3261568
------------------------------------------------------------------------------

in R:

## load package
library(margins)

## 
fit <- glm(am ~ factor(cyl), data = mtcars, family=binomial(link=logit))
summary(margins(fit))

> summary(margins(fit))
 factor     AME     SE       z      p   lower   upper
   cyl6 -0.2987 0.2303 -1.2973 0.1945 -0.7500  0.1526
   cyl8 -0.5844 0.1636 -3.5714 0.0004 -0.9051 -0.2637
@leeper

This comment has been minimized.

Copy link
Owner

leeper commented Mar 13, 2019

Keep in mind what Stata's margins command is doing by default (calculating "predictive margins"). The specific command you've typed is equivalent to margins, over(cyl) which is also a subsetting operation. This package calculates "marginal effects" (the equivalent of Stata's margins, dydx(*) command). This means the effect on the outcome of changing from the baseline/reference/intercept to a given value of the independent variable.

The "prediction" package provides functionality akin to margins, at(...):

> library("prediction")
> summary(prediction(fit))
 Prediction      SE     z         p  lower  upper
     0.4062 0.07402 5.488 4.056e-08 0.2612 0.5513
> summary(prediction(fit, at = list(cyl = c("4", "6", "8"))))
 at(cyl) Prediction     SE     z         p   lower  upper
       4     0.7273 0.1187 6.127 8.942e-10  0.4946 0.9599
       6     0.4286 0.1339 3.201 1.372e-03  0.1661 0.6910
       8     0.1429 0.1339 1.067 2.860e-01 -0.1196 0.4053

which is not a subsetting operation but provides similar inference to your example Stata code.

@leeper leeper added the question label Mar 13, 2019

@kaiemjoy

This comment has been minimized.

Copy link
Author

kaiemjoy commented Mar 14, 2019

Thanks for clarifying, this is a big help. I'm trying to get predicted SEs from a glmer model on the probability scale. I noticed that "prediction" will compute the fit for merMod object but not the SEs. Any suggestions on where to turn for the SEs?

Thank you

@leeper

This comment has been minimized.

Copy link
Owner

leeper commented Mar 14, 2019

There are some suggestions here: leeper/prediction#9

@kaiemjoy

This comment has been minimized.

Copy link
Author

kaiemjoy commented Mar 16, 2019

Thanks, appreciate it.

The "emmeans" package is also working

@leeper leeper closed this Mar 17, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.