Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Summarize divergent transitions in glance #71

Open
nehill197 opened this issue Oct 12, 2023 · 0 comments
Open

Summarize divergent transitions in glance #71

nehill197 opened this issue Oct 12, 2023 · 0 comments
Assignees

Comments

@nehill197
Copy link
Member

Divergent transitions in Stan indicate that something is wrong with your model. Currently, it is only in debug mode that the Stan warning messages are printed, which include warnings of divergent transitions.

We should add a column (divergences/diverge/divergent) that summarizes whether or not there were any divergent transitions in the glance table for all models.

Since a single divergent transition is enough to warrant concern, this should be a binary column for Stan models. JAGS models should show NA.

Potentially helpful code:

# Returns parameter arrays separated into divergent and non-divergent transitions
partition_div <- function(fit) {
  nom_params <- rstan::extract(fit, permuted=FALSE)
  n_chains <- dim(nom_params)[2]
  params <- as.data.frame(do.call(rbind, lapply(1:n_chains, function(n) nom_params[,n,])))
  
  sampler_params <- rstan::get_sampler_params(fit, inc_warmup=FALSE)
  divergent <- do.call(rbind, sampler_params)[,'divergent__']
  params$divergent <- divergent
  
  div_params <- params[params$divergent == 1,]
  nondiv_params <- params[params$divergent == 0,]
  
  return(list(div_params, nondiv_params))
}
div <- partition_div(analysis$stanfit)
paste("There were", nrow(div[[1]]), "divergent transitions")
@nehill197 nehill197 self-assigned this Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant