Reputation: 1303
I'm wondering how to obtain a contrast output (for a custom set of contrasts) as a difference in probabilities rather then a difference in log-odds or the odds ratio. I know how to do this for ALL comparisons in emmeans by specifying regrid()
prior to a pairs()
call, but is there a way to do this for only certain contrasts?
Reproducible code:
data("PimaIndiansDiabetes2", package = "mlbench")
PimaIndiansDiabetes2 <- na.omit(PimaIndiansDiabetes2)
# Convert a couple of numerical variables to binary variables
# Make pregnant binary (0 = 0, ≥ 1 = 1)
PimaIndiansDiabetes2$preg_cat <- factor(ifelse(PimaIndiansDiabetes2$pregnant == 0, "0", "≥1"), levels = c("0", "≥1"))
# Make pressure binary (< 80 = low, ≥ 80 = high)
PimaIndiansDiabetes2$press_cat <- factor(ifelse(PimaIndiansDiabetes2$pressure < 80, "low", "high"), levels = c("low", "high"))
# Logistic model
mod <- glm(diabetes ~ preg_cat + press_cat + glucose, family = binomial, data = PimaIndiansDiabetes2)
summary(mod)
library(emmeans)
# Get means for 4 combinations of pregnancy and pressure categories (setting glucose to 100)
(emm <- emmeans(mod, ~ preg_cat + press_cat, at = list(glucose = 100))) # log-odds
emmeans(mod, ~ preg_cat + press_cat, at = list(glucose = 100), type = "response") # probabilities
# Now say we want to get the 'effect' of preg_count, stratified by press_cat (and keeping glucose = 100)
custom <- list(`preg_count effect for press_cat = low` = c(-1,1,0,0),
`preg_count effect for press_cat = high` = c(0,0,-1,1))
# Effect on log-odds scale - note they are the same as this scale is additive (linear)
contrast(emm, custom) |>
summary(infer = T)
# Effect on probability scale ???? - note they are NOT the same as this scale is multiplicative (non-linear)
contrast(emm, custom) |>
regrid() |>
summary(infer = T)
# The above doesn't work and only generates output as an odds-ratio
# I can get diffs in probabilities for ALL pairwise comparisons using:
emmeans(mod, ~ preg_cat + press_cat, at = list(glucose = 100)) |>
regrid() |> pairs()
# But I only want the comparisons in the custom contrast list
Upvotes: 2
Views: 325
Reputation: 6810
You need to regrid before calling contrast()
.
contrast(regrid(emm), custom)
Upvotes: 2