ciTools icon indicating copy to clipboard operation
ciTools copied to clipboard

Error with add_ci() fed with a glmer(...,family=Gamma) model

Open NicolasDeguines opened this issue 4 years ago • 3 comments

Dear John,
I am running GLMM models with family=Gamma and I want to use add_ci() to compute confidence intervals around predictions (many thanks for developing this by the way).
Unfortunately, add_ci() often encounters the following error: Error in quantile.default(newX[, i], ...) : missing values and NaN's not allowed if 'na.rm' is FALSE

often but not always; I can re-run add_ci() and it might fail or not.

And it seems to be associated with this type of warning message: In lme4::bootMer(fit, my_pred, nsim = nSims, type = "parametric", : some bootstrap runs failed (1/100)

Here is the code I’m running: modfinal<-glmer(cumulative_duration ~ winter + month + s_cumul_dura_small + big_treat + (1 | id_first_feederpair), data=data_FRICOE, family = Gamma(link = "log")), with cumulative_duration being the time (in seconds) spent by the common chaffinch (Fringilla coelebs) on a feeder.

newdatpred <- expand.grid(winter=as.factor("4") ,month=as.factor("Jan") ,s_cumul_dura_small=0 ,big_treat=as.factor(c("NONE","PICPIC")) ,id_first_feederpair=as.factor("0060564"))

add_ci(newdatpred,modfinal,alpha=0.01,response=T,includeRanef=F,type="boot",nSims=100,names=c("lowBCI","uppBCI")) (Note: nSims set to 100 for speed reason, but planning on using 1000 for final results)

I hope I'm giving enough useful details on this. I'm happy to share more as needed. I attach the data table data_FRICOE.txt if it can help reproduce this error (several runs of add_ci() might be required given it does not always yield this error) data_FRICOE.txt

NicolasDeguines avatar Sep 26 '19 12:09 NicolasDeguines

Hi, I learned from a colleague that lme4's bootMer function has changed recently and causes some problems for some people.

More importantly, I'm unable to fit your model, which is a prerequisite for honest interval estimates. I tried

library(ciTools)
library(tidyverse)
library(lme4)

dat <- read_tsv("data_FRICOE.txt")
dat$month <- as.factor(dat$month)
dat$big_treat <- as.factor(dat$big_treat)
dat$id_first_feederpair <- as.factor(dat$id_first_feederpair)

modfinal<-glmer(cumulative_duration ~ winter + month + s_cumul_dura_small + big_treat + (1 | id_first_feederpair), data=dat, family = Gamma(link = "log"))

which yields

Warning message:
In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv,  :
  Model failed to converge with max|grad| = 0.0152259 (tol = 0.001, component 1)

Edit: Whoops, didn't notice winter is a factor. The model fits now and offers the following parameter estimates:

Random effects:
 Groups              Name        Std.Dev.
 id_first_feederpair (Intercept) 0.8468  
 Residual                        0.9532  
Number of obs: 329, groups:  id_first_feederpair, 22
Fixed Effects:
       (Intercept)             winter5            monthFeb            monthJan  
           4.15943             0.18107             0.55187             0.23501  
s_cumul_dura_small     big_treatPICPIC  
           0.07051            -0.10886  

Does that match you?

jthaman avatar Oct 05 '19 22:10 jthaman

Yes, perfect match! Sorry I forgot to mention I was treating winter and month as factor variables: data_FRICOE$winter<-factor(data_FRICOE$winter,levels=c("4","5")) data_FRICOE$month<-factor(data_FRICOE$month,levels=c("Dec","Jan","Feb")) data_FRICOE$big_treat<-factor(data_FRICOE$big_treat,levels=c("NONE","PICPIC"))

NicolasDeguines avatar Oct 07 '19 10:10 NicolasDeguines

Hi there, I started having this exact problem - same error with a gamma-family log-link model, a handful of bootstrap runs failing. I got it to work once just randomly but every other time it's failed. Did you ever stumble on a fix or work-around?

incoreygible avatar Jul 10 '23 19:07 incoreygible