3

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`

Jacob
  • 33
  • 4

2 Answers2

5

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
IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

I was looking for a solution to the same problem and stumbled over this tread. After some more researching I found that the following works (at least for glm-models):

obj <- glm(y ~ x, ...) # model specification
if (obj$converged) { 
# do stuff
} else { 
# do something else 
}
user605364
  • 11
  • 2