Search code examples
rregressionglmlme4emmeans

converting the glmer output from logit to response scale


I hoping get some can help solving this mystery in my mind. The 7th coefficient in my glmer() call is 0.28779305 on the logit scale.

This coefficient can be also obtained by using contrast() in the emmeans package. However, this package apparently outputs the 7th coefficient on a different scale, the response scale.

I wonder how to convert the estimate given by the contrast() call so it matches the 7th coefficient in my glmer() call?

ps. This answer provides some insight but I don't see a way the coefficient from these two packages might be related.

library(lme4)
library(emmeans)
library(broom.mixed)

dat <- read.csv("https://raw.githubusercontent.com/fpqq/w/main/d.csv")

form2 <- y ~ item_type*time + (1 | user_id)

m2 <- glmer(form2, family = binomial, data = dat,
            control =
              glmerControl(optimizer = "bobyqa"))

coef(summary(m2))[7,]
#  Estimate Std. Error    z value   Pr(>|z|)   # This Estimate 
#0.28779305 0.11271202 2.55334842 0.01066927   # is on logit scale
#------------------------------------------------------------------
EMM <- emmeans(m2, ~ item_type * time)

CON <- list(c1 = c(1, 0, -1, 0, -1, 0, 1, 0))

contrast(regrid(EMM), CON)
# contrast estimate     SE  df z.ratio p.value # This Estimate
# c1          0.106 0.0299 Inf 3.526   0.0004  # is on response scale

Solution

  • The bottom line is that if you do

    contrast(EMM, CON)
    

    then maybe you'll get that coefficient you are asking about. That's assuming that the 7th coefficient is correctly identified.

    The EMM object contains information about estimates of 8 different probabilities (call them p1, p2, ..., p8), on the logit scale.

    • If you do summary(EMM), you will get estimates of logit(p1), logit(p2), ..., logit(p8).
    • If you do summary(EMM, type = "response"), you will get estimates of p1, p2, ..., p8
    • If you do contrast(EMM, CON) you will get an estimate of logit(p1) - logit(p3) - logit(p5) + logit(p7) = log(o1) - log(o3) - log(o5) + log(o7), where oj = pj / (1 - pj) is the odds for the jth case.
    • If you do contrast(EMM, CON, type = "response"), you get an estimate of exp(log(o1) - log(o3) - log(o5) + log(o7)) = (o1*o7) / (o3*o5)

    Now, as documented, REMM = regrid(EMM) undoes the logit transformation once and for all. It preserves no memory of where it came from, it just has information about estimates on the response scale and their covariance matrix. Thus

    • If you do summary(REMM), you get estimates of p1, p2, ..., p8
    • If you do summary(REMM, type = "response"), you get estimates of p1, p2, ..., p8. There is no transformation information in REMM, it is already on the response scale.
    • If you do contrast(REMM, CON) (or contrast(REMM, CON, type = "response")), you get an estimate of p1 - p3 - p5 + p7.

    The emmeans package documents all of this, and in addition contains several vignettes with examples. In particular, the one on transformations and the one on comparisons and contrasts are especially pertinent here.