Bayesplot mcmc_pairs divergences error

Hope this is my error and simple! Using bayesplot version 1.15.0.9000 with a reproducible example, some but not all divergences are shown in the mcmc_pairs plot.

library(rstan)
rstan_options(auto_write = TRUE)
library(brms)
library(cmdstanr)
options(mc.cores=2, brms.normalize=TRUE,brms.backend="cmdstanr")
library(bayesplot)

set.seed(123)
x<-runif(100)
y<-sin(12*x)*exp(-3*x)+rnorm(100,0,0.3)
plot(x,y)
y[1:5]<-runif(5,-1.5,-1)
plot(x,y)
points(x[1:5],y[1:5],pch=16,col=2)

bf1<-bf(y~s(x,k=7))
dat<-data.frame(y,x)

m1<-brm(bf1,dat,init=0,control=list(seed=1591897794))
#init and seed for reproducibility
#29 div

np<-nuts_params(m1)
dx<-which(np$Par=="divergent__"&np$Val==1)
length(dx)
#29
np[dx,]
#25 in C2, 4 in C4

vars<-variables(m1)
vars

pc<-pairs_condition(chains=list(1:2,3:4))
pc

mcmc_pairs(m1,pars=vars[2:3],np=np, np_style=pairs_style_np(div_shape=16,div_size=2),condition=pc)
#shows 8, with 4 in each plot
#should show 25 in top right, 4 in bottom left

np[dx[26:29],]
#the 4 in C4
np[dx[26:29],2]

arr<-as.array(m1)
dim(arr)
arr[np[dx[26:29],2],4,2:3]
#the bottom left plot is correct

arr[np[dx[1],2],2,2:3]
#not shown on top right plot
#others also not shown

#same happens with rstan backend

Thanks for reporting this. This seems to be a real bug that was introduced at some point. I’m going to move this over to a GitHub issue and try to fix it:

I’m sure you’ll fix it, but in case this helps

mcmc_parcoord(m1,pars=vars[6:7],np=np,np_style=parcoord_style_np(div_size=0.2,div_alpha=1),alpha=0)

#a bit hard to view but it may show 29 lines, certainly at least 25

Thanks. This is fixed now on GitHub. You can install it using

remotes::install_github("stan-dev/bayesplot")

The cause was that when ggplot2 deprecated aes_(), which we used in mcmc_pairs(), we switched to using aes() but missed a spot in the code that needed to be adjusted to account for aes() doing delayed evaluation.

thanks. For anyone viewing the pairs plot, the reason the upper right plot now shows 22 red dots rather than 25 is that 3 of the divergences occur twice with identical parameters. parcoord is also fine, as it was before the bug was fixed. thanks again.

Thanks again for reporting this and providing an example demonstrating it