0

I'm running 1000 models in a loop, but some are not converging. Does anyone know the code to save these errors, so I can look into them? Basically I have a model form that runs 1000 times based on a dataframe, and then I save the model coefficients. Though I get all 1000 model coefficients, I do get an error at the end that some did not converge.

Code:

 for(i in 1:n_rand){
   print(i)
   modform <- formula(paste("RESPONSE ~",
        colnames(dataframe)[(11+i)],"+(1|X)+(1|Y)"))
   aaa <- lmer(modform, data=dataframe)
   nullcoef_tab$COEFF[i] <- fixef(aaa)[2]
}
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
skotani
  • 3
  • 1
  • this is a duplicate of https://stackoverflow.com/questions/63794583/how-to-get-convergence-error-messages-or-max-gradient-in-lme4/63795367#63795367 , https://stackoverflow.com/questions/64114841/capturing-convergence-message-from-lme4-package-in-r/64115175#64115175 – Ben Bolker Feb 15 '22 at 01:00

1 Answers1

0

I'm not sure I have an example handy that has the right kind of convergence problems, but you should look at the @optinfo$conv component of your fitted models:

library(lme4)
data(Orthodont, package = "nlme")
fm1 <- lmer(distance ~ age + (age|Subject) + (0+nsex|Subject) +
   (0 + nsexage|Subject), data=Orthodont)
fm1@optinfo$conv

Results:

$opt
[1] 0  ## convergence code from optimizer

$lme4
$lme4$messages
[1] "boundary (singular) fit: see help('isSingular')"
fm2 <- lmer(distance ~ age + (age*nsex|Subject), Orthodont, 
    control = lmerControl(check.nobs.vs.nRE = "ignore"))
fm2@optinfo$conv
$opt
[1] 0

$lme4
$lme4$code
[1] -3

$lme4$messages
[1] "unable to evaluate scaled gradient"                                       
[2] "Model failed to converge: degenerate  Hessian with 4 negative eigenvalues"
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453