swh:1:snp:cb3ee08893d9ebea95d32b41026209106231a62e
Raw File
Tip revision: 1ee50c471e33b2b4cf30fe1dc261a69fe2a3f3e8 authored by Sibylle Sturtz on 14 November 2005, 00:00:00 UTC
version 1.0-1
Tip revision: 1ee50c4
bugs.R
"bugs" <-
function(data, inits, parameters.to.save, model.file = "model.txt",
    n.chains = 3, n.iter = 2000, n.burnin = floor(n.iter / 2),
    n.thin = max(1, floor(n.chains * (n.iter - n.burnin) / 1000)), 
    bin = (n.iter - n.burnin) / n.thin,
    debug = FALSE, DIC = TRUE, digits = 5, codaPkg = FALSE, 
    bugs.directory = "c:/Program Files/WinBUGS14/", working.directory = NULL,
    clearWD = FALSE){

  # Checking number of inits, which is NOT save here:
  if(!missing(inits) && !is.function(inits) && !is.null(inits) && (length(inits) != n.chains)) 
    stop("Number of initialized chains (length(inits)) != n.chains")
  if(!is.null(working.directory)){
      savedWD <- getwd()
      setwd(working.directory)
      on.exit(setwd(savedWD))
  }
  if(!file.exists(model.file)) stop(paste(model.file, "does not exist."))
  if(file.info(model.file)$isdir) stop(paste(model.file, "is a directory, but a file is required."))  
  if(!(length(data) == 1 && is.vector(data) && is.character(data) && data == "data.txt"))
    bugs.data(data, dir = getwd(), digits)  
  else if(!file.exists(data))
    stop("File data.txt does not exist.")
  bugs.inits(inits, n.chains, digits)
  if(DIC) parameters.to.save <- c(parameters.to.save, "deviance")
  # Model files with extension ".bug" need to be renamed to ".txt"
  if(length(grep("\\.bug$", model.file))){
    new.model.file <- sub("\\.bug$", "\\.txt", model.file)
    file.copy(model.file, new.model.file, overwrite = TRUE)
    on.exit(file.remove(new.model.file), add = TRUE)
  }
  else new.model.file <- model.file
  bugs.script(parameters.to.save, n.chains, n.iter, n.burnin, n.thin,
    bugs.directory, new.model.file, debug=debug, is.inits=!is.null(inits), 
    bin = bin, DIC = DIC)
  bugs.run(n.burnin, bugs.directory)
  if(codaPkg){
    return(file.path(getwd(), paste("coda", 1:n.chains, ".txt", sep="")))
  }
  else{
    sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), 
        model.file = model.file, is.DIC = DIC)
    if(clearWD)
        file.remove(c("data.txt", "log.odc", "codaIndex.txt",
            paste("inits", 1:n.chains, ".txt", sep=""),
            paste("coda", 1:n.chains, ".txt", sep="")))
    class(sims) <- "bugs"
    return(sims)
  }
}
back to top