I made a model using R2jags. I like the jags syntax but I find the output produced by R2jags not easy to use. I recently read about the rstanarm package. It has many useful functions and is well supported by the tidybayes and bayesplot packages for easy model diagnostics and visualisation. However, I'm not a fan of the syntax used to write a model in rstanarm. Ideally, I would like to get the best of the two worlds, that is writing the model in R2jags and convert the output into a Stanreg object to use rstanarm functions.
Is that possible? If so, how?
I think then question isn't necessarily whether or not it's possible - I suspect it probably is. The question really is how much time you're prepared to spend doing it. All you'd have to do is try to replicate in structure the object that gets created by
rstanarm, to the extent that it's possible with theR2jagsoutput. That would make it so that some post-processing tasks would probably work.If I might be so bold, I suspect a better use of your time would be to turn the
R2jagsobject into something that could be used with the post-processing functions you want to use. For example, it only takes a small modification to the JAGS output to make all of themcmc_*()plotting functions frombayesplotwork. Here's an example. Below is the example model from thejags()function help.Now, what the
mcmc_*()plotting functions frombayesplotexpect is a list of matrices of MCMC draws where the column names give the name of the parameter. By default,jags()puts all of them into a single matrix. In the above case, there are 5000 iterations in total, with 2500 as burnin (leaving 2500 sampled) and then.thinis set to 2 in this case (jags()has an algorithm for identifying the thinning parameter), but in any case, thejagsfit$BUGSoutput$n.keepelement identifies how many iterations are kept. In this case, it's 1250. So you could use that to make a list of two matrices from the output.Now, you'd just have to call some of the plotting functions:
or
So, instead of trying to replicate all of the output that
rstanarmproduces, it might be a better use of your time to try to bend thejagsoutput into a format that would be amenable to the post-processing functions you want to use.EDIT - added possibility for
pp_check()frombayesplot.The posterior draws of
yin this case are in thethetaparameters. So, we make an object that has elementsyandyrepand make it of classfooWe can then write a
pp_checkmethod for objects of classfoo. This come straight out of the help file forbayesplot::pp_check().Then, just call the function: