Error code when using pp_check with brms

First time posting here, so apologies if I am doing something incorrectly. I am fitting some basic models in RStudio using brms (and simulated data) as follows:

brm(data = dat, y ~ x + (1 | id),
warmup =10000, iter = 30000, cores=3, seed=123,
chains = 4)

and then trying to produce some pp_check graphics:

pp_check(bdefault)

These tend to work at first, but as I keep going I keep getting this error message:


Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
polygon edge not found


I have tried for weeks to resolve this. It usually goes away after restarting RStudio, but then I lose all of my models. I have tried using different fonts and many other potential fixes. Does anyone have any advice? Thanks in advance.

This error usually indicates that ggplot (which is what pp_check is using to generate the plot) can’t find a font it’s trying to use. Unfortunately, there can be various underlying causes for this. Do you get the same error if you try to generate a plot using ggplot directly? For example, what happens if you do ggplot(mtcars, aes(hp, mpg)) + geom_point()?

There are a number of links that discuss this problem (for example, see here and here) and might give you some ideas for how to diagnose and resolve it.

By the way, you can save your brm model objects so that you don’t need to refit the same model. For example, you can do:

m1 = brm(data = dat, y ~ x + (1 | id),
         warmup =10000, iter = 30000, cores=3, seed=123, chains = 4, 
         file="model1")

This will save the model object to a file called model1.rds in your working directory. You can load this file later (with my.model = readRDS("model1.rds")) rather than refitting the model. Also, if you have saved the model object this way, if you try to run the code above again (in the current session or a future session) brm will just load the saved model object (which will be called m1 in your R environment), rather than refitting the model.

Also, a typical rule of thumb is to run four chains with 2,000 total iterations each, the first 1,000 of which are for warmup, giving a total of 4,000 posterior samples in the final model fit. You might need to tweak this in certain situations, but you might be able to get by with a lot less than 30,000 iterations per chain.

Thank you for the ideas. I will keep looking at links such as those you provided.

Yes, I can recreate the graph you mentioned using ggplot directly. Thanks for the tips on model saving. That will be very helpful! (As an aside, I have a very large dataset (~15K observations) with extreme non-normality. I kept increasing the iterations and warmup until it converged.

Thanks again for your help. I will keep trying to figure this out.

pp_check is a function from the bayesplot package and bayesplot uses an internal ggplot2 theme that includes a serif font (see help for bayesplot_theme_set() for more info). You might be able to make the problem go away by switching to a font or font family that ggplot can find. This won’t solve the underlying problem, but it might get you up and running for now. Here are some examples of changing the font/font family in bayesplot:

library(bayesplot)
library(ggplot2)

# Fake data
y <- example_y_data()
yrep <- example_yrep_draws()

# Extra theme element to make text larger
thm = list(theme(text=element_text(size=rel(5))))

# pp_check with bayesplot default serif font
pp_check(y, yrep[1:50,], ppc_dens_overlay) + thm

# Change to sans-serif font
bayesplot_theme_set(theme_default() + theme(text=element_text(family="sans")))
# Or: bayesplot_theme_set(theme_default() + theme(text=element_text(family="")))
pp_check(y, yrep[1:50,], ppc_dens_overlay) + thm

bayesplot_theme_set(theme_default() + theme(text=element_text(family="Arial")))
pp_check(y, yrep[1:50,], ppc_dens_overlay) + thm

bayesplot_theme_set(theme_default() + theme(text=element_text(family="Times")))
pp_check(y, yrep[1:50,], ppc_dens_overlay) + thm

bayesplot_theme_set(theme_default() + theme(text=element_text(family="Courier")))
pp_check(y, yrep[1:50,], ppc_dens_overlay) + thm

Created on 2022-05-10 by the reprex package (v2.0.1)

The Arial absolutely did the trick! Thank you! I am hoping if this happens again this trick will work. Much appreciated!