Skip to content
Prev 16380 / 20628 Next

prevent for-loop from stopping after an lmer warning/error

Hi Ben,


Thank you for pointing this out. Indeed the loops only stops after errors, not warnings.


By the way, I am still stuck with my issue. I've made some progress with using tryCatch() as suggested, but still have not resolved my issue. As far as I can tell, currently there are two issues with my code. First, when I query the returned exception in `tryCatch()`, e.g. `poss.err.mod1` I get the following error:

    <simpleError in tryCatchList(expr, classes, parentenv, handlers): argument "expr" is missing, with no default>

Still, the warnings are correctly written to file, which is puzzling. Second, when no error is detected, the code seems to be ignored (i.e. no model summaries are written to file). So the loop is running vacuously, skipping errors but apparently handling them and doing nothing else. If anyone has any advice on how to debug this, please let me know!

    # divert console stream to file
    options(warn=1)
    wngs=file("warnings_log.txt",open="w+",blocking=TRUE)
    sink(wngs,type="message")

    for(iter in 1:1000){

      data = read.csv(paste("sample", iter,".csv", sep=""), header=TRUE)

      #error handling case #1
      poss.err.mod1 = tryCatch(

        lmer(y ~ x1 + (1|ranef), data = data, REML = FALSE), #try part
        error=function(e) e #catch part
      )

      #error handling case #2
      poss.err.mod2 = tryCatch(

        lmer(y ~ x1 + x2 + (1|ranef), data = data, REML = FALSE), #try part
        error=function(e) e #catch part
      )

      #real work #1
      if(!inherits(poss.err.mod1, "error")){

        mod1 = lmer(y ~ x1 + (1|ranef1), data = data, REML = FALSE)
        write.csv(summmary$mod2, paste("mod2.sum_", iter, ".csv", sep="")

      }else{

        write(unlist(mod1 at optinfo$conv$lme4$messages), paste("mod1.errors_", iter, ".txt", sep=""))
      }

      #real work #2
      if(!inherits(poss.err.mod2, "error")){

        mod2 = lmer(y ~ x1 + x2 + (1|ranef1), data = data, REML = FALSE)
        write.csv(summmary$mod2, paste("mod2.sum_", iter, ".csv",)

      }else{

        write(unlist(mod2 at optinfo$conv$lme4$messages), paste("mod1.errors_", iter, ".txt", sep=""))
      }

      cat("Iteration", iter, "completed!\n")
    }

    #close log file & restore warnings stream to console
    closeAllConnections()
Message-ID: <BLUPR02MB12170598732C3E9863431B11CFA80@BLUPR02MB1217.namprd02.prod.outlook.com>
In-Reply-To: <CABghstRoZ=BkUzrvLJgv+yEZzF6x5GJdDVN9+VVjroMhu9JXqw@mail.gmail.com>