florianhartig / BayesianTools

General-Purpose MCMC and SMC Samplers and Tools for Bayesian Statistics
https://cran.r-project.org/web/packages/BayesianTools/index.html
119 stars 29 forks source link

gelmanDiagnostics(plot = FALSE) fails with plot error when MCMC is performed on a single parameter #221

Closed twest820 closed 3 years ago

twest820 commented 3 years ago

Looks like the problem here is is.na(diag$mpsrf) is called even when diag is empty, leading to an if check on an empty object.

> mcmcGelman = gelmanDiagnostics(mcmcOut, plot = FALSE)
Error in if (plot == T & !is.na(diag$mpsrf)) do.call(gP, as.list(match.call())) : 
  argument is of length zero
florianhartig commented 3 years ago

Thanks, this is true. There are a number of other issues for summaries when working with 1d parameters. I guess it's rare that a user would like to run an MCMC with one parameter, but I should nevertheless tidy this up!

twest820 commented 3 years ago

Yeah, there are usually easier and better optimization methods than MCMC for a single parameter. Single parameter is useful for simplified testing and examples, though. From some of the search hits in the BayesianTools I think at least one or two of the package's tests are single parameter and single parameter also makes a good hello world for users new to the package (and probably MCMC as well). Another case is debugging, since if you're having some high dimensional convergence issue or some other problem it can be helpful to start checking low dimensional slices. The latter is how I found this, actually.

From some looking at how BayesianTools interacts with the coda package, I think the snippet below provides a workaround for this issue.

chainInCodaFormat = getSample(mcmcBurnin, coda = TRUE)
coda::gelman.diag(chainInCodaFormat)
florianhartig commented 3 years ago

Thanks for that, I had already implemented a fix!

florianhartig commented 3 years ago

btw., I agree about the use of 1-d for examples!