[R] Going from Coda Files to R2WinBUGS

Jarrett Byrnes jebyrnes at ucdavis.edu
Mon Apr 30 00:19:25 CEST 2007

I'm currently using JAGS as my Bayesian program of choice due to  
working off of an older mac running OSX.  I'd like to utilize some of  
the functions from R2WinBUGS, however.  As such, I'm attempting to  
write something to go from coda output dumped by JAGS into the bugs  
object format - I've looked for functions that will convert from an  
mcmc object to a bugs object, but have had no luck as of yet.  I've  
attempted to adapt Yu-Sung Su's method over at http:// 
by.html .  However, whenever I run it for a coda file set generated  
by jags, I get the following error:

Error in if (trans[i] == "log") { : missing value where TRUE/FALSE  

This is for a run whose jags.ind is as follows

cypraea.effect	1	10000
intercept	10001	20000
sponge.sd	20001	30000
deviance	30001	40000

When I debuged R2WinBUGS:::monitor, which is where the whole thing  
borks, I found that trans is as follows

cypraea.effect      intercept      sponge.sd       deviance
             ""           	  NA		             NA             		NA

And the error comes when first looking at intercept, which is NA, not  
"".  I am somewhat unclear as to why this is so.

The code for the method is as follows.  Any thoughts would be greatly  
appreciated, and if this works out, feel free to use it yourself!   
Could be quite useful!


#note, the test run was something along the lines of c.bugs<-coda2bugs 

coda2bugs<-function(codafile="jags.out", indexfile="jags.ind",  
					n.iter=NA, n.burnin=NA, n.thin=1, DIC=FALSE, file.rm=T, ...){


		#first, split up the coda file for R2WinBUGS
		codaSplit(codafile, indexfile)
		#get the parameter names

		#determine the n.iter
		#you will need to put the n.burnin in yourself
		#for the cypraea example, it is 1000
		bugs.fit <- R2WinBUGS:::bugs.sims(varNames, n.chains=n.chains,
				 n.iter=n.iter, n.burnin=n.burnin, n.thin=n.thin, DIC = DIC)
		bugs.fit$isDIC <- FALSE
		#clean up the new coda files
			for(i in rownames(index.table)){

codaSplit<-function(codafile="jags.out", indexfile="jags.ind"){
	write.table(index.table, "codaIndex.txt", quote=F, row.names=F,  
col.names=F, sep="\t")
	#write the new coda files
	for(i in rownames(index.table)){
		new.file=paste("coda",i,".txt", sep="")
		write.table(new.out, new.file, row.names=F, col.names=F, sep="\t")

More information about the R-help mailing list