rbrms

Retrieve warnings from a model fitted with brms


I am attempting to extract the warnings that were generated during the fitting of a Bayesian model using the brms::brm() function, after the model has already been fitted.

Consider the following model as an example:

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

Upon fitting, 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

Now, if I save the model and restart the session I'd like to be able to retrieve those warnings from the mdl object. I am unable to retrieve the warnings using the following command:

mdl <- readRDS(file = 'mdl.rds')
mdl # The warnings do not show up
warnings(mdl)

My goal is to retrieve the warnings from a series of .rds files that contain models fitted with brm(). I am aware that I could save the warnings along with the model during the fitting process (as discussed in my previous post), but that is not the solution I am seeking in this case.

How can I extract these warnings?


Solution

  • I'm almost certain that the warnings are not stored as part of the object, so if you haven't stored them yourself when fitting the model (as in the previous post you linked) you're out of luck; that information is no longer available.

    To try to confirm we could look at the names of the elements of the stored/restored brmsfit object:

    names(mdl)
     [1] "formula"   "data"      "prior"     "data2"     "stanvars"  "model"    
     [7] "ranef"     "save_pars" "algorithm" "backend"   "threads"   "opencl"   
    [13] "stan_args" "fit"       "basis"     "criteria"  "file"      "version"  
    [19] "family"    "autocor"   "cov_ranef" "stan_funs" "data.name"
    

    None of them has a name ("warnings", "info", etc.) that suggests it might contain warning information.

    We could also look at the output of str(mdl). This is voluminous, so instead I'll search the output for the string "warn" (and not find it):

    capture.output(str(mdl)) |> 
       grep(pattern = "warn", ignore.case = TRUE, value = TRUE)
    ## character(0)
    

    This doesn't guarantee that the warnings aren't stored somewhere we're not seeing them with str() (e.g. inside an environment), but strongly suggests they're not.