Search code examples
rbrms

Capturing Specific Warnings from a Model Fitted with brms


I'm trying to capture and store the specific warnings that occasionally appear when fitting a Bayesian model using the brms::brm() function in R. The challenge is to do this without having to fit the model twice, which is time-consuming.

For example, when fitting the following model:

mdl <- brms::brm(Sepal.Length ~ Sepal.Width*Petal.Length*Petal.Width, data = iris, seed = 123)

I receive two warning messages:

Warning messages:
1: There were 8 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See
https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded 
2: Examine the pairs() plot to diagnose sampling problems

I want to capture these warnings that are specific to the model fit. This is particularly important as this model is fitted within a lengthy script that may generate numerous warnings before and after the model is fitted. Ideally, I would like to create a function like get_model_warnings(mdl).

Here's my current approach:

expandFunctions::reset.warnings()
mdl <- brms::brm(Sepal.Length ~ Sepal.Width*Petal.Length*Petal.Width, data = iris, seed = 123)
mdl_warning <- names(last.warning)

This code does capture the warnings, but it's not ideal as it clears all previous warnings. Also, it does not allow to retroactively capture the warnings that were thrown when fitting the model.


Solution

  • Using purrr quietly which stores the warnings along with the model result:

    library(purrr)
    library(brms)
    
    quiet_brm <- quietly(brm)
    
    mdl <- quiet_brm(Sepal.Length ~ Sepal.Width * Petal.Length * Petal.Width, data = iris, seed = 123)
    
    mdl$warnings
    #> [1] "There were 9 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See\nhttps://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded"
    #> [2] "Examine the pairs() plot to diagnose sampling problems\n"
    

    Created on 2024-04-18 with reprex v2.1.0