glmer logit - 概率尺度上的交互效应(用 `predict` 复制`effects`)
Posted
技术标签:
【中文标题】glmer logit - 概率尺度上的交互效应(用 `predict` 复制`effects`)【英文标题】:glmer logit - interaction effects on probability scale (replicating `effects` with `predict`) 【发布时间】:2017-08-19 10:08:03 【问题描述】:我正在使用 lme4 包运行 glmer logit 模型。我对各种二向和三向交互效应及其解释感兴趣。为简化起见,我只关心固定效应系数。
我设法想出了一个代码来计算并在 logit 尺度上绘制这些影响,但我无法将它们转换为预测的概率尺度。最终我想复制effects
包的输出。
该示例依赖于UCLA's data on cancer patients。
library(lme4)
library(ggplot2)
library(plyr)
getmode <- function(v)
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
facmin <- function(n)
min(as.numeric(levels(n)))
facmax <- function(x)
max(as.numeric(levels(x)))
hdp <- read.csv("http://www.ats.ucla.edu/stat/data/hdp.csv")
head(hdp)
hdp <- hdp[complete.cases(hdp),]
hdp <- within(hdp,
Married <- factor(Married, levels = 0:1, labels = c("no", "yes"))
DID <- factor(DID)
HID <- factor(HID)
CancerStage <- revalue(hdp$CancerStage, c("I"="1", "II"="2", "III"="3", "IV"="4"))
)
到这里为止,就是我需要的所有数据管理、功能和包。
m <- glmer(remission ~ CancerStage*LengthofStay + Experience +
(1 | DID), data = hdp, family = binomial(link="logit"))
summary(m)
这是模型。这需要一分钟,然后会出现以下警告:
Warning message:
In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
Model failed to converge with max|grad| = 0.0417259 (tol = 0.001, component 1)
尽管我不太确定是否应该担心警告,但我还是使用估计来绘制感兴趣的交互作用的平均边际效应。首先,我准备要输入 predict
函数的数据集,然后使用固定效应参数计算边际效应和置信区间。
newdat <- expand.grid(
remission = getmode(hdp$remission),
CancerStage = as.factor(seq(facmin(hdp$CancerStage), facmax(hdp$CancerStage),1)),
LengthofStay = seq(min(hdp$LengthofStay, na.rm=T),max(hdp$LengthofStay, na.rm=T),1),
Experience = mean(hdp$Experience, na.rm=T))
mm <- model.matrix(terms(m), newdat)
newdat$remission <- predict(m, newdat, re.form = NA)
pvar1 <- diag(mm %*% tcrossprod(vcov(m), mm))
cmult <- 1.96
## lower and upper CI
newdat <- data.frame(
newdat, plo = newdat$remission - cmult*sqrt(pvar1),
phi = newdat$remission + cmult*sqrt(pvar1))
我相当有信心这些是 logit 量表上的正确估计,但也许我错了。总之,剧情是这样的:
plot_remission <- ggplot(newdat, aes(LengthofStay,
fill=factor(CancerStage), color=factor(CancerStage))) +
geom_ribbon(aes(ymin = plo, ymax = phi), colour=NA, alpha=0.2) +
geom_line(aes(y = remission), size=1.2) +
xlab("Length of Stay") + xlim(c(2, 10)) +
ylab("Probability of Remission") + ylim(c(0.0, 0.5)) +
labs(colour="Cancer Stage", fill="Cancer Stage") +
theme_minimal()
plot_remission
我认为现在 OY 量表是在 logit 量表上测量的,但为了理解它,我想将其转换为预测概率。基于wikipedia,类似exp(value)/(exp(value)+1)
的东西应该可以达到预测概率。虽然我可以做到 newdat$remission <- exp(newdat$remission)/(exp(newdat$remission)+1)
我不确定我应该如何为置信区间做到这一点?
最终我想得到effects
包生成的相同情节。那就是:
eff.m <- effect("CancerStage*LengthofStay", m, KR=T)
eff.m <- as.data.frame(eff.m)
plot_remission2 <- ggplot(eff.m, aes(LengthofStay,
fill=factor(CancerStage), color=factor(CancerStage))) +
geom_ribbon(aes(ymin = lower, ymax = upper), colour=NA, alpha=0.2) +
geom_line(aes(y = fit), size=1.2) +
xlab("Length of Stay") + xlim(c(2, 10)) +
ylab("Probability of Remission") + ylim(c(0.0, 0.5)) +
labs(colour="Cancer Stage", fill="Cancer Stage") +
theme_minimal()
plot_remission2
尽管我可以只使用 effects
包,但遗憾的是它无法与我必须为自己的工作运行的许多模型一起编译:
Error in model.matrix(mod2) %*% mod2$coefficients :
non-conformable arguments
In addition: Warning message:
In vcov.merMod(mod) :
variance-covariance matrix computed from finite-difference Hessian is
not positive definite or contains NA values: falling back to var-cov estimated from RX
解决这个问题需要调整估算程序,目前我想避免这种情况。另外,我也很好奇 effects
在这里实际做了什么。
如果有任何关于如何调整我的初始语法以达到预测概率的建议,我将不胜感激!
【问题讨论】:
我认为如果你这样做,你的情节会更容易阅读:ggplot(newdat, aes(LengthofStay, fill=factor(CancerStage), color=factor(CancerStage))) + geom_ribbon(aes(ymin=plo, ymax=phi), colour=NA, alpha=0.2) + geom_line(aes(y = remission), size=1.2) + xlab("Length of Stay") + ylab("Probability of Remission") + labs(colour="Cancer Stage", fill="Cancer Stage") + theme_minimal()
你绝对应该担心收敛警告。
我真的不明白为什么这是一个不可能回答的问题......我要求的内容是否不清楚?
我同意@JacobSocolar 的观点。我认为您的模型不收敛的事实将导致虚假的模型估计。所以要小心。
当然,谢谢!但这是一个相当侧面的观点。如何使用基于predict
的初始语法来绘制反映预测概率的图?
【参考方案1】:
要获得与您的问题中提供的effect
函数类似的结果,您只需使用您提供的转换将预测值和置信区间的边界从 logit 标度反向转换为原始标度: exp(x)/(1+exp(x))
。
这个转换可以用plogis
函数在base R中完成:
> a <- 1:5
> plogis(a)
[1] 0.7310586 0.8807971 0.9525741 0.9820138 0.9933071
> exp(a)/(1+exp(a))
[1] 0.7310586 0.8807971 0.9525741 0.9820138 0.9933071
所以使用来自@eipi10 的建议,使用丝带代替虚线(我也发现这个演示文稿更具可读性):
ggplot(newdat, aes(LengthofStay, fill=factor(CancerStage), color=factor(CancerStage))) +
geom_ribbon(aes(ymin = plogis(plo), ymax = plogis(phi)), colour=NA, alpha=0.2) +
geom_line(aes(y = plogis(remission)), size=1.2) +
xlab("Length of Stay") + xlim(c(2, 10)) +
ylab("Probability of Remission") + ylim(c(0.0, 0.5)) +
labs(colour="Cancer Stage", fill="Cancer Stage") +
theme_minimal()
结果是一样的(effects_3.1-2
和lme4_1.1-13
):
> compare <- merge(newdat, eff.m)
> compare[, c("remission", "plo", "phi")] <-
+ sapply(compare[, c("remission", "plo", "phi")], plogis)
> head(compare)
CancerStage LengthofStay remission Experience plo phi fit se lower upper
1 1 10 0.20657613 17.64129 0.12473504 0.3223392 0.20657613 0.3074726 0.12473625 0.3223368
2 1 2 0.35920425 17.64129 0.27570456 0.4522040 0.35920425 0.1974744 0.27570598 0.4522022
3 1 4 0.31636299 17.64129 0.26572506 0.3717650 0.31636299 0.1254513 0.26572595 0.3717639
4 1 6 0.27642711 17.64129 0.22800277 0.3307300 0.27642711 0.1313108 0.22800360 0.3307290
5 1 8 0.23976445 17.64129 0.17324422 0.3218821 0.23976445 0.2085896 0.17324530 0.3218805
6 2 10 0.09957493 17.64129 0.06218598 0.1557113 0.09957493 0.2609519 0.06218653 0.1557101
> compare$remission-compare$fit
[1] 8.604228e-16 1.221245e-15 1.165734e-15 1.054712e-15 9.714451e-16 4.718448e-16 1.221245e-15 1.054712e-15 8.326673e-16
[10] 6.383782e-16 4.163336e-16 7.494005e-16 6.383782e-16 5.689893e-16 4.857226e-16 2.567391e-16 1.075529e-16 1.318390e-16
[19] 1.665335e-16 2.081668e-16
置信边界之间的差异更高但仍然很小:
> compare$plo-compare$lower
[1] -1.208997e-06 -1.420235e-06 -8.815678e-07 -8.324261e-07 -1.076016e-06 -5.481007e-07 -1.429258e-06 -8.133438e-07 -5.648821e-07
[10] -5.806940e-07 -5.364281e-07 -1.004792e-06 -6.314904e-07 -4.007381e-07 -4.847205e-07 -3.474783e-07 -1.398476e-07 -1.679746e-07
[19] -1.476577e-07 -2.332091e-07
但是,如果我使用正态分布的实分位数cmult <- qnorm(0.975)
而不是cmult <- 1.96
,我也会在这些边界上获得非常小的差异:
> compare$plo-compare$lower
[1] 5.828671e-16 9.992007e-16 9.992007e-16 9.436896e-16 7.771561e-16 3.053113e-16 9.992007e-16 8.604228e-16 6.938894e-16
[10] 5.134781e-16 2.289835e-16 4.718448e-16 4.857226e-16 4.440892e-16 3.469447e-16 1.006140e-16 3.382711e-17 6.765422e-17
[19] 1.214306e-16 1.283695e-16
【讨论】:
谢谢!这很有帮助!不幸的是,尽管这两个图之间仍然存在细微差别,但我将它们带到了相同的比例,因此它在曲线中可见(我添加了xlim
和ylim
)。您还可以看到差异,例如compare <- merge(newdat, eff.m) head(compare) compare$remission-compare$fit
确实,在这个例子中差异非常小,但我想了解偏差来自哪里,所以我可以在我的研究中消除它。 PS:我编辑了这些图并添加了plyr
包。感谢您的回答!
查看编辑后的回复。我无法复制任何显着差异。也许软件包版本有所不同?注意,您还应该在代码中添加library(effects)
并删除您的第一个图的ylim
(此图在 logit 标度上,因此 0,0.5 限制超出了图的范围)以上是关于glmer logit - 概率尺度上的交互效应(用 `predict` 复制`effects`)的主要内容,如果未能解决你的问题,请参考以下文章
如何绘制新数据的预测与 R 中的 gee、lme、glmer 和 gamm4 相匹配?
如何使用超过 5000 万个观测值的样本计算具有固定效应的 logit 模型的边际效应
固定效应 logit:R 中调整的 r square-bife 包
为啥在 sotfmax_cross_entropy_with_logits 中将 logit 解释为“未缩放的对数概率”?