Search code examples
rglmconvergence

Catching the convergence or not by glmer in R


I am doing simulations with glmer function. For every simulation I extract estimates,...into a database, but I also want to have a variable that would indicate whether simulated data converged properly or not. (I get warnings,for ex. singular convergence, false convergence,...but the estimates are given anyway).

I try

assign("last.warning", NULL, envir = baseenv()) # clear the previous warning
mod1=glmer(y~x+(1+x|study), family="binomial", data=test1)
warningss1=ifelse(length(warnings())>0, "yes", "no"); warningss1

It will always return me no even it divergent`


Solution

  • I wouldn't bang my head against the general mechanism for warnings, any more than we both already have done, anyway. There's no way that I can find to zero out or reset the warnings log. It's fairly effectively hidden away. Instead look at the object, say its name is gm1, you get with failure to converge. (I just reduced the sample size until convergence failure occurred.):

        gm1@optinfo$conv$lme4$messages
    #[1] "Model failed to converge with max|grad| = 0.10941 (tol = 0.001, component 5)"
    #[2] " Hessian is numerically singular: parameters are not uniquely determined"   
    
    any( grepl("failed to converge", gm1@optinfo$conv$lme4$messages) )
    #[1] TRUE
    
    #with a convergent run:
    > any( grepl("failed to converge", gm1@optinfo$conv$lme4$messages) )
    #[1] FALSE 
    >   gm1@optinfo$conv$lme4$messages
    #NULL