Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 209860

tryCatch: access object from expr in warning statement?

$
0
0

I have a glmer model that throws a gradient convergence warning, e.g.:

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

I've checked out this warning (singularity and gradient), and decided to restart the model with the previous parameter estimates. Because of the structure of my code, it would be handy to somehow automatically prompt the model to be refit if this warning is thrown.

My initial thought was to use a tryCatch statement. Is there a way to restart the model (m2 below) with parameter estimates from the expr part of the statement (m1) without having to fit the initial model (m1) twice?

For example, the code below (with glmer(...) appropriately filled in) works:

tryCatch(
  expr = {
    glmer(...)
  },
  warning = function(cond){
    m1 <- glmer(...)
    m2 <- update(m1, start = getME(m1, c("theta", "fixef")))
    return(m2)
})

But is it possible to do something rather like...

tryCatch(
  expr = {
    m1 <- glmer(...)
  },
  warning = function(cond){
    m2 <- update(m1, start = getME(m1, c("theta", "fixef")))
    return(m2)
})

... except without throwing an error as this second code block does (since the warning section doesn't see that first m1 assignment)?


Viewing all articles
Browse latest Browse all 209860

Trending Articles