rmarginal-effectsr-marginaleffects

Predicted probabilities with the marginaleffects package


I'm looking for the most user-friendly way of calculating predicted probabilities with confidence intervals from a logistic regression model using the marginaleffects package in R.

Let's consider a toy model:

mod <- glm(am ~ factor(cyl), data = mtcars, family = binomial)

I could do

invlogit <- \(x) exp(x)/(1+exp(x))
hypotheses(mod, hypothesis = c("invlogit(`(Intercept)`) = 0",
                               "invlogit(`(Intercept)` + `factor(cyl)6`) = 0",
                               "invlogit(`(Intercept)` + `factor(cyl)8`) = 0"))

to get the predicted probabilities in the three groups given by the cyl-variable with corresponding confidence intervals (yeah, I know they're bad here because of the low sample, but let's forget about that).

Is there some other way of doing this where I don't have to make the invlogit-function and perhaps where the specification of the three groups is done a bit easier. With Stata's margins function you can simply write margins cyl or margins var1#var2 to get the predicted probabilities for each combination of two categorical variables.


Solution

  • Aren't you just looking for marginaleffects::marginal_means?

    mod <- glm(am ~ factor(cyl), data = mtcars, family = binomial)
    marginaleffects::marginal_means(mod)
    
    #> Term Value  Mean Pr(>|z|) 2.5 % 97.5 %
    #>  cyl     4 0.727    0.147 0.414  0.910
    #>  cyl     6 0.429    0.706 0.144  0.770
    #>  cyl     8 0.143    0.019 0.036  0.427
    #>
    #> Results averaged over levels of: cyl 
    #> Columns: rowid, term, value, cyl, estimate, p.value, conf.low, conf.high, am, wts 
    

    Note that in any case you don't need to define invlogit; it does the same thing as base R's plogis