[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:// 
yusung.blogspot.com/2007/01/analyzing-coda-statistics-produced- 
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  
needed

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!

-Jarrett


#note, the test run was something along the lines of c.bugs<-coda2bugs 
(n.burnin=1000)

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

		require(R2WinBUGS)
		

		#first, split up the coda file for R2WinBUGS
		codaSplit(codafile, indexfile)
		
		#get the parameter names
		index.table<-read.table(indexfile)
		
		varNames<-as.vector(index.table[,1])

		#determine the n.iter
		if(is.na(n.iter)){n.iter<-index.table[1,3]}
		
		#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)
		
		class(bugs.fit)<-"bugs"
		
		bugs.fit$isDIC <- FALSE
		
		#clean up the new coda files
		if(file.rm==T){
			file.remove("codaIndex.txt")
			for(i in rownames(index.table)){
				file.remove(paste("coda",i,".txt",sep=""))
			}
		}
		
		
		return(bugs.fit)
}

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



More information about the R-help mailing list