繁体   English   中英

在R中使用confint计算固定效果的CI

[英]Calculating CIs of fixed effects using confint in R

我想执行自举以在二项式GLMM中获得我固定效果的95%顺式:

m <- glmer(cbind(df$Valid.detections, df$Missed.detections) ~ distance + 
              Habitat + Replicate + transmitter.depth + receiver.depth + 
              wind.speed + wtc + Transmitter + (1 | Unit) + 
              (1 | SUR.ID) + distance:Transmitter + 
              distance:Habitat + distance:transmitter.depth + distance:receiver.depth + 
              distance:wind.speed, data = df, family = binomial(link=logit),control=glmerControl(calc.derivs=F))

我发现confint()函数可以实现此目的,因此我指定了该函数:

confint(m, method = "boot", boot.type = "basic", seed = 123, nsim = 1000)

在我决定终止之前,该功能已经运行了8个多小时。 终止后,我返回了以下警告消息(x10):

Warning messages:
1: In (function (fn, par, lower = rep.int(-Inf, n), upper = rep.int(Inf,  :
  failure to converge in 10000 evaluations

我的问题是:1)我是否需要担心这些警告消息? 如果是这样,我该如何处理?,2)因为8个小时后它仍在运行,所以我不知道执行此功能需要多长时间。 因此,在执行此功能时最好具有某种进度条。 我读到confint()可以从bootMer中获取参数,因此我加入了参数.progress =“ txt”,结果是:

confint(m, method = "boot", boot.type = "basic", seed = 123, nsim = 1000, .progress = "txt")

但它似乎不起作用。 另外,如果有更好的方法可以实现相同的目标,我欢迎您提出建议。

谢谢你的帮助

这是一个例子:

library("lme4")
(t1 <- system.time(
    gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
                 data = cbpp, family = binomial)))
##    user  system elapsed 
##   0.188   0.000   0.186

nranpars <- length(getME(gm1,"theta"))
nfixpars <- length(fixef(gm1))

(t2 <- system.time(c1 <- confint(gm1,method="boot", nsim=1000,
                  parm=(nranpars+1):(nranpars+nfixpars),
                  .progress="txt")))

##    user  system elapsed 
## 221.958   0.164 222.187

请注意,此进度条只有80个字符长,因此仅在每次1000/80 = 12个引导程序迭代后才递增。 如果您的原始模型花了一个小时才能适应,那么您不应该期望在12小时后看到任何进度栏活动...

(t3 <- system.time(c2 <- confint(gm1,
                  parm=(nranpars+1):(nranpars+nfixpars))))

##    user  system elapsed 
##   5.212   0.012   5.236 

1000个引导程序代表可能会过大-如果模型拟合缓慢,则200个引导程序代表可能会获得合理的配置项。

我也尝试了optimizer="nloptwrap" ,希望它可以加快速度。 确实做到了,尽管有一个缺点(见下文)。

(t4 <- system.time(
    gm1B <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
                 data = cbpp, family = binomial, 
                 control=glmerControl(optimizer="nloptwrap"))))
##   user  system elapsed 
##  0.064   0.008   0.075 

(t5 <- system.time(c3 <- confint(gm1B,method="boot", nsim=1000,
                  parm=(nranpars+1):(nranpars+nfixpars),
                  .progress="txt",PBargs=list(style=3))))
##
##   user  system elapsed 
## 65.264   2.160  67.504

这要快得多, 但是会发出有关收敛的警告(在本例中为37)。 根据all.equal() ,以这种方式计算的置信区间只有大约2%的差异。 (包装中仍有一些皱纹可以整理出来...)

加快速度的最佳选择是并行化-不幸的是,这样会使您失去使用进度条的能力...

(t6 <- system.time(c4 <- confint(gm1,method="boot", nsim=1000,
                  parm=(nranpars+1):(nranpars+nfixpars),
                  parallel="multicore", ncpus=4)))

## 
##     user  system elapsed 
##  310.355   0.916 116.917

这会花费更多的用户时间(它计算了所有内核上使用的时间),但是将使用时间减少了一半。 (最好用4个内核来做得更好,但是速度还是要快两倍。这些是虚拟Linux机器上的虚拟内核,实际(非虚拟)内核可能会有更好的性能。)

结合使用nloptwrap和多核,我可以将时间缩短到91秒(用户)/ 36秒(已用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM