Skip to content

mcmc_pairs sometimes plots wrong number of divergences #555

Description

@jgabry

First reported on the Stan forum by user @gregd:

https://discourse.mc-stan.org/t/bayesplot-mcmc-pairs-divergences-error/41457

Here's the code that was given to demonstrate the problem.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions