Saving warning messages

205 views
Skip to first unread message

Fernanda Vallejos

unread,
Jun 23, 2024, 10:18:00 AM6/23/24
to R-inla discussion group

Hello everyone! I'm conducting a simulation study adjusting BYM models to different simulated datasets. In some scenarios, the model fits, but warning messages like these are generated:


*** warning *** max_correction = 25.01 >= 25.00, 'vb.correction' is aborted *** You can change the emergency value (current value=25.00) by *** 'control.inla=list(control.vb=list(emergency=...))' *** Please (re-)consider your model, priors, confounding, etc. *** warning *** delta[0] is NAN, 'vb.correction' is aborted *** Please (re-)consider your model, priors, confounding, etc."

I would like to save these messages to identify in which simulation they occurred, but I'm having trouble doing so. Please help me!  


besag.iid.fun <- function(result) {
    ady <- inla.read.graph(filename = "map.adj")
    train_df <- dplyr::bind_cols(
        data.frame(result$codigo_comuna),
        x1masE = result$x1masE,
        x2masE = result$x2masE,
        E = result$E,
        y = result$y,
        real.RR = result$real.RR,
        re_u = result$re_u,
        re_v = result$re_v
    )
   
    # Lista para almacenar warnings
    warnings_list <- list()
   
    # Función para manejar advertencias
    warning_handler <- function(w) {
        warnings_list[[length(warnings_list) + 1]] <<- conditionMessage(w)
        invokeRestart("muffleWarning")
    }
   
    # Ejecutar inla con tryCatch para capturar advertencias
    resultadobym <- tryCatch({
        withCallingHandlers(
            inla(
                formula = y ~ x1masE + x2masE +
                    f(re_u, model = "besag", graph = ady, scale.model = TRUE, constr = TRUE) +
                    f(re_v, model = "iid"),
                family = "poisson",
                data = train_df,
                E = E,
                control.compute = list(waic = TRUE, cpo = TRUE, dic = TRUE, return.marginals.predictor = TRUE),
                control.predictor = list(compute = TRUE)
            ),
            warning = warning_handler
        )
    }, error = function(e) e)
   
    # Verificar si hubo un error
    if (inherits(resultadobym, "error")) {
        return(list(
            error = "Se produjo un error al ejecutar inla."
        ))
    }
   
    # Extracción de resultados
    efectos.fijos <- resultadobym$summary.fixed
    waic <- resultadobym$waic$waic
    dic <- resultadobym$dic$dic
    cpo <- resultadobym$cpo$cpo
    precision_u <- resultadobym$summary.hyperpar$mean[1]
    precision_v <- resultadobym$summary.hyperpar$mean[2]
    RR.ajustada.post <- resultadobym$summary.fitted.values[,"mean"]
    LL.ajustada.post <- resultadobym$summary.fitted.values[, "0.025quant"]
    UL.ajustada.post <- resultadobym$summary.fitted.values[, "0.975quant"]
    sd_precision_u <- resultadobym$summary.hyperpar[1,2]
    q1.precision_u <- resultadobym$summary.hyperpar[1,3]
    q0.5precision_u <- resultadobym$summary.hyperpar[1,4]
    q3precision_u <- resultadobym$summary.hyperpar[1,5]
    sd_precision_v <- resultadobym$summary.hyperpar[2,2]
    q1.precision_v <- resultadobym$summary.hyperpar[2,3]
    q0.5precision_v <- resultadobym$summary.hyperpar[2,4]
    q3precision_v <- resultadobym$summary.hyperpar[2,5]
    x1masE <- result$x1masE
    x2masE <- result$x2masE
   
    # Determinar el mensaje de warning
    warning_message <- if (length(warnings_list) > 0) {
        paste(warnings_list, collapse = "; ")
    } else {
        "OK"
    }
   
    return(list(
        efectos.fijos = efectos.fijos,
        waic = waic,
        dic = dic,
        cpo = cpo,
        x1masE = x1masE,
        x2masE = x2masE,
        precision_u = precision_u,
        precision_v = precision_v,
        real.RR = result$real.RR,
        RR.ajustada.post = RR.ajustada.post,
        UL.ajustada.post = UL.ajustada.post,
        LL.ajustada.post = LL.ajustada.post,
        sd_precision_u = sd_precision_u,
        q1.precision_u = q1.precision_u,
        q0.5precision_u = q0.5precision_u,
        q3precision_u = q3precision_u,
        sd_precision_v = sd_precision_v,
        q1.precision_v = q1.precision_v,
        q0.5precision_v = q0.5precision_v,
        q3precision_v = q3precision_v,
        warnings = warning_message  # Agregar el mensaje de warning
    ))
}

Thank you!!

Håvard Rue

unread,
Jun 23, 2024, 8:09:40 PM6/23/24
to Fernanda Vallejos, R-inla discussion group
please make sure these

x1masE + x2masE +

are somewhat scaled and centered.


the 'warning' that is show, says that the correction for the mean computed, is
to large to be trusted, and this usually happens when the model is to complex
for the data, or the other way around




On Sun, 2024-06-23 at 07:18 -0700, Fernanda Vallejos wrote:
> x1masE + x2masE +

--
Håvard Rue
hr...@r-inla.org

Fernanda Vallejos

unread,
Jun 23, 2024, 8:38:16 PM6/23/24
to R-inla discussion group
Thank you Dr. Rue!! Could you please help me identify the function that is programmed to send this warning? I am fitting 45,000 data sets and would like to determine the scenarios in which this warning occurs. 
Thank you for your time and help.

Helpdesk (Haavard Rue)

unread,
Jun 24, 2024, 10:53:03 AM6/24/24
to Fernanda Vallejos, R-inla discussion group

try to rescale covariates and put informative priors for the fixed effects and
also intercept. you want to reach a model without the warning
he...@r-inla.org

Helpdesk (Haavard Rue)

unread,
Jun 24, 2024, 10:56:27 AM6/24/24
to Fernanda Vallejos, R-inla discussion group
maybe like this


formula = y ~ x1masE + x2masE +
f(re_u, model = "besag", graph = ady, scale.model = TRUE,
constr = TRUE,
hyper=list(prec=list(prior="pc.prec", param=c(0.5, 0.01)))) +
f(re_v, model = "iid", 
hyper=list(prec=list(prior="pc.prec", param=c(0.5, 0.01)))),
family = "poisson",
control.fixed = list(prec.intercept=1, prec=1)

Fernanda Vallejos

unread,
Jun 24, 2024, 9:22:52 PM6/24/24
to R-inla discussion group
I have fewer warnings, but I still have some. I suppose this is because some scenarios are too unrealistic for the model to fit well. Therefore, to identify in which simulation the error occurs, I want to be able to pinpoint the simulation where the error is present to explore what happens in those cases. That's why I want to "capture" the warning messages that are generated as the models are fitted. To do this, I need to find the part of the functions that generate those warnings, or something general that allows me to save them, since the tryCatch function does not effectively detect warnings from INLA as actual warnings. I have searched within the functions but cannot find the function that generates the messages.
Thank you again for your time and patience!

Helpdesk (Haavard Rue)

unread,
Jun 24, 2024, 11:53:19 PM6/24/24
to Fernanda Vallejos, R-inla discussion group
n <- 30
y <- rpois(n, exp(2))
idx <- 1:n
for (emerg in c(0.001, 25)) {
cat("emergency = ", emerg, "\n")
r <- inla(y ~ 1 + f(idx, vb.correct = T, initial = log(1), fixed = T),
data = data.frame(y, idx),
control.inla = list(control.vb = list(emergency = emerg),
int.strategy = "eb"),
family = "poisson")
if (length(grep("max_correction = ", r$logfile)) > 0) {
cat("VB ABORTED\n")
} else {
cat("VB OK\n")

Fernanda Vallejos

unread,
Jun 28, 2024, 8:48:00 PM6/28/24
to R-inla discussion group
Thank you, Dr. Rue!!! I have finally partially adapted the function and have partially achieved the warning I need! 
Reply all
Reply to author
Forward
0 new messages