Reputation: 1489
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
Upvotes: 1
Views: 1242
Reputation: 226662
Following on from @RussLenth's answer:
contrast(EMM, CON)
gives you log(o1) - log(o3) - log(o5) + log(o7)
, and it is indeed equal to the 7th coefficient value.contrast(REMM, CON)
(i.e. regridded) gives you p1 - p3 - p5 - p7
.(where oi
= odds in group i, pi
= probability in group i, oi = pi/(1-pi)
).
While it is possible to convert an individual log-odds value to a probability (if loi
is the log-odds value, then pi = 1/(1+exp(-loi))
), I don't think there's any way to convert a linear combination of log-odds values directly to the corresponding linear combination of probability values; instead, you'd have to do what emmeans
is doing anyway — i.e., convert the individual log-odds values to the probability scale and then compute the linear combination. (In fact emmeans
is going the other direction — computing the probabilities from the log-odds when you specify regridding.)
Your question also reveals a misunderstanding of what's going on here: you say
apparently outputs the 7th coefficient on a different scale, the response scale
The package does not output the 7th coefficient on the response scale; instead, it applies the same contrast that would give the 7th coefficient on the log scale to the group values on the probability scale. "[T]he 7th coefficient on the response scale" would be
L(log(o1) - log(o3) - log(o5) + log(o7))
(where L(x)
is the logistic function 1/(1+exp(-x))
[plogis()
in R]). This is not the same as
L(log(o1)) - L(log(o3)) - L(log(o5)) + L(log(o7))
which is what emmeans
gives you.
Upvotes: 3
Reputation: 6800
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.
summary(EMM)
, you will get estimates of logit(p1), logit(p2), ..., logit(p8)
.summary(EMM, type = "response")
, you will get estimates of p1, p2, ..., p8
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.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
summary(REMM)
, you get estimates of p1, p2, ..., p8
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.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.
Upvotes: 2