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.
Aren't you just looking for
marginaleffects::marginal_means
?Note that in any case you don't need to define
invlogit
; it does the same thing as base R'splogis