Wednesday, May 21, 2008

R2jags: A Package for Running jags from R

I have wrote about my experience with jags here. Martyn Plummer has updated jags recently and now jags can get as he claimed. Jags is fast and its source code is more manipulable (It's written in C++).

I and Masanao wrote a package, R2jags, for running jags from R. To be sure, Martyn Plummer has wrote a package, rjags, for this purpose. But our package try to mimic the way in which R2WinBUGS does. So WinBUGS users do not have to readjust themselves when they want to use this package. Nonetheless, install R2jags is a bit tricky because we need to install other unlisted R packages. Here are the steps:

To use JAGS under Windows, there are 4 steps:

Install JAGS:
http://www-fis.iarc.fr/~martyn/software/jags/

Install rjags package: (from CRAN, from R console)
install.packages("rjags")

Install our R2jags: (type the following in R console)
install.packages("R2jags")

When you finish all above steps, in R, type:

library(R2jags)

And ?jags to see the example code.

There are 2 functions: jags() and jags2().

jags() is very much like bugs() in R2WinBUGS. But you can update your model if you found out that your model is not converged.

jags2() is a function we wrote before rjags is working. The difference between jags() and jags2() is that jags2() cannot be updated. But jags2() does give you coda files. Thus if you are a fan of analyzing coda files, jags() is the one you want.

We haven't uploaded R2jags to CRAN yet. We need more input. So we have R2jags in R-forge. Thus if you have any suggestion, please go to:

http://r-forge.r-project.org/projects/r2jags

We welcome all your inputs!

14 comments:

bbolker said...

JAGS has been updated -- the new Windows binary is at

http://www-ice.iarc.fr/~martyn/software/jags/jags-1.0.3-setup.exe

(might make sense for Martyn to have a symbolic link pointing to the most recent version ?)

Yu-Sung Su said...

Thanks for letting me know.

In the meantimes, before Dr. Plummer updates rjags, you can download the rjags here (I revised it to make it work).

http://yusung.googlepages.com/rjags_1.0.2-3.zip

If you want to use new R2jags, update it in R console by typing the following:

install.packages("R2jags",repos="http://R-Forge.R-project.org")

Ira said...

Thanks for providing R2jags. I've found it helpful as a new JAGS user. I also wanted to say that R2jags compiles and runs fine on my mac. You might want to include a mac binary on CRAN? If you don't have a mac I'd be happy to help with this.

Yu-Sung Su said...

Hi Ira,

Thank you. The reason CRAN does not complie a Mac binary for R2jags is because there is no Mac binary for rjags either. So if you can compile both rjags and R2jags as the tar binary files, you can share them to Mac users. Or you can send them to me and I will put them on my website.

Ira said...

Ah .. I didn't think very far before posting that comment ;). All I'd done is requested the source package and it compiled fine and then ran your test code. I guess it also downloaded and compiled rjags as well so I'm not sure why no binary exists for this (as it was successful). Perhaps there is an issue with older macs? I'm not sure really .. but as Martyn Plummer has a mac binary on his webpage I guess the rjags binary will arrive soon enough.

For now ... I guess it's useful for mac users to know that everything will compile from source provided they have jags previously installed.

Alok R. Saboo said...

I am experimenting with JAGS and found your blog. Had some quick question and would appreciate if you could help me with that.
1. I understand that JAGS can handle multivariate DVs. But how do I read in and refer to multivariate DVs? In WinBUGS I could just enter y[,1] y[,2] and so on...how do I achieve the same thing with JAGS? My data is in Excel and I am copying it using the clipboard.

Is there a sample program with multivariate DVs? Thanks..

Yu-Sung Su said...

To Alok,

I believe you can do the same way as you did in WinBUGS in JAGS.

lx said...

Hi Dr. Su,

Thank you for R2jags! It is VERY helpful.

I encountered one problem when I was running simulations with R2jags. At a certain number of the simulations (I gave seeds), it always give the following error:

Error in parse(file) : unexpected symbol at
100: 0, 0, 0, 0, 0, 0)
101: `x`
Calls: LR3snp.fun -> jags -> read.jagsdata -> eval -> parse
Execution halted

Looks like it had some problem with the jags data created, whereas it was fine for all the previous simulations. Here is how I generated the data:
A = array(c(1,1,0,0,0,0),c(2,3))
n=2*samplesize; k=2
y = c(rep(1,samplesize),rep(0,samplesize))
x = get.null(2,0.1,size = 1000)
data=list("n","y","x","A")

Could it be the R version problem? Since I was running it under R 2.10.0, and I don't have the latest version available on our server.

Thank you so much,

Xiang

lx said...

Hi Dr. Su,

I just tried the sample data from R2jags help document (school.txt), and copied exactly as it is in the document, but got the same error as:
Error in parse(file) :
jagsdata.txt:101:1: unexpected symbol
100: 0, 0, 0, 0, 0, 0)
101: `x`
^

Do you think it is a compiler problem? I am working on the super computer system at the university of Minnesota.

Thank you,

Xiang

lx said...

Hi Dr. Su,

Sorry for the last two comments. I found out the problem. I was running several jobs with the same working directory and they all output the jagsdata.txt file, and keep append to the file since append=TRUE in the dump function. I think it stopped somewhere that the jagsdata.txt file did not got deleted and messed up all my following tests.

It is all fixed now and thank you again,

Xiang

Yu-Sung Su said...

I'm glad that you solve it!

lingpipe-blog.com said...

The instructions work fine for Linux. But not for Windows. It'd be great to have step-by-step instructions that work out of the box, because it was a real struggle to get it working.

After more than two hours banging my head against this with lots of kibbitzing from the peanut gallery in the playroom, I finally did three things at once, after which it worked (so I still don't know what's necessary).

1. I started with 64-bit versions, but wound up reinstalling the latest version of R in 32-bit mode in case that was the problem. Ditto for removing 64-bit JAGS and reinsalling 32-bit JAGS. This may not be necessary.

2. resetting the JAGS_HOME and JAGS_ROOT environment variables to the top-level unpack location,

C:\Program Files (x86)\JAGS\JAGS-2.1.0

Again, I don't know if this is important.

3. going in as admin and setting permissions on the JAGS library files, which for some reason, unpacked without executable permission for anyone but admins.

I did NOT need to specify the lib paths in the install.packages("rjags") command, but that worked for Linux.

Bill Harris said...

Thanks for R2jags, Su. I've used it on some smaller models successfully, but I'm having problems with a larger one, as you can see described at http://sourceforge.net/projects/mcmc-jags/forums/forum/610037/topic/4532046. Is there anything I can do to figure out what might be going on and how to fix it?

Kinh N.V. said...

dear Yu-Sung Su, I read that jags still using ‘pD=var(deviance)/2’ instead of pD = Dbar-Dhat. Will be an update in the future? I read elsewhere that JAGS has already changed to the later.