2014-10-28 88 views
0

我想進行引導,以獲得我的固定效應的95%CI在二項式GLMM:計算的R中使用confint固定效應的CI

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") 

,但它似乎並沒有工作。或者,如果有更好的方法來實現相同的目標,我願意接受建議。

感謝所有幫助

+0

在倒數第二句中,「似乎不起作用」是什麼意思?你需要加載'plyr'包 - 抱歉,如果不清楚。概況置信區間會更快,並且可能足夠準確。 – 2014-10-28 16:53:51

+0

你也可以使用'parm'參數來限制你正在分析哪些參數(即跳過隨機效果參數......) – 2014-10-28 17:16:59

+0

你可以增加最大迭代次數(參見'?glmerControl')以擺脫警告。您應該期望自舉時間與樣本數大致呈線性關係(即,只要首先擬合模型,1000個自舉樣本應該取1000倍的數量級,儘管內置了一些技巧以使其成爲快一點)。 – 2014-10-28 17:18:53

回答

3

下面是一個例子:

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機器上的虛擬內核,真實(非虛擬)內核可能會有更好的性能。)

With nloptwrap and multicore結合我可以把時間縮短到91秒(用戶)/ 36秒(經過)。

+0

謝謝你本人,我試着用200個引導程序而不是1000個引導程序代碼(引導程序的數量是基於我讀的某篇文章的)。 – FlyingDutch 2014-10-29 09:41:33

+0

搜索完模型後返回警告消息後,我遇到http://stackoverflow.com/questions/19478686/increase-iterations-for-new-version-of-lmer並將迭代次數調整爲20000,警告消失了。 – FlyingDutch 2014-10-29 09:59:51

+0

當我運行你的或我的代碼(這並不重要),以適應與nloptr優化器模型我得到以下警告消息:警告消息: 1:在數據(「nloptr.default.options」,包=「 nloptr「,envir = ee): 找不到數據集'nloptr.default.options'。有任何想法嗎? – FlyingDutch 2014-10-29 11:03:24