Tuesday, January 16, 2007

Analyzing coda files produced by BUGS

Sometimes, if we forget to save BUGS output, we run into a nightmare of fitting the model all over again. This would mean thousands of iteration and couple hours. But here is the tip for situation like this. If you fit your model through R2WinBUGS, you might have code files left in your working directory.

Converting coda files into BUGS objects.

Let say our coda files are: coda1.txt, coda2.txt, coda3.txt and codaIndex.txt.

setwd("d:\tmp") # set your working directory to where coda files are

library(R2WinBUGS)

bugs.fit <- R2WinBUGS:::bugs.sims(c("mu", "sigma", "theta"), n.chains=3, n.iter=6000, n.burnin=1000, n.thin=1, DIC = FALSE)
# you need to read coda files to know and input these numbers

class(bugs.fit) <- "bugs"

If you did save DIC as log.txt from WinBUGS, then:

bugs.fit$is.DIC <- TRUE

If not, then:

bugs.fit$is.DIC <- FALSE

After these steps, bugs.fit will be a bugs object again. You can save it save("bugs.fit", file="bugsfit"). Or do the analysis like:

plot(bugs.fit)
print(bugs.fit)

1 comment:

jebyrnes said...

I've tried to adapt this so that coda files generated by JAGS can be used (so, I've written a function that splits the coda file into parts, then uses the syntax you have here) - but I keep getting the following error

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

Any thoughts?

See here for a more complete description and code

http://tolstoy.newcastle.edu.au/R/e2/help/07/04/15719.html