R2jags & BCEA (& the examples from BMHE)
[This article was first published on Gianluca Baio's blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Recently, Yu-Sung Su and Masanao Yajima, the developers of the R2jags package, have released a new version (the current one is 0.03-11). As far as I understand it, one of the main changes is that since the update, R2jags no longer depends on the R2WinBUGS package (although it “imports” it). Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The consequence of this is that you can no longer use the R2WinBUGS functions, such as for example bugs or attach.bugs(), by just loading R2jags. In fact, there’s a new function attach.jags() that allows you to attach the object you obtain as a result of the call to the jags function and containing, among other things, the MCMC simulations.
More importantly, if you also use BCEA and try to replicate the examples I describe in BMHE (for example see here, here, here and here) you are in trouble. All the code I have produced was running OK under the previous version of R2jags, but now you do get an error message when you try to attach the JAGS object using the attach.bugs() command.
Fortunately, this is not a huge problem and you actually have two options to solve it: the first one is to add to all those code a formal call to R2WinBUGS, eg library(R2WinBUGS). This will make the attach.bugs() command available again and so the rest of the code will run OK.
The second way is to actually use the attach.jags() command directly. In this, you don’t need to load R2WinBUGS; however (because, as Sheldon Cooper would say: “what’s life without whimsy?”), in this case you have to change the argument to this function, since attach.jags() takes a rjags object, while attach.bugs() wants a bugs object.
So, for example, assume you have the following code.
library(R2jags)
model <- jags(data,inits,parameters.to.save,
model.file=”some_file.txt”, n.chains=2,
n.iter, n.burnin, n.thin, DIC=TRUE,
working.directory=working.dir, progress.bar=”text”)
and you want to make the object model (and all the elements contained in it) available to your R session, you can either do
library(R2WinBUGS)
attach.bugs(model$BUGSoutput)
(notice that model is an object in the class rjags, while its element BUGSoutput is in the class bugs), or do
attach.jags(model)
directly.
To leave a comment for the author, please follow the link and comment on their blog: Gianluca Baio's blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.