From: <gg...@us...> - 2006-11-24 13:36:30
|
Revision: 3 http://svn.sourceforge.net/bugs-r/?rev=3&view=rev Author: ggorjan Date: 2006-11-24 05:24:51 -0800 (Fri, 24 Nov 2006) Log Message: ----------- populating SVN for R2WinBUGS Added Paths: ----------- trunk/R2WinBUGS/Changes trunk/R2WinBUGS/Changes0 trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/NAMESPACE trunk/R2WinBUGS/R/ trunk/R2WinBUGS/R/as.bugs.array.R trunk/R2WinBUGS/R/attach.all.R trunk/R2WinBUGS/R/bugs.R trunk/R2WinBUGS/R/bugs.data.R trunk/R2WinBUGS/R/bugs.inits.R trunk/R2WinBUGS/R/bugs.log.R trunk/R2WinBUGS/R/bugs.plot.inferences.R trunk/R2WinBUGS/R/bugs.plot.summary.R trunk/R2WinBUGS/R/bugs.run.R trunk/R2WinBUGS/R/bugs.script.R trunk/R2WinBUGS/R/bugs.sims.R trunk/R2WinBUGS/R/bugs.update.settings.R trunk/R2WinBUGS/R/conv.par.R trunk/R2WinBUGS/R/decode.parameter.name.R trunk/R2WinBUGS/R/formatdata.R trunk/R2WinBUGS/R/monitor.R trunk/R2WinBUGS/R/openbugs.R trunk/R2WinBUGS/R/plot.bugs.R trunk/R2WinBUGS/R/print.bugs.R trunk/R2WinBUGS/R/read.bugs.R trunk/R2WinBUGS/R/wineutils.R trunk/R2WinBUGS/R/write.datafile.R trunk/R2WinBUGS/R/write.model.R trunk/R2WinBUGS/data/ trunk/R2WinBUGS/data/schools.txt trunk/R2WinBUGS/inst/ trunk/R2WinBUGS/inst/CITATION trunk/R2WinBUGS/inst/doc/ trunk/R2WinBUGS/inst/doc/R2WinBUGS.Rnw trunk/R2WinBUGS/inst/doc/R2WinBUGS.pdf trunk/R2WinBUGS/inst/doc/RdRW.sty trunk/R2WinBUGS/inst/doc/Z.cls trunk/R2WinBUGS/inst/doc/benzolsw.pdf trunk/R2WinBUGS/inst/doc/bugs.tex trunk/R2WinBUGS/inst/doc/countssw.pdf trunk/R2WinBUGS/inst/doc/expectedsw.pdf trunk/R2WinBUGS/inst/doc/literatur.bib trunk/R2WinBUGS/inst/doc/literatur.bst trunk/R2WinBUGS/inst/doc/plot.pdf trunk/R2WinBUGS/inst/model/ trunk/R2WinBUGS/inst/model/schools.txt trunk/R2WinBUGS/man/ trunk/R2WinBUGS/man/as.bugs.array.Rd trunk/R2WinBUGS/man/attach.all.Rd trunk/R2WinBUGS/man/bugs.Rd trunk/R2WinBUGS/man/bugs.data.Rd trunk/R2WinBUGS/man/bugs.inits.Rd trunk/R2WinBUGS/man/bugs.log.Rd trunk/R2WinBUGS/man/bugs.plot.Rd trunk/R2WinBUGS/man/bugs.run.Rd trunk/R2WinBUGS/man/bugs.script.Rd trunk/R2WinBUGS/man/bugs.sims.Rd trunk/R2WinBUGS/man/bugs.update.settings.Rd trunk/R2WinBUGS/man/decode.parameter.name.Rd trunk/R2WinBUGS/man/monitor.Rd trunk/R2WinBUGS/man/openbugs.Rd trunk/R2WinBUGS/man/plot.bugs.Rd trunk/R2WinBUGS/man/print.bugs.Rd trunk/R2WinBUGS/man/read.bugs.Rd trunk/R2WinBUGS/man/schools.Rd trunk/R2WinBUGS/man/write.datafile.Rd trunk/R2WinBUGS/man/write.model.Rd Added: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes (rev 0) +++ trunk/R2WinBUGS/Changes 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,71 @@ +Changes to R2WinBUGS: +===================== + +During the process of packaging R2WinBUGS_0.1 - R2WinBUGS_0.2-1, +quite a lot of changes had been made. Those changes are not +documented anywhere ... + + +Update 0.2-2 (28 Apr 2004): +- schools data: original (see references) instead of the rounded data + +Update 0.2-3 (10 Sept 2004): +- bugs.data.inits split to bugs.inits and bugs.data, the latter exported + from the Namespace. + Now we can use already written data files in bugs(). + +Update 0.2-4 (05 Oct 2004): +- bugs.script() did not work for large n.iter values in update step + (no scientific notation allowed) + +Update 0.2-5 (20 Oct 2004): +- bugs() and bugs.script() have a new argument bin that allows to specify + a number of iterations. After each "bin" iterations the coda files are saved. + +Update 0.2-6 (18 May 2005): +- bugs() changes in order to return a file names of coda output files +- new read.bugs() returns a coda mcmc.list object, if codaPkg=TRUE. + +Update 0.2-8 (30 May 2005): +- bugs passed DIC to bugs.script in order to be able to disable it + +Update 0.2-9 (26 July 2005): +- bugs has new argument clearWD + +Update 1.0-0 (05 Aug 2005): +- attach.all(), detach.all(), attach.bugs() and detach.bugs() added/changed + more or less according to Andrew Gelman's current bugs.R + +Update 1.0-1 (14 Nov 2005): +- make inits=NULL work (again ?) + +Update 1.1-0 (14 Dec 2005): +- Contribution by Ben Bolker and Yun Yan's rbugs package: + make R2WinBUGS work under WINE + +Update 1.1-1 (17 Feb 2006): +- WINE tweaks (mainly by Gregor Gorjanc) + +Update 2.0-0 (08 May 2006): +- bugs() doc fix/updates on scrambling +- bugs.run() has new arg useWINE (by Gregor Gorjanc) +- bugs() and bugs.script() patched for WINEPATH issue +- bugs.script() changed to save log file in ASCII +- new function bugs.log() by Gregor Gorjanc +- new functions as.bugs.array, openbugs and sort.name by + Jouni Kerman and Andrew Gelman +- new function write.model() based on ideas from Jouni Kerman + +Update 2.0-1 (26 May 2006): +- some wine patches for 2.2-0 by Gregor Gorjanc + +Update 2.0-2 (26 July 2006): +- changes for DIC, making use of BUGS internal calculations +- some doc fixes + +Update 2.0-3 (06 October 2006): +- \\. -> [.] in regular expressions + +Update 2.0-4 (01 November 2006): +- print.bugs / plot.bugs documentation fixes +- write.model() fix Added: trunk/R2WinBUGS/Changes0 =================================================================== --- trunk/R2WinBUGS/Changes0 (rev 0) +++ trunk/R2WinBUGS/Changes0 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,142 @@ +During the process of packaging R2WinBUGS_0.1, quite a lot of changes had +been made. Those changes are not documented anywhere ... + + +Changes prior to R2WinBUGS_0.1: +=============================== + +Update 30 Oct 2003: + 1. Minor change to a return() statement to be compatible with R 1.8 + 2. Just a warning (from Ben Goodrich): if you are running Bugs.R inside + a loop, you have to be careful with the data() and the inits() + functions since they will not necessarily recognize locally-defined + variables. One workaround is to define the variables used in data() + and inits() using global assignments (<<-), but this can sometimes + make the program run slower. +Update 29 Aug 2003: + 1. Fixed "bugs.data.inits" function so you can use data that have the + same names as R functions. + 2. Changed T and F to TRUE and FALSE everywhere in case the variables + T and F are used as data in the main program + 3. Caution: if you are entering the data as a list of variable names + (see 10 Apr 2003 update, item 1), the data to be input into must + be global variables. This can be relevant if you are running bugs() + inside an R function. + 4. Caution: bugs() has difficulty processing ragged arrays. It is + better to save a whole matrix (e.g., "theta") rather than parts + (e.g., "theta[1:10,1]", "theta[1:5,2]"). If you want to save + part of a vector, you should do it as "theta[1:2]", not "theta[1]", + "theta[2]". +Update 30 Apr 2003: added time monitoring +Update 29 Apr 2003: + 1. The "attach.all" function (no longer called "attach2") overwrites + so that all components of a list or data frame get attached. + 2. Program now looks in the directory /winbug~1/ rather than /winbug~2/ + 3. Graphics parameters for margins are returned to their original state + at the end of the program. + 4. Added "digits.summary" option to the numerical display. + 5. Added "last.values" output: a list that can be input as "inits" + if you want to run the simulations longer. +Update 13 Apr 2003: fixed new bug in round.bugs(). Now all numbers are + saved in scientific notation. +Update 10 Apr 2003: + 1. It is now possible to enter the data as a list of variable names. + For example, before you had to enter data as, + data <- list (n=8, y=c(28,8,-3,7,-1,1,18,12)) + or + n <- 8 + y <- c(28,8,-3,7,-1,1,18,12) + data <- list (n=n, y=y) + Now you can enter the data as, + n <- 8 + y <- c(28,8,-3,7,-1,1,18,12) + data <- list ("n", "y") + The bugs() function will figure out which method you are using (based + on whether "data" is a list of numbers or a vector of character + strings). + This doesn't look like much, but it's convenient when you're entering + lots of data! + 2. It is now possible to enter the initial values as a function, + so as to automatically a random list of inits for each of the chains. + For example, in the 8-schools example below, we can do: + inits <- function() + list (theta=rnorm(J,0,1), mu.theta=rnorm(1,0,100), sigma.theta=runif(1,0,100)) + + to set up the inits as a function (rather than setting up n.chains + sets of specific initial values). Then, the function call, + schools.sim <- bugs (data, inits, parameters, "schools.txt", n.chains=3, n.iter=1000) + automatically sets up 3 sets of initial values (each a list of + theta, mu.theta, sigma.theta). + 3. Bug in the initial rounding (the function round.bugs()) has been fixed. + Thanks for Mark Clements for finding the bug and fixing it! + Also, we have set the default rounding to 5 digits instead of 2. +Update 01 Apr 2003: use layout() rather than split.screen() for graphical + display +Update 18 Mar 2003: + 1. Get the Bugs configuration information from the original file + (Registry_default.odc) rather than overwriting each time. (Fixes a + bug that occurred when R was interrupted in the middle of a Bugs run.) + 2. Display different colored dots in the right panel of the graphical + display, to show the medians from each chain. +Update 13 Mar 2003: fix minor bug in monitor() +Update 10 Mar 2003: fix bug in pD and DIC calculations +Update 7 Mar 2003: + 1. Fix display.parallel=T option by adding min.width so that very + intervals are still visible. + 2. Compute pD separately for each sequence (which gives much more + reasonable estimates before convergence). +Update 8 Feb 2003: minor fixes in graphical display +Update 6 Feb 2003: + 1. Approximate "effective sample size" n.eff given for each parameter. + 2. More explanatory material displayed. + 3. Use bringToTop() to automatically bring up the graphics window. +Update 4 Feb 2003: + 1. Automatically compute the deviance, DIC, and pD. Bugs will not + always compute DIC and pD, so we do so using the definition, + DIC = E(deviance) + pD, using var(deviance)/2 as an estimate of pD. + (This is derived from the chi^2 distribution. We can't use the + Spiegelhalter et al. definition of DIC because we don't have access + to the deviance function.) + 2. Set default for n.thin so that, after thinning, the total number + saved iterations, n.sims, is approximately 1000. +Update 14 Jan 2003 to run with the new WinBugs1.4. You may see an error + message and need to fix the dos.location assignment in bugs(). +Update 6 Jan 2003: + 1. Fix of bug that occurred with uppercase and lowercase variable names + 2. Set default for n.thin so that no more than about 500 iterations + will be saved from each sequence + 3. New option "display.parallel" added to show 80% inferences from + parallel sequences on the right panel of the graphical display. This + can be useful to understand what is going on when there are + convergence problems. +Update 26 Dec 2002: fix of minwidth in bugs.plot.summary +Update 11 Dec 2002: + 1. Automatic fixing of adaptive phases. Now you no longer need to run + for thousands of iterations when slice or Metropolis sampling is used. + 2. Various minor fixes +Update 10 Dec 2002: + 1. Cool graphical display of convergence and inferences! + 2. New "attach2" function that overwrites so that all components of + the list are attached +Update 29 Nov 2002: + 1. Fix of bug in 24 Nov update. + 2. Fix of bug in 16 Nov update. + 3. Length of chains is now pecified in terms of "n.iter" rather than + "n.keep". +Update 24 Nov 2002: improved treatment of "parameters.to.save". For + example, you can now use "alpha" to indicate an entire array of parameters, + whereas before you had to save "alpha[]" or "alpha[,]" or whatever. +Update 16 Nov 2002: mean, sd, median added to outputs +Update 4 Nov 2002: more error-flagging added +Update 26 Oct 2002: + 1. Parameters saved in order of the "parameters.to.save" vector + (not alphabetical order). + 2. Output saved in both matrix and list form. + 3. With the attach.sims=T setting (which is the default), the simulations + for all the saved parameters are saved as R objects. This is + convenient for later use of the simulations. +Updates to 16 Oct 2002: more error-flagging added, mean/sd added to summary, + fixing scientific notation so Bugs can always read data and inits +Update 21 Sept 2002: "quit=F" option changed to "debug=T" +First version written 18 Sept 2002 by Andrew Gelman, + adapted from the EmBedBugs package by Kenneth Rice Added: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION (rev 0) +++ trunk/R2WinBUGS/DESCRIPTION 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,19 @@ +Package: R2WinBUGS +Title: Running WinBUGS and OpenBUGS from R +Date: 2006-11-01 +Version: 2.0-4 +Author: originally written by Andrew Gelman <ge...@st...>; + changes and packaged by Sibylle Sturtz <st...@st...> + and Uwe Ligges <li...@st...>. + With considerable contributions by Gregor Gorjanc <gre...@bf...> + and Jouni Kerman <ke...@st...>. +Description: Using this package, + it is possible to call a BUGS model, summarize inferences and + convergence in a table and graph, and save the simulations in arrays for easy access in R. +Depends: R (>= 2.2.0) +Suggests: coda (>= 0.9-0), BRugs (>= 0.3-0) +SystemRequirements: WinBUGS 1.4 on Windows +URL: http://www.stat.columbia.edu/~gelman/bugsR/ +Maintainer: Sibylle Sturtz <st...@st...> +License: GPL version 2 +Packaged: Thu Nov 2 08:53:20 2006; ligges Added: trunk/R2WinBUGS/NAMESPACE =================================================================== --- trunk/R2WinBUGS/NAMESPACE (rev 0) +++ trunk/R2WinBUGS/NAMESPACE 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,4 @@ +export(bugs, attach.all, detach.all, attach.bugs, detach.bugs, bugs.data, read.bugs, +bugs.log, openbugs, as.bugs.array, write.model) +S3method(print, bugs) +S3method(plot, bugs) Added: trunk/R2WinBUGS/R/as.bugs.array.R =================================================================== --- trunk/R2WinBUGS/R/as.bugs.array.R (rev 0) +++ trunk/R2WinBUGS/R/as.bugs.array.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,135 @@ +as.bugs.array <- function(sims.array, model.file=NULL, program=NULL, DIC=FALSE, n.iter=NULL, n.burnin=0, n.thin=1) +{ + # Jouni Kerman's function to convert a 3-way array to a Bugs object + # + # 'sims.array' is supposed to be a 3-way array with + # n.sims*n.chains*n.parameters simulations, and + # the 3rd component of dimnames(x) should have the parameter names. + d <- dim(sims.array) + n.keep <- d[1] + n.chains <- d[2] + n.parameters <- d[3] + n.sims <- n.keep*n.chains + if (is.null(n.iter)) n.iter <- (n.keep + n.burnin)*n.thin + # + parameter.names <- dimnames(sims.array)[[3]] + if (is.null(parameter.names)) { + parameter.names <- paste("P", 1:n.parameters, sep="") + dimnames(sims.array)[[3]] <- parameter.names + } + parameters.to.save <- unique(sapply(strsplit(parameter.names,"\\["),"[",1)) + # + sims <- matrix(NA, n.sims, n.parameters) + root.long <- character(n.parameters) + indexes.long <- vector(n.parameters, mode = "list") + for (i in 1:n.parameters) { + temp <- decode.parameter.name(parameter.names[i]) + root.long[i] <- temp$root + indexes.long[[i]] <- temp$indexes + } + n.roots <- length(parameters.to.save) + left.bracket.short <- as.vector(regexpr("[[]", parameters.to.save)) + right.bracket.short <- as.vector(regexpr("[]]", parameters.to.save)) + root.short <- ifelse(left.bracket.short == -1, parameters.to.save, + substring(parameters.to.save, 1, left.bracket.short - 1)) + dimension.short <- rep(0, n.roots) + indexes.short <- vector(n.roots, mode = "list") + n.indexes.short <- vector(n.roots, mode = "list") + long.short <- vector(n.roots, mode = "list") + length.short <- numeric(n.roots) + for (j in 1:n.roots) { + long.short[[j]] <- (1:n.parameters)[root.long == root.short[j]] + length.short[j] <- length(long.short[[j]]) + if (length.short[j] == 0){ + stop(paste("parameter", root.short[[j]], "is not in the model")) + } + else if (length.short[j] > 1) { + dimension.short[j] <- length(indexes.long[[long.short[[j]][1]]]) + n.indexes.short[[j]] <- numeric(dimension.short[j]) + for (k in 1:dimension.short[j]){ + n.indexes.short[[j]][k] <- length(unique(unlist(lapply(indexes.long[long.short[[j]]], .subset, k)))) + } + length.short[j] <- prod(n.indexes.short[[j]]) + if (length(long.short[[j]]) != length.short[j]){ + stop(paste("error in parameter", root.short[[j]], + "in parameters.to.save")) + } + indexes.short[[j]] <- as.list(numeric(length.short[j])) + for (k in 1:length.short[j]){ + indexes.short[[j]][[k]] <- indexes.long[[long.short[[j]][k]]] + } + } + } + rank.long <- unlist(long.short) + # ----- + # yes, it's inefficient to do this, but for now I'm just letting this be as it is: + for (k in 1:n.parameters) { + sims[,k] <- as.vector(sims.array[,,k]) + } + # ---- + dimnames(sims) <- list(NULL, parameter.names) + summary <- monitor(sims.array, n.chains, keep.all = TRUE) + last.values <- as.list(numeric(n.chains)) + for (i in 1:n.chains) { + n.roots.0 <- if (!is.null(DIC)) + n.roots - 1 + else n.roots + last.values[[i]] <- as.list(numeric(n.roots.0)) + names(last.values[[i]]) <- root.short[1:n.roots.0] + for (j in 1:n.roots.0) { + if (dimension.short[j] <= 1) { + last.values[[i]][[j]] <- sims.array[n.keep, i, long.short[[j]]] + names(last.values[[i]][[j]]) <- NULL + } + else{ + last.values[[i]][[j]] <- aperm(array(sims.array[n.keep, i, + long.short[[j]]], rev(n.indexes.short[[j]])), dimension.short[j]:1) + } + } + } + sims <- sims[sample(n.sims), ] + sims.list <- summary.mean <- summary.sd <- summary.median <- vector(n.roots, + mode = "list") + names(sims.list) <- names(summary.mean) <- names(summary.sd) <- names(summary.median) <- root.short + for (j in 1:n.roots) { + if (length.short[j] == 1) { + sims.list[[j]] <- sims[, long.short[[j]]] + summary.mean[[j]] <- summary[long.short[[j]], "mean"] + summary.sd[[j]] <- summary[long.short[[j]], "sd"] + summary.median[[j]] <- summary[long.short[[j]], "50%"] + } + else { + temp2 <- dimension.short[j]:1 + sims.list[[j]] <- aperm(array(sims[, long.short[[j]]], + c(n.sims, rev(n.indexes.short[[j]]))), c(1, (dimension.short[j]+1):2)) + summary.mean[[j]] <- aperm(array(summary[long.short[[j]], + "mean"], rev(n.indexes.short[[j]])), temp2) + summary.sd[[j]] <- aperm(array(summary[long.short[[j]], + "sd"], rev(n.indexes.short[[j]])), temp2) + summary.median[[j]] <- aperm(array(summary[long.short[[j]], + "50%"], rev(n.indexes.short[[j]])), temp2) + } + } + summary <- summary[rank.long, ] + all <- list(n.chains = n.chains, n.iter = n.iter, n.burnin = n.burnin, + n.thin = n.thin, n.keep = n.keep, n.sims = n.sims, + sims.array = sims.array[,,rank.long,drop = FALSE], sims.list = sims.list, + sims.matrix = sims[, rank.long], summary = summary, mean = summary.mean, + sd = summary.sd, median = summary.median, root.short = root.short, + long.short = long.short, dimension.short = dimension.short, + indexes.short = indexes.short, last.values = last.values, program=program, + model.file=model.file, is.DIC=!is.null(DIC), DIC=DIC) + if(sum(DIC)) { + deviance <- all$sims.array[, , dim(sims.array)[3], drop = FALSE] + dim(deviance) <- dim(deviance)[1:2] + pD <- numeric(n.chains) + DIC <- numeric(n.chains) + for (i in 1:n.chains) { + pD[i] <- var(deviance[, i])/2 + DIC[i] <- mean(deviance[, i]) + pD[i] + } + all <- c(all, list(pD = mean(pD), DIC = mean(DIC))) + } + class(all) <- "bugs" + return(all) +} Property changes on: trunk/R2WinBUGS/R/as.bugs.array.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/attach.all.R =================================================================== --- trunk/R2WinBUGS/R/attach.all.R (rev 0) +++ trunk/R2WinBUGS/R/attach.all.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,36 @@ +attach.all <- function(x, overwrite = NA, name = "attach.all"){ + rem <- names(x) %in% ls(.GlobalEnv) + if(!any(rem)) overwrite <- FALSE + rem <- names(x)[rem] + if(is.na(overwrite)){ + question <- paste("The following objects in .GlobalEnv will mask\nobjects in the attached database:\n", + paste(rem, collapse=", "), + "\nRemove these objects from .GlobalEnv?", sep="") + if(interactive()){ + if(.Platform$OS.type == "windows") + overwrite <- "YES" == winDialog(type = "yesno", question) + else + overwrite <- 1 == menu(c("YES", "NO"), graphics = FALSE, title = question) + } + else overwrite <- FALSE + } + if(overwrite) remove(list=rem, envir=.GlobalEnv) + attach(x, name=name) +} + +attach.bugs <- function (x, overwrite = NA){ + if(class(x) != "bugs") + stop("attach.all() requires a bugs object.") + if("bugs.sims" %in% search()){ + detach("bugs.sims")} + x$sims.list$n.sims <- x$n.sims # put n.sims into sims.list for convenience + r2 <- attach.all(x$sims.list, overwrite = overwrite, name = "bugs.sims") + invisible (bugs.sims = r2) +} + +detach.all <- function(name = "attach.all") + do.call("detach", list(name=name)) + +detach.bugs <- function(){ + detach.all("bugs.sims") +} Property changes on: trunk/R2WinBUGS/R/attach.all.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.R =================================================================== --- trunk/R2WinBUGS/R/bugs.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,70 @@ +"bugs" <- +function(data, inits, parameters.to.save, model.file = "model.bug", + 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/", + program = c("winbugs", "openbugs", "WinBugs", "OpenBugs"), + working.directory = NULL, + clearWD = FALSE, useWINE = .Platform$OS.type != "windows", WINE = Sys.getenv("WINE"), + newWINE = FALSE, WINEPATH = NULL){ + + ## If OpenBUGS, we only call openbugs() and exit... + program <- match.arg(program) + if (program %in% c("openbugs", "OpenBugs")) + return(openbugs(data, inits, parameters.to.save, model.file, + n.chains, n.iter, n.burnin, n.thin, DIC, bugs.directory, + working.directory, digits)) + + ## 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 (useWINE) { # attempt to find wine and winepath + if (!nchar(WINE)) { + WINE <- system("locate wine | grep bin/wine$", intern = TRUE) + WINE <- WINE[length(WINE)] + } + if (!length(WINE)) stop("couldn't locate WINE binary") + if (!nchar(WINEPATH)) { + WINEPATH <- system("locate winepath | grep bin/winepath$", intern = TRUE) + WINEPATH <- WINEPATH[length(WINEPATH)] + } + if (!length(WINEPATH)) stop("couldn't locate WINEPATH binary") + } + 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(try(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, + new.model.file, debug=debug, is.inits=!is.null(inits), + bin = bin, DIC = DIC, useWINE = useWINE, newWINE = newWINE, WINEPATH = WINEPATH) + bugs.run(n.burnin, bugs.directory, WINE = WINE, useWINE = useWINE) + + if(codaPkg) + return(file.path(getwd(), paste("coda", 1:n.chains, ".txt", sep=""))) + + sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), + model.file = model.file, is.DIC = !is.null(DIC), program = program) + if(clearWD) + file.remove(c("data.txt", "log.odc", "log.txt", "codaIndex.txt", + paste("inits", 1:n.chains, ".txt", sep=""), + paste("coda", 1:n.chains, ".txt", sep=""))) + class(sims) <- "bugs" + return(sims) +} Property changes on: trunk/R2WinBUGS/R/bugs.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.data.R =================================================================== --- trunk/R2WinBUGS/R/bugs.data.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.data.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,12 @@ +"bugs.data" <- +function(data, dir = getwd(), digits = 5){ + if(is.numeric(unlist(data))) + write.datafile(lapply(data, formatC, digits = digits, format = "E"), + file.path(dir, "data.txt")) + else { + data.list <- lapply(as.list(data), get, pos = parent.frame(2)) + names(data.list) <- as.list(data) + write.datafile(lapply(data.list, formatC, digits = digits, format = "E"), + file.path(dir, "data.txt")) + } +} Property changes on: trunk/R2WinBUGS/R/bugs.data.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.inits.R =================================================================== --- trunk/R2WinBUGS/R/bugs.inits.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.inits.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,13 @@ +"bugs.inits" <- +function (inits, n.chains, digits){ + if(!is.null(inits)){ + for (i in 1:n.chains){ + if (is.function(inits)) + write.datafile(lapply(inits(), formatC, digits = digits, format = "E"), + paste ("inits", i, ".txt", sep="")) + else + write.datafile(lapply(inits[[i]], formatC, digits = digits, format = "E"), + paste ("inits", i, ".txt", sep="")) + } + } +} Property changes on: trunk/R2WinBUGS/R/bugs.inits.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.log.R =================================================================== --- trunk/R2WinBUGS/R/bugs.log.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.log.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,46 @@ +bugs.log <- function(file) +{ + if(!file.exists(file)) + stop("Log file", file, "does not exist") + logfile <- readLines(file) + + statsStart <- which(logfile == "Node statistics") + 2 + if(!length(statsStart)) + stop("Log file", file, "does not contain node statistics.") + ## + 2 to remove + ## "Node statistics" + ## "\t node\t mean\t sd\t MC error\t2.5%\tmedian\t97.5%\tstart\tsample" + + statsEnd <- which(logfile == "dic.stats()") - 1 + ## + 1 to remove + ## "dic.stats()" + + statsTable <- logfile[statsStart:statsEnd] + statsTable <- sub("\t", "", statsTable) + statsTable <- sapply(strsplit(statsTable, "\t"), "[") + colnames(statsTable) <- statsTable[1,] + statsTable <- t(apply(statsTable[-1,], 2, as.numeric)) + colnames(statsTable) <- c("mean", "sd", "MC error", "2.5%", "median", "97.5%", "start", "sample") + + ## DIC + DICStart <- which(logfile == "DIC") + 3 + ## + 3 to remove + ## "DIC" + ## "Dbar = post.mean of -2logL; Dhat = -2LogL at post.mean of stochastic nodes" + ## "\tDbar\tDhat\tpD\tDIC\t" + + DICEnd <- grep("history(", logfile, fixed = TRUE) - 1 + ## - 1 to remove + ## "history(+, ..." + + if(!length(DICEnd) || !length(DICStart) || (DICEnd < DICStart)){ + DICTable <- NA + }else{ + DICTable <- logfile[DICStart:DICEnd] + DICTable <- sapply(strsplit(DICTable, "\t"), "[") + colnames(DICTable) <- DICTable[1,] + DICTable <- t(apply(DICTable[-1,], 2, as.numeric)) + colnames(DICTable) <- c("Dbar", "Dhat", "pD", "DIC") + } + return(list(stats = statsTable, DIC = DICTable)) +} Property changes on: trunk/R2WinBUGS/R/bugs.log.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.plot.inferences.R =================================================================== --- trunk/R2WinBUGS/R/bugs.plot.inferences.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.plot.inferences.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,110 @@ +"bugs.plot.inferences" <- +function (sims, display.parallel, ...){ + if (.Device=="windows" || + (.Device=="null device" && options("device")=="windows")){ + cex.names <- .7 + cex.axis <- .6 + cex.tiny <- .4 + cex.points <- .7 + standard.width <- 30 + max.width <- 40 + min.width <- .02 + } + else { + cex.names <- .7 + cex.axis <- .6 + cex.tiny <- .4 + cex.points <- .3 + standard.width <- 30 + max.width <- 40 + min.width <- .01 + } + rootnames <- sims$root.short + n.roots <- length(rootnames) + sims.array <- sims$sims.array + n.chains <- sims$n.chains + dimension.short <- sims$dimension.short + indexes.short <- sims$indexes.short + long.short <- sims$long.short + height <- .6 + par (mar=c(0,0,1,0)) + plot (c(0,1), c(-n.roots-.5,-.4), + ann=FALSE, bty="n", xaxt="n", yaxt="n", type="n") + W <- max(strwidth(rootnames, cex=cex.names)) + B <- (1-W)/3.8 + A <- 1-3.5*B + if (display.parallel) + text (A, -.4, "80% interval for each chain", adj=0, cex=cex.names) + else + text (A, -.4, "medians and 80% intervals", adj=0, cex=cex.names) + num.height <- strheight (1:9, cex=cex.tiny) + for (k in 1:n.roots){ + text (0, -k, rootnames[k], adj=0, cex=cex.names) + J <- min (length(long.short[[k]]), max.width) + if (k==1) + index <- 1:J + else + index <- sum (unlist(lapply(long.short,length))[1:(k-1)]) + 1:J + spacing <- 3.5/max(J,standard.width) + med <- numeric(J) + i80 <- matrix( , J, 2) + med.chains <- matrix( , J, sims$n.chains) + i80.chains <- array(NA, c(J, sims$n.chains, 2)) + for (j in 1:J){ + med[j] <- median(sims.array[,,index[j]]) + i80[j,] <- quantile(sims.array[,,index[j]], c(.1,.9)) + for (m in 1:n.chains){ + med.chains[j,m] <- quantile (sims.array[,m,index[j]], .5) + i80.chains[j,m,] <- quantile (sims.array[,m,index[j]], c(.1,.9)) + } + } + rng <- range (i80, i80.chains) + p.rng <- pretty(rng, n = 2) + b <- height/(max(p.rng) - min(p.rng)) + a <- -(k+height/2) - b*p.rng[1] + lines (A+c(0,0), -k+c(-height/2,height/2)) +# +# plot a line at zero (if zero is in the range of the mini-plot) +# + if (min(p.rng)<0 & max(p.rng)>0) + lines (A+B*spacing*c(0,J+1), rep (a,2), lwd=.5, col="gray") + + for (x in p.rng){ + text (A-B*.2, a+b*x, x, cex=cex.axis) + lines (A+B*c(-.05,0), rep(a+b*x,2)) + } + for (j in 1:J){ + if (display.parallel){ + for (m in 1:n.chains){ + interval <- a + b*i80.chains[j,m,] + if (interval[2]-interval[1] < min.width) + interval <- mean(interval) + c(-1,1)*min.width/2 + lines (A+B*spacing*rep(j+.6*(m-(n.chains+1)/2)/n.chains,2), + interval, lwd=.5, col=m+1) + } + } + else { + lines (A+B*spacing*rep(j,2), a + b*i80[j,], lwd=.5) + for (m in 1:n.chains) +# points (A+B*spacing*j, a + b*med[j], pch=20, cex=cex.points) + points (A+B*spacing*j, a + b*med.chains[j,m], pch=20, cex=cex.points, + col=m+1) + } + dk <- dimension.short[k] + if (dk>0){ + for (m in 1:dk){ + index0 <- indexes.short[[k]][[j]][m] + if (j==1) + text(A+B*spacing*j, -k-height/2-.05-num.height*(m-1), index0, + cex=cex.tiny) + else if (index0!=indexes.short[[k]][[j-1]][m] & + (index0%%(floor(log10(index0)+1))==0)) + text(A+B*spacing*j, -k-height/2-.05-num.height*(m-1), index0, + cex=cex.tiny) + } + } + } + if (J<length(long.short[[k]])) text (-.015, -k, "*", + cex=cex.names, col="red") + } +} Property changes on: trunk/R2WinBUGS/R/bugs.plot.inferences.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.plot.summary.R =================================================================== --- trunk/R2WinBUGS/R/bugs.plot.summary.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.plot.summary.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,124 @@ +"bugs.plot.summary" <- +function (sims, ...){ + DIC <- sims$is.DIC + if (.Device=="windows" || + (.Device=="null device" && options("device")=="windows")){ + cex.names <- .7 + cex.top <- .7 + cex.points <- .7 + max.length <- 50 + min.width <- .01 + } + else { + cex.names <- .7 + cex.top <- .7 + cex.points <- .3 + max.length <- 80 + min.width <- .005 + } + summ <- sims$summary + sims.array <- sims$sims.array + n.chains <- sims$n.chains + n.parameters <- nrow(summ) + + J0 <- unlist(lapply(sims$long.short, length)) + if (DIC) J0 <- J0[1:(length(J0)-1)] # don't display deviance summaries + J <- J0 + total <- ceiling(sum(J+.5)) + while ((total > max.length) && max(J)>1){### vielleicht optimieren ... + J[J==max(J)] <- max(J)-1 + total <- ceiling(sum(J+.5)) + } + pos <- -1 + ypos <- NULL + id <- NULL + ystart <- NULL + jj <- 1:J[1] + n.roots <- length(sims$root.short) + if (DIC) n.roots <- n.roots-1 # don't display deviance summaries + ystart <- numeric(n.roots) + for (k in 1:n.roots){ + ystart[k] <- pos + ypos <- c(ypos, pos - seq(0, J[k]-1)) + id <- c(id, 1:J[k]) + pos <- pos - J[k] -.5 + if (k>1) jj <- c(jj, sum(J0[1:(k-1)]) + (1:J[k])) + } + bottom <- min(ypos)-1 + + med <- numeric(sum(J)) + i80 <- matrix( , sum(J), 2) + i80.chains <- array (NA, c(sum(J), n.chains, 2)) + for (j in 1:sum(J)){ + med[j] <- median (sims.array[,,jj[j]]) + i80[j,] <- quantile (sims.array[,,jj[j]], c(.1,.9)) + for (m in 1:n.chains) + i80.chains[j,m,] <- quantile (sims.array[,m,jj[j]], c(.1,.9)) + } + rng <- range (i80, i80.chains) + p.rng <- pretty(rng, n = 2) + b <- 2 / (max(p.rng) - min(p.rng)) + a <- -b * p.rng[1] + + par (mar=c(0,0,1,3)) + plot (c(0,1), c(min(bottom, -max.length)-3,2.5), + ann=FALSE, bty="n", xaxt="n", yaxt="n", type="n") + W <- max(strwidth(unlist(dimnames(summ)[[1]]), cex=cex.names)) + B <- (1-W)/3.6 + A <- 1-3.5*B + B <- (1-A)/3.5 + b <- B*b + a <- A + B*a + text (A+B*1, 2.5, "80% interval for each chain", cex=cex.top) + lines (A+B*c(0,2), c(0,0)) + lines (A+B*c(0,2), rep(bottom,2)) + if(n.chains > 1){ + text (A+B*3, 2.6, "R-hat", cex=cex.top) + lines (A+B*c(2.5,3.5), c(0,0)) + lines (A+B*c(2.5,3.5), rep(bottom,2)) + } +# +# line at zero +# + if (min(p.rng)<0 & max(p.rng)>0) + lines (rep(a,2), c(0,bottom), lwd=.5, col="gray") + + for (x in p.rng){ + text (a+b*x, 1, x, cex=cex.names) + lines (rep(a+b*x,2), c(0,-.2)) + text (a+b*x, bottom-1, x, cex=cex.names) + lines (rep(a+b*x,2), bottom+c(0,.2)) + } + if(n.chains > 1) + for (x in seq(1,2,.5)){ + text (A+B*(1.5+seq(1,2,.5)), rep(1,3), c("1","1.5","2+"), cex=cex.names) + lines (A+B*rep(1.5+x,2), c(0,-.2)) + text (A+B*(1.5+seq(1,2,.5)), rep(bottom-1,3), c("1","1.5","2+"), + cex=cex.names) + lines (A+B*rep(1.5+x,2), bottom+c(0,.2)) + } + for (j in 1:sum(J)){ + name <- dimnames(summ)[[1]][jj[j]] + if (id[j]==1) + text (0, ypos[j], name, adj=0, cex=cex.names) + else { + pos <- as.vector(regexpr("[[]", name)) + text (strwidth(substring(name,1,pos-1),cex=cex.names), + ypos[j], substring(name, pos, nchar(name)), adj=0, cex=cex.names) + } + for (m in 1:n.chains){ + interval <- a + b*i80.chains[j,m,] + if (interval[2]-interval[1] < min.width) + interval <- mean(interval) + c(-1,1)*min.width/2 + lines (interval, rep(ypos[j]-.1*(m-(n.chains+1)/2),2), lwd=1, col=m+1) + if(n.chains > 1) + points (A+B*(1.5 + min(max(summ[jj[j],"Rhat"],1),2)), ypos[j], pch=20, cex=cex.points) + } + } + for (k in 1:n.roots){ + if (J[k]<J0[k]) text (-.015, ystart[k], "*", cex=cex.names, + col="red") + } + if (sum(J!=J0)>0) text (0, bottom-3, + "* array truncated for lack of space", adj=0, cex=cex.names, col="red") +} Property changes on: trunk/R2WinBUGS/R/bugs.plot.summary.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.run.R =================================================================== --- trunk/R2WinBUGS/R/bugs.run.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.run.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,35 @@ +"bugs.run" <- + function(n.burnin, bugs.directory, WINE = "", + useWINE = .Platform$OS.type != "windows", newWINE = TRUE){ + +if(useWINE && !newWINE) bugs.directory <- win2native(bugs.directory) + +## Update the lengths of the adaptive phases in the Bugs updaters + try(bugs.update.settings(n.burnin, bugs.directory)) +## Return the lengths of the adaptive phases to their original settings + on.exit(try(file.copy(file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), + file.path(bugs.directory, "System/Rsrc/Registry.odc"), + overwrite = TRUE))) +## Search Win*.exe (WinBUGS executable) within bugs.directory + dos.location <- file.path(bugs.directory, + grep("^Win[[:alnum:]]*[.]exe$", list.files(bugs.directory), value = TRUE)[1]) + if(!file.exists(dos.location)) + stop(paste("WinBUGS executable does not exist in", bugs.directory)) +## Call Bugs and have it run with script.txt + bugsCall <- paste("\"", dos.location, "\" /par \"", + native2win(file.path(getwd(), "script.txt"), newWINE = newWINE), + "\"", sep = "") + if (useWINE) + bugsCall <- paste(WINE, bugsCall) + temp <- system(bugsCall) + + if(temp == -1) + stop("Error in bugs.run().\nCheck that WinBUGS is in the specified directory.") +## Stop and print an error message if Bugs did not run correctly + if (length(grep("Bugs did not run correctly", + scan("coda1.txt", character(), quiet=TRUE, sep="\n"))) > 0) + stop("Look at the log file and\ntry again with debug=TRUE and figure out what went wrong within Bugs.") +} + + + Property changes on: trunk/R2WinBUGS/R/bugs.run.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.script.R =================================================================== --- trunk/R2WinBUGS/R/bugs.script.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.script.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,48 @@ +"bugs.script" <- +function (parameters.to.save, n.chains, n.iter, n.burnin, + n.thin, model.file, debug=FALSE, is.inits, bin, + DIC = FALSE, useWINE = FALSE, newWINE = FALSE, WINEPATH = NULL){ +### Write file script.txt for Bugs to read +### if (n.chains<2) stop ("n.chains must be at least 2") + + if((ceiling(n.iter/n.thin) - ceiling(n.burnin/n.thin)) < 2) + stop ("(n.iter-n.burnin)/n.thin must be at least 2") + working.directory <- getwd() + script <- "script.txt" + model <- if(length(grep("\\\\", model.file)) || length(grep("/", model.file))){ + model.file + } else file.path(working.directory, model.file) + data <- file.path(working.directory, "data.txt") + history <- file.path(working.directory, "history.odc") + coda <- file.path(working.directory, "coda") + logfile <- file.path(working.directory, "log.odc") + logfileTxt <- file.path(working.directory, "log.txt") + inits <- sapply(paste(working.directory, "/inits", 1:n.chains, ".txt", sep=""), native2win) + initlist <- paste("inits (", 1:n.chains, ", '", inits, "')\n", sep="") + savelist <- paste("set (", parameters.to.save, ")\n", sep="") + redo <- ceiling((n.iter-n.burnin)/(n.thin*bin)) + cat( + "display ('log')\n", + "check ('", native2win(model), "')\n", + "data ('", native2win(data), "')\n", + "compile (", n.chains, ")\n", + if(is.inits) initlist, + "gen.inits()\n", + "thin.updater (", n.thin, ")\n", + "update (", ceiling(n.burnin/n.thin), ")\n", + savelist, + if(DIC) "dic.set()\n", + rep( + c("update (", formatC(ceiling(bin), format = "d"), ")\n", + "coda (*, '", native2win(coda), "')\n"),redo), + "stats (*)\n", + if(DIC) "dic.stats()\n", + "history (*, '", native2win(history), "')\n", + "save ('", native2win(logfile), "')\n", + "save ('", native2win(logfileTxt), "')\n", + file=script, sep="", append=FALSE) + if (!debug) cat ("quit ()\n", file=script, append=TRUE) + sims.files <- paste ("coda", 1:n.chains, ".txt", sep="") + for (i in 1:n.chains) cat ("WinBUGS did not run correctly.\n", + file=sims.files[i], append=FALSE) +} Property changes on: trunk/R2WinBUGS/R/bugs.script.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.sims.R =================================================================== --- trunk/R2WinBUGS/R/bugs.sims.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.sims.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,130 @@ +"bugs.sims" <- +function (parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC = TRUE){ +# Read the simulations from Bugs into R, format them, and monitor convergence + sims.files <- paste ("coda", 1:n.chains, ".txt", sep="") + index <- read.table ("codaIndex.txt", header=FALSE, sep="\t") + parameter.names <- as.vector(index[,1]) + n.keep <- index[1,3] - index[1,2] + 1 + n.parameters <- length(parameter.names) + n.sims <- n.keep*n.chains + sims <- matrix( , n.sims, n.parameters) + sims.array <- array (NA, c(n.keep, n.chains, n.parameters)) + root.long <- character(n.parameters) + indexes.long <- vector(n.parameters, mode = "list") + for (i in 1:n.parameters){ + temp <- decode.parameter.name(parameter.names[i]) + root.long[i] <- temp$root + indexes.long[[i]] <- temp$indexes + } + n.roots <- length(parameters.to.save) + left.bracket.short <- as.vector (regexpr("[[]", parameters.to.save)) + right.bracket.short <- as.vector (regexpr("[]]", parameters.to.save)) + root.short <- ifelse (left.bracket.short==-1, parameters.to.save, + substring (parameters.to.save, 1, left.bracket.short-1)) + dimension.short <- rep(0, n.roots) + indexes.short <- vector(n.roots, mode = "list") + n.indexes.short <- vector(n.roots, mode = "list") + long.short <- vector(n.roots, mode = "list") + length.short <- numeric(n.roots) + ##SS, UL##: Let's optimize the following loops ... + for (j in 1:n.roots){ + long.short[[j]] <- (1:n.parameters)[root.long==root.short[j]] + length.short[j] <- length(long.short[[j]]) + if (length.short[j]==0) + stop (paste ("parameter", root.short[[j]], "is not in the model")) + else if (length.short[j]>1){ + dimension.short[j] <- length(indexes.long[[long.short[[j]][1]]]) + n.indexes.short[[j]] <- numeric(dimension.short[j]) + for (k in 1:dimension.short[j]) n.indexes.short[[j]][k] <- length ( + unique (unlist (lapply (indexes.long[long.short[[j]]], .subset, k)))) + length.short[j] <- prod(n.indexes.short[[j]]) + if (length(long.short[[j]])!=length.short[j]) stop (paste + ("error in parameter", root.short[[j]], "in parameters.to.save")) + indexes.short[[j]] <- as.list(numeric(length.short[j])) + for (k in 1:length.short[j]) + indexes.short[[j]][[k]] <- indexes.long[[long.short[[j]][k]]] + } + } +# rank.long <- rank(paste(rep(root.short,length.short), +# (1:n.parameters)/10^ceiling(log10(n.parameters)),sep=".")) + rank.long <- unlist(long.short) + + for (i in 1:n.chains){ + sims.i <- scan (sims.files[i], quiet=TRUE) [2*(1:(n.keep*n.parameters))] + sims[(n.keep*(i-1)+1):(n.keep*i), ] <- sims.i + sims.array[,i,] <- sims.i + } + dimnames (sims) <- list (NULL, parameter.names) + dimnames (sims.array) <- list (NULL, NULL, parameter.names) +# +# Perform convergence checks and compute medians and quantiles. +# + summary <- monitor (sims.array, n.chains, keep.all=TRUE) +# +# Create outputs +# + last.values <- as.list (numeric(n.chains)) + for (i in 1:n.chains){ + n.roots.0 <- if(DIC) n.roots-1 else n.roots + last.values[[i]] <- as.list (numeric(n.roots.0)) + names(last.values[[i]]) <- root.short[1:n.roots.0] + for (j in 1:n.roots.0){ + if (dimension.short[j]<=1){ + last.values[[i]][[j]] <- sims.array[n.keep,i,long.short[[j]]] + names(last.values[[i]][[j]]) <- NULL + } + else + last.values[[i]][[j]] <- aperm (array(sims.array[n.keep,i,long.short[[j]]], + rev(n.indexes.short[[j]])), dimension.short[j]:1) + } + } + sims <- sims [sample(n.sims),] # scramble (for convenience in analysis) + sims.list <- summary.mean <- summary.sd <- summary.median <- vector(n.roots, mode = "list") + names(sims.list) <- names(summary.mean) <- names(summary.sd) <- names(summary.median) <- root.short + for (j in 1:n.roots){ + if (length.short[j]==1){ + sims.list[[j]] <- sims[,long.short[[j]]] + summary.mean[[j]] <- summary[long.short[[j]],"mean"] + summary.sd[[j]] <- summary[long.short[[j]],"sd"] + summary.median[[j]] <- summary[long.short[[j]],"50%"] + } + else{ + temp2 <- dimension.short[j]:1 + sims.list[[j]] <- aperm (array (sims[,long.short[[j]]], + c(n.sims,rev(n.indexes.short[[j]]))), c(1,(dimension.short[j]+1):2)) + summary.mean[[j]] <- aperm (array (summary[long.short[[j]],"mean"], + rev(n.indexes.short[[j]])), temp2) + summary.sd[[j]] <- aperm (array (summary[long.short[[j]],"sd"], + rev(n.indexes.short[[j]])), temp2) + summary.median[[j]] <- aperm (array (summary[long.short[[j]],"50%"], + rev(n.indexes.short[[j]])), temp2) + } + } + summary <- summary[rank.long,] + all <- list (n.chains=n.chains, n.iter=n.iter, n.burnin=n.burnin, + n.thin=n.thin, n.keep=n.keep, n.sims=n.sims, + sims.array=sims.array[,,rank.long,drop=FALSE], sims.list=sims.list, + sims.matrix=sims[,rank.long], summary=summary, mean=summary.mean, + sd=summary.sd, median=summary.median, root.short=root.short, + long.short=long.short, dimension.short=dimension.short, + indexes.short=indexes.short, last.values=last.values) + if(DIC){ + LOG <- bugs.log("log.txt")$DIC + if(any(is.na(LOG))){ + deviance <- all$sims.array[, , dim(sims.array)[3], drop = FALSE] + dim(deviance) <- dim(deviance)[1:2] + pD <- numeric(n.chains) + DIC <- numeric(n.chains) + for (i in 1:n.chains) { + pD[i] <- var(deviance[, i])/2 + DIC[i] <- mean(deviance[, i]) + pD[i] + } + all <- c(all, list(pD = mean(pD), DIC = mean(DIC), DICbyR = TRUE)) + } else { + DIC <- LOG[nrow(LOG),4] + pD <- LOG[nrow(LOG),3] + all <- c(all, list(pD = pD, DIC = DIC, DICbyR = FALSE)) + } + } + return(all) +} Property changes on: trunk/R2WinBUGS/R/bugs.sims.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/bugs.update.settings.R =================================================================== --- trunk/R2WinBUGS/R/bugs.update.settings.R (rev 0) +++ trunk/R2WinBUGS/R/bugs.update.settings.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,30 @@ +"bugs.update.settings" <- +function (n.burnin, bugs.directory){ + char.burnin <- as.character(n.burnin - 1) + file.copy(file.path(bugs.directory, "System/Rsrc/Registry.odc"), + file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), + overwrite = TRUE) + registry <- readBin(file.path(bugs.directory, "System/Rsrc/Registry.odc"), + "character", 400, size = 1, endian = "little") + locale <- Sys.getlocale("LC_CTYPE") + Sys.setlocale("LC_CTYPE", "C") + info <- registry[regexpr("Int", registry, fixed = TRUE, useBytes = TRUE) > 0] + while(regexpr("\r", info) > 0){ + newline <- regexpr("\r", info) + info <- substring(info, newline + 1) + line <- substring(info, 1, regexpr("\r", info) - 1) + if(regexpr("AdaptivePhase", line) > 0){ + numpos <- regexpr("Int", line, fixed = TRUE, useBytes = TRUE) + 4 + num <- substring(line, numpos) + if (as.numeric(num) > n.burnin){ + blanks <- rep(" ", nchar(num, type = "chars") - nchar(char.burnin, type = "chars")) + num.new <- paste(paste(blanks, collapse = ""), char.burnin, sep = "") + line.new <- sub(num, num.new, line) + registry <- sub(line, line.new, registry) + } + } + } + Sys.setlocale("LC_CTYPE", locale) + writeBin (registry, + file.path(bugs.directory, "System/Rsrc/Registry.odc"), endian = "little") +} Property changes on: trunk/R2WinBUGS/R/bugs.update.settings.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/conv.par.R =================================================================== --- trunk/R2WinBUGS/R/conv.par.R (rev 0) +++ trunk/R2WinBUGS/R/conv.par.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,86 @@ +"conv.par" <- +function (x, n.chains, Rupper.keep = TRUE) { + m <- ncol(x) + n <- nrow(x) + +# We compute the following statistics: +# +# xdot: vector of sequence means +# s2: vector of sequence sample variances (dividing by n-1) +# W = mean(s2): within MS +# B = n*var(xdot): between MS. +# muhat = mean(xdot): grand mean; unbiased under strong stationarity +# varW = var(s2)/m: estimated sampling var of W +# varB = B^2 * 2/(m+1): estimated sampling var of B +# covWB = (n/m)*(cov(s2,xdot^2) - 2*muhat*cov(s^2,xdot)): +# estimated sampling cov(W,B) +# sig2hat = ((n-1)/n))*W + (1/n)*B: estimate of sig2; unbiased under +# strong stationarity +# quantiles: emipirical quantiles from last half of simulated sequences + + xdot <- apply(x,2,mean) + muhat <- mean(xdot) + s2 <- apply(x,2,var) + W <- mean(s2) + quantiles <- quantile (as.vector(x), probs=c(.025,.25,.5,.75,.975)) + + if ((W > 1.e-8) && (n.chains > 1)) { # non-degenerate case + + B <- n*var(xdot) + varW <- var(s2)/m + varB <- B^2 * 2/(m-1) + covWB <- (n/m)*(cov(s2,xdot^2) - 2*muhat*cov(s2,xdot)) + sig2hat <- ((n-1)*W + B)/n + +# Posterior interval post.range combines all uncertainties +# in a t interval with center muhat, scale sqrt(postvar), +# and postvar.df degrees of freedom. +# +# postvar = sig2hat + B/(mn): variance for the posterior interval +# The B/(mn) term is there because of the +# sampling variance of muhat. +# varpostvar: estimated sampling variance of postvar + + postvar <- sig2hat + B/(m*n) + varpostvar <- max(0, + (((n-1)^2) * varW + (1 + 1/m)^2 * varB + 2 * (n-1) * (1 + 1/m) * covWB) / n^2) + post.df <- min(2*(postvar^2/varpostvar), 1000) + +# Estimated potential scale reduction (that would be achieved by +# continuing simulations forever) has two components: an estimate and +# an approx. 97.5% upper bound. +# +# confshrink = sqrt(postvar/W), +# multiplied by sqrt(df/(df-2)) as an adjustment for the +### CHANGED TO sqrt((df+3)/(df+1)) +# width of the t-interval with df degrees of freedom. +# +# postvar/W = (n-1)/n + (1+1/m)(1/n)(B/W); we approximate the sampling dist. +# of (B/W) by an F distribution, with degrees of freedom estimated +# from the approximate chi-squared sampling dists for B and W. (The +# F approximation assumes that the sampling dists of B and W are independent; +# if they are positively correlated, the approximation is conservative.) + + confshrink.range <- postvar/W + if(Rupper.keep){ + varlo.df <- 2*(W^2/varW) + confshrink.range <- c(confshrink.range, + (n-1)/n + (1+1/m)*(1/n)*(B/W) * qf(.975, m-1, varlo.df)) + } + confshrink.range <- sqrt(confshrink.range * (post.df+3) / (post.df+1)) + +# Calculate effective sample size: m*n*min(sigma.hat^2/B,1) +# This is a crude measure of sample size because it relies on the between +# variance, B, which can only be estimated with m degrees of freedom. + + n.eff <- m*n*min(sig2hat/B,1) + list(quantiles=quantiles, confshrink=confshrink.range, + n.eff=n.eff) + + } + else { # degenerate case: all entries in "data matrix" are identical + list (quantiles=quantiles, confshrink = rep(1, Rupper.keep + 1), + n.eff=1) + + } +} Property changes on: trunk/R2WinBUGS/R/conv.par.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/decode.parameter.name.R =================================================================== --- trunk/R2WinBUGS/R/decode.parameter.name.R (rev 0) +++ trunk/R2WinBUGS/R/decode.parameter.name.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,22 @@ +"decode.parameter.name" <- +function (a){ +# +# Decodes Bugs parameter names +# (e.g., "beta[3,14]" becomes "beta" with 2 indexes: 3 and 14) +# for use by the bugs.sim() function +# + left.bracket <- regexpr ("[[]", a) + if (left.bracket==-1){ + root <- a + dimension <- 0 + indexes <- NA + } + else { + root <- substring (a, 1, left.bracket-1) + right.bracket <- regexpr ("[]]", a) + a <- substring (a, left.bracket+1, right.bracket-1) + indexes <- as.numeric(unlist(strsplit(a, ","))) + dimension <- length(indexes) + } + return (list (root=root, dimension=dimension, indexes=indexes)) +} Property changes on: trunk/R2WinBUGS/R/decode.parameter.name.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/formatdata.R =================================================================== --- trunk/R2WinBUGS/R/formatdata.R (rev 0) +++ trunk/R2WinBUGS/R/formatdata.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,24 @@ +"formatdata" <- +function (datalist){ + if (!is.list(datalist) || is.data.frame(datalist)) + stop("Argument to formatdata() must be a list.") + n <- length(datalist) + datalist.string <- vector(n, mode = "list") + for (i in 1:n) { + if (length(datalist[[i]]) == 1) + datalist.string[[i]] <- paste(names(datalist)[i], + "=", as.character(datalist[[i]]), sep = "") + if (is.vector(datalist[[i]]) && length(datalist[[i]]) > 1) + datalist.string[[i]] <- paste(names(datalist)[i], + "=c(", paste(as.character(datalist[[i]]), collapse = ", "), + ")", sep = "") + if (is.array(datalist[[i]])) + datalist.string[[i]] <- paste(names(datalist)[i], + "= structure(.Data= c(", paste(as.character(as.vector(aperm(datalist[[i]]))), + collapse = ", "), "), .Dim=c(", paste(as.character(dim(datalist[[i]])), + collapse = ", "), "))", sep = "") + } + datalist.tofile <- paste("list(", paste(unlist(datalist.string), + collapse = ", "), ")", sep = "") + return(datalist.tofile) +} Property changes on: trunk/R2WinBUGS/R/formatdata.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/monitor.R =================================================================== --- trunk/R2WinBUGS/R/monitor.R (rev 0) +++ trunk/R2WinBUGS/R/monitor.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,51 @@ +"monitor" <- +function (a, n.chains, trans=NULL, keep.all=FALSE, Rupper.keep=FALSE) { + +# If keep.all=T: a is a n x m x k array: +# m sequences of length n, k variables measured +# If keep.all=F: a is a 2n x m x k array (first half will be discarded) +# +# trans is a vector of length k: "" if no transformation, or "log" or "logit" +# (If trans is not defined, it will be set to "log" for parameters that +# are all-positive and 0 otherwise.) +# +# If Rupper.keep=T: keep Rupper. (Otherwise don't display it.) + invlogit <- function (x) {1 / (1 + exp(-x))} + nparams <- if(length(dim(a)) < 3) 1 else dim(a)[length(dim(a))] + # Calculation and initialization of the required matrix "output" + output <- matrix( , ncol = if(n.chains > 1){if(Rupper.keep) 10 else 9} else 7, nrow = nparams) + if (length(dim(a))==2) a <- array (a, c(dim(a),1)) + if (!keep.all){ + n <- floor(dim(a)[1]/2) + a <- a[(n+1):(2*n), , , drop = FALSE] + } + if (is.null(trans)) + trans <- ifelse ((apply (a<=0, 3, sum))==0, "log", "") + for (i in 1:nparams){ + # Rupper.keep: discard Rupper (nobody ever uses it) + ai <- a[ , , i, drop = FALSE] + if (trans[i]=="log"){ + conv.p <- conv.par(log(ai), n.chains, Rupper.keep=Rupper.keep) # reason???? + conv.p <- list(quantiles = exp(conv.p$quantiles), + confshrink = conv.p$confshrink, n.eff = conv.p$n.eff) + } + else if (trans[i]=="logit"){ + conv.p <- conv.par(logit(ai), n.chains, Rupper.keep=Rupper.keep) + conv.p <- list(quantiles = invlogit(conv.p$quantiles), + confshrink = conv.p$confshrink, n.eff = conv.p$n.eff) + } + else conv.p <- conv.par(ai, n.chains, Rupper.keep=Rupper.keep) + output[i, ] <- c(mean(ai), sd(as.vector(ai)), + conv.p$quantiles, + if(n.chains > 1) conv.p$confshrink, + if(n.chains > 1) round(conv.p$n.eff, min(0, 1 - floor(log10(conv.p$n.eff)))) + ) + } + if(n.chains > 1) + dimnames(output) <- list(dimnames(a)[[3]], c("mean","sd", + "2.5%","25%","50%","75%","97.5%", "Rhat", if(Rupper.keep) "Rupper","n.eff")) + else + dimnames(output) <- list(dimnames(a)[[3]], c("mean","sd", + "2.5%","25%","50%","75%","97.5%")) + return (output) +} Property changes on: trunk/R2WinBUGS/R/monitor.R ___________________________________________________________________ Name: svn:keywords + Id Added: trunk/R2WinBUGS/R/openbugs.R =================================================================== --- trunk/R2WinBUGS/R/openbugs.R (rev 0) +++ trunk/R2WinBUGS/R/openbugs.R 2006-11-24 13:24:51 UTC (rev 3) @@ -0,0 +1,85 @@ +openbugs <- function(d... [truncated message content] |
From: <li...@us...> - 2007-04-07 18:04:19
|
Revision: 9 http://svn.sourceforge.net/bugs-r/?rev=9&view=rev Author: ligges Date: 2007-04-07 11:04:11 -0700 (Sat, 07 Apr 2007) Log Message: ----------- R2WinBUGS has been ported to S-PLUS by Insightful Corp. Special thanks to Dawn Woodard! Modified Paths: -------------- trunk/R2WinBUGS/Changes trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/R/as.bugs.array.R trunk/R2WinBUGS/R/attach.all.R trunk/R2WinBUGS/R/bugs.R trunk/R2WinBUGS/R/bugs.data.R trunk/R2WinBUGS/R/bugs.inits.R trunk/R2WinBUGS/R/bugs.plot.inferences.R trunk/R2WinBUGS/R/bugs.plot.summary.R trunk/R2WinBUGS/R/bugs.run.R trunk/R2WinBUGS/R/bugs.sims.R trunk/R2WinBUGS/R/bugs.update.settings.R trunk/R2WinBUGS/R/conv.par.R trunk/R2WinBUGS/R/monitor.R trunk/R2WinBUGS/R/openbugs.R trunk/R2WinBUGS/R/plot.bugs.R trunk/R2WinBUGS/R/wineutils.R trunk/R2WinBUGS/R/write.model.R trunk/R2WinBUGS/man/bugs.Rd trunk/R2WinBUGS/man/bugs.log.Rd trunk/R2WinBUGS/man/bugs.run.Rd trunk/R2WinBUGS/man/bugs.script.Rd trunk/R2WinBUGS/man/bugs.sims.Rd trunk/R2WinBUGS/man/openbugs.Rd trunk/R2WinBUGS/man/write.model.Rd Modified: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/Changes 2007-04-07 18:04:11 UTC (rev 9) @@ -69,3 +69,6 @@ Update 2.0-4 (01 November 2006): - print.bugs / plot.bugs documentation fixes - write.model() fix + +Update 2.1-1 (07 April 2007): +- Ported to S-PLUS by Insightful Corp. Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/DESCRIPTION 2007-04-07 18:04:11 UTC (rev 9) @@ -1,18 +1,22 @@ Package: R2WinBUGS -Title: Running WinBUGS and OpenBUGS from R -Date: 2006-11-01 -Version: 2.0-4 +Title: Running WinBUGS and OpenBUGS from R / S-PLUS +Date: 2007-04-07 +Version: 2.1-1 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> and Uwe Ligges <li...@st...>. With considerable contributions by Gregor Gorjanc <gre...@bf...> and Jouni Kerman <ke...@st...>. + Ported to S-PLUS by Insightful Corp. Description: Using this package, it is possible to call a BUGS model, summarize inferences and - convergence in a table and graph, and save the simulations in arrays for easy access in R. -Depends: R (>= 2.2.0) + convergence in a table and graph, and save the simulations in arrays for easy access + in R / S-PLUS. In S-PLUS, the openbugs functionality and the windows emulation + functionality is not yet available. +Depends: R (>= 2.4.0) Suggests: coda (>= 0.9-0), BRugs (>= 0.3-0) SystemRequirements: WinBUGS 1.4 on Windows URL: http://www.stat.columbia.edu/~gelman/bugsR/ Maintainer: Sibylle Sturtz <st...@st...> License: GPL version 2 +Dialect: R, S-PLUS Modified: trunk/R2WinBUGS/R/as.bugs.array.R =================================================================== --- trunk/R2WinBUGS/R/as.bugs.array.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/as.bugs.array.R 2007-04-07 18:04:11 UTC (rev 9) @@ -71,9 +71,11 @@ summary <- monitor(sims.array, n.chains, keep.all = TRUE) last.values <- as.list(numeric(n.chains)) for (i in 1:n.chains) { - n.roots.0 <- if (!is.null(DIC)) - n.roots - 1 - else n.roots + if (is.R()) { + n.roots.0 <- if(!is.null(DIC)) n.roots - 1 else n.roots + } else { + n.roots.0 <- if(DIC) n.roots - 1 else n.roots + } last.values[[i]] <- as.list(numeric(n.roots.0)) names(last.values[[i]]) <- root.short[1:n.roots.0] for (j in 1:n.roots.0) { @@ -111,6 +113,11 @@ } } summary <- summary[rank.long, ] + if (is.R()) + is.DIC = !is.null(DIC) + else + is.DIC = DIC + all <- list(n.chains = n.chains, n.iter = n.iter, n.burnin = n.burnin, n.thin = n.thin, n.keep = n.keep, n.sims = n.sims, sims.array = sims.array[,,rank.long,drop = FALSE], sims.list = sims.list, @@ -118,7 +125,7 @@ sd = summary.sd, median = summary.median, root.short = root.short, long.short = long.short, dimension.short = dimension.short, indexes.short = indexes.short, last.values = last.values, program=program, - model.file=model.file, is.DIC=!is.null(DIC), DIC=DIC) + model.file=model.file, is.DIC=is.DIC, DIC=DIC) if(sum(DIC)) { deviance <- all$sims.array[, , dim(sims.array)[3], drop = FALSE] dim(deviance) <- dim(deviance)[1:2] Modified: trunk/R2WinBUGS/R/attach.all.R =================================================================== --- trunk/R2WinBUGS/R/attach.all.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/attach.all.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,11 +1,12 @@ attach.all <- function(x, overwrite = NA, name = "attach.all"){ + if(is.R()){ rem <- names(x) %in% ls(.GlobalEnv) if(!any(rem)) overwrite <- FALSE rem <- names(x)[rem] if(is.na(overwrite)){ question <- paste("The following objects in .GlobalEnv will mask\nobjects in the attached database:\n", - paste(rem, collapse=", "), - "\nRemove these objects from .GlobalEnv?", sep="") + paste(rem, collapse=", "), + "\nRemove these objects from .GlobalEnv?", sep="") if(interactive()){ if(.Platform$OS.type == "windows") overwrite <- "YES" == winDialog(type = "yesno", question) @@ -16,6 +17,9 @@ } if(overwrite) remove(list=rem, envir=.GlobalEnv) attach(x, name=name) + } else { + attach.default(x, name = name) + } } attach.bugs <- function (x, overwrite = NA){ @@ -25,12 +29,24 @@ detach("bugs.sims")} x$sims.list$n.sims <- x$n.sims # put n.sims into sims.list for convenience r2 <- attach.all(x$sims.list, overwrite = overwrite, name = "bugs.sims") - invisible (bugs.sims = r2) + if (is.R()) + invisible (bugs.sims = r2) + else + invisible (r2) } -detach.all <- function(name = "attach.all") +detach.all <- function(name = "attach.all"){ + if (is.R()){ do.call("detach", list(name=name)) + } else { + do.call("detach", list(what=name)) + } +} detach.bugs <- function(){ + if (is.R()){ detach.all("bugs.sims") + } else { + invisible(detach.all("bugs.sims")) + } } Modified: trunk/R2WinBUGS/R/bugs.R =================================================================== --- trunk/R2WinBUGS/R/bugs.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/bugs.R 2007-04-07 18:04:11 UTC (rev 9) @@ -10,17 +10,23 @@ clearWD = FALSE, useWINE = .Platform$OS.type != "windows", WINE = Sys.getenv("WINE"), newWINE = FALSE, WINEPATH = NULL){ - ## If OpenBUGS, we only call openbugs() and exit... program <- match.arg(program) if (program %in% c("openbugs", "OpenBugs")) - return(openbugs(data, inits, parameters.to.save, model.file, - n.chains, n.iter, n.burnin, n.thin, DIC, bugs.directory, - working.directory, digits)) - + if (is.R()){ + ## If OpenBUGS, we only call openbugs() and exit... + return(openbugs(data, inits, parameters.to.save, model.file, + n.chains, n.iter, n.burnin, n.thin, DIC, bugs.directory, + working.directory, digits)) + } else { + stop ("OpenBUGS is not yet available in S-PLUS") + } ## 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 (useWINE) { # attempt to find wine and winepath + if(useWINE) { + if (!is.R()) + stop ("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") + ## attempt to find wine and winepath if (!nchar(WINE)) { WINE <- system("locate wine | grep bin/wine$", intern = TRUE) WINE <- WINE[length(WINE)] @@ -31,7 +37,7 @@ WINEPATH <- WINEPATH[length(WINEPATH)] } if (!length(WINEPATH)) stop("couldn't locate WINEPATH binary") - } + } if(!is.null(working.directory)){ savedWD <- getwd() setwd(working.directory) @@ -59,8 +65,13 @@ if(codaPkg) return(file.path(getwd(), paste("coda", 1:n.chains, ".txt", sep=""))) - sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), - model.file = model.file, is.DIC = !is.null(DIC), program = program) + if (is.R()){ + sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), + model.file = model.file, is.DIC = !is.null(DIC), program = program) + } else { + sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), + model.file = model.file, is.DIC = DIC, program = program) + } if(clearWD) file.remove(c("data.txt", "log.odc", "log.txt", "codaIndex.txt", paste("inits", 1:n.chains, ".txt", sep=""), Modified: trunk/R2WinBUGS/R/bugs.data.R =================================================================== --- trunk/R2WinBUGS/R/bugs.data.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/bugs.data.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,12 +1,222 @@ -"bugs.data" <- +"bugs.data" <- function(data, dir = getwd(), digits = 5){ if(is.numeric(unlist(data))) - write.datafile(lapply(data, formatC, digits = digits, format = "E"), - file.path(dir, "data.txt")) + if(is.R()) { + write.datafile(lapply(data, formatC, digits = digits, format = "E"), + file.path(dir, "data.txt")) + } + else { + writeDatafileS4(data, towhere = "data.txt") + } else { - data.list <- lapply(as.list(data), get, pos = parent.frame(2)) - names(data.list) <- as.list(data) - write.datafile(lapply(data.list, formatC, digits = digits, format = "E"), - file.path(dir, "data.txt")) - } + if(is.R()) { + data.list <- lapply(as.list(data), get, pos = parent.frame(2)) + names(data.list) <- as.list(data) + write.datafile(lapply(data.list, formatC, digits = digits, format = "E"), + file.path(dir, "data.txt")) + } + else { + data.list <- lapply(as.list(data), get, where = parent.frame(2)) + names(data.list) <- unlist(data) + writeDatafileS4(data.list, towhere = "data.txt") + } + } } + +if (!is.R()) { + +"writeDatafileS4" <- +# +# Writes to file "towhere" text defining a list containing "DATA" in a form compatable with WinBUGS. +# Required arguments: +# DATA - either a data frame or else a list consisting of any combination of scalars, vectors, arrays or data frames (but not lists). +# If a list, all list elements that are not data.frames must be named. Names of data.frames in DATA are ignored. +# Optional arguments: +# towhere - file to receive output. Is clipboard by default, which is convenient for pasting into a WinBUTS ODC file. +# fill - If numeric, number of columns for output. If FALSE, output will be on one line. If TRUE (default), number of +# columns is given by .Options$width. +# Value: +# Text defining a list is output to file "towhere". +# Details: +# The function performs considerable checking of DATA argument. Since WinBUGS requires numeric input, no factors or character vectors +# are allowed. All data must be named, either as named elements of DATA (if it is a list) or else using the names given in data frames. +# Data frames may contain matrices. +# Arrays of any dimension are rearranged to be in row-major order, as required by WinBUGS. Scientific notation is also handled properly. +# In particular, the number will consist of a mantissa _containing a decimal point_ followed by "E", then either "+" or "-", and finally +# a _two-digit_ number. S-Plus does not always provide a decimal point in the mantissa, uses "e" instead of "E", followed by +# either a "+" or "-" and then _three_ digits. +# Written by Terry Elrod. Disclaimer: This function is used at the user's own risk. +# Please send comments to Ter...@UA.... +# Revision history: 2002-11-19. Fixed to handle missing values properly. +function(DATA, towhere = "clipboard", fill = TRUE) +{ + formatDataS4 = + # + # Prepared DATA for input to WinBUGS. + function(DATA) + { + toSingleS4 = + # + # Takes numeric vector and removes digit of exponent in scientific notation (if any) + function(x) + { + xdim <- dim(x) + x <- as.character(as.single(x)) + # First to look for positives: + pplus <- regMatchPos(x, "e\\+0") + pplusind <- apply(pplus, 1, function(y) + (!any(is.na(y)))) + if(any(pplusind)) { + # Making sure that periods are in mantissa... + init <- substring(x[pplusind], 1, pplus[ + pplusind, 1] - 1) + #...preceeding exponent + pper <- regMatchPos(init, "\\.") + pperind <- apply(pper, 1, function(y) + (all(is.na(y)))) + if(any(pperind)) + init[pperind] <- paste(init[pperind], + ".0", sep = "") + # Changing the format of the exponent... + x[pplusind] <- paste(init, "E+", substring( + x[pplusind], pplus[pplusind, 2] + 1), + sep = "") + } + # Then to look for negatives: + pminus <- regMatchPos(x, "e\\-0") + pminusind <- apply(pminus, 1, function(y) + (!any(is.na(y)))) + if(any(pminusind)) { + # Making sure that periods are in mantissa... + init <- substring(x[pminusind], 1, pminus[ + pminusind, 1] - 1) + #...preceeding exponent + pper <- regMatchPos(init, "\\.") + pperind <- apply(pper, 1, function(y) + (all(is.na(y)))) + if(any(pperind)) + init[pperind] <- paste(init[pperind], + ".0", sep = "") + # Changing the format of the exponent... + x[pminusind] <- paste(init, "E-", substring( + x[pminusind], pminus[pminusind, 2] + + 1), sep = "") + } + x + } + if(!is.list(DATA)) + stop("DATA must be a named list or data frame.") + dlnames <- names(DATA) + if(is.data.frame(DATA)) + DATA <- as.list(DATA) + # + # Checking for lists in DATA.... + lind <- sapply(DATA, is.list) + # Checking for data frames in DATA.... + dfind <- sapply(DATA, is.data.frame) + # Any lists that are not data frames?... + if(any(lind & !dfind)) stop("DATA may not contain lists.") + # Checking for unnamed elements of list that are not data frames.... + if(any(dlnames[!dfind] == "")) stop( + "When DATA is a list, all its elements that are not data frames must be named." + ) + # Checking for duplicate names.... + dupnames <- unique(dlnames[duplicated(dlnames)]) + if(length(dupnames) > 0) + stop(paste( + "The following names are used more than once in DATA:", + paste(dupnames, collapse = ", "))) + if(any(dfind)) { + dataold <- DATA + DATA <- vector("list", 0) + for(i in seq(along = dataold)) { + if(dfind[i]) + DATA <- c(DATA, as.list(dataold[[i]])) + else DATA <- c(DATA, dataold[i]) + } + dataold <- NULL + } + dlnames <- names(DATA) + dupnames <- unique(dlnames[duplicated(dlnames)]) + # Checking for duplicated names again (now that columns of data frames are included).... + if(length(dupnames) > 0) stop(paste( + "The following names are used more than once in DATA (at least once within a data frame):", + paste(dupnames, collapse = ", "))) + # Checking for factors.... + factorind <- sapply(DATA, is.factor) + if(any(factorind)) + stop(paste( + "DATA may not include factors. One or more factor variables were detected:", + paste(dlnames[factorind], collapse = ", "))) + # Checking for character vectors.... + charind <- sapply(DATA, is.character) + if(any(charind)) + stop(paste( + "WinBUGS does not handle character data. One or more character variables were detected:", + paste(dlnames[charind], collapse = ", "))) + # Checking for complex vectors.... + complexind <- sapply(DATA, is.complex) + if(any(complexind)) + stop(paste( + "WinBUGS does not handle complex data. One or more complex variables were detected:", + paste(dlnames[complexind], collapse = ", "))) + # Checking for values farther from zero than 1E+38 (which is limit of single precision).... + toobigind <- sapply(DATA, function(x) + { + y <- abs(x[!is.na(x)]) + any(y[y > 0] > 9.9999999999999998e+37) + } + ) + if(any(toobigind)) + stop(paste( + "WinBUGS works in single precision. The following variables contain data outside the range +/-1.0E+38: ", + paste(dlnames[toobigind], collapse = ", "), + ".\n", sep = "")) + # Checking for values in range +/-1.0E-38 (which is limit of single precision).... + toosmallind <- sapply(DATA, function(x) + { + y <- abs(x[!is.na(x)]) + any(y[y > 0] < 9.9999999999999996e-39) + } + ) + n <- length(dlnames) + data.string <- as.list(rep(NA, n)) + for(i in 1:n) { + if(length(DATA[[i]]) == 1) { + ac <- toSingleS4(DATA[[i]]) + data.string[[i]] <- paste(names(DATA)[i], "=", + ac, sep = "") + next + } + if(is.vector(DATA[[i]]) & length(DATA[[i]]) > 1) { + ac <- toSingleS4(DATA[[i]]) + data.string[[i]] <- paste(names(DATA)[i], "=c(", + paste(ac, collapse = ", "), ")", sep = + "") + next + } + if(is.array(DATA[[i]])) { + ac <- toSingleS4(aperm(DATA[[i]])) + data.string[[i]] <- paste(names(DATA)[i], + "= structure(.Data= c(", paste(ac, + collapse = ", "), "), \n .Dim=c(", + paste(as.character(dim(DATA[[i]])), + collapse = ", "), "))", sep = "") + } + } + data.tofile <- paste("list(", paste(unlist(data.string), + collapse = ", "), ")", sep = "") + if(any(toosmallind)) + warning(paste( + "WinBUGS works in single precision. The following variables contained nonzero data", + "\ninside the range +/-1.0E-38 that were set to zero: ", + paste(dlnames[toosmallind], collapse = ", "), + ".\n", sep = "")) + return(data.tofile) + } + rslt <- formatDataS4(DATA) + cat(rslt, file = towhere, fill = fill) + invisible(0) +} + +} # ends if (!is.R()) Modified: trunk/R2WinBUGS/R/bugs.inits.R =================================================================== --- trunk/R2WinBUGS/R/bugs.inits.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/bugs.inits.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,13 +1,22 @@ -"bugs.inits" <- +"bugs.inits" <- function (inits, n.chains, digits){ - if(!is.null(inits)){ - for (i in 1:n.chains){ - if (is.function(inits)) - write.datafile(lapply(inits(), formatC, digits = digits, format = "E"), - paste ("inits", i, ".txt", sep="")) - else - write.datafile(lapply(inits[[i]], formatC, digits = digits, format = "E"), - paste ("inits", i, ".txt", sep="")) - } - } + if(!is.null(inits)) { + for(i in 1:n.chains) { + if(is.function(inits)) + if(is.R()) { + write.datafile(lapply(inits(), formatC, digits = digits, format = "E"), + paste("inits", i, ".txt", sep = "")) + } else { + writeDatafileS4(inits(), towhere = + paste("inits", i, ".txt", sep = "")) + } + else if(is.R()) { + write.datafile(lapply(inits[[i]], formatC, digits = digits, format = "E"), + paste("inits", i, ".txt", sep = "")) + } else { + writeDatafileS4(inits[[i]], towhere = paste( + "inits", i, ".txt", sep = "")) + } + } + } } Modified: trunk/R2WinBUGS/R/bugs.plot.inferences.R =================================================================== --- trunk/R2WinBUGS/R/bugs.plot.inferences.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/bugs.plot.inferences.R 2007-04-07 18:04:11 UTC (rev 9) @@ -28,8 +28,18 @@ long.short <- sims$long.short height <- .6 par (mar=c(0,0,1,0)) + + ## if in Splus, suppress printing of warnings during the plotting. + ## otherwise a warning is generated + if (!is.R()){ + warn.settings <- options("warn")[[1]] + options (warn = -1) + } plot (c(0,1), c(-n.roots-.5,-.4), ann=FALSE, bty="n", xaxt="n", yaxt="n", type="n") + if (!is.R()) + options(warn = warn.settings) + W <- max(strwidth(rootnames, cex=cex.names)) B <- (1-W)/3.8 A <- 1-3.5*B Modified: trunk/R2WinBUGS/R/bugs.plot.summary.R =================================================================== --- trunk/R2WinBUGS/R/bugs.plot.summary.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/bugs.plot.summary.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,6 +1,7 @@ "bugs.plot.summary" <- function (sims, ...){ DIC <- sims$is.DIC + if (.Device=="windows" || (.Device=="null device" && options("device")=="windows")){ cex.names <- .7 @@ -29,7 +30,11 @@ J[J==max(J)] <- max(J)-1 total <- ceiling(sum(J+.5)) } - pos <- -1 + if (is.R()){ + pos <- -1 + } else { + pos <- -1.5 + } ypos <- NULL id <- NULL ystart <- NULL @@ -39,13 +44,25 @@ ystart <- numeric(n.roots) for (k in 1:n.roots){ ystart[k] <- pos - ypos <- c(ypos, pos - seq(0, J[k]-1)) + if (is.R()) { + ypos <- c(ypos, pos - seq(0, J[k]-1)) + } else { + # In S-PLUS, increase the vertical spacing + ypos <- c(ypos, pos - 1.5*seq(0, J[k]-1)) + } id <- c(id, 1:J[k]) - pos <- pos - J[k] -.5 + if (is.R()) { + pos <- pos - J[k] -.5 + } else { + pos <- pos - 1.5*J[k] -0.75 + } if (k>1) jj <- c(jj, sum(J0[1:(k-1)]) + (1:J[k])) } - bottom <- min(ypos)-1 - + if (is.R()){ + bottom <- min(ypos)-1 + } else { + bottom <- min(ypos)-1.5 + } med <- numeric(sum(J)) i80 <- matrix( , sum(J), 2) i80.chains <- array (NA, c(sum(J), n.chains, 2)) @@ -61,8 +78,17 @@ a <- -b * p.rng[1] par (mar=c(0,0,1,3)) + # if in Splus, suppress printing of warnings during the plotting. + # otherwise a warning is generated + if (!is.R()){ + warn.settings <- options("warn")[[1]] + options (warn = -1) + } plot (c(0,1), c(min(bottom, -max.length)-3,2.5), ann=FALSE, bty="n", xaxt="n", yaxt="n", type="n") + if (!is.R()) + options(warn = warn.settings) + W <- max(strwidth(unlist(dimnames(summ)[[1]]), cex=cex.names)) B <- (1-W)/3.6 A <- 1-3.5*B @@ -100,7 +126,16 @@ for (j in 1:sum(J)){ name <- dimnames(summ)[[1]][jj[j]] if (id[j]==1) - text (0, ypos[j], name, adj=0, cex=cex.names) + if (is.R()) { + text (0, ypos[j], name, adj=0, cex=cex.names) + } else { + # in S-PLUS, strwidth is an upper bound on the length of the string, + # so we must align the brackets differently than in R + pos <- as.vector(regexpr("[[]", name)) + text (0, ypos[j], substring(name, 1, pos-1), adj=0, cex=cex.names) + text (strwidth(substring(name,1,pos-1),cex=cex.names), + ypos[j], substring(name, pos, nchar(name)), adj=0, cex=cex.names) + } else { pos <- as.vector(regexpr("[[]", name)) text (strwidth(substring(name,1,pos-1),cex=cex.names), Modified: trunk/R2WinBUGS/R/bugs.run.R =================================================================== --- trunk/R2WinBUGS/R/bugs.run.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/bugs.run.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,15 +1,24 @@ "bugs.run" <- function(n.burnin, bugs.directory, WINE = "", useWINE = .Platform$OS.type != "windows", newWINE = TRUE){ - + +if(useWINE && !is.R()) + stop ("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") if(useWINE && !newWINE) bugs.directory <- win2native(bugs.directory) ## Update the lengths of the adaptive phases in the Bugs updaters try(bugs.update.settings(n.burnin, bugs.directory)) ## Return the lengths of the adaptive phases to their original settings - on.exit(try(file.copy(file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), - file.path(bugs.directory, "System/Rsrc/Registry.odc"), - overwrite = TRUE))) + if (is.R()){ + on.exit(try(file.copy(file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), + file.path(bugs.directory, "System/Rsrc/Registry.odc"), + overwrite = TRUE))) + } else { + on.exit(try(splus.file.copy(file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), + file.path(bugs.directory, "System/Rsrc/Registry.odc"), + overwrite = TRUE))) + } + ## Search Win*.exe (WinBUGS executable) within bugs.directory dos.location <- file.path(bugs.directory, grep("^Win[[:alnum:]]*[.]exe$", list.files(bugs.directory), value = TRUE)[1]) @@ -26,9 +35,15 @@ if(temp == -1) stop("Error in bugs.run().\nCheck that WinBUGS is in the specified directory.") ## Stop and print an error message if Bugs did not run correctly - if (length(grep("Bugs did not run correctly", - scan("coda1.txt", character(), quiet=TRUE, sep="\n"))) > 0) - stop("Look at the log file and\ntry again with debug=TRUE and figure out what went wrong within Bugs.") + if(is.R()) { + if(length(grep("Bugs did not run correctly", + scan("coda1.txt", character(), quiet=TRUE, sep="\n"))) > 0) + stop("Look at the log file and\ntry again with debug=TRUE and figure out what went wrong within Bugs.") + } else { + if (length(grep("Bugs did not run correctly", + scan("coda1.txt", character(), sep="\n"))) > 0) + stop("Look at the log file and\ntry again with debug=TRUE and figure out what went wrong within Bugs.") + } } Modified: trunk/R2WinBUGS/R/bugs.sims.R =================================================================== --- trunk/R2WinBUGS/R/bugs.sims.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/bugs.sims.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,10 +1,18 @@ "bugs.sims" <- function (parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC = TRUE){ -# Read the simulations from Bugs into R, format them, and monitor convergence +## Read the simulations from Bugs into R, format them, and monitor convergence sims.files <- paste ("coda", 1:n.chains, ".txt", sep="") - index <- read.table ("codaIndex.txt", header=FALSE, sep="\t") - parameter.names <- as.vector(index[,1]) - n.keep <- index[1,3] - index[1,2] + 1 + index <- read.table("codaIndex.txt", header = FALSE, sep = "\t") # read in the names of the parameters and the indices of their samples + ## in Splus, read.table interprets the first row of the file as row names, + ## while in R it does not + if(is.R()) { + parameter.names <- as.vector(index[, 1]) + n.keep <- index[1, 3] - index[1, 2] + 1 + } + else { + parameter.names <- row.names(index) + n.keep <- index[1, 2] - index[1, 1] + 1 + } n.parameters <- length(parameter.names) n.sims <- n.keep*n.chains sims <- matrix( , n.sims, n.parameters) @@ -50,7 +58,9 @@ rank.long <- unlist(long.short) for (i in 1:n.chains){ - sims.i <- scan (sims.files[i], quiet=TRUE) [2*(1:(n.keep*n.parameters))] + if(is.R()) + sims.i <- scan(sims.files[i], quiet = TRUE)[2 * (1:(n.keep * n.parameters))] + else sims.i <- scan(sims.files[i])[2 * (1:(n.keep * n.parameters))] sims[(n.keep*(i-1)+1):(n.keep*i), ] <- sims.i sims.array[,i,] <- sims.i } @@ -112,6 +122,8 @@ LOG <- bugs.log("log.txt")$DIC if(any(is.na(LOG))){ deviance <- all$sims.array[, , dim(sims.array)[3], drop = FALSE] + if(!is.R()) + dimnames(deviance) <- NULL dim(deviance) <- dim(deviance)[1:2] pD <- numeric(n.chains) DIC <- numeric(n.chains) @@ -128,3 +140,9 @@ } return(all) } + +if (!is.R()){ + .subset <- function(x, index){ + return (x[index]) + } +} Modified: trunk/R2WinBUGS/R/bugs.update.settings.R =================================================================== --- trunk/R2WinBUGS/R/bugs.update.settings.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/bugs.update.settings.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,20 +1,36 @@ "bugs.update.settings" <- function (n.burnin, bugs.directory){ + if (!is.R()) + if (!require(rwBin)) stop ("The rwBin package is required") + char.burnin <- as.character(n.burnin - 1) - file.copy(file.path(bugs.directory, "System/Rsrc/Registry.odc"), + if (is.R()){ + file.copy(file.path(bugs.directory, "System/Rsrc/Registry.odc"), file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), - overwrite = TRUE) + overwrite = TRUE) + } else { + splus.file.copy(file.path(bugs.directory, "System/Rsrc/Registry.odc"), + file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), + overwrite = TRUE) + } registry <- readBin(file.path(bugs.directory, "System/Rsrc/Registry.odc"), "character", 400, size = 1, endian = "little") locale <- Sys.getlocale("LC_CTYPE") Sys.setlocale("LC_CTYPE", "C") - info <- registry[regexpr("Int", registry, fixed = TRUE, useBytes = TRUE) > 0] + if (is.R()) + info <- registry[regexpr("Int", registry, fixed = TRUE, useBytes = TRUE) > 0] + else + info <- registry[regexpr("Int", registry, fixed = TRUE) > 0] while(regexpr("\r", info) > 0){ newline <- regexpr("\r", info) info <- substring(info, newline + 1) line <- substring(info, 1, regexpr("\r", info) - 1) if(regexpr("AdaptivePhase", line) > 0){ - numpos <- regexpr("Int", line, fixed = TRUE, useBytes = TRUE) + 4 + if (is.R()) + numpos <- regexpr("Int", line, fixed = TRUE, useBytes = TRUE) + 4 + else + numpos <- regexpr("Int", line, fixed = TRUE) + 4 + num <- substring(line, numpos) if (as.numeric(num) > n.burnin){ blanks <- rep(" ", nchar(num, type = "chars") - nchar(char.burnin, type = "chars")) @@ -28,3 +44,21 @@ writeBin (registry, file.path(bugs.directory, "System/Rsrc/Registry.odc"), endian = "little") } + +if (!is.R()){ + +"splus.file.copy"<- +function(from, to, overwrite = FALSE) +{ + require(rwBin) + if(!file.exists(from)) + stop("File: ", from, " does not exist") + if(!overwrite && file.exists(to)) + stop("File: ", to, " already exists and overwrite is FALSE") + n <- file.info(from)$size + z <- writeBin(readBin(from, what = "integer", size = 1, n = n), to, + size = 1) + invisible(z) +} + +} #ends if (!is.R()) Modified: trunk/R2WinBUGS/R/conv.par.R =================================================================== --- trunk/R2WinBUGS/R/conv.par.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/conv.par.R 2007-04-07 18:04:11 UTC (rev 9) @@ -29,7 +29,7 @@ B <- n*var(xdot) varW <- var(s2)/m varB <- B^2 * 2/(m-1) - covWB <- (n/m)*(cov(s2,xdot^2) - 2*muhat*cov(s2,xdot)) + covWB <- (n/m)*(var(s2, xdot^2) - 2*muhat*var(s2, xdot)) sig2hat <- ((n-1)*W + B)/n # Posterior interval post.range combines all uncertainties Modified: trunk/R2WinBUGS/R/monitor.R =================================================================== --- trunk/R2WinBUGS/R/monitor.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/monitor.R 2007-04-07 18:04:11 UTC (rev 9) @@ -30,6 +30,9 @@ confshrink = conv.p$confshrink, n.eff = conv.p$n.eff) } else if (trans[i]=="logit"){ + if (!is.R()){ + logit <- function (x) { log(x /(1- x)) } + } conv.p <- conv.par(logit(ai), n.chains, Rupper.keep=Rupper.keep) conv.p <- list(quantiles = invlogit(conv.p$quantiles), confshrink = conv.p$confshrink, n.eff = conv.p$n.eff) Modified: trunk/R2WinBUGS/R/openbugs.R =================================================================== --- trunk/R2WinBUGS/R/openbugs.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/openbugs.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,3 +1,5 @@ +if (is.R()){ + openbugs <- 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)), @@ -83,3 +85,5 @@ a.stem <- substr(a, 1, ifelse(bracket.pos>0, bracket.pos-1, nchar(a))) return(a[order(match(a.stem, b))]) } + +} Modified: trunk/R2WinBUGS/R/plot.bugs.R =================================================================== --- trunk/R2WinBUGS/R/plot.bugs.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/plot.bugs.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,7 +1,11 @@ plot.bugs <- function (x, display.parallel = FALSE, ...){ mar.old <- par("mar") pty.old <- par(pty = "m") - layout(matrix(c(1,2),1,2)) + if (is.R()) + layout(matrix(c(1,2),1,2)) + else + par(mfrow = c(1,2)) + bugs.plot.summary (x, ...) bugs.plot.inferences (x, display.parallel, ...) header <- "" @@ -12,5 +16,42 @@ header <- paste(header, x$n.chains, " chains, each with ", x$n.iter, " iterations (first ", x$n.burnin, " discarded)", sep = "") mtext(header, outer = TRUE, line = -1, cex = 0.7) - par(pty = pty.old[[1]], mar = mar.old) + if (is.R()) par(pty = pty.old[[1]], mar = mar.old) + else invisible(par(pty = pty.old[[1]], mar = mar.old)) } + +if (!is.R()) { + +strwidth <-function(s, units = c("user", "inches", "figure"), cex = NULL) { + s<-as.character(s) + if (!missing(cex)) { + oldcex <- par(cex=cex) + on.exit(par(oldcex)) + } + units <- match.arg(units) + if (units == "user") { + nchar(s) * par("cxy")[1] + } else if (units == "inches") { + nchar(s) * par("cin")[1] + } else if (units == "figure") { + nchar(s) * par("cin")[1] / par("fin")[1] + } +} + +strheight <- function(s, units = "user", cex = NULL) { + s<-as.character(s) + if (!missing(cex)) { + oldcex <- par(cex=cex) + on.exit(par(oldcex)) + } + units <- match.arg(units) + if (units == "user") { + par("cxy")[2] + } else if (units == "inches") { + par("cin")[2] + } else if (units == "figure") { + par("cin")[2] / par("fin")[2] + } +} + +} #ends if (!is.R()) Modified: trunk/R2WinBUGS/R/wineutils.R =================================================================== --- trunk/R2WinBUGS/R/wineutils.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/wineutils.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,5 +1,7 @@ ## from Jun Yan's rbugs package, extended +if (is.R()){ + ## get drive mapping table from ~/.wine/config winedriveMap <- function(config="~/.wine/config") { if (!file.exists(config)) return (NULL); @@ -48,10 +50,16 @@ else x } +} # end if (is.R()) + native2win <- function(x, useWINE=.Platform$OS.type != "windows", newWINE=TRUE) { # native -> win - if (useWINE && !newWINE) return(winedriveRTr(x)) - if (useWINE && newWINE) { - x <- system(paste(WINEPATH, "-w", x), intern = TRUE) - return(gsub("\\\\", "/", x)) ## under wine BUGS cannot use \ or \\ - } else x + if(is.R()){ + if (useWINE && !newWINE) return(winedriveRTr(x)) + if (useWINE && newWINE) { + x <- system(paste(WINEPATH, "-w", x), intern = TRUE) + return(gsub("\\\\", "/", x)) ## under wine BUGS cannot use \ or \\ + } else x + } else { #S-PLUS + gsub("\\\\", "/", x) + } } Modified: trunk/R2WinBUGS/R/write.model.R =================================================================== --- trunk/R2WinBUGS/R/write.model.R 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/R/write.model.R 2007-04-07 18:04:11 UTC (rev 9) @@ -1,7 +1,14 @@ write.model <- function(model, con = "model.bug") { - model.text <- attr(model, "source") - model.text <- sub("^\\s*function\\s*\\(\\s*\\)", "model", model.text) + if (is.R()){ + model.text <- attr(model, "source") + model.text <- sub("^\\s*function\\s*\\(\\s*\\)", "model", model.text) + } else { + ## In S-PLUS the source code of a function can be obtained with + ## as.character(function_name). This omits the "function_name <- function()" piece + model.text <- as.character(model) + model.text <- paste("model", model.text) + } model.text <- gsub("%_%", "", model.text) writeLines(model.text, con = con) } Modified: trunk/R2WinBUGS/man/bugs.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.Rd 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/man/bugs.Rd 2007-04-07 18:04:11 UTC (rev 9) @@ -1,9 +1,9 @@ \name{bugs} \alias{bugs} -\title{Run WinBUGS and OpenBUGS from R} +\title{Run WinBUGS and OpenBUGS from R or S-PLUS} \description{The \code{bugs} function takes data and starting values as input. It automatically writes a WinBUGS script, calls the model, -and saves the simulations for easy access in R.} +and saves the simulations for easy access in R or S-PLUS.} \usage{ bugs(data, inits, parameters.to.save, model.file = "model.bug", n.chains = 3, n.iter = 2000, n.burnin = floor(n.iter/2), @@ -57,7 +57,8 @@ through function \code{\link{read.bugs}}. (not used if \code{program = "openbugs"})} \item{bugs.directory}{directory that contains the WinBUGS executable} \item{program}{the program to use, either \code{winbugs}/\code{WinBugs} or \code{openbugs}/\code{OpenBugs}, - the latter makes use of function \code{\link{openbugs}} and requires the CRAN package \pkg{BRugs}.} + the latter makes use of function \code{\link{openbugs}} and requires the CRAN package \pkg{BRugs}. + The \code{openbugs}/\code{OpenBugs} choice is not available in S-PLUS.} \item{working.directory}{sets working directory during execution of this function; WinBUGS' in- and output will be stored in this directory; if \code{NULL}, the current working directory is chosen.} @@ -69,7 +70,8 @@ \item{useWINE}{logical; attempt to use the WINE emulator to run WinBUGS, defaults to \code{TRUE} on Windows, and \code{FALSE} otherwise. If WINE is used, the arguments \code{bugs.directory} and \code{working.directory} must be given in form of Linux paths - rather than Windows paths (if not \code{NULL}).} + rather than Windows paths (if not \code{NULL}). + The \code{useWINE = TRUE} option is not available in S-PLUS.} \item{WINE}{character; name of WINE binary file} \item{newWINE}{Set this one to \code{TRUE} for new versions of WINE.} \item{WINEPATH}{Path the WINE, it is tried hard to get the information automatically if not given.} @@ -78,12 +80,12 @@ To run: \enumerate{ \item Write a WinBUGS model in a ASCII file. -\item Go into R. +\item Go into R / S-PLUS. \item Prepare the inputs to the \code{bugs} function and run it (see Example). -\item A WinBUGS window will pop up amd R will freeze up. The model +\item A WinBUGS window will pop up and R / S-PLUS will freeze up. The model will now run in WinBUGS. It might take awhile. You will see things happening in the Log window within WinBUGS. When WinBugs - is done, its window will close and R will work again. + is done, its window will close and R / S-PLUS will work again. \item If an error message appears, re-run with \code{debug = TRUE}. }} \value{ Modified: trunk/R2WinBUGS/man/bugs.log.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.log.Rd 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/man/bugs.log.Rd 2007-04-07 18:04:11 UTC (rev 9) @@ -17,7 +17,7 @@ } \details{ In later releases of R2WiNBUGS, this function is considered to read the relevant data from the log file rather than -analysing and calculating the relevant data in R again. +analysing and calculating the relevant data in R / S-PLUS again. } \seealso{The main function that generates the log file is \code{\link{bugs}}.} \keyword{IO} Modified: trunk/R2WinBUGS/man/bugs.run.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.run.Rd 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/man/bugs.run.Rd 2007-04-07 18:04:11 UTC (rev 9) @@ -13,7 +13,8 @@ \item{bugs.directory}{directory that contains the WinBUGS executable} \item{WINE}{name of WINE binary, for Windows emulation} \item{useWINE}{logical; attempt to use the WINE emulator to run WinBUGS, - defaults to \code{TRUE} on Windows, and \code{FALSE} otherwise.} + defaults to \code{TRUE} on Windows, and \code{FALSE} otherwise. + The \code{useWINE = TRUE} option is not available in S-PLUS.} \item{newWINE}{set this one to \code{TRUE} for new versions of WINE.} } \value{ Modified: trunk/R2WinBUGS/man/bugs.script.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.script.Rd 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/man/bugs.script.Rd 2007-04-07 18:04:11 UTC (rev 9) @@ -20,7 +20,8 @@ or have to be generated by WinBUGS} \item{bin}{number of iterations between saving of results} \item{DIC}{logical; if \code{TRUE}, compute deviance, pD, and DIC automatically in WinBUGS} - \item{useWINE}{logical; use WINE to run WinBUGS under Linux} + \item{useWINE}{logical; use WINE to run WinBUGS under Linux. + The \code{useWINE = TRUE} option is not available in S-PLUS.} \item{newWINE}{set this one to \code{TRUE} for new versions of WINE.} \item{WINEPATH}{Path the WINE, \code{bugs} tries hard to get the information automatically and pass it to this function, if not given.} Modified: trunk/R2WinBUGS/man/bugs.sims.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.sims.Rd 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/man/bugs.sims.Rd 2007-04-07 18:04:11 UTC (rev 9) @@ -2,7 +2,7 @@ \alias{bugs.sims} \title{WinBUGS output reader - intended for internal use only} \description{ -Reads simulations from WinBUGS into R, formats them, monitors convergence, +Reads simulations from WinBUGS into R/ S-PLUS, formats them, monitors convergence, performs convergence checks, and computes medians and quantiles. Intended for internal use only.} \usage{ bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, Modified: trunk/R2WinBUGS/man/openbugs.Rd =================================================================== --- trunk/R2WinBUGS/man/openbugs.Rd 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/man/openbugs.Rd 2007-04-07 18:04:11 UTC (rev 9) @@ -2,7 +2,8 @@ \alias{openbugs} \title{Wrapper to run OpenBUGS} \description{The \code{openbugs} function takes data and starting values as input. -It automatically calls the package \pkg{BRugs} and runs something similar to \code{\link[BRugs]{BRugsFit}}.} +It automatically calls the package \pkg{BRugs} and runs something similar to \code{\link[BRugs]{BRugsFit}}. +Not available in S-PLUS.} \usage{ openbugs(data, inits, parameters.to.save, model.file = "model.txt", n.chains = 3, n.iter = 2000, Modified: trunk/R2WinBUGS/man/write.model.Rd =================================================================== --- trunk/R2WinBUGS/man/write.model.Rd 2006-12-29 16:10:15 UTC (rev 8) +++ trunk/R2WinBUGS/man/write.model.Rd 2007-04-07 18:04:11 UTC (rev 9) @@ -1,12 +1,12 @@ \name{write.model} \alias{write.model} \title{Creating a WinBUGS model file} -\description{Convert R function to a WinBUGS model file} +\description{Convert R / S-PLUS function to a WinBUGS model file} \usage{ write.model(model, con = "model.bug") } \arguments{ - \item{model}{R function containg the BUGS model in the BUGS model language, for minor differences see Section Details.} + \item{model}{R / S-PLUS function containing the BUGS model in the BUGS model language, for minor differences see Section Details.} \item{con}{passed to \code{link{writeLines}} which actually writes the model file} } \value{ @@ -18,7 +18,7 @@ As a difference, BUGS syntax allows truncation specification like this: \code{dnorm(...) I(...)} -but this is illegal in R. To overcome this incompatibility, use \code{\%_\%} before \code{I(...)}: +but this is illegal in R / S-PLUS. To overcome this incompatibility, use \code{\%_\%} before \code{I(...)}: \code{dnorm(...) \%_\% I(...)}. The dummy operator \code{\%_\%} will be removed before the BUGS code is saved. } @@ -36,8 +36,14 @@ sigma.theta ~ dunif (0, 1000) } -## some temporary filename: -filename <- file.path(tempdir(), "schoolsmodel.bug") +if (is.R()){ # for R + ## some temporary filename: + filename <- file.path(tempdir(), "schoolsmodel.bug") +} else{ # for S-PLUS + ## put the file in the working directory: + filename <- "schoolsmodel.bug" +} + ## write model file: write.model(schoolsmodel, filename) ## and let's take a look: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: Gregor G. <gre...@bf...> - 2007-04-08 15:02:31
|
li...@us... wrote: > Revision: 9 > http://svn.sourceforge.net/bugs-r/?rev=9&view=rev > Author: ligges > Date: 2007-04-07 11:04:11 -0700 (Sat, 07 Apr 2007) > > Log Message: > ----------- > R2WinBUGS has been ported to S-PLUS by Insightful Corp. > Special thanks to Dawn Woodard! Great. It is nice to see some development going on. Unfortunately, I did not have any time since setting up SF project to work on R2BUGS stuff. I hope this will change in near future. -- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty www: http://www.bfro.uni-lj.si/MR/ggorjan Zootechnical Department blog: http://ggorjan.blogspot.com Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale fax: +386 (0)1 72 17 888 Slovenia, Europe tel: +386 (0)1 72 17 861 ---------------------------------------------------------------------- |
From: <li...@us...> - 2007-05-20 15:45:37
|
Revision: 20 http://svn.sourceforge.net/bugs-r/?rev=20&view=rev Author: ligges Date: 2007-05-20 08:45:39 -0700 (Sun, 20 May 2007) Log Message: ----------- Reverted some former changes: - Added some tricks (defining no-op-functions) to make codetools calm. - we do depend on coda now and we are importing parts of its Namespace now, in order to make codetools calm (S-PLUS does not like the "::" operator). Modified Paths: -------------- trunk/R2WinBUGS/Changes trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/NAMESPACE trunk/R2WinBUGS/R/attach.all.R trunk/R2WinBUGS/R/bugs.data.R trunk/R2WinBUGS/R/read.bugs.R Modified: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes 2007-05-20 15:42:40 UTC (rev 19) +++ trunk/R2WinBUGS/Changes 2007-05-20 15:45:39 UTC (rev 20) @@ -74,3 +74,7 @@ - Ported to S-PLUS by Insightful Corp. - some fixes for codetools checks +Update 2.1-4 (20 May 2007): +- Depending on coda now (Namespace issues) +- some more fixes for codetools checks +- Vignette has been updated Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2007-05-20 15:42:40 UTC (rev 19) +++ trunk/R2WinBUGS/DESCRIPTION 2007-05-20 15:45:39 UTC (rev 20) @@ -1,7 +1,7 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS -Date: 2007-05-13 -Version: 2.1-3 +Date: 2007-05-20 +Version: 2.1-4 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> and Uwe Ligges <li...@st...>. @@ -13,8 +13,8 @@ convergence in a table and graph, and save the simulations in arrays for easy access in R / S-PLUS. In S-PLUS, the openbugs functionality and the windows emulation functionality is not yet available. -Depends: R (>= 2.4.0) -Suggests: coda (>= 0.9-0), BRugs (>= 0.3-0) +Depends: R (>= 2.4.0), coda (>= 0.9-0) +Suggests: BRugs (>= 0.3-0) SystemRequirements: WinBUGS 1.4 URL: http://www.stat.columbia.edu/~gelman/bugsR/ Maintainer: Sibylle Sturtz <st...@st...> Modified: trunk/R2WinBUGS/NAMESPACE =================================================================== --- trunk/R2WinBUGS/NAMESPACE 2007-05-20 15:42:40 UTC (rev 19) +++ trunk/R2WinBUGS/NAMESPACE 2007-05-20 15:45:39 UTC (rev 20) @@ -1,3 +1,5 @@ +importFrom(coda, mcmc.list, read.coda) + export(bugs, attach.all, detach.all, @@ -12,3 +14,4 @@ S3method(print, bugs) S3method(plot, bugs) + Modified: trunk/R2WinBUGS/R/attach.all.R =================================================================== --- trunk/R2WinBUGS/R/attach.all.R 2007-05-20 15:42:40 UTC (rev 19) +++ trunk/R2WinBUGS/R/attach.all.R 2007-05-20 15:45:39 UTC (rev 20) @@ -18,6 +18,9 @@ if(overwrite) remove(list=rem, envir=.GlobalEnv) attach(x, name=name) } else { + ## next line is a dirty trick for R'd codetools check in R-2.5.0 + ## (should be removed after codetoold have been improved): + attach.default <- get("attach.default") attach.default(x, name = name) } } Modified: trunk/R2WinBUGS/R/bugs.data.R =================================================================== --- trunk/R2WinBUGS/R/bugs.data.R 2007-05-20 15:42:40 UTC (rev 19) +++ trunk/R2WinBUGS/R/bugs.data.R 2007-05-20 15:45:39 UTC (rev 20) @@ -24,6 +24,15 @@ } +if(is.R()){ + ## need some fake functions for codetools + "writeDatafileS4" <- toSingleS4 <- function(...) + stop("This function is not intended to be called in R!") +} else { + +### The rest of this file is for S-PLUS only... + + "writeDatafileS4" <- # # Writes to file "towhere" text defining a list containing "DATA" in a form compatable with WinBUGS. @@ -224,3 +233,5 @@ } x } + +} Modified: trunk/R2WinBUGS/R/read.bugs.R =================================================================== --- trunk/R2WinBUGS/R/read.bugs.R 2007-05-20 15:42:40 UTC (rev 19) +++ trunk/R2WinBUGS/R/read.bugs.R 2007-05-20 15:45:39 UTC (rev 20) @@ -1,13 +1,7 @@ read.bugs <- function(codafiles, ...){ - if(!require(coda)) + if(!is.R() && !require("coda")) stop("package 'coda' is required to use this function") - if (is.R()){ - coda::mcmc.list(lapply(codafiles, coda::read.coda, - index.file = file.path(dirname(codafiles[1]), "codaIndex.txt"), - ...)) - }else{ - mcmc.list(lapply(codafiles, read.coda, - index.file = file.path(dirname(codafiles[1]), "codaIndex.txt"), - ...)) - } + mcmc.list(lapply(codafiles, read.coda, + index.file = file.path(dirname(codafiles[1]), "codaIndex.txt"), + ...)) } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gg...@us...> - 2007-06-13 00:59:07
|
Revision: 30 http://svn.sourceforge.net/bugs-r/?rev=30&view=rev Author: ggorjan Date: 2007-06-12 17:59:08 -0700 (Tue, 12 Jun 2007) Log Message: ----------- There was a bug related to use of OpenBUGS and as.bugs.array due to using DIC as a placholder for DIC stats and not a TRUE/FALSE - now using isDIC more consistently as well as DICbyR to distinguish between BUGS and R calculation of pD. I have also made some cosmetic changes in help as well as code files. Modified Paths: -------------- trunk/R2WinBUGS/Changes trunk/R2WinBUGS/R/as.bugs.array.R trunk/R2WinBUGS/R/bugs.R trunk/R2WinBUGS/R/bugs.plot.summary.R trunk/R2WinBUGS/R/bugs.sims.R trunk/R2WinBUGS/R/openbugs.R trunk/R2WinBUGS/R/print.bugs.R trunk/R2WinBUGS/man/as.bugs.array.Rd trunk/R2WinBUGS/man/attach.all.Rd trunk/R2WinBUGS/man/bugs.Rd trunk/R2WinBUGS/man/bugs.data.Rd trunk/R2WinBUGS/man/bugs.inits.Rd trunk/R2WinBUGS/man/bugs.log.Rd trunk/R2WinBUGS/man/bugs.plot.Rd trunk/R2WinBUGS/man/bugs.run.Rd trunk/R2WinBUGS/man/bugs.script.Rd trunk/R2WinBUGS/man/bugs.sims.Rd trunk/R2WinBUGS/man/bugs.update.settings.Rd trunk/R2WinBUGS/man/decode.parameter.name.Rd trunk/R2WinBUGS/man/monitor.Rd trunk/R2WinBUGS/man/openbugs.Rd trunk/R2WinBUGS/man/plot.bugs.Rd trunk/R2WinBUGS/man/print.bugs.Rd trunk/R2WinBUGS/man/read.bugs.Rd trunk/R2WinBUGS/man/write.datafile.Rd trunk/R2WinBUGS/man/write.model.Rd Modified: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/Changes 2007-06-13 00:59:08 UTC (rev 30) @@ -1,229 +1,230 @@ -Changes to R2WinBUGS: -===================== - -Update 2.1-5 (12 June 2007): -- proper indexing of CODA files also in R version -- fixed useWINE documentation in bugs help page -- note on supported BUGS versions -- as.bugs.array now returns info on used rule for pD i.e. var(deviance)/2 -- some internal fixes related to is.null(DIC) -- merging and reversing ChangeLog files - -Update 2.1-4 (20 May 2007): -- Depending on coda now (Namespace issues) -- some more fixes for codetools checks -- Vignette has been updated - -Update 2.1-3 (13 May 2007): -- Ported to S-PLUS by Insightful Corp. -- some fixes for codetools checks - -Update 2.0-4 (01 November 2006): -- print.bugs / plot.bugs documentation fixes -- write.model() fix - -Update 2.0-3 (06 October 2006): -- \\. -> [.] in regular expressions - -Update 2.0-2 (26 July 2006): -- changes for DIC, making use of BUGS internal calculations -- some doc fixes - -Update 2.0-1 (26 May 2006): -- some wine patches for 2.2-0 by Gregor Gorjanc - -Update 2.0-0 (08 May 2006): -- bugs() doc fix/updates on scrambling -- bugs.run() has new arg useWINE (by Gregor Gorjanc) -- bugs() and bugs.script() patched for WINEPATH issue -- bugs.script() changed to save log file in ASCII -- new function bugs.log() by Gregor Gorjanc -- new functions as.bugs.array, openbugs and sort.name by - Jouni Kerman and Andrew Gelman -- new function write.model() based on ideas from Jouni Kerman - -Update 1.1-1 (17 Feb 2006): -- WINE tweaks (mainly by Gregor Gorjanc) - -Update 1.1-0 (14 Dec 2005): -- Contribution by Ben Bolker and Yun Yan's rbugs package: - make R2WinBUGS work under WINE - -Update 1.0-1 (14 Nov 2005): -- make inits=NULL work (again ?) - -Update 1.0-0 (05 Aug 2005): -- attach.all(), detach.all(), attach.bugs() and detach.bugs() added/changed - more or less according to Andrew Gelman's current bugs.R - -Update 0.2-9 (26 July 2005): -- bugs has new argument clearWD - -Update 0.2-8 (30 May 2005): -- bugs passed DIC to bugs.script in order to be able to disable it - -Update 0.2-6 (18 May 2005): -- bugs() changes in order to return a file names of coda output files -- new read.bugs() returns a coda mcmc.list object, if codaPkg=TRUE. - -Update 0.2-5 (20 Oct 2004): -- bugs() and bugs.script() have a new argument bin that allows to specify - a number of iterations. After each "bin" iterations the coda files are saved. - -Update 0.2-4 (05 Oct 2004): -- bugs.script() did not work for large n.iter values in update step - (no scientific notation allowed) - -Update 0.2-3 (10 Sept 2004): -- bugs.data.inits split to bugs.inits and bugs.data, the latter exported - from the Namespace. - Now we can use already written data files in bugs(). - -Update 0.2-2 (28 Apr 2004): -- schools data: original (see references) instead of the rounded data - -During the process of packaging R2WinBUGS_0.1 - R2WinBUGS_0.2-1, -quite a lot of changes had been made. Those changes are not -documented anywhere ... - -During the process of packaging R2WinBUGS_0.1, quite a lot of changes had -been made. Those changes are not documented anywhere ... - -Changes prior to R2WinBUGS_0.1: -=============================== - -Update 30 Oct 2003: - 1. Minor change to a return() statement to be compatible with R 1.8 - 2. Just a warning (from Ben Goodrich): if you are running Bugs.R inside - a loop, you have to be careful with the data() and the inits() - functions since they will not necessarily recognize locally-defined - variables. One workaround is to define the variables used in data() - and inits() using global assignments (<<-), but this can sometimes - make the program run slower. -Update 29 Aug 2003: - 1. Fixed "bugs.data.inits" function so you can use data that have the - same names as R functions. - 2. Changed T and F to TRUE and FALSE everywhere in case the variables - T and F are used as data in the main program - 3. Caution: if you are entering the data as a list of variable names - (see 10 Apr 2003 update, item 1), the data to be input into must - be global variables. This can be relevant if you are running bugs() - inside an R function. - 4. Caution: bugs() has difficulty processing ragged arrays. It is - better to save a whole matrix (e.g., "theta") rather than parts - (e.g., "theta[1:10,1]", "theta[1:5,2]"). If you want to save - part of a vector, you should do it as "theta[1:2]", not "theta[1]", - "theta[2]". -Update 30 Apr 2003: added time monitoring -Update 29 Apr 2003: - 1. The "attach.all" function (no longer called "attach2") overwrites - so that all components of a list or data frame get attached. - 2. Program now looks in the directory /winbug~1/ rather than /winbug~2/ - 3. Graphics parameters for margins are returned to their original state - at the end of the program. - 4. Added "digits.summary" option to the numerical display. - 5. Added "last.values" output: a list that can be input as "inits" - if you want to run the simulations longer. -Update 13 Apr 2003: fixed new bug in round.bugs(). Now all numbers are - saved in scientific notation. -Update 10 Apr 2003: - 1. It is now possible to enter the data as a list of variable names. - For example, before you had to enter data as, - data <- list (n=8, y=c(28,8,-3,7,-1,1,18,12)) - or - n <- 8 - y <- c(28,8,-3,7,-1,1,18,12) - data <- list (n=n, y=y) - Now you can enter the data as, - n <- 8 - y <- c(28,8,-3,7,-1,1,18,12) - data <- list ("n", "y") - The bugs() function will figure out which method you are using (based - on whether "data" is a list of numbers or a vector of character - strings). - This doesn't look like much, but it's convenient when you're entering - lots of data! - 2. It is now possible to enter the initial values as a function, - so as to automatically a random list of inits for each of the chains. - For example, in the 8-schools example below, we can do: - inits <- function() - list (theta=rnorm(J,0,1), mu.theta=rnorm(1,0,100), sigma.theta=runif(1,0,100)) - - to set up the inits as a function (rather than setting up n.chains - sets of specific initial values). Then, the function call, - schools.sim <- bugs (data, inits, parameters, "schools.txt", n.chains=3, n.iter=1000) - automatically sets up 3 sets of initial values (each a list of - theta, mu.theta, sigma.theta). - 3. Bug in the initial rounding (the function round.bugs()) has been fixed. - Thanks for Mark Clements for finding the bug and fixing it! - Also, we have set the default rounding to 5 digits instead of 2. -Update 01 Apr 2003: use layout() rather than split.screen() for graphical - display -Update 18 Mar 2003: - 1. Get the Bugs configuration information from the original file - (Registry_default.odc) rather than overwriting each time. (Fixes a - bug that occurred when R was interrupted in the middle of a Bugs run.) - 2. Display different colored dots in the right panel of the graphical - display, to show the medians from each chain. -Update 13 Mar 2003: fix minor bug in monitor() -Update 10 Mar 2003: fix bug in pD and DIC calculations -Update 7 Mar 2003: - 1. Fix display.parallel=T option by adding min.width so that very - intervals are still visible. - 2. Compute pD separately for each sequence (which gives much more - reasonable estimates before convergence). -Update 8 Feb 2003: minor fixes in graphical display -Update 6 Feb 2003: - 1. Approximate "effective sample size" n.eff given for each parameter. - 2. More explanatory material displayed. - 3. Use bringToTop() to automatically bring up the graphics window. -Update 4 Feb 2003: - 1. Automatically compute the deviance, DIC, and pD. Bugs will not - always compute DIC and pD, so we do so using the definition, - DIC = E(deviance) + pD, using var(deviance)/2 as an estimate of pD. - (This is derived from the chi^2 distribution. We can't use the - Spiegelhalter et al. definition of DIC because we don't have access - to the deviance function.) - 2. Set default for n.thin so that, after thinning, the total number - saved iterations, n.sims, is approximately 1000. -Update 14 Jan 2003 to run with the new WinBugs1.4. You may see an error - message and need to fix the dos.location assignment in bugs(). -Update 6 Jan 2003: - 1. Fix of bug that occurred with uppercase and lowercase variable names - 2. Set default for n.thin so that no more than about 500 iterations - will be saved from each sequence - 3. New option "display.parallel" added to show 80% inferences from - parallel sequences on the right panel of the graphical display. This - can be useful to understand what is going on when there are - convergence problems. -Update 26 Dec 2002: fix of minwidth in bugs.plot.summary -Update 11 Dec 2002: - 1. Automatic fixing of adaptive phases. Now you no longer need to run - for thousands of iterations when slice or Metropolis sampling is used. - 2. Various minor fixes -Update 10 Dec 2002: - 1. Cool graphical display of convergence and inferences! - 2. New "attach2" function that overwrites so that all components of - the list are attached -Update 29 Nov 2002: - 1. Fix of bug in 24 Nov update. - 2. Fix of bug in 16 Nov update. - 3. Length of chains is now pecified in terms of "n.iter" rather than - "n.keep". -Update 24 Nov 2002: improved treatment of "parameters.to.save". For - example, you can now use "alpha" to indicate an entire array of parameters, - whereas before you had to save "alpha[]" or "alpha[,]" or whatever. -Update 16 Nov 2002: mean, sd, median added to outputs -Update 4 Nov 2002: more error-flagging added -Update 26 Oct 2002: - 1. Parameters saved in order of the "parameters.to.save" vector - (not alphabetical order). - 2. Output saved in both matrix and list form. - 3. With the attach.sims=T setting (which is the default), the simulations - for all the saved parameters are saved as R objects. This is - convenient for later use of the simulations. -Updates to 16 Oct 2002: more error-flagging added, mean/sd added to summary, - fixing scientific notation so Bugs can always read data and inits -Update 21 Sept 2002: "quit=F" option changed to "debug=T" -First version written 18 Sept 2002 by Andrew Gelman, - adapted from the EmBedBugs package by Kenneth Rice +Changes to R2WinBUGS: +===================== + +Update 2.1-5 (12 June 2007): +- proper indexing of CODA files also in R version +- fixed useWINE documentation in bugs help page +- note on supported BUGS versions +- as.bugs.array now returns info on used rule for pD i.e. var(deviance)/2 + or Dbar-Dhat +- some internal fixes related to handling of DIC +- merging and reversing ChangeLog files + +Update 2.1-4 (20 May 2007): +- Depending on coda now (Namespace issues) +- some more fixes for codetools checks +- Vignette has been updated + +Update 2.1-3 (13 May 2007): +- Ported to S-PLUS by Insightful Corp. +- some fixes for codetools checks + +Update 2.0-4 (01 November 2006): +- print.bugs / plot.bugs documentation fixes +- write.model() fix + +Update 2.0-3 (06 October 2006): +- \\. -> [.] in regular expressions + +Update 2.0-2 (26 July 2006): +- changes for DIC, making use of BUGS internal calculations +- some doc fixes + +Update 2.0-1 (26 May 2006): +- some wine patches for 2.2-0 by Gregor Gorjanc + +Update 2.0-0 (08 May 2006): +- bugs() doc fix/updates on scrambling +- bugs.run() has new arg useWINE (by Gregor Gorjanc) +- bugs() and bugs.script() patched for WINEPATH issue +- bugs.script() changed to save log file in ASCII +- new function bugs.log() by Gregor Gorjanc +- new functions as.bugs.array, openbugs and sort.name by + Jouni Kerman and Andrew Gelman +- new function write.model() based on ideas from Jouni Kerman + +Update 1.1-1 (17 Feb 2006): +- WINE tweaks (mainly by Gregor Gorjanc) + +Update 1.1-0 (14 Dec 2005): +- Contribution by Ben Bolker and Yun Yan's rbugs package: + make R2WinBUGS work under WINE + +Update 1.0-1 (14 Nov 2005): +- make inits=NULL work (again ?) + +Update 1.0-0 (05 Aug 2005): +- attach.all(), detach.all(), attach.bugs() and detach.bugs() added/changed + more or less according to Andrew Gelman's current bugs.R + +Update 0.2-9 (26 July 2005): +- bugs has new argument clearWD + +Update 0.2-8 (30 May 2005): +- bugs passed DIC to bugs.script in order to be able to disable it + +Update 0.2-6 (18 May 2005): +- bugs() changes in order to return a file names of coda output files +- new read.bugs() returns a coda mcmc.list object, if codaPkg=TRUE. + +Update 0.2-5 (20 Oct 2004): +- bugs() and bugs.script() have a new argument bin that allows to specify + a number of iterations. After each "bin" iterations the coda files are saved. + +Update 0.2-4 (05 Oct 2004): +- bugs.script() did not work for large n.iter values in update step + (no scientific notation allowed) + +Update 0.2-3 (10 Sept 2004): +- bugs.data.inits split to bugs.inits and bugs.data, the latter exported + from the Namespace. + Now we can use already written data files in bugs(). + +Update 0.2-2 (28 Apr 2004): +- schools data: original (see references) instead of the rounded data + +During the process of packaging R2WinBUGS_0.1 - R2WinBUGS_0.2-1, +quite a lot of changes had been made. Those changes are not +documented anywhere ... + +During the process of packaging R2WinBUGS_0.1, quite a lot of changes had +been made. Those changes are not documented anywhere ... + +Changes prior to R2WinBUGS_0.1: +=============================== + +Update 30 Oct 2003: + 1. Minor change to a return() statement to be compatible with R 1.8 + 2. Just a warning (from Ben Goodrich): if you are running Bugs.R inside + a loop, you have to be careful with the data() and the inits() + functions since they will not necessarily recognize locally-defined + variables. One workaround is to define the variables used in data() + and inits() using global assignments (<<-), but this can sometimes + make the program run slower. +Update 29 Aug 2003: + 1. Fixed "bugs.data.inits" function so you can use data that have the + same names as R functions. + 2. Changed T and F to TRUE and FALSE everywhere in case the variables + T and F are used as data in the main program + 3. Caution: if you are entering the data as a list of variable names + (see 10 Apr 2003 update, item 1), the data to be input into must + be global variables. This can be relevant if you are running bugs() + inside an R function. + 4. Caution: bugs() has difficulty processing ragged arrays. It is + better to save a whole matrix (e.g., "theta") rather than parts + (e.g., "theta[1:10,1]", "theta[1:5,2]"). If you want to save + part of a vector, you should do it as "theta[1:2]", not "theta[1]", + "theta[2]". +Update 30 Apr 2003: added time monitoring +Update 29 Apr 2003: + 1. The "attach.all" function (no longer called "attach2") overwrites + so that all components of a list or data frame get attached. + 2. Program now looks in the directory /winbug~1/ rather than /winbug~2/ + 3. Graphics parameters for margins are returned to their original state + at the end of the program. + 4. Added "digits.summary" option to the numerical display. + 5. Added "last.values" output: a list that can be input as "inits" + if you want to run the simulations longer. +Update 13 Apr 2003: fixed new bug in round.bugs(). Now all numbers are + saved in scientific notation. +Update 10 Apr 2003: + 1. It is now possible to enter the data as a list of variable names. + For example, before you had to enter data as, + data <- list (n=8, y=c(28,8,-3,7,-1,1,18,12)) + or + n <- 8 + y <- c(28,8,-3,7,-1,1,18,12) + data <- list (n=n, y=y) + Now you can enter the data as, + n <- 8 + y <- c(28,8,-3,7,-1,1,18,12) + data <- list ("n", "y") + The bugs() function will figure out which method you are using (based + on whether "data" is a list of numbers or a vector of character + strings). + This doesn't look like much, but it's convenient when you're entering + lots of data! + 2. It is now possible to enter the initial values as a function, + so as to automatically a random list of inits for each of the chains. + For example, in the 8-schools example below, we can do: + inits <- function() + list (theta=rnorm(J,0,1), mu.theta=rnorm(1,0,100), sigma.theta=runif(1,0,100)) + + to set up the inits as a function (rather than setting up n.chains + sets of specific initial values). Then, the function call, + schools.sim <- bugs (data, inits, parameters, "schools.txt", n.chains=3, n.iter=1000) + automatically sets up 3 sets of initial values (each a list of + theta, mu.theta, sigma.theta). + 3. Bug in the initial rounding (the function round.bugs()) has been fixed. + Thanks for Mark Clements for finding the bug and fixing it! + Also, we have set the default rounding to 5 digits instead of 2. +Update 01 Apr 2003: use layout() rather than split.screen() for graphical + display +Update 18 Mar 2003: + 1. Get the Bugs configuration information from the original file + (Registry_default.odc) rather than overwriting each time. (Fixes a + bug that occurred when R was interrupted in the middle of a Bugs run.) + 2. Display different colored dots in the right panel of the graphical + display, to show the medians from each chain. +Update 13 Mar 2003: fix minor bug in monitor() +Update 10 Mar 2003: fix bug in pD and DIC calculations +Update 7 Mar 2003: + 1. Fix display.parallel=T option by adding min.width so that very + intervals are still visible. + 2. Compute pD separately for each sequence (which gives much more + reasonable estimates before convergence). +Update 8 Feb 2003: minor fixes in graphical display +Update 6 Feb 2003: + 1. Approximate "effective sample size" n.eff given for each parameter. + 2. More explanatory material displayed. + 3. Use bringToTop() to automatically bring up the graphics window. +Update 4 Feb 2003: + 1. Automatically compute the deviance, DIC, and pD. Bugs will not + always compute DIC and pD, so we do so using the definition, + DIC = E(deviance) + pD, using var(deviance)/2 as an estimate of pD. + (This is derived from the chi^2 distribution. We can't use the + Spiegelhalter et al. definition of DIC because we don't have access + to the deviance function.) + 2. Set default for n.thin so that, after thinning, the total number + saved iterations, n.sims, is approximately 1000. +Update 14 Jan 2003 to run with the new WinBugs1.4. You may see an error + message and need to fix the dos.location assignment in bugs(). +Update 6 Jan 2003: + 1. Fix of bug that occurred with uppercase and lowercase variable names + 2. Set default for n.thin so that no more than about 500 iterations + will be saved from each sequence + 3. New option "display.parallel" added to show 80% inferences from + parallel sequences on the right panel of the graphical display. This + can be useful to understand what is going on when there are + convergence problems. +Update 26 Dec 2002: fix of minwidth in bugs.plot.summary +Update 11 Dec 2002: + 1. Automatic fixing of adaptive phases. Now you no longer need to run + for thousands of iterations when slice or Metropolis sampling is used. + 2. Various minor fixes +Update 10 Dec 2002: + 1. Cool graphical display of convergence and inferences! + 2. New "attach2" function that overwrites so that all components of + the list are attached +Update 29 Nov 2002: + 1. Fix of bug in 24 Nov update. + 2. Fix of bug in 16 Nov update. + 3. Length of chains is now pecified in terms of "n.iter" rather than + "n.keep". +Update 24 Nov 2002: improved treatment of "parameters.to.save". For + example, you can now use "alpha" to indicate an entire array of parameters, + whereas before you had to save "alpha[]" or "alpha[,]" or whatever. +Update 16 Nov 2002: mean, sd, median added to outputs +Update 4 Nov 2002: more error-flagging added +Update 26 Oct 2002: + 1. Parameters saved in order of the "parameters.to.save" vector + (not alphabetical order). + 2. Output saved in both matrix and list form. + 3. With the attach.sims=T setting (which is the default), the simulations + for all the saved parameters are saved as R objects. This is + convenient for later use of the simulations. +Updates to 16 Oct 2002: more error-flagging added, mean/sd added to summary, + fixing scientific notation so Bugs can always read data and inits +Update 21 Sept 2002: "quit=F" option changed to "debug=T" +First version written 18 Sept 2002 by Andrew Gelman, + adapted from the EmBedBugs package by Kenneth Rice Modified: trunk/R2WinBUGS/R/as.bugs.array.R =================================================================== --- trunk/R2WinBUGS/R/as.bugs.array.R 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/R/as.bugs.array.R 2007-06-13 00:59:08 UTC (rev 30) @@ -1,10 +1,12 @@ -as.bugs.array <- function(sims.array, model.file=NULL, program=NULL, DIC=FALSE, n.iter=NULL, n.burnin=0, n.thin=1) +as.bugs.array <- function(sims.array, model.file=NULL, program=NULL, + DIC=FALSE, DICOutput=NULL, + n.iter=NULL, n.burnin=0, n.thin=1) { - # Jouni Kerman's function to convert a 3-way array to a Bugs object - # - # 'sims.array' is supposed to be a 3-way array with - # n.sims*n.chains*n.parameters simulations, and - # the 3rd component of dimnames(x) should have the parameter names. + ## Jouni Kerman's function to convert a 3-way array to a Bugs object + ## + ## 'sims.array' is supposed to be a 3-way array with + ## n.sims*n.chains*n.parameters simulations, and + ## the 3rd component of dimnames(x) should have the parameter names. d <- dim(sims.array) n.keep <- d[1] n.chains <- d[2] @@ -30,7 +32,7 @@ n.roots <- length(parameters.to.save) left.bracket.short <- as.vector(regexpr("[[]", parameters.to.save)) right.bracket.short <- as.vector(regexpr("[]]", parameters.to.save)) - root.short <- ifelse(left.bracket.short == -1, parameters.to.save, + root.short <- ifelse(left.bracket.short == -1, parameters.to.save, substring(parameters.to.save, 1, left.bracket.short - 1)) dimension.short <- rep(0, n.roots) indexes.short <- vector(n.roots, mode = "list") @@ -51,7 +53,7 @@ } length.short[j] <- prod(n.indexes.short[[j]]) if (length(long.short[[j]]) != length.short[j]){ - stop(paste("error in parameter", root.short[[j]], + stop(paste("error in parameter", root.short[[j]], "in parameters.to.save")) } indexes.short[[j]] <- as.list(numeric(length.short[j])) @@ -71,11 +73,7 @@ summary <- monitor(sims.array, n.chains, keep.all = TRUE) last.values <- as.list(numeric(n.chains)) for (i in 1:n.chains) { - if (is.R()) { - n.roots.0 <- if(!is.null(DIC)) n.roots - 1 else n.roots - } else { - n.roots.0 <- if(DIC) n.roots - 1 else n.roots - } + n.roots.0 <- if(DIC) n.roots - 1 else n.roots last.values[[i]] <- as.list(numeric(n.roots.0)) names(last.values[[i]]) <- root.short[1:n.roots.0] for (j in 1:n.roots.0) { @@ -90,7 +88,7 @@ } } sims <- sims[sample(n.sims), ] - sims.list <- summary.mean <- summary.sd <- summary.median <- vector(n.roots, + sims.list <- summary.mean <- summary.sd <- summary.median <- vector(n.roots, mode = "list") names(sims.list) <- names(summary.mean) <- names(summary.sd) <- names(summary.median) <- root.short for (j in 1:n.roots) { @@ -102,31 +100,28 @@ } else { temp2 <- dimension.short[j]:1 - sims.list[[j]] <- aperm(array(sims[, long.short[[j]]], + sims.list[[j]] <- aperm(array(sims[, long.short[[j]]], c(n.sims, rev(n.indexes.short[[j]]))), c(1, (dimension.short[j]+1):2)) - summary.mean[[j]] <- aperm(array(summary[long.short[[j]], + summary.mean[[j]] <- aperm(array(summary[long.short[[j]], "mean"], rev(n.indexes.short[[j]])), temp2) - summary.sd[[j]] <- aperm(array(summary[long.short[[j]], + summary.sd[[j]] <- aperm(array(summary[long.short[[j]], "sd"], rev(n.indexes.short[[j]])), temp2) - summary.median[[j]] <- aperm(array(summary[long.short[[j]], + summary.median[[j]] <- aperm(array(summary[long.short[[j]], "50%"], rev(n.indexes.short[[j]])), temp2) } } summary <- summary[rank.long, ] - if (is.R()) - is.DIC = !is.null(DIC) - else - is.DIC = DIC - - all <- list(n.chains = n.chains, n.iter = n.iter, n.burnin = n.burnin, + + all <- list(n.chains = n.chains, n.iter = n.iter, n.burnin = n.burnin, n.thin = n.thin, n.keep = n.keep, n.sims = n.sims, - sims.array = sims.array[,,rank.long,drop = FALSE], sims.list = sims.list, - sims.matrix = sims[, rank.long], summary = summary, mean = summary.mean, - sd = summary.sd, median = summary.median, root.short = root.short, - long.short = long.short, dimension.short = dimension.short, + sims.array = sims.array[,,rank.long,drop = FALSE], sims.list = sims.list, + sims.matrix = sims[, rank.long], summary = summary, mean = summary.mean, + sd = summary.sd, median = summary.median, root.short = root.short, + long.short = long.short, dimension.short = dimension.short, indexes.short = indexes.short, last.values = last.values, program=program, - model.file=model.file, is.DIC=is.DIC, DIC=DIC) - if(sum(DIC)) { + model.file=model.file) + + if(DIC && is.null(DICOutput)) { ## calculate DIC from deviance deviance <- all$sims.array[, , dim(sims.array)[3], drop = FALSE] dim(deviance) <- dim(deviance)[1:2] pD <- numeric(n.chains) @@ -135,8 +130,15 @@ pD[i] <- var(deviance[, i])/2 DIC[i] <- mean(deviance[, i]) + pD[i] } - all <- c(all, list(pD = mean(pD), DIC = mean(DIC))) + all <- c(all, list(isDIC=TRUE, DICbyR=TRUE, + pD=mean(pD), DIC=mean(DIC))) + } else if(DIC && !is.null(DICOutput)) { ## use DIC from BUGS + all <- c(all, list(isDIC=TRUE, DICbyR=FALSE, + pD=DICOutput[nrow(DICOutput),4], + DIC=DICOutput[nrow(DICOutput),3])) + } else { + all <- c(all, isDIC=FALSE) } class(all) <- "bugs" - return(all) + all } Modified: trunk/R2WinBUGS/R/bugs.R =================================================================== --- trunk/R2WinBUGS/R/bugs.R 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/R/bugs.R 2007-06-13 00:59:08 UTC (rev 30) @@ -5,18 +5,18 @@ bin=(n.iter - n.burnin) / n.thin, debug=FALSE, DIC=TRUE, digits=5, codaPkg=FALSE, bugs.directory="c:/Program Files/WinBUGS14/", - program=c("winbugs", "openbugs", "WinBugs", "OpenBugs"), + program=c("WinBUGS", "OpenBUGS", "winbugs", "openbugs"), working.directory=NULL, clearWD=FALSE, useWINE=.Platform$OS.type != "windows", WINE=Sys.getenv("WINE"), newWINE=FALSE, WINEPATH=NULL) { program <- match.arg(program) - if(program %in% c("openbugs", "OpenBugs")) { + if(program %in% c("openbugs", "OpenBUGS", "OpenBugs")) { if(is.R()) { ## If OpenBUGS, we only call openbugs() and exit... return(openbugs(data, inits, parameters.to.save, model.file, - n.chains, n.iter, n.burnin, n.thin, DIC, bugs.directory, - working.directory, digits)) + n.chains, n.iter, n.burnin, n.thin, DIC=DIC, + bugs.directory, working.directory, digits)) } else { stop ("OpenBUGS is not yet available in S-PLUS") } @@ -72,7 +72,7 @@ return(file.path(getwd(), paste("coda", 1:n.chains, ".txt", sep=""))) sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), - model.file=model.file, is.DIC=DIC, program=program) + model.file=model.file, program=program) if(clearWD) { file.remove(c("data.txt", "log.odc", "log.txt", "codaIndex.txt", paste("inits", 1:n.chains, ".txt", sep=""), Modified: trunk/R2WinBUGS/R/bugs.plot.summary.R =================================================================== --- trunk/R2WinBUGS/R/bugs.plot.summary.R 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/R/bugs.plot.summary.R 2007-06-13 00:59:08 UTC (rev 30) @@ -1,6 +1,6 @@ "bugs.plot.summary" <- function (sims, ...){ - DIC <- sims$is.DIC + isDIC <- sims$isDIC if (.Device=="windows" || (.Device=="null device" && options("device")=="windows")){ @@ -23,7 +23,7 @@ n.parameters <- nrow(summ) J0 <- unlist(lapply(sims$long.short, length)) - if (DIC) J0 <- J0[1:(length(J0)-1)] # don't display deviance summaries + if (isDIC) J0 <- J0[1:(length(J0)-1)] # don't display deviance summaries J <- J0 total <- ceiling(sum(J+.5)) while ((total > max.length) && max(J)>1){### vielleicht optimieren ... @@ -40,7 +40,7 @@ ystart <- NULL jj <- 1:J[1] n.roots <- length(sims$root.short) - if (DIC) n.roots <- n.roots-1 # don't display deviance summaries + if (isDIC) n.roots <- n.roots-1 # don't display deviance summaries ystart <- numeric(n.roots) for (k in 1:n.roots){ ystart[k] <- pos Modified: trunk/R2WinBUGS/R/bugs.sims.R =================================================================== --- trunk/R2WinBUGS/R/bugs.sims.R 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/R/bugs.sims.R 2007-06-13 00:59:08 UTC (rev 30) @@ -123,8 +123,9 @@ long.short=long.short, dimension.short=dimension.short, indexes.short=indexes.short, last.values=last.values) if(DIC) { + ## Read DIC from BUGS log LOG <- bugs.log("log.txt")$DIC - if(any(is.na(LOG))) { + if(any(is.na(LOG))) { ## Something went wrong --> Use Gelmans tweak deviance <- all$sims.array[, , dim(sims.array)[3], drop = FALSE] if(!is.R()) dimnames(deviance) <- NULL dim(deviance) <- dim(deviance)[1:2] @@ -134,12 +135,14 @@ pD[i] <- var(deviance[, i])/2 DIC[i] <- mean(deviance[, i]) + pD[i] } - all <- c(all, list(pD = mean(pD), DIC = mean(DIC), DICbyR = TRUE)) - } else { - DIC <- LOG[nrow(LOG),4] - pD <- LOG[nrow(LOG),3] - all <- c(all, list(pD = pD, DIC = DIC, DICbyR = FALSE)) + all <- c(all, list(isDIC=TRUE, DICbyR=TRUE, + pD=mean(pD), DIC=mean(DIC))) + } else { ## Use BUGS calculation of DIC + all <- c(all, list(isDIC=TRUE, DICbyR=FALSE, + pD=LOG[nrow(LOG),3], DIC=LOG[nrow(LOG),4])) } + } else { + all <- c(all, isDIC=FALSE) } all } Modified: trunk/R2WinBUGS/R/openbugs.R =================================================================== --- trunk/R2WinBUGS/R/openbugs.R 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/R/openbugs.R 2007-06-13 00:59:08 UTC (rev 30) @@ -1,20 +1,21 @@ -if (is.R()){ -openbugs <- 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)), +if (is.R()) { + +openbugs <- 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)), DIC = TRUE, bugs.directory = "c:/Program Files/OpenBUGS/", - working.directory=NULL, digits = 5) { - -# switching from bugs() to BRugsFit() notation - if(!require(BRugs)) + working.directory=NULL, digits = 5) +{ + if(!require(BRugs)) stop("BRugs is required") + ## switching from bugs() to BRugsFit() notation modelFile <- model.file numChains <- n.chains nBurnin <- n.burnin nIter <- n.iter - n.burnin nThin <- n.thin - if(!is.null(DIC)) parameters.to.save <- c(parameters.to.save, "deviance") + if(DIC) parameters.to.save <- c(parameters.to.save, "deviance") parametersToSave <- parameters.to.save if(!is.null(working.directory)) { savedWD <- getwd() @@ -27,7 +28,7 @@ if(file.info(modelFile)$isdir) { stop(modelFile, " is a directory, but a file is required") } -# kludge to add carriage-returns (line-feeds?) to the model file + ## kludge to add carriage-returns (line-feeds?) to the model file if(!length(grep("\r\n", readChar(modelFile, 10^3)))) { message("Carriage returns added to model file ", modelFile) model <- readLines(modelFile) @@ -41,24 +42,27 @@ BRugs::modelCompile(numChains) if(missing(inits) || is.null(inits)) { BRugs::modelGenInits() - } - else { - if(is.list(inits) || is.function(inits) || (is.character(inits) && - !any(file.exists(inits)))){ - inits <- BRugs::bugsInits(inits = inits, numChains = numChains, digits = digits) + } else { + if(is.list(inits) || is.function(inits) || (is.character(inits) && + !any(file.exists(inits)))) { + inits <- BRugs::bugsInits(inits = inits, numChains = numChains, + digits = digits) } BRugs::modelInits(inits) BRugs::modelGenInits() } - BRugs::samplesSetThin(nThin) -# set the adaptive phases - adaptivelines <- scan(system.file("OpenBUGS", "Bugs", "Rsrc", "Registry.txt", package="BRugs"), what="character") + ## BRugs::samplesSetThin(nThin) + ## set the adaptive phases + adaptivelines <- scan(system.file("OpenBUGS", "Bugs", "Rsrc", + "Registry.txt", package="BRugs"), + what="character") factories <- sub(".adaptivePhase", "", - adaptivelines[grep("adaptivePhase",adaptivelines)]) - sapply(factories, BRugs::modelSetAP, max(0, nBurnin-1)) - + adaptivelines[grep("adaptivePhase",adaptivelines)]) + sapply(factories, BRugs::modelSetAP, max(0, nBurnin-1)) + BRugs::modelUpdate(nBurnin) - if(!is.null(DIC)) { + BRugs::samplesSetThin(nThin) + if(DIC) { BRugs::dicSet() on.exit(BRugs::dicClear(), add = TRUE) } @@ -69,21 +73,25 @@ n.saved.per.chain <- nrow(samples)/numChains samples.array <- array(samples, c(n.saved.per.chain, numChains, ncol(samples))) dimnames(samples.array)[[3]] <- dimnames(samples)[[2]] - if(!is.null(DIC)) DIC <- BRugs::dicStats() - bugs.output <- as.bugs.array(samples.array, modelFile, program="OpenBUGS", - n.iter=n.iter, n.burnin=n.burnin, n.thin=n.thin, DIC=DIC) - return(bugs.output) + if(DIC) { + DICOutput <- BRugs::dicStats() + } else { + DICOutput <- NULL + } + bugs.output <- as.bugs.array(sims.array=samples.array, + model.file=modelFile, program="OpenBUGS", + DIC=DIC, DICOutput=DICOutput, + n.iter=n.iter, n.burnin=n.burnin, n.thin=n.thin) + bugs.output } - - -# sorter called by openbugs() to save the parameters in the specified order +## sorter called by openbugs() to save the parameters in the specified order sort.name <- function(a, b){ -# sort the scalar parameter names in the vector "a" according to the ordering -# of their stems in the shorter vector "b" + ## sort the scalar parameter names in the vector "a" according to the + ## ordering of their stems in the shorter vector "b" bracket.pos <- regexpr("\\[", a) a.stem <- substr(a, 1, ifelse(bracket.pos>0, bracket.pos-1, nchar(a))) - return(a[order(match(a.stem, b))]) + a[order(match(a.stem, b))] } } Modified: trunk/R2WinBUGS/R/print.bugs.R =================================================================== --- trunk/R2WinBUGS/R/print.bugs.R 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/R/print.bugs.R 2007-06-13 00:59:08 UTC (rev 30) @@ -12,22 +12,23 @@ if(x$n.thin > 1) cat(", n.thin =", x$n.thin) cat("\n n.sims =", x$n.sims, "iterations saved\n") print(round(x$summary, digits.summary), ...) + if(x$n.chains > 1) { cat("\nFor each parameter, n.eff is a crude measure of effective sample size,") cat("\nand Rhat is the potential scale reduction factor (at convergence, Rhat=1).\n") } - msgDICrule <- ifelse(x$DICbyR, - "(using the rule, pD = var(deviance)/2)\n", - "(using the rule, pD = Dbar-Dhat)\n") - msgDIC <- "DIC is an estimate of expected predictive error (lower deviance is better).\n" - if(length(x$DIC) == 1 && x$DIC) { - cat("\npD =", fround(x$pD, 1), "and DIC =", fround(x$DIC, 1), msgDICrule) - cat(msgDIC) + + if(x$isDIC) { + msgDICRule <- ifelse(x$DICbyR, + "(using the rule, pD = var(deviance)/2)", ## Gelman tweak + "(using the rule, pD = Dbar-Dhat)") ## BUGS + cat(paste("\nDIC info ", msgDICRule, "\n", sep="")) + if(length(x$DIC) == 1) { + cat("pD =", fround(x$pD, 1), "and DIC =", fround(x$DIC, 1)) + } else if(length(x$DIC)>1) { + print(round(x$DIC, 1)) + } + cat("\nDIC is an estimate of expected predictive error (lower deviance is better).\n") } - else if(length(x$DIC)>1) { - cat("\n") - print(round(x$DIC, 1)) - cat(msgDIC) - } invisible(x) } Modified: trunk/R2WinBUGS/man/as.bugs.array.Rd =================================================================== --- trunk/R2WinBUGS/man/as.bugs.array.Rd 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/man/as.bugs.array.Rd 2007-06-13 00:59:08 UTC (rev 30) @@ -1,38 +1,58 @@ \name{as.bugs.array} \alias{as.bugs.array} \title{Convert to bugs object} -\description{Function converting results from Markov chain simulations that are not from BUGS - to be displayed using \code{\link{plot.bugs}}. + +\description{Function converting results from Markov chain simulations, + that might not be from BUGS, to bugs object. Used mainly to display + results with \code{\link{plot.bugs}}. } + \usage{ -as.bugs.array(sims.array, model.file=NULL, program=NULL, - DIC=FALSE, n.iter=NULL, n.burnin=0, n.thin=1) +as.bugs.array(sims.array, model.file=NULL, program=NULL, + DIC=FALSE, DICOutput=NULL, n.iter=NULL, n.burnin=0, n.thin=1) } + \arguments{ \item{sims.array}{3-way array of simulation output, with dimensions n.keep, n.chains, and length of combined parameter vector.} \item{model.file}{file containing the model written in WinBUGS code} \item{program}{the program used} - \item{DIC}{logical; whether DIC is given} - \item{n.iter}{number of total iterations per chain used for generating \code{sims.array}} - \item{n.burnin}{length of burn in, i.e. number of iterations to discarded - at the beginning for generating \code{sims.array}} - \item{n.thin}{thinning rate, a positive integer, used for generating \code{sims.array}} + \item{DIC}{logical; whether DIC should be calculated, see also + argument \code{DICOutput} and details} + \item{DICOutput}{DIC value} + \item{n.iter}{number of total iterations per chain used for generating + \code{sims.array}} + \item{n.burnin}{length of burn in, i.e. number of iterations to + discarded at the beginning for generating \code{sims.array}} + \item{n.thin}{thinning rate, a positive integer, used for generating + \code{sims.array}} } + \details{ -This function takes a 3-way array of simulations and makes it into a \code{\link{bugs}} -object that can be conveniently displayed using \code{print} and -\code{plot} and accessed using \code{attach.bugs}. If the third -dimension of sims() has names, the resulting bugs object will respect -that naming convention. For example, if the parameter names are -\dQuote{alpha[1]}, \dQuote{alpha[2]}, ..., \dQuote{alpha[8]}, \dQuote{mu}, \dQuote{tau}, then -\code{as.bugs.array} will know that alpha is a vector of length 8, and mu and -tau are scalar parameters. These will all be plotted appropriately by -\code{plot} and attached appropriately by \code{attach.bugs}.} -\value{ - A \code{\link{bugs}} object is returned. + +This function takes a 3-way array of simulations and makes it into a +\code{\link{bugs}} object that can be conveniently displayed using +\code{print} and \code{plot} and accessed using \code{attach.bugs}. If +the third dimension of sims() has names, the resulting bugs object will +respect that naming convention. For example, if the parameter names are +\dQuote{alpha[1]}, \dQuote{alpha[2]}, ..., \dQuote{alpha[8]}, +\dQuote{mu}, \dQuote{tau}, then \code{as.bugs.array} will know that +alpha is a vector of length 8, and mu and tau are scalar parameters. +These will all be plotted appropriately by \code{plot} and attached +appropriately by \code{attach.bugs}. + +If \code{DIC=TRUE} then DIC can be either already passed to argument +\code{DICOutput} as it is done in \code{\link{openbugs}} or calculated +from deviance values in \code{sims.array}. + } -\author{Jouni Kerman, \email{ke...@st...} with modification by Andrew Gelman, \email{ge...@st...}, - packaged by Uwe Ligges, \email{li...@st...}.} + +\value{A \code{\link{bugs}} object is returned} + +\author{Jouni Kerman, \email{ke...@st...} with modification + by Andrew Gelman, \email{ge...@st...}, packaged by Uwe + Ligges, \email{li...@st...}.} + \seealso{\code{\link{bugs}}} \keyword{interface} +\keyword{manip} Modified: trunk/R2WinBUGS/man/attach.all.Rd =================================================================== --- trunk/R2WinBUGS/man/attach.all.Rd 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/man/attach.all.Rd 2007-06-13 00:59:08 UTC (rev 30) @@ -4,78 +4,95 @@ \alias{attach.bugs} \alias{detach.bugs} \title{Attach / detach elements of (bugs) objects to search path} -\description{The database is attached/detached to the search path. See \code{\link{attach}} for details.} + +\description{The database is attached/detached to the search path. See + \code{\link{attach}} for details.} + \usage{ attach.all(x, overwrite = NA, name = "attach.all") attach.bugs(x, overwrite = NA) detach.all(name = "attach.all") detach.bugs() } + \arguments{ - \item{x}{An object, which must be of class \code{bugs} for \code{attach.bugs}.} - \item{overwrite}{If \code{TRUE}, objects with identical names in the Workspace (.GlobalEnv) - that are masking objects in the database to be attached will be deleted. + \item{x}{An object, which must be of class \code{bugs} for + \code{attach.bugs}.} + \item{overwrite}{If \code{TRUE}, objects with identical names in the + Workspace (.GlobalEnv) that are masking objects in the database to + be attached will be deleted. If \code{NA} (the default) and an + interactive session is running, a dialog box asks the user whether + masking objects should be deleted. In non-interactive mode, + behaviour is identical to \code{overwrite=FALSE}, i.e. nothing will + be deleted.} + \item{name}{The name of the environment where \code{x} will be + attached / which will be detached.} +} - If \code{NA} (the default) and an interactive session is running, a dialog box asks the user - whether masking objects should be deleted. - In non-interactive mode, behaviour is identical to \code{overwrite=FALSE}, i.e. nothing will be deleted.} - \item{name}{The name of the environment where \code{x} will be attached / which will be detached.} -} \details{ - While \code{attach.all} attaches all elements of an object \code{x} to a database called \code{name}, - \code{attach.bugs} attaches all elements of \code{x$sims.list} to the database \code{bugs.sims} - itself making use of \code{attach.all}.\cr - \code{detach.all} and \code{detach.bugs} are removing the databases - mentioned above.\cr - \code{attach.all} also attaches \code{n.sims} (the number of - simulations saved from the MCMC runs) to the database.\cr\cr + While \code{attach.all} attaches all elements of an object \code{x} to + a database called \code{name}, \code{attach.bugs} attaches all + elements of \code{x$sims.list} to the database \code{bugs.sims} itself + making use of \code{attach.all}.\cr - Each scalar parameter in the model is attached as vectors of length - \code{n.sims}, each vector is attached as a 2-way array (with first - dimension equal to \code{n.sims}, each matrix is attached as a 3-way - array, and so forth.} + \code{detach.all} and \code{detach.bugs} are removing the databases + mentioned above.\cr \code{attach.all} also attaches \code{n.sims} (the + number of simulations saved from the MCMC runs) to the database.\cr\cr + + Each scalar parameter in the model is attached as vectors of length + \code{n.sims}, each vector is attached as a 2-way array (with first + dimension equal to \code{n.sims}), each matrix is attached as a 3-way + array, and so forth. +} + \note{ - Without detaching, do not use \code{attach.all} or \code{attach.bugs} on another (\code{bugs}) object, - because instead of the given name, an object called \code{name} is attached. - Therefore strange things may happen... + Without detaching, do not use \code{attach.all} or \code{attach.bugs} + on another (\code{bugs}) object, because instead of the given name, an + object called \code{name} is attached. Therefore strange things may + happen \ldots } + \value{ - \code{attach.all} and \code{attach.bugs} invisibly return the \code{\link{environment}}(s).\cr - \code{detach.all} and \code{detach.bugs} detach the \code{environment}(s) named \code{name} - created by \code{attach.all}. + \code{attach.all} and \code{attach.bugs} invisibly return the + \code{\link{environment}}(s).\cr + + \code{detach.all} and \code{detach.bugs} detach the + \code{environment}(s) named \code{name} created by \code{attach.all}. } + \seealso{\code{\link{bugs}}, \code{\link{attach}}, \code{\link{detach}}} + \examples{ # An example model file is given in: model.file <- file.path(.path.package("R2WinBUGS"), "model", "schools.txt") # Some example data (see ?schools for details): -data(schools) +data(schools) J <- nrow(schools) y <- schools$estimate sigma.y <- schools$sd data <- list ("J", "y", "sigma.y") inits <- function(){ - list(theta = rnorm(J, 0, 100), mu.theta = rnorm(1, 0, 100), + list(theta = rnorm(J, 0, 100), mu.theta = rnorm(1, 0, 100), sigma.theta = runif(1, 0, 100)) } -parameters <- c("theta", "mu.theta", "sigma.theta") +parameters <- c("theta", "mu.theta", "sigma.theta") \dontrun{ ## You may need to edit "bugs.directory", ## also you need write access in the working directory: -schools.sim <- bugs(data, inits, parameters, model.file, - n.chains = 3, n.iter = 1000, +schools.sim <- bugs(data, inits, parameters, model.file, + n.chains = 3, n.iter = 1000, bugs.directory = "c:/Program Files/WinBUGS14/", working.directory = NULL) # Do some inferential summaries attach.bugs(schools.sim) -# posterior probability that the coaching program in school A +# posterior probability that the coaching program in school A # is better than in school C: print(mean(theta[,1] > theta[,3])) -# 50% posterior interval for the difference between school A's +# 50% posterior interval for the difference between school A's # and school C's program: -print(quantile(theta[,1] - theta[,3], c(.25, .75))) -plot(theta[,1], theta[,3]) +print(quantile(theta[,1] - theta[,3], c(.25, .75))) +plot(theta[,1], theta[,3]) detach.bugs() } } Modified: trunk/R2WinBUGS/man/bugs.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.Rd 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/man/bugs.Rd 2007-06-13 00:59:08 UTC (rev 30) @@ -1,9 +1,11 @@ \name{bugs} \alias{bugs} \title{Run WinBUGS and OpenBUGS from R or S-PLUS} -\description{The \code{bugs} function takes data and starting values as input. -It automatically writes a WinBUGS script, calls the model, -and saves the simulations for easy access in R or S-PLUS.} + +\description{The \code{bugs} function takes data and starting values as + input. It automatically writes a WinBUGS script, calls the model, and + saves the simulations for easy access in R or S-PLUS.} + \usage{ bugs(data, inits, parameters.to.save, model.file="model.bug", n.chains=3, n.iter=2000, n.burnin=floor(n.iter/2), @@ -11,11 +13,12 @@ bin=(n.iter - n.burnin) / n.thin, debug=FALSE, DIC=TRUE, digits=5, codaPkg=FALSE, bugs.directory="c:/Program Files/WinBUGS14/", - program=c("winbugs", "openbugs", "WinBugs", "OpenBugs"), + program=c("WinBUGS", "OpenBUGS", "winbugs", "openbugs"), working.directory=NULL, clearWD=FALSE, useWINE=.Platform$OS.type != "windows", WINE=Sys.getenv("WINE"), newWINE=FALSE, WINEPATH=NULL) } + \arguments{ \item{data}{either a named list (names corresponding to variable names in the \code{model.file}) of the data for the WinBUGS model, @@ -32,7 +35,7 @@ parameters to save which should be monitored} \item{model.file}{file containing the model written in WinBUGS code. The extension can be either \file{.bug} or \file{.txt}. - If the extension is \file{.bug} and \code{program=="winbugs"}, + If the extension is \file{.bug} and \code{program=="WinBUGS"}, a copy of the file with extension \file{.txt} will be created in the \code{bugs()} call and removed afterwards. Note that similarly named \file{.txt} files will be overwritten.} @@ -62,12 +65,12 @@ \item{codaPkg}{logical; if \code{FALSE} (default) a \code{bugs} object is returned, if \code{TRUE} file names of WinBUGS output are returned for easy access by the \pkg{coda} package through function - \code{\link{read.bugs}} (not used if \code{program="openbugs"}).} + \code{\link{read.bugs}} (not used if \code{program="OpenBUGS"}).} \item{bugs.directory}{directory that contains the WinBUGS executable} \item{program}{the program to use, either - \code{winbugs}/\code{WinBugs} or \code{openbugs}/\code{OpenBugs}, + \code{winbugs}/\code{WinBUGS} or \code{openbugs}/\code{OpenBUGS}, the latter makes use of function \code{\link{openbugs}} and requires - the CRAN package \pkg{BRugs}. The \code{openbugs}/\code{OpenBugs} + the CRAN package \pkg{BRugs}. The \code{openbugs}/\code{OpenBUGS} choice is not available in S-PLUS.} \item{working.directory}{sets working directory during execution of this function; WinBUGS' in- and output will be stored in this @@ -98,7 +101,7 @@ \item A WinBUGS window will pop up and R / S-PLUS will freeze up. The model will now run in WinBUGS. It might take awhile. You will see things happening in the Log window within WinBUGS. When - WinBugs is done, its window will close and R / S-PLUS will work + WinBUGS is done, its window will close and R / S-PLUS will work again. \item If an error message appears, re-run with \code{debug=TRUE}. } @@ -171,8 +174,8 @@ packaged by Sibylle Sturtz, \email{st...@st...}, and Uwe Ligges.} -\seealso{\code{\link{print.bugs}}, \code{\link{plot.bugs}}, and the - \pkg{coda} package} +\seealso{\code{\link{print.bugs}}, \code{\link{plot.bugs}}, as well as + \pkg{coda} and \pkg{BRugs} packages} \examples{ # An example model file is given in: @@ -214,5 +217,6 @@ plot(schools.sim) } } + \keyword{interface} \keyword{models} Modified: trunk/R2WinBUGS/man/bugs.data.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.data.Rd 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/man/bugs.data.Rd 2007-06-13 00:59:08 UTC (rev 30) @@ -1,19 +1,27 @@ \name{bugs.data} \alias{bugs.data} \title{Writing input for WinBUGS} -\description{Write file \file{data.txt} for WinBUGS to read.} + +\description{Write file \file{data.txt} for WinBUGS to read - for + internal use.} + \usage{ bugs.data(data, dir = getwd(), digits = 5) } \arguments{ - \item{data}{either a named list (names corresponding to variable names in the \code{model.file}) - of the data for the WinBUGS model, \emph{or} - a vector or list of the names of the data objects used by the model} + \item{data}{either a named list (names corresponding to variable names + in the \code{model.file}) of the data for the WinBUGS model, + \emph{or} a vector or list of the names of the data objects used by + the model} \item{dir}{the directory to write the file \file{data.txt} to} - \item{digits}{number of significant digits used for WinBUGS input, see \code{\link{formatC}}} + \item{digits}{number of significant digits used for WinBUGS input, see + \code{\link{formatC}}} } -\value{ - Does not return anything. -} -\seealso{\code{\link{bugs}}} + +\value{Nothing, but as a side effect, the data file \file{data.txt} is + written} + +\seealso{The main function to be called by the user is \code{\link{bugs}}.} +\keyword{internal} \keyword{file} +\keyword{IO} \ No newline at end of file Modified: trunk/R2WinBUGS/man/bugs.inits.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.inits.Rd 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/man/bugs.inits.Rd 2007-06-13 00:59:08 UTC (rev 30) @@ -1,20 +1,26 @@ \name{bugs.inits} \alias{bugs.inits} \title{Writing input for WinBUGS - intended for internal use only} -\description{Write files \file{inits1.txt}, \file{inits2.txt}, etc., in the working directory -for WinBUGS to read. For internal use only.} + +\description{Write files \file{inits1.txt}, \file{inits2.txt}, etc., in + the working directory for WinBUGS to read - for internal use.} + \usage{ bugs.inits(inits, n.chains, digits) } \arguments{ - \item{inits}{a list with \code{n.chains} elements; each element of the list is - itself a list of starting values for the WinBUGS model, \emph{or} - a function creating (possibly random) initial values} + \item{inits}{a list with \code{n.chains} elements; each element of the + list is itself a list of starting values for the WinBUGS model, + \emph{or} a function creating (possibly random) initial values} \item{n.chains}{number of Markov chains} - \item{digits}{number of significant digits used for WinBUGS input, see \code{\link{formatC}}} + \item{digits}{number of significant digits used for WinBUGS input, see + \code{\link{formatC}}} } -\value{ - Does not return anything. -} + +\value{Nothing, but as a side effect, the inits files \file{inits*.txt} + are written} + \seealso{The main function to be called by the user is \code{\link{bugs}}.} \keyword{internal} +\keyword{IO} +\keyword{file} Modified: trunk/R2WinBUGS/man/bugs.log.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.log.Rd 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/man/bugs.log.Rd 2007-06-13 00:59:08 UTC (rev 30) @@ -10,7 +10,7 @@ \item{file}{Location of the WinBUGS logfile} } \value{ - A list with components + A list with components: \item{stats}{A matrix containing summary statistics for each saved parameter. Comparable to the information in the element \code{summary} of a bugs object as returned by \code{\link{bugs}}.} @@ -24,3 +24,4 @@ } \seealso{The main function that generates the log file is \code{\link{bugs}}.} \keyword{IO} +\keyword{file} Modified: trunk/R2WinBUGS/man/bugs.plot.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.plot.Rd 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/man/bugs.plot.Rd 2007-06-13 00:59:08 UTC (rev 30) @@ -2,23 +2,29 @@ \alias{bugs.plot.summary} \alias{bugs.plot.inferences} \title{Plotting summary information - intended for internal use only} -\description{Plotting summary information - intended for internal use only} +\description{Plotting summary information - intended for internal use} \usage{ bugs.plot.summary(sims, ...) bugs.plot.inferences(sims, display.parallel, ...) } \arguments{ - \item{sims}{an object of class `bugs', see \code{\link{bugs}} for details} - \item{display.parallel}{display parallel intervals in both halves of the summary plots; - this is a convergence-monitoring tool and is not necessary once you have approximate convergence - (default is \code{FALSE})} + \item{sims}{an object of class `bugs', see \code{\link{bugs}} for + details} + \item{display.parallel}{display parallel intervals in both halves of + the summary plots; this is a convergence-monitoring tool and is not + necessary once you have approximate convergence (default is + \code{FALSE})} \item{...}{further arguments to be passed to low-level plot functions} } + \details{ -\code{bugs.plot.summary} (left hand side of plot) and \code{bugs.plot.inferences} (right hand side of plot). + \code{bugs.plot.summary} (left hand side of plot) and + \code{bugs.plot.inferences} (right hand side of plot). } -\value{ -Does not return anything, but prints and plots as side-effects. -} -\seealso{The main function to be called by the user is \code{plot}, see \code{\link{plot.bugs}} for details.} + +\value{Does not return anything, but prints and plots as side-effects.} + +\seealso{The main function to be called by the user is \code{plot}, see + \code{\link{plot.bugs}} for details.} + \keyword{internal} Modified: trunk/R2WinBUGS/man/bugs.run.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.run.Rd 2007-06-12 10:10:26 UTC (rev 29) +++ trunk/R2WinBUGS/man/bugs.run.Rd 2007-06-13 00:59:08 UTC (rev 30) @@ -1,27 +1,31 @@ \name{bugs.run} \alias{bugs.run} -\title{Calling WinBUGS - intended for internal use only} -\description{ -Updates the lengths of the adaptive phases in the WinBUGS registry (using \code{\link{bugs.update.settings}}), -calls WinBUGS and runs it with \file{script.txt}. Intended for internal use only} +\title{Calling WinBUGS} + +\description{Updates the lengths of the adaptive phases in the WinBUGS + registry (using \code{\link{bugs.update.settings}}), calls WinBUGS and + runs it with \file{script.txt} - intended for internal use} + \usage{ -bugs.run(n.burnin, bugs.directory, WINE = "", - useWINE = .Platform$OS.type != "windows", +bugs.run(n.burnin, bugs.directory, WINE = "", + useWINE = .Platform$OS.type != "windows", newWINE = TRUE, WINEPATH = NULL) } \arguments{ \item{n.burnin}{length of bu... [truncated message content] |
From: <gg...@us...> - 2007-09-18 13:21:31
|
Revision: 67 http://bugs-r.svn.sourceforge.net/bugs-r/?rev=67&view=rev Author: ggorjan Date: 2007-09-18 06:21:09 -0700 (Tue, 18 Sep 2007) Log Message: ----------- move so that NEWS file will be also in installed package Added Paths: ----------- trunk/R2WinBUGS/inst/NEWS Removed Paths: ------------- trunk/R2WinBUGS/Changes Deleted: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes 2007-09-18 13:17:39 UTC (rev 66) +++ trunk/R2WinBUGS/Changes 2007-09-18 13:21:09 UTC (rev 67) @@ -1,237 +0,0 @@ -Changes to R2WinBUGS: -===================== - -Update 2.1-6 (23 August 2007): -- reverting back to use thin.updater since it is faster, however CODA files - have "wrong" indexes -- WINE arguments are now set in such a way that defaults work on Windows - and Linux -- minor formatting in the code and help files - -Update 2.1-5 (12 June 2007): -- proper indexing of CODA files also in R version -- fixed useWINE documentation in bugs help page -- note on supported BUGS versions -- as.bugs.array now returns info on used rule for pD i.e. var(deviance)/2 - or Dbar-Dhat -- some internal fixes related to handling of DIC -- merging and reversing ChangeLog files - -Update 2.1-4 (20 May 2007): -- Depending on coda now (Namespace issues) -- some more fixes for codetools checks -- Vignette has been updated - -Update 2.1-3 (13 May 2007): -- Ported to S-PLUS by Insightful Corp. -- some fixes for codetools checks - -Update 2.0-4 (01 November 2006): -- print.bugs / plot.bugs documentation fixes -- write.model() fix - -Update 2.0-3 (06 October 2006): -- \\. -> [.] in regular expressions - -Update 2.0-2 (26 July 2006): -- changes for DIC, making use of BUGS internal calculations -- some doc fixes - -Update 2.0-1 (26 May 2006): -- some wine patches for 2.2-0 by Gregor Gorjanc - -Update 2.0-0 (08 May 2006): -- bugs() doc fix/updates on scrambling -- bugs.run() has new arg useWINE (by Gregor Gorjanc) -- bugs() and bugs.script() patched for WINEPATH issue -- bugs.script() changed to save log file in ASCII -- new function bugs.log() by Gregor Gorjanc -- new functions as.bugs.array, openbugs and sort.name by - Jouni Kerman and Andrew Gelman -- new function write.model() based on ideas from Jouni Kerman - -Update 1.1-1 (17 Feb 2006): -- WINE tweaks (mainly by Gregor Gorjanc) - -Update 1.1-0 (14 Dec 2005): -- Contribution by Ben Bolker and Yun Yan's rbugs package: - make R2WinBUGS work under WINE - -Update 1.0-1 (14 Nov 2005): -- make inits=NULL work (again ?) - -Update 1.0-0 (05 Aug 2005): -- attach.all(), detach.all(), attach.bugs() and detach.bugs() added/changed - more or less according to Andrew Gelman's current bugs.R - -Update 0.2-9 (26 July 2005): -- bugs has new argument clearWD - -Update 0.2-8 (30 May 2005): -- bugs passed DIC to bugs.script in order to be able to disable it - -Update 0.2-6 (18 May 2005): -- bugs() changes in order to return a file names of coda output files -- new read.bugs() returns a coda mcmc.list object, if codaPkg=TRUE. - -Update 0.2-5 (20 Oct 2004): -- bugs() and bugs.script() have a new argument bin that allows to specify - a number of iterations. After each "bin" iterations the coda files are saved. - -Update 0.2-4 (05 Oct 2004): -- bugs.script() did not work for large n.iter values in update step - (no scientific notation allowed) - -Update 0.2-3 (10 Sept 2004): -- bugs.data.inits split to bugs.inits and bugs.data, the latter exported - from the Namespace. - Now we can use already written data files in bugs(). - -Update 0.2-2 (28 Apr 2004): -- schools data: original (see references) instead of the rounded data - -During the process of packaging R2WinBUGS_0.1 - R2WinBUGS_0.2-1, -quite a lot of changes had been made. Those changes are not -documented anywhere ... - -During the process of packaging R2WinBUGS_0.1, quite a lot of changes had -been made. Those changes are not documented anywhere ... - -Changes prior to R2WinBUGS_0.1: -=============================== - -Update 30 Oct 2003: - 1. Minor change to a return() statement to be compatible with R 1.8 - 2. Just a warning (from Ben Goodrich): if you are running Bugs.R inside - a loop, you have to be careful with the data() and the inits() - functions since they will not necessarily recognize locally-defined - variables. One workaround is to define the variables used in data() - and inits() using global assignments (<<-), but this can sometimes - make the program run slower. -Update 29 Aug 2003: - 1. Fixed "bugs.data.inits" function so you can use data that have the - same names as R functions. - 2. Changed T and F to TRUE and FALSE everywhere in case the variables - T and F are used as data in the main program - 3. Caution: if you are entering the data as a list of variable names - (see 10 Apr 2003 update, item 1), the data to be input into must - be global variables. This can be relevant if you are running bugs() - inside an R function. - 4. Caution: bugs() has difficulty processing ragged arrays. It is - better to save a whole matrix (e.g., "theta") rather than parts - (e.g., "theta[1:10,1]", "theta[1:5,2]"). If you want to save - part of a vector, you should do it as "theta[1:2]", not "theta[1]", - "theta[2]". -Update 30 Apr 2003: added time monitoring -Update 29 Apr 2003: - 1. The "attach.all" function (no longer called "attach2") overwrites - so that all components of a list or data frame get attached. - 2. Program now looks in the directory /winbug~1/ rather than /winbug~2/ - 3. Graphics parameters for margins are returned to their original state - at the end of the program. - 4. Added "digits.summary" option to the numerical display. - 5. Added "last.values" output: a list that can be input as "inits" - if you want to run the simulations longer. -Update 13 Apr 2003: fixed new bug in round.bugs(). Now all numbers are - saved in scientific notation. -Update 10 Apr 2003: - 1. It is now possible to enter the data as a list of variable names. - For example, before you had to enter data as, - data <- list (n=8, y=c(28,8,-3,7,-1,1,18,12)) - or - n <- 8 - y <- c(28,8,-3,7,-1,1,18,12) - data <- list (n=n, y=y) - Now you can enter the data as, - n <- 8 - y <- c(28,8,-3,7,-1,1,18,12) - data <- list ("n", "y") - The bugs() function will figure out which method you are using (based - on whether "data" is a list of numbers or a vector of character - strings). - This doesn't look like much, but it's convenient when you're entering - lots of data! - 2. It is now possible to enter the initial values as a function, - so as to automatically a random list of inits for each of the chains. - For example, in the 8-schools example below, we can do: - inits <- function() - list (theta=rnorm(J,0,1), mu.theta=rnorm(1,0,100), sigma.theta=runif(1,0,100)) - - to set up the inits as a function (rather than setting up n.chains - sets of specific initial values). Then, the function call, - schools.sim <- bugs (data, inits, parameters, "schools.txt", n.chains=3, n.iter=1000) - automatically sets up 3 sets of initial values (each a list of - theta, mu.theta, sigma.theta). - 3. Bug in the initial rounding (the function round.bugs()) has been fixed. - Thanks for Mark Clements for finding the bug and fixing it! - Also, we have set the default rounding to 5 digits instead of 2. -Update 01 Apr 2003: use layout() rather than split.screen() for graphical - display -Update 18 Mar 2003: - 1. Get the Bugs configuration information from the original file - (Registry_default.odc) rather than overwriting each time. (Fixes a - bug that occurred when R was interrupted in the middle of a Bugs run.) - 2. Display different colored dots in the right panel of the graphical - display, to show the medians from each chain. -Update 13 Mar 2003: fix minor bug in monitor() -Update 10 Mar 2003: fix bug in pD and DIC calculations -Update 7 Mar 2003: - 1. Fix display.parallel=T option by adding min.width so that very - intervals are still visible. - 2. Compute pD separately for each sequence (which gives much more - reasonable estimates before convergence). -Update 8 Feb 2003: minor fixes in graphical display -Update 6 Feb 2003: - 1. Approximate "effective sample size" n.eff given for each parameter. - 2. More explanatory material displayed. - 3. Use bringToTop() to automatically bring up the graphics window. -Update 4 Feb 2003: - 1. Automatically compute the deviance, DIC, and pD. Bugs will not - always compute DIC and pD, so we do so using the definition, - DIC = E(deviance) + pD, using var(deviance)/2 as an estimate of pD. - (This is derived from the chi^2 distribution. We can't use the - Spiegelhalter et al. definition of DIC because we don't have access - to the deviance function.) - 2. Set default for n.thin so that, after thinning, the total number - saved iterations, n.sims, is approximately 1000. -Update 14 Jan 2003 to run with the new WinBugs1.4. You may see an error - message and need to fix the dos.location assignment in bugs(). -Update 6 Jan 2003: - 1. Fix of bug that occurred with uppercase and lowercase variable names - 2. Set default for n.thin so that no more than about 500 iterations - will be saved from each sequence - 3. New option "display.parallel" added to show 80% inferences from - parallel sequences on the right panel of the graphical display. This - can be useful to understand what is going on when there are - convergence problems. -Update 26 Dec 2002: fix of minwidth in bugs.plot.summary -Update 11 Dec 2002: - 1. Automatic fixing of adaptive phases. Now you no longer need to run - for thousands of iterations when slice or Metropolis sampling is used. - 2. Various minor fixes -Update 10 Dec 2002: - 1. Cool graphical display of convergence and inferences! - 2. New "attach2" function that overwrites so that all components of - the list are attached -Update 29 Nov 2002: - 1. Fix of bug in 24 Nov update. - 2. Fix of bug in 16 Nov update. - 3. Length of chains is now pecified in terms of "n.iter" rather than - "n.keep". -Update 24 Nov 2002: improved treatment of "parameters.to.save". For - example, you can now use "alpha" to indicate an entire array of parameters, - whereas before you had to save "alpha[]" or "alpha[,]" or whatever. -Update 16 Nov 2002: mean, sd, median added to outputs -Update 4 Nov 2002: more error-flagging added -Update 26 Oct 2002: - 1. Parameters saved in order of the "parameters.to.save" vector - (not alphabetical order). - 2. Output saved in both matrix and list form. - 3. With the attach.sims=T setting (which is the default), the simulations - for all the saved parameters are saved as R objects. This is - convenient for later use of the simulations. -Updates to 16 Oct 2002: more error-flagging added, mean/sd added to summary, - fixing scientific notation so Bugs can always read data and inits -Update 21 Sept 2002: "quit=F" option changed to "debug=T" -First version written 18 Sept 2002 by Andrew Gelman, - adapted from the EmBedBugs package by Kenneth Rice Copied: trunk/R2WinBUGS/inst/NEWS (from rev 65, trunk/R2WinBUGS/Changes) =================================================================== --- trunk/R2WinBUGS/inst/NEWS (rev 0) +++ trunk/R2WinBUGS/inst/NEWS 2007-09-18 13:21:09 UTC (rev 67) @@ -0,0 +1,237 @@ +Changes to R2WinBUGS: +===================== + +Update 2.1-6 (23 August 2007): +- reverting back to use thin.updater since it is faster, however CODA files + have "wrong" indexes +- WINE arguments are now set in such a way that defaults work on Windows + and Linux +- minor formatting in the code and help files + +Update 2.1-5 (12 June 2007): +- proper indexing of CODA files also in R version +- fixed useWINE documentation in bugs help page +- note on supported BUGS versions +- as.bugs.array now returns info on used rule for pD i.e. var(deviance)/2 + or Dbar-Dhat +- some internal fixes related to handling of DIC +- merging and reversing ChangeLog files + +Update 2.1-4 (20 May 2007): +- Depending on coda now (Namespace issues) +- some more fixes for codetools checks +- Vignette has been updated + +Update 2.1-3 (13 May 2007): +- Ported to S-PLUS by Insightful Corp. +- some fixes for codetools checks + +Update 2.0-4 (01 November 2006): +- print.bugs / plot.bugs documentation fixes +- write.model() fix + +Update 2.0-3 (06 October 2006): +- \\. -> [.] in regular expressions + +Update 2.0-2 (26 July 2006): +- changes for DIC, making use of BUGS internal calculations +- some doc fixes + +Update 2.0-1 (26 May 2006): +- some wine patches for 2.2-0 by Gregor Gorjanc + +Update 2.0-0 (08 May 2006): +- bugs() doc fix/updates on scrambling +- bugs.run() has new arg useWINE (by Gregor Gorjanc) +- bugs() and bugs.script() patched for WINEPATH issue +- bugs.script() changed to save log file in ASCII +- new function bugs.log() by Gregor Gorjanc +- new functions as.bugs.array, openbugs and sort.name by + Jouni Kerman and Andrew Gelman +- new function write.model() based on ideas from Jouni Kerman + +Update 1.1-1 (17 Feb 2006): +- WINE tweaks (mainly by Gregor Gorjanc) + +Update 1.1-0 (14 Dec 2005): +- Contribution by Ben Bolker and Yun Yan's rbugs package: + make R2WinBUGS work under WINE + +Update 1.0-1 (14 Nov 2005): +- make inits=NULL work (again ?) + +Update 1.0-0 (05 Aug 2005): +- attach.all(), detach.all(), attach.bugs() and detach.bugs() added/changed + more or less according to Andrew Gelman's current bugs.R + +Update 0.2-9 (26 July 2005): +- bugs has new argument clearWD + +Update 0.2-8 (30 May 2005): +- bugs passed DIC to bugs.script in order to be able to disable it + +Update 0.2-6 (18 May 2005): +- bugs() changes in order to return a file names of coda output files +- new read.bugs() returns a coda mcmc.list object, if codaPkg=TRUE. + +Update 0.2-5 (20 Oct 2004): +- bugs() and bugs.script() have a new argument bin that allows to specify + a number of iterations. After each "bin" iterations the coda files are saved. + +Update 0.2-4 (05 Oct 2004): +- bugs.script() did not work for large n.iter values in update step + (no scientific notation allowed) + +Update 0.2-3 (10 Sept 2004): +- bugs.data.inits split to bugs.inits and bugs.data, the latter exported + from the Namespace. + Now we can use already written data files in bugs(). + +Update 0.2-2 (28 Apr 2004): +- schools data: original (see references) instead of the rounded data + +During the process of packaging R2WinBUGS_0.1 - R2WinBUGS_0.2-1, +quite a lot of changes had been made. Those changes are not +documented anywhere ... + +During the process of packaging R2WinBUGS_0.1, quite a lot of changes had +been made. Those changes are not documented anywhere ... + +Changes prior to R2WinBUGS_0.1: +=============================== + +Update 30 Oct 2003: + 1. Minor change to a return() statement to be compatible with R 1.8 + 2. Just a warning (from Ben Goodrich): if you are running Bugs.R inside + a loop, you have to be careful with the data() and the inits() + functions since they will not necessarily recognize locally-defined + variables. One workaround is to define the variables used in data() + and inits() using global assignments (<<-), but this can sometimes + make the program run slower. +Update 29 Aug 2003: + 1. Fixed "bugs.data.inits" function so you can use data that have the + same names as R functions. + 2. Changed T and F to TRUE and FALSE everywhere in case the variables + T and F are used as data in the main program + 3. Caution: if you are entering the data as a list of variable names + (see 10 Apr 2003 update, item 1), the data to be input into must + be global variables. This can be relevant if you are running bugs() + inside an R function. + 4. Caution: bugs() has difficulty processing ragged arrays. It is + better to save a whole matrix (e.g., "theta") rather than parts + (e.g., "theta[1:10,1]", "theta[1:5,2]"). If you want to save + part of a vector, you should do it as "theta[1:2]", not "theta[1]", + "theta[2]". +Update 30 Apr 2003: added time monitoring +Update 29 Apr 2003: + 1. The "attach.all" function (no longer called "attach2") overwrites + so that all components of a list or data frame get attached. + 2. Program now looks in the directory /winbug~1/ rather than /winbug~2/ + 3. Graphics parameters for margins are returned to their original state + at the end of the program. + 4. Added "digits.summary" option to the numerical display. + 5. Added "last.values" output: a list that can be input as "inits" + if you want to run the simulations longer. +Update 13 Apr 2003: fixed new bug in round.bugs(). Now all numbers are + saved in scientific notation. +Update 10 Apr 2003: + 1. It is now possible to enter the data as a list of variable names. + For example, before you had to enter data as, + data <- list (n=8, y=c(28,8,-3,7,-1,1,18,12)) + or + n <- 8 + y <- c(28,8,-3,7,-1,1,18,12) + data <- list (n=n, y=y) + Now you can enter the data as, + n <- 8 + y <- c(28,8,-3,7,-1,1,18,12) + data <- list ("n", "y") + The bugs() function will figure out which method you are using (based + on whether "data" is a list of numbers or a vector of character + strings). + This doesn't look like much, but it's convenient when you're entering + lots of data! + 2. It is now possible to enter the initial values as a function, + so as to automatically a random list of inits for each of the chains. + For example, in the 8-schools example below, we can do: + inits <- function() + list (theta=rnorm(J,0,1), mu.theta=rnorm(1,0,100), sigma.theta=runif(1,0,100)) + + to set up the inits as a function (rather than setting up n.chains + sets of specific initial values). Then, the function call, + schools.sim <- bugs (data, inits, parameters, "schools.txt", n.chains=3, n.iter=1000) + automatically sets up 3 sets of initial values (each a list of + theta, mu.theta, sigma.theta). + 3. Bug in the initial rounding (the function round.bugs()) has been fixed. + Thanks for Mark Clements for finding the bug and fixing it! + Also, we have set the default rounding to 5 digits instead of 2. +Update 01 Apr 2003: use layout() rather than split.screen() for graphical + display +Update 18 Mar 2003: + 1. Get the Bugs configuration information from the original file + (Registry_default.odc) rather than overwriting each time. (Fixes a + bug that occurred when R was interrupted in the middle of a Bugs run.) + 2. Display different colored dots in the right panel of the graphical + display, to show the medians from each chain. +Update 13 Mar 2003: fix minor bug in monitor() +Update 10 Mar 2003: fix bug in pD and DIC calculations +Update 7 Mar 2003: + 1. Fix display.parallel=T option by adding min.width so that very + intervals are still visible. + 2. Compute pD separately for each sequence (which gives much more + reasonable estimates before convergence). +Update 8 Feb 2003: minor fixes in graphical display +Update 6 Feb 2003: + 1. Approximate "effective sample size" n.eff given for each parameter. + 2. More explanatory material displayed. + 3. Use bringToTop() to automatically bring up the graphics window. +Update 4 Feb 2003: + 1. Automatically compute the deviance, DIC, and pD. Bugs will not + always compute DIC and pD, so we do so using the definition, + DIC = E(deviance) + pD, using var(deviance)/2 as an estimate of pD. + (This is derived from the chi^2 distribution. We can't use the + Spiegelhalter et al. definition of DIC because we don't have access + to the deviance function.) + 2. Set default for n.thin so that, after thinning, the total number + saved iterations, n.sims, is approximately 1000. +Update 14 Jan 2003 to run with the new WinBugs1.4. You may see an error + message and need to fix the dos.location assignment in bugs(). +Update 6 Jan 2003: + 1. Fix of bug that occurred with uppercase and lowercase variable names + 2. Set default for n.thin so that no more than about 500 iterations + will be saved from each sequence + 3. New option "display.parallel" added to show 80% inferences from + parallel sequences on the right panel of the graphical display. This + can be useful to understand what is going on when there are + convergence problems. +Update 26 Dec 2002: fix of minwidth in bugs.plot.summary +Update 11 Dec 2002: + 1. Automatic fixing of adaptive phases. Now you no longer need to run + for thousands of iterations when slice or Metropolis sampling is used. + 2. Various minor fixes +Update 10 Dec 2002: + 1. Cool graphical display of convergence and inferences! + 2. New "attach2" function that overwrites so that all components of + the list are attached +Update 29 Nov 2002: + 1. Fix of bug in 24 Nov update. + 2. Fix of bug in 16 Nov update. + 3. Length of chains is now pecified in terms of "n.iter" rather than + "n.keep". +Update 24 Nov 2002: improved treatment of "parameters.to.save". For + example, you can now use "alpha" to indicate an entire array of parameters, + whereas before you had to save "alpha[]" or "alpha[,]" or whatever. +Update 16 Nov 2002: mean, sd, median added to outputs +Update 4 Nov 2002: more error-flagging added +Update 26 Oct 2002: + 1. Parameters saved in order of the "parameters.to.save" vector + (not alphabetical order). + 2. Output saved in both matrix and list form. + 3. With the attach.sims=T setting (which is the default), the simulations + for all the saved parameters are saved as R objects. This is + convenient for later use of the simulations. +Updates to 16 Oct 2002: more error-flagging added, mean/sd added to summary, + fixing scientific notation so Bugs can always read data and inits +Update 21 Sept 2002: "quit=F" option changed to "debug=T" +First version written 18 Sept 2002 by Andrew Gelman, + adapted from the EmBedBugs package by Kenneth Rice This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <li...@us...> - 2008-02-01 09:42:53
|
Revision: 85 http://bugs-r.svn.sourceforge.net/bugs-r/?rev=85&view=rev Author: ligges Date: 2008-02-01 01:42:28 -0800 (Fri, 01 Feb 2008) Log Message: ----------- bugfix if DIC is not available ... Modified Paths: -------------- trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/R/bugs.log.R Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2008-01-22 12:27:56 UTC (rev 84) +++ trunk/R2WinBUGS/DESCRIPTION 2008-02-01 09:42:28 UTC (rev 85) @@ -1,7 +1,7 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS -Date: 2008-01-18 -Version: 2.1-7 +Date: 2008-02-01 +Version: 2.1-8 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> and Uwe Ligges <li...@st...>. Modified: trunk/R2WinBUGS/R/bugs.log.R =================================================================== --- trunk/R2WinBUGS/R/bugs.log.R 2008-01-22 12:27:56 UTC (rev 84) +++ trunk/R2WinBUGS/R/bugs.log.R 2008-02-01 09:42:28 UTC (rev 85) @@ -11,10 +11,11 @@ log.txt <- readLines(file) extract <- function (m, line.match, skip=0, empty.left.col=TRUE) { start <- (skip + which(m == line.match)[1]) - if(is.na(start)) return(NULL) - if(length(start) < 1) return(NULL) + if(is.na(start)) return(NA) + if(length(start) < 1) return(NA) mx <- strsplit(m[-(1:start)], "\t") n.cols <- length(mx[[1]]) + if(n.cols < 1) return(NA) mxlen <- sapply(mx, length) end <- which(mxlen!=n.cols)[1] - 1 mx <- mx[1:end] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <li...@us...> - 2007-05-13 11:44:00
|
Revision: 16 http://svn.sourceforge.net/bugs-r/?rev=16&view=rev Author: ligges Date: 2007-05-13 04:43:49 -0700 (Sun, 13 May 2007) Log Message: ----------- prepare for release Modified Paths: -------------- trunk/R2WinBUGS/Changes trunk/R2WinBUGS/DESCRIPTION Modified: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes 2007-05-08 22:06:05 UTC (rev 15) +++ trunk/R2WinBUGS/Changes 2007-05-13 11:43:49 UTC (rev 16) @@ -70,5 +70,5 @@ - print.bugs / plot.bugs documentation fixes - write.model() fix -Update 2.1-1 (07 April 2007): +Update 2.1-2 (12 May 2007): - Ported to S-PLUS by Insightful Corp. Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2007-05-08 22:06:05 UTC (rev 15) +++ trunk/R2WinBUGS/DESCRIPTION 2007-05-13 11:43:49 UTC (rev 16) @@ -1,7 +1,7 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS -Date: 2007-04-07 -Version: 2.1-1 +Date: 2007-05-12 +Version: 2.1-2 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> and Uwe Ligges <li...@st...>. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <li...@us...> - 2007-05-13 16:49:33
|
Revision: 17 http://svn.sourceforge.net/bugs-r/?rev=17&view=rev Author: ligges Date: 2007-05-13 09:49:34 -0700 (Sun, 13 May 2007) Log Message: ----------- The codetools checks revealed quite a lot of bugs and inconsistencies. Unfortunately we cannot make codetools calm since codetools do not expect S-PLUS. Again, prepared for release. Modified Paths: -------------- trunk/R2WinBUGS/Changes trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/R/attach.all.R trunk/R2WinBUGS/R/bugs.R trunk/R2WinBUGS/R/bugs.data.R trunk/R2WinBUGS/R/bugs.run.R trunk/R2WinBUGS/R/bugs.script.R trunk/R2WinBUGS/R/bugs.update.settings.R trunk/R2WinBUGS/R/openbugs.R trunk/R2WinBUGS/R/read.bugs.R trunk/R2WinBUGS/R/wineutils.R trunk/R2WinBUGS/R/write.model.R trunk/R2WinBUGS/man/bugs.run.Rd Modified: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/Changes 2007-05-13 16:49:34 UTC (rev 17) @@ -70,5 +70,7 @@ - print.bugs / plot.bugs documentation fixes - write.model() fix -Update 2.1-2 (12 May 2007): +Update 2.1-3 (13 May 2007): - Ported to S-PLUS by Insightful Corp. +- some fixes for codetools checks + Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/DESCRIPTION 2007-05-13 16:49:34 UTC (rev 17) @@ -1,7 +1,7 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS -Date: 2007-05-12 -Version: 2.1-2 +Date: 2007-05-13 +Version: 2.1-3 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> and Uwe Ligges <li...@st...>. Modified: trunk/R2WinBUGS/R/attach.all.R =================================================================== --- trunk/R2WinBUGS/R/attach.all.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/attach.all.R 2007-05-13 16:49:34 UTC (rev 17) @@ -29,10 +29,7 @@ detach("bugs.sims")} x$sims.list$n.sims <- x$n.sims # put n.sims into sims.list for convenience r2 <- attach.all(x$sims.list, overwrite = overwrite, name = "bugs.sims") - if (is.R()) - invisible (bugs.sims = r2) - else - invisible (r2) + invisible (r2) } detach.all <- function(name = "attach.all"){ Modified: trunk/R2WinBUGS/R/bugs.R =================================================================== --- trunk/R2WinBUGS/R/bugs.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/bugs.R 2007-05-13 16:49:34 UTC (rev 17) @@ -60,7 +60,7 @@ bugs.script(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, new.model.file, debug=debug, is.inits=!is.null(inits), bin = bin, DIC = DIC, useWINE = useWINE, newWINE = newWINE, WINEPATH = WINEPATH) - bugs.run(n.burnin, bugs.directory, WINE = WINE, useWINE = useWINE) + bugs.run(n.burnin, bugs.directory, WINE = WINE, useWINE = useWINE, WINEPATH = WINEPATH) if(codaPkg) return(file.path(getwd(), paste("coda", 1:n.chains, ".txt", sep=""))) Modified: trunk/R2WinBUGS/R/bugs.data.R =================================================================== --- trunk/R2WinBUGS/R/bugs.data.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/bugs.data.R 2007-05-13 16:49:34 UTC (rev 17) @@ -1,29 +1,28 @@ "bugs.data" <- function(data, dir = getwd(), digits = 5){ if(is.numeric(unlist(data))) - if(is.R()) { - write.datafile(lapply(data, formatC, digits = digits, format = "E"), - file.path(dir, "data.txt")) - } - else { - writeDatafileS4(data, towhere = "data.txt") - } + if(is.R()) { + write.datafile(lapply(data, formatC, digits = digits, format = "E"), + file.path(dir, "data.txt")) + } + else { + writeDatafileS4(data, towhere = "data.txt") + } else { - if(is.R()) { - data.list <- lapply(as.list(data), get, pos = parent.frame(2)) - names(data.list) <- as.list(data) - write.datafile(lapply(data.list, formatC, digits = digits, format = "E"), - file.path(dir, "data.txt")) - } - else { - data.list <- lapply(as.list(data), get, where = parent.frame(2)) - names(data.list) <- unlist(data) - writeDatafileS4(data.list, towhere = "data.txt") - } - } + if(is.R()) { + data.list <- lapply(as.list(data), get, pos = parent.frame(2)) + names(data.list) <- as.list(data) + write.datafile(lapply(data.list, formatC, digits = digits, format = "E"), + file.path(dir, "data.txt")) + } + else { + data.list <- lapply(as.list(data), get, where = parent.frame(2)) + names(data.list) <- unlist(data) + writeDatafileS4(data.list, towhere = "data.txt") + } + } } -if (!is.R()) { "writeDatafileS4" <- # @@ -50,128 +49,128 @@ # Revision history: 2002-11-19. Fixed to handle missing values properly. function(DATA, towhere = "clipboard", fill = TRUE) { - formatDataS4 = - # - # Prepared DATA for input to WinBUGS. - function(DATA) - { - if(!is.list(DATA)) - stop("DATA must be a named list or data frame.") - dlnames <- names(DATA) - if(is.data.frame(DATA)) - DATA <- as.list(DATA) - # - # Checking for lists in DATA.... - lind <- sapply(DATA, is.list) - # Checking for data frames in DATA.... - dfind <- sapply(DATA, is.data.frame) - # Any lists that are not data frames?... - if(any(lind & !dfind)) stop("DATA may not contain lists.") - # Checking for unnamed elements of list that are not data frames.... - if(any(dlnames[!dfind] == "")) stop( - "When DATA is a list, all its elements that are not data frames must be named." - ) - # Checking for duplicate names.... - dupnames <- unique(dlnames[duplicated(dlnames)]) - if(length(dupnames) > 0) - stop(paste( - "The following names are used more than once in DATA:", - paste(dupnames, collapse = ", "))) - if(any(dfind)) { - dataold <- DATA - DATA <- vector("list", 0) - for(i in seq(along = dataold)) { - if(dfind[i]) - DATA <- c(DATA, as.list(dataold[[i]])) - else DATA <- c(DATA, dataold[i]) - } - dataold <- NULL - } - dlnames <- names(DATA) - dupnames <- unique(dlnames[duplicated(dlnames)]) - # Checking for duplicated names again (now that columns of data frames are included).... - if(length(dupnames) > 0) stop(paste( - "The following names are used more than once in DATA (at least once within a data frame):", - paste(dupnames, collapse = ", "))) - # Checking for factors.... - factorind <- sapply(DATA, is.factor) - if(any(factorind)) - stop(paste( - "DATA may not include factors. One or more factor variables were detected:", - paste(dlnames[factorind], collapse = ", "))) - # Checking for character vectors.... - charind <- sapply(DATA, is.character) - if(any(charind)) - stop(paste( - "WinBUGS does not handle character data. One or more character variables were detected:", - paste(dlnames[charind], collapse = ", "))) - # Checking for complex vectors.... - complexind <- sapply(DATA, is.complex) - if(any(complexind)) - stop(paste( - "WinBUGS does not handle complex data. One or more complex variables were detected:", - paste(dlnames[complexind], collapse = ", "))) - # Checking for values farther from zero than 1E+38 (which is limit of single precision).... - toobigind <- sapply(DATA, function(x) - { - y <- abs(x[!is.na(x)]) - any(y[y > 0] > 9.9999999999999998e+37) - } - ) - if(any(toobigind)) - stop(paste( - "WinBUGS works in single precision. The following variables contain data outside the range +/-1.0E+38: ", - paste(dlnames[toobigind], collapse = ", "), - ".\n", sep = "")) - # Checking for values in range +/-1.0E-38 (which is limit of single precision).... - toosmallind <- sapply(DATA, function(x) - { - y <- abs(x[!is.na(x)]) - any(y[y > 0] < 9.9999999999999996e-39) - } - ) - n <- length(dlnames) - data.string <- as.list(rep(NA, n)) - for(i in 1:n) { - if(length(DATA[[i]]) == 1) { - ac <- toSingleS4(DATA[[i]]) - data.string[[i]] <- paste(names(DATA)[i], "=", - ac, sep = "") - next - } - if(is.vector(DATA[[i]]) & length(DATA[[i]]) > 1) { - ac <- toSingleS4(DATA[[i]]) - data.string[[i]] <- paste(names(DATA)[i], "=c(", - paste(ac, collapse = ", "), ")", sep = - "") - next - } - if(is.array(DATA[[i]])) { - ac <- toSingleS4(aperm(DATA[[i]])) - data.string[[i]] <- paste(names(DATA)[i], - "= structure(.Data= c(", paste(ac, - collapse = ", "), "), \n .Dim=c(", - paste(as.character(dim(DATA[[i]])), - collapse = ", "), "))", sep = "") - } - } - data.tofile <- paste("list(", paste(unlist(data.string), - collapse = ", "), ")", sep = "") - if(any(toosmallind)) - warning(paste( - "WinBUGS works in single precision. The following variables contained nonzero data", - "\ninside the range +/-1.0E-38 that were set to zero: ", - paste(dlnames[toosmallind], collapse = ", "), - ".\n", sep = "")) - return(data.tofile) - } - rslt <- formatDataS4(DATA) - cat(rslt, file = towhere, fill = fill) - invisible(0) + formatDataS4 = + # + # Prepared DATA for input to WinBUGS. + function(DATA) + { + if(!is.list(DATA)) + stop("DATA must be a named list or data frame.") + dlnames <- names(DATA) + if(is.data.frame(DATA)) + DATA <- as.list(DATA) + # + # Checking for lists in DATA.... + lind <- sapply(DATA, is.list) + # Checking for data frames in DATA.... + dfind <- sapply(DATA, is.data.frame) + # Any lists that are not data frames?... + if(any(lind & !dfind)) stop("DATA may not contain lists.") + # Checking for unnamed elements of list that are not data frames.... + if(any(dlnames[!dfind] == "")) stop( + "When DATA is a list, all its elements that are not data frames must be named." + ) + # Checking for duplicate names.... + dupnames <- unique(dlnames[duplicated(dlnames)]) + if(length(dupnames) > 0) + stop(paste( + "The following names are used more than once in DATA:", + paste(dupnames, collapse = ", "))) + if(any(dfind)) { + dataold <- DATA + DATA <- vector("list", 0) + for(i in seq(along = dataold)) { + if(dfind[i]) + DATA <- c(DATA, as.list(dataold[[i]])) + else DATA <- c(DATA, dataold[i]) + } + dataold <- NULL + } + dlnames <- names(DATA) + dupnames <- unique(dlnames[duplicated(dlnames)]) + # Checking for duplicated names again (now that columns of data frames are included).... + if(length(dupnames) > 0) stop(paste( + "The following names are used more than once in DATA (at least once within a data frame):", + paste(dupnames, collapse = ", "))) + # Checking for factors.... + factorind <- sapply(DATA, is.factor) + if(any(factorind)) + stop(paste( + "DATA may not include factors. One or more factor variables were detected:", + paste(dlnames[factorind], collapse = ", "))) + # Checking for character vectors.... + charind <- sapply(DATA, is.character) + if(any(charind)) + stop(paste( + "WinBUGS does not handle character data. One or more character variables were detected:", + paste(dlnames[charind], collapse = ", "))) + # Checking for complex vectors.... + complexind <- sapply(DATA, is.complex) + if(any(complexind)) + stop(paste( + "WinBUGS does not handle complex data. One or more complex variables were detected:", + paste(dlnames[complexind], collapse = ", "))) + # Checking for values farther from zero than 1E+38 (which is limit of single precision).... + toobigind <- sapply(DATA, function(x) + { + y <- abs(x[!is.na(x)]) + any(y[y > 0] > 9.9999999999999998e+37) + } + ) + if(any(toobigind)) + stop(paste( + "WinBUGS works in single precision. The following variables contain data outside the range +/-1.0E+38: ", + paste(dlnames[toobigind], collapse = ", "), + ".\n", sep = "")) + # Checking for values in range +/-1.0E-38 (which is limit of single precision).... + toosmallind <- sapply(DATA, function(x) + { + y <- abs(x[!is.na(x)]) + any(y[y > 0] < 9.9999999999999996e-39) + } + ) + n <- length(dlnames) + data.string <- as.list(rep(NA, n)) + for(i in 1:n) { + if(length(DATA[[i]]) == 1) { + ac <- toSingleS4(DATA[[i]]) + data.string[[i]] <- paste(names(DATA)[i], "=", + ac, sep = "") + next + } + if(is.vector(DATA[[i]]) & length(DATA[[i]]) > 1) { + ac <- toSingleS4(DATA[[i]]) + data.string[[i]] <- paste(names(DATA)[i], "=c(", + paste(ac, collapse = ", "), ")", sep = + "") + next + } + if(is.array(DATA[[i]])) { + ac <- toSingleS4(aperm(DATA[[i]])) + data.string[[i]] <- paste(names(DATA)[i], + "= structure(.Data= c(", paste(ac, + collapse = ", "), "), \n .Dim=c(", + paste(as.character(dim(DATA[[i]])), + collapse = ", "), "))", sep = "") + } + } + data.tofile <- paste("list(", paste(unlist(data.string), + collapse = ", "), ")", sep = "") + if(any(toosmallind)) + warning(paste( + "WinBUGS works in single precision. The following variables contained nonzero data", + "\ninside the range +/-1.0E-38 that were set to zero: ", + paste(dlnames[toosmallind], collapse = ", "), + ".\n", sep = "")) + return(data.tofile) + } + rslt <- formatDataS4(DATA) + cat(rslt, file = towhere, fill = fill) + invisible(0) } -toSingleS4 = +toSingleS4 <- # # Takes numeric vector and removes digit of exponent in scientific notation (if any) # @@ -180,50 +179,48 @@ # Revision history: 2002-11-19. Fixed to handle missing values properly. function(x) { - xdim <- dim(x) - x <- as.character(as.single(x)) + xdim <- dim(x) + x <- as.character(as.single(x)) - # First to look for positives: - pplus <- regMatchPos(x, "e\\+0") - pplusind <- apply(pplus, 1, function(y) - (!any(is.na(y)))) - if(any(pplusind)) { - # Making sure that periods are in mantissa... - init <- substring(x[pplusind], 1, pplus[ - pplusind, 1] - 1) - #...preceeding exponent - pper <- regMatchPos(init, "\\.") - pperind <- apply(pper, 1, function(y) - (all(is.na(y)))) - if(any(pperind)) - init[pperind] <- paste(init[pperind], - ".0", sep = "") - # Changing the format of the exponent... - x[pplusind] <- paste(init, "E+", substring( - x[pplusind], pplus[pplusind, 2] + 1), - sep = "") - } - # Then to look for negatives: - pminus <- regMatchPos(x, "e\\-0") - pminusind <- apply(pminus, 1, function(y) - (!any(is.na(y)))) - if(any(pminusind)) { - # Making sure that periods are in mantissa... - init <- substring(x[pminusind], 1, pminus[ - pminusind, 1] - 1) - #...preceeding exponent - pper <- regMatchPos(init, "\\.") - pperind <- apply(pper, 1, function(y) - (all(is.na(y)))) - if(any(pperind)) - init[pperind] <- paste(init[pperind], - ".0", sep = "") - # Changing the format of the exponent... - x[pminusind] <- paste(init, "E-", substring( - x[pminusind], pminus[pminusind, 2] + - 1), sep = "") - } - x + # First to look for positives: + pplus <- regMatchPos(x, "e\\+0") + pplusind <- apply(pplus, 1, function(y) + (!any(is.na(y)))) + if(any(pplusind)) { + # Making sure that periods are in mantissa... + init <- substring(x[pplusind], 1, pplus[ + pplusind, 1] - 1) + #...preceeding exponent + pper <- regMatchPos(init, "\\.") + pperind <- apply(pper, 1, function(y) + (all(is.na(y)))) + if(any(pperind)) + init[pperind] <- paste(init[pperind], + ".0", sep = "") + # Changing the format of the exponent... + x[pplusind] <- paste(init, "E+", substring( + x[pplusind], pplus[pplusind, 2] + 1), + sep = "") + } + # Then to look for negatives: + pminus <- regMatchPos(x, "e\\-0") + pminusind <- apply(pminus, 1, function(y) + (!any(is.na(y)))) + if(any(pminusind)) { + # Making sure that periods are in mantissa... + init <- substring(x[pminusind], 1, pminus[ + pminusind, 1] - 1) + #...preceeding exponent + pper <- regMatchPos(init, "\\.") + pperind <- apply(pper, 1, function(y) + (all(is.na(y)))) + if(any(pperind)) + init[pperind] <- paste(init[pperind], + ".0", sep = "") + # Changing the format of the exponent... + x[pminusind] <- paste(init, "E-", substring( + x[pminusind], pminus[pminusind, 2] + + 1), sep = "") + } + x } - -} # ends if (!is.R()) Modified: trunk/R2WinBUGS/R/bugs.run.R =================================================================== --- trunk/R2WinBUGS/R/bugs.run.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/bugs.run.R 2007-05-13 16:49:34 UTC (rev 17) @@ -1,6 +1,7 @@ "bugs.run" <- function(n.burnin, bugs.directory, WINE = "", - useWINE = .Platform$OS.type != "windows", newWINE = TRUE){ + useWINE = .Platform$OS.type != "windows", + newWINE = TRUE, WINEPATH = NULL){ if(useWINE && !is.R()) stop ("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") @@ -26,7 +27,7 @@ stop(paste("WinBUGS executable does not exist in", bugs.directory)) ## Call Bugs and have it run with script.txt bugsCall <- paste("\"", dos.location, "\" /par \"", - native2win(file.path(getwd(), "script.txt"), newWINE = newWINE), + native2win(file.path(getwd(), "script.txt"), newWINE = newWINE, WINEPATH = WINEPATH), "\"", sep = "") if (useWINE) bugsCall <- paste(WINE, bugsCall) Modified: trunk/R2WinBUGS/R/bugs.script.R =================================================================== --- trunk/R2WinBUGS/R/bugs.script.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/bugs.script.R 2007-05-13 16:49:34 UTC (rev 17) @@ -17,7 +17,8 @@ coda <- file.path(working.directory, "coda") logfile <- file.path(working.directory, "log.odc") logfileTxt <- file.path(working.directory, "log.txt") - inits <- sapply(paste(working.directory, "/inits", 1:n.chains, ".txt", sep=""), native2win) + inits <- sapply(paste(working.directory, "/inits", 1:n.chains, ".txt", sep=""), + function(x) native2win(x, , WINEPATH = WINEPATH)) initlist <- paste("inits (", 1:n.chains, ", '", inits, "')\n", sep="") savelist <- paste("set (", parameters.to.save, ")\n", sep="") redo <- ceiling((n.iter-n.burnin)/(n.thin*bin)) @@ -34,15 +35,15 @@ ## Therefore, if the samples are read into S-PLUS using the coda package, ## the thinning will be correctly labelled in the resulting mcmc object. ## In R, the thinning is always labelled as 1, even if thinning was done. - thinUpdateCommand <- paste("update (", n.burnin, ")\n", - "thin.samples (", n.thin, ")\n", sep = "") - bin = bin * n.thin + thinUpdateCommand <- paste("update (", n.burnin, ")\n", + "thin.samples (", n.thin, ")\n", sep = "") + bin = bin * n.thin } cat( "display ('log')\n", - "check ('", native2win(model), "')\n", - "data ('", native2win(data), "')\n", + "check ('", native2win(model, WINEPATH=WINEPATH), "')\n", + "data ('", native2win(data, WINEPATH=WINEPATH), "')\n", "compile (", n.chains, ")\n", if(is.inits) initlist, "gen.inits()\n", @@ -51,12 +52,12 @@ if(DIC) "dic.set()\n", rep( c("update (", formatC(ceiling(bin), format = "d"), ")\n", - "coda (*, '", native2win(coda), "')\n"),redo), + "coda (*, '", native2win(coda, WINEPATH=WINEPATH), "')\n"),redo), "stats (*)\n", if(DIC) "dic.stats()\n", - "history (*, '", native2win(history), "')\n", - "save ('", native2win(logfile), "')\n", - "save ('", native2win(logfileTxt), "')\n", + "history (*, '", native2win(history, WINEPATH=WINEPATH), "')\n", + "save ('", native2win(logfile, WINEPATH=WINEPATH), "')\n", + "save ('", native2win(logfileTxt, WINEPATH=WINEPATH), "')\n", file=script, sep="", append=FALSE) if (!debug) cat ("quit ()\n", file=script, append=TRUE) sims.files <- paste ("coda", 1:n.chains, ".txt", sep="") Modified: trunk/R2WinBUGS/R/bugs.update.settings.R =================================================================== --- trunk/R2WinBUGS/R/bugs.update.settings.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/bugs.update.settings.R 2007-05-13 16:49:34 UTC (rev 17) @@ -1,13 +1,13 @@ "bugs.update.settings" <- function (n.burnin, bugs.directory){ - + char.burnin <- as.character(n.burnin - 1) - if (is.R()){ - file.copy(file.path(bugs.directory, "System/Rsrc/Registry.odc"), + if (is.R()){ + file.copy(file.path(bugs.directory, "System/Rsrc/Registry.odc"), file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), overwrite = TRUE) } else { - splus.file.copy(file.path(bugs.directory, "System/Rsrc/Registry.odc"), + splus.file.copy(file.path(bugs.directory, "System/Rsrc/Registry.odc"), file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), overwrite = TRUE) } @@ -15,19 +15,19 @@ "character", 400, size = 1, endian = "little") locale <- Sys.getlocale("LC_CTYPE") Sys.setlocale("LC_CTYPE", "C") - if (is.R()) - info <- registry[regexpr("Int", registry, fixed = TRUE, useBytes = TRUE) > 0] - else - info <- registry[regexpr("Int", registry, fixed = TRUE) > 0] + if (is.R()) + info <- registry[regexpr("Int", registry, fixed = TRUE, useBytes = TRUE) > 0] + else + info <- registry[regexpr("Int", registry, fixed = TRUE) > 0] while(regexpr("\r", info) > 0){ newline <- regexpr("\r", info) info <- substring(info, newline + 1) line <- substring(info, 1, regexpr("\r", info) - 1) if(regexpr("AdaptivePhase", line) > 0){ if (is.R()) - numpos <- regexpr("Int", line, fixed = TRUE, useBytes = TRUE) + 4 + numpos <- regexpr("Int", line, fixed = TRUE, useBytes = TRUE) + 4 else - numpos <- regexpr("Int", line, fixed = TRUE) + 4 + numpos <- regexpr("Int", line, fixed = TRUE) + 4 num <- substring(line, numpos) if (as.numeric(num) > n.burnin){ @@ -43,8 +43,6 @@ file.path(bugs.directory, "System/Rsrc/Registry.odc"), endian = "little") } -if (!is.R()){ - "splus.file.copy"<- function(from, to, overwrite = FALSE) { @@ -57,5 +55,3 @@ size = 1) invisible(z) } - -} #ends if (!is.R()) Modified: trunk/R2WinBUGS/R/openbugs.R =================================================================== --- trunk/R2WinBUGS/R/openbugs.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/openbugs.R 2007-05-13 16:49:34 UTC (rev 17) @@ -33,43 +33,43 @@ model <- readLines(modelFile) try(writeLines(model, modelFile)) } - modelCheck(modelFile) + BRugs::modelCheck(modelFile) if(!(is.vector(data) && is.character(data) && all(file.exists(data)))) { - data <- bugsData(data, digits = digits) + data <- BRugs::bugsData(data, digits = digits) } - modelData(data) - modelCompile(numChains) + BRugs::modelData(data) + BRugs::modelCompile(numChains) if(missing(inits) || is.null(inits)) { - modelGenInits() + BRugs::modelGenInits() } else { if(is.list(inits) || is.function(inits) || (is.character(inits) && !any(file.exists(inits)))){ - inits <- bugsInits(inits = inits, numChains = numChains, digits = digits) + inits <- BRugs::bugsInits(inits = inits, numChains = numChains, digits = digits) } - modelInits(inits) - modelGenInits() + BRugs::modelInits(inits) + BRugs::modelGenInits() } - samplesSetThin(nThin) + BRugs::samplesSetThin(nThin) # set the adaptive phases adaptivelines <- scan(system.file("OpenBUGS", "Bugs", "Rsrc", "Registry.txt", package="BRugs"), what="character") factories <- sub(".adaptivePhase", "", adaptivelines[grep("adaptivePhase",adaptivelines)]) - sapply(factories, modelSetAP, max(0, nBurnin-1)) + sapply(factories, BRugs::modelSetAP, max(0, nBurnin-1)) - modelUpdate(nBurnin) + BRugs::modelUpdate(nBurnin) if(!is.null(DIC)) { - dicSet() - on.exit(dicClear(), add = TRUE) + BRugs::dicSet() + on.exit(BRugs::dicClear(), add = TRUE) } - samplesSet(parametersToSave) - modelUpdate(nIter) - params <- sort.name(samplesMonitors("*"), parametersToSave) - samples <- sapply(params, samplesSample) + BRugs::samplesSet(parametersToSave) + BRugs::modelUpdate(nIter) + params <- sort.name(BRugs::samplesMonitors("*"), parametersToSave) + samples <- sapply(params, BRugs::samplesSample) n.saved.per.chain <- nrow(samples)/numChains samples.array <- array(samples, c(n.saved.per.chain, numChains, ncol(samples))) dimnames(samples.array)[[3]] <- dimnames(samples)[[2]] - if(!is.null(DIC)) DIC <- dicStats() + if(!is.null(DIC)) DIC <- BRugs::dicStats() bugs.output <- as.bugs.array(samples.array, modelFile, program="OpenBUGS", n.iter=n.iter, n.burnin=n.burnin, n.thin=n.thin, DIC=DIC) return(bugs.output) Modified: trunk/R2WinBUGS/R/read.bugs.R =================================================================== --- trunk/R2WinBUGS/R/read.bugs.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/read.bugs.R 2007-05-13 16:49:34 UTC (rev 17) @@ -1,7 +1,7 @@ read.bugs <- function(codafiles, ...){ if(!require(coda)) stop("package 'coda' is required to use this function") - mcmc.list(lapply(codafiles, read.coda, + coda::mcmc.list(lapply(codafiles, coda::read.coda, index.file = file.path(dirname(codafiles[1]), "codaIndex.txt"), ...)) } Modified: trunk/R2WinBUGS/R/wineutils.R =================================================================== --- trunk/R2WinBUGS/R/wineutils.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/wineutils.R 2007-05-13 16:49:34 UTC (rev 17) @@ -52,7 +52,7 @@ } # end if (is.R()) -native2win <- function(x, useWINE=.Platform$OS.type != "windows", newWINE=TRUE) { # native -> win +native2win <- function(x, useWINE=.Platform$OS.type != "windows", newWINE=TRUE, WINEPATH=NULL) { # native -> win if(is.R()){ if (useWINE && !newWINE) return(winedriveRTr(x)) if (useWINE && newWINE) { Modified: trunk/R2WinBUGS/R/write.model.R =================================================================== --- trunk/R2WinBUGS/R/write.model.R 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/R/write.model.R 2007-05-13 16:49:34 UTC (rev 17) @@ -21,8 +21,6 @@ writeLines(model.text, con = con) } -if (!is.R()){ - replaceScientificNotation <- function(text){ ## Change the format of any numbers in "text" that are in S-PLUS ## scientific notation to WinBUGS scientific notation @@ -31,7 +29,7 @@ ## Find the first instance ## Note that the number may or may not have a decimal point. sciNoteLoc <- regexpr("[0-9]*\\.{0,1}[0-9]*e\\+0[0-9]{2}", text) - + ## For every instance, replace the number while(sciNoteLoc > -1){ sciNoteEnd <- sciNoteLoc + attr(sciNoteLoc, "match.length")-1 @@ -54,4 +52,3 @@ text } -} ## ends if (!is.R()) Modified: trunk/R2WinBUGS/man/bugs.run.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.run.Rd 2007-05-13 11:43:49 UTC (rev 16) +++ trunk/R2WinBUGS/man/bugs.run.Rd 2007-05-13 16:49:34 UTC (rev 17) @@ -6,7 +6,8 @@ calls WinBUGS and runs it with \file{script.txt}. Intended for internal use only} \usage{ bugs.run(n.burnin, bugs.directory, WINE = "", - useWINE = .Platform$OS.type != "windows", newWINE = TRUE) + useWINE = .Platform$OS.type != "windows", + newWINE = TRUE, WINEPATH = NULL) } \arguments{ \item{n.burnin}{length of burn in} @@ -16,6 +17,8 @@ defaults to \code{TRUE} on Windows, and \code{FALSE} otherwise. The \code{useWINE = TRUE} option is not available in S-PLUS.} \item{newWINE}{set this one to \code{TRUE} for new versions of WINE.} + \item{WINEPATH}{Path the WINE, \code{bugs} tries hard to get the information automatically and + pass it to this function, if not given.} } \value{ Does not return anything. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gg...@us...> - 2007-06-12 09:12:58
|
Revision: 23 http://svn.sourceforge.net/bugs-r/?rev=23&view=rev Author: ggorjan Date: 2007-06-12 02:12:59 -0700 (Tue, 12 Jun 2007) Log Message: ----------- bugs.script.R - using Dawn's solution for proper indexing of CODA files also for R side - removed one commented and useless line at the top read.bugs.Rd - documented Dawn's solution Modified Paths: -------------- trunk/R2WinBUGS/R/bugs.script.R trunk/R2WinBUGS/man/read.bugs.Rd Modified: trunk/R2WinBUGS/R/bugs.script.R =================================================================== --- trunk/R2WinBUGS/R/bugs.script.R 2007-05-23 17:44:29 UTC (rev 22) +++ trunk/R2WinBUGS/R/bugs.script.R 2007-06-12 09:12:59 UTC (rev 23) @@ -3,7 +3,6 @@ n.thin, model.file, debug=FALSE, is.inits, bin, DIC = FALSE, useWINE = FALSE, newWINE = FALSE, WINEPATH = NULL){ ### Write file script.txt for Bugs to read -### if (n.chains<2) stop ("n.chains must be at least 2") if((ceiling(n.iter/n.thin) - ceiling(n.burnin/n.thin)) < 2) stop ("(n.iter-n.burnin)/n.thin must be at least 2") @@ -17,29 +16,16 @@ coda <- file.path(working.directory, "coda") logfile <- file.path(working.directory, "log.odc") logfileTxt <- file.path(working.directory, "log.txt") - inits <- sapply(paste(working.directory, "/inits", 1:n.chains, ".txt", sep=""), + inits <- sapply(paste(working.directory, "/inits", 1:n.chains, ".txt", sep=""), function(x) native2win(x, , WINEPATH = WINEPATH)) initlist <- paste("inits (", 1:n.chains, ", '", inits, "')\n", sep="") savelist <- paste("set (", parameters.to.save, ")\n", sep="") redo <- ceiling((n.iter-n.burnin)/(n.thin*bin)) - - if (is.R()){ - thinUpdateCommand <- paste("thin.updater (", n.thin, ")\n", - "update (", ceiling(n.burnin/n.thin), ")\n", sep = "") - } else{ - ## In S-PLUS, the handling of the thinning is done differently than in R. - ## bin represents the number of iterations between saves, before thinning, - ## where in R it is the number of iterations between saves, after thinning. - ## This alternative handling of the thinning is done so that the resulting - ## samples have the correct iteration indexes in the output (coda) files. - ## Therefore, if the samples are read into S-PLUS using the coda package, - ## the thinning will be correctly labelled in the resulting mcmc object. - ## In R, the thinning is always labelled as 1, even if thinning was done. - thinUpdateCommand <- paste("update (", n.burnin, ")\n", - "thin.samples (", n.thin, ")\n", sep = "") - bin = bin * n.thin - } - + + thinUpdateCommand <- paste("update (", n.burnin, ")\n", + "thin.samples (", n.thin, ")\n", sep = "") + bin = bin * n.thin + cat( "display ('log')\n", "check ('", native2win(model, WINEPATH=WINEPATH), "')\n", @@ -56,7 +42,7 @@ "stats (*)\n", if(DIC) "dic.stats()\n", "history (*, '", native2win(history, WINEPATH=WINEPATH), "')\n", - "save ('", native2win(logfile, WINEPATH=WINEPATH), "')\n", + "save ('", native2win(logfile, WINEPATH=WINEPATH), "')\n", "save ('", native2win(logfileTxt, WINEPATH=WINEPATH), "')\n", file=script, sep="", append=FALSE) if (!debug) cat ("quit ()\n", file=script, append=TRUE) Modified: trunk/R2WinBUGS/man/read.bugs.Rd =================================================================== --- trunk/R2WinBUGS/man/read.bugs.Rd 2007-05-23 17:44:29 UTC (rev 22) +++ trunk/R2WinBUGS/man/read.bugs.Rd 2007-06-12 09:12:59 UTC (rev 23) @@ -1,19 +1,34 @@ \name{read.bugs} \alias{read.bugs} \title{Read output files in CODA format} -\description{This function reads Markov Chain Monte Carlo output in the CODA format produced by WinBUGS - and returns an object of class \code{\link[coda]{mcmc.list}} for further output analysis - using the \sQuote{coda} package.} + +\description{This function reads Markov Chain Monte Carlo output in the + CODA format produced by WinBUGS and returns an object of class + \code{\link[coda]{mcmc.list}} for further output analysis using the + \pkg{coda} package.} + \usage{ read.bugs(codafiles, ...) } + \arguments{ - \item{codafiles}{character vector of filenames (e.g. returned from \code{\link{bugs}} - in call such as \code{bugs(....., codaPkg = TRUE, .....)}). - Each of the files contains coda output for one chain produced by WinBUGS, - the \emph{directory} name of the corresponding file \file{codaIndex.txt} - is extracted from the first element of \code{codafiles}.} - \item{...}{further arguments to be passed to \code{\link[coda]{read.coda}}} + \item{codafiles}{character vector of filenames (e.g. returned from + \code{\link{bugs}} in call such as \code{bugs(....., codaPkg=TRUE, + .....)}). Each of the files contains coda output for one chain + produced by WinBUGS, the \emph{directory} name of the corresponding + file \file{codaIndex.txt} is extracted from the first element of + \code{codafiles}.} + \item{...}{further arguments to be passed to + \code{\link[coda]{read.coda}}} } -\seealso{\code{\link{bugs}}, \code{\link[coda]{read.coda}}, \code{\link[coda]{mcmc.list}}} + +\details{ + \code{\link{bugs}} (actually it is \code{\link{bugs.script}}) writes + BUGS script in such a way that iteration numbers in resulting CODA + files correspond to iteration numbers before burnin and thinning. +} + +\seealso{\code{\link{bugs}}, \code{\link[coda]{read.coda}}, +\code{\link[coda]{mcmc.list}}} + \keyword{file} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gg...@us...> - 2007-06-12 09:57:26
|
Revision: 25 http://svn.sourceforge.net/bugs-r/?rev=25&view=rev Author: ggorjan Date: 2007-06-12 02:57:21 -0700 (Tue, 12 Jun 2007) Log Message: ----------- some formatting Modified Paths: -------------- trunk/R2WinBUGS/R/bugs.log.R trunk/R2WinBUGS/R/print.bugs.R trunk/R2WinBUGS/man/bugs.log.Rd Modified: trunk/R2WinBUGS/R/bugs.log.R =================================================================== --- trunk/R2WinBUGS/R/bugs.log.R 2007-06-12 09:55:29 UTC (rev 24) +++ trunk/R2WinBUGS/R/bugs.log.R 2007-06-12 09:57:21 UTC (rev 25) @@ -3,44 +3,47 @@ if(!file.exists(file)) stop("Log file", file, "does not exist") logfile <- readLines(file) - + + ## --- Stats --- + statsStart <- which(logfile == "Node statistics") + 2 if(!length(statsStart)) stop("Log file", file, "does not contain node statistics.") ## + 2 to remove ## "Node statistics" ## "\t node\t mean\t sd\t MC error\t2.5%\tmedian\t97.5%\tstart\tsample" - + statsEnd <- which(logfile == "dic.stats()") - 1 - ## + 1 to remove + ## - 1 to remove ## "dic.stats()" - + statsTable <- logfile[statsStart:statsEnd] statsTable <- sub("\t", "", statsTable) statsTable <- sapply(strsplit(statsTable, "\t"), "[") colnames(statsTable) <- statsTable[1,] statsTable <- t(apply(statsTable[-1,], 2, as.numeric)) colnames(statsTable) <- c("mean", "sd", "MC error", "2.5%", "median", "97.5%", "start", "sample") - - ## DIC + + ## --- DIC --- + DICStart <- which(logfile == "DIC") + 3 ## + 3 to remove ## "DIC" ## "Dbar = post.mean of -2logL; Dhat = -2LogL at post.mean of stochastic nodes" ## "\tDbar\tDhat\tpD\tDIC\t" - - DICEnd <- grep("history(", logfile, fixed = TRUE) - 1 + + DICEnd <- grep("history(", logfile, fixed=TRUE) - 1 ## - 1 to remove ## "history(+, ..." - - if(!length(DICEnd) || !length(DICStart) || (DICEnd < DICStart)){ + + if(!length(DICEnd) || !length(DICStart) || (DICEnd < DICStart)){ DICTable <- NA - }else{ + } else { DICTable <- logfile[DICStart:DICEnd] DICTable <- sapply(strsplit(DICTable, "\t"), "[") colnames(DICTable) <- DICTable[1,] DICTable <- t(apply(DICTable[-1,], 2, as.numeric)) colnames(DICTable) <- c("Dbar", "Dhat", "pD", "DIC") } - return(list(stats = statsTable, DIC = DICTable)) + list(stats=statsTable, DIC=DICTable) } Modified: trunk/R2WinBUGS/R/print.bugs.R =================================================================== --- trunk/R2WinBUGS/R/print.bugs.R 2007-06-12 09:55:29 UTC (rev 24) +++ trunk/R2WinBUGS/R/print.bugs.R 2007-06-12 09:57:21 UTC (rev 25) @@ -1,29 +1,33 @@ -fround <- function(x, digits) +fround <- function(x, digits) format(round(x, digits), nsmall=digits) - -print.bugs <- function(x, digits.summary = 1, ...){ + +print.bugs <- function(x, digits.summary = 1, ...) +{ if(!is.null(x$model.file)) cat("Inference for Bugs model at \"", x$model.file, "\", ", sep="") if(!is.null(x$program)) cat("fit using ", x$program, ",", sep="") - cat("\n ", x$n.chains, " chains, each with ", x$n.iter, + cat("\n ", x$n.chains, " chains, each with ", x$n.iter, " iterations (first ", x$n.burnin, " discarded)", sep = "") if(x$n.thin > 1) cat(", n.thin =", x$n.thin) cat("\n n.sims =", x$n.sims, "iterations saved\n") print(round(x$summary, digits.summary), ...) - if(x$n.chains > 1){ + if(x$n.chains > 1) { cat("\nFor each parameter, n.eff is a crude measure of effective sample size,") cat("\nand Rhat is the potential scale reduction factor (at convergence, Rhat=1).\n") } - if(length(x$DIC)==1 && x$DIC){ - cat("\npD =", fround(x$pD, 1), "and DIC =", fround(x$DIC, 1), - if(x$DICbyR) "(using the rule, pD = var(deviance)/2)\n" else "(using the rule, pD = Dbar-Dhat)\n") - cat("DIC is an estimate of expected predictive error (lower deviance is better).\n") + msgDICrule <- ifelse(x$DICbyR, + "(using the rule, pD = var(deviance)/2)\n", + "(using the rule, pD = Dbar-Dhat)\n") + msgDIC <- "DIC is an estimate of expected predictive error (lower deviance is better).\n" + if(length(x$DIC) == 1 && x$DIC) { + cat("\npD =", fround(x$pD, 1), "and DIC =", fround(x$DIC, 1), msgDICrule) + cat(msgDIC) } else if(length(x$DIC)>1) { - cat("\n") - print(round(x$DIC, 1)) - cat("DIC is an estimate of expected predictive error (lower deviance is better).\n") + cat("\n") + print(round(x$DIC, 1)) + cat(msgDIC) } invisible(x) } Modified: trunk/R2WinBUGS/man/bugs.log.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.log.Rd 2007-06-12 09:55:29 UTC (rev 24) +++ trunk/R2WinBUGS/man/bugs.log.Rd 2007-06-12 09:57:21 UTC (rev 25) @@ -1,23 +1,26 @@ \name{bugs.log} \alias{bugs.log} \title{Read data from WinBUGS logfile} -\description{Read data such as summary statistics and DIC information from the WinBUGS logfile.} +\description{Read data such as summary statistics and DIC information + from the WinBUGS logfile} \usage{ bugs.log(file) } \arguments{ - \item{file}{Location of the WinBUGS logfile.} + \item{file}{Location of the WinBUGS logfile} } \value{ A list with components - \item{stats}{A matrix containing summary statistics for each element of each saved parameter. - Comparable to the information in the element \code{summary} of a bugs object as returned by \code{\link{bugs}}.} - \item{DIC}{A matrix containg the DIC statistics as returned from WinBUGS - (which is different from the way \code{\link{bugs}} calculates it).} + \item{stats}{A matrix containing summary statistics for each saved + parameter. Comparable to the information in the element + \code{summary} of a bugs object as returned by \code{\link{bugs}}.} + \item{DIC}{A matrix containing the DIC statistics as returned from + WinBUGS.} } \details{ -In later releases of R2WiNBUGS, this function is considered to read the relevant data from the log file rather than -analysing and calculating the relevant data in R / S-PLUS again. +In later releases of R2WiNBUGS, this function is considered to read the +relevant data from the log file rather than analysing and calculating +the relevant data in R / S-PLUS again. } \seealso{The main function that generates the log file is \code{\link{bugs}}.} \keyword{IO} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gg...@us...> - 2007-06-12 09:58:15
|
Revision: 26 http://svn.sourceforge.net/bugs-r/?rev=26&view=rev Author: ggorjan Date: 2007-06-12 02:58:15 -0700 (Tue, 12 Jun 2007) Log Message: ----------- bugs.Rd - fixed useWINE documentation - some formatting bugs.R - fixed use of is.null(DIC) - some formatting Modified Paths: -------------- trunk/R2WinBUGS/R/bugs.R trunk/R2WinBUGS/man/bugs.Rd Modified: trunk/R2WinBUGS/R/bugs.R =================================================================== --- trunk/R2WinBUGS/R/bugs.R 2007-06-12 09:57:21 UTC (rev 25) +++ trunk/R2WinBUGS/R/bugs.R 2007-06-12 09:58:15 UTC (rev 26) @@ -1,81 +1,83 @@ "bugs" <- -function(data, inits, parameters.to.save, model.file = "model.bug", - 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/", - program = c("winbugs", "openbugs", "WinBugs", "OpenBugs"), - working.directory = NULL, - clearWD = FALSE, useWINE = .Platform$OS.type != "windows", WINE = Sys.getenv("WINE"), - newWINE = FALSE, WINEPATH = NULL){ - +function(data, inits, parameters.to.save, model.file="model.bug", + 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/", + program=c("winbugs", "openbugs", "WinBugs", "OpenBugs"), + working.directory=NULL, + clearWD=FALSE, useWINE=.Platform$OS.type != "windows", WINE=Sys.getenv("WINE"), + newWINE=FALSE, WINEPATH=NULL) +{ program <- match.arg(program) - if (program %in% c("openbugs", "OpenBugs")) - if (is.R()){ - ## If OpenBUGS, we only call openbugs() and exit... - return(openbugs(data, inits, parameters.to.save, model.file, - n.chains, n.iter, n.burnin, n.thin, DIC, bugs.directory, - working.directory, digits)) + if(program %in% c("openbugs", "OpenBugs")) { + if(is.R()) { + ## If OpenBUGS, we only call openbugs() and exit... + return(openbugs(data, inits, parameters.to.save, model.file, + n.chains, n.iter, n.burnin, n.thin, DIC, bugs.directory, + working.directory, digits)) } else { - stop ("OpenBUGS is not yet available in S-PLUS") + stop ("OpenBUGS is not yet available in S-PLUS") } + } ## 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(useWINE) { - if (!is.R()) - stop ("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") + if(useWINE) { + if(!is.R()) + stop ("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") ## attempt to find wine and winepath - if (!nchar(WINE)) { - WINE <- system("locate wine | grep bin/wine$", intern = TRUE) - WINE <- WINE[length(WINE)] + if(!nchar(WINE)) { + WINE <- system("locate wine | grep bin/wine$", intern=TRUE) + WINE <- WINE[length(WINE)] } - if (!length(WINE)) stop("couldn't locate WINE binary") - if (!nchar(WINEPATH)) { - WINEPATH <- system("locate winepath | grep bin/winepath$", intern = TRUE) - WINEPATH <- WINEPATH[length(WINEPATH)] + if(!length(WINE)) stop("couldn't locate WINE binary") + if(!nchar(WINEPATH)) { + WINEPATH <- system("locate winepath | grep bin/winepath$", intern=TRUE) + WINEPATH <- WINEPATH[length(WINEPATH)] } - if (!length(WINEPATH)) stop("couldn't locate WINEPATH binary") - } - if(!is.null(working.directory)){ - savedWD <- getwd() - setwd(working.directory) - on.exit(setwd(savedWD)) + if(!length(WINEPATH)) stop("couldn't locate WINEPATH binary") } - 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) + 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))){ + 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(try(file.remove(new.model.file)), add = TRUE) - } else new.model.file <- model.file + file.copy(model.file, new.model.file, overwrite=TRUE) + on.exit(try(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, - new.model.file, debug=debug, is.inits=!is.null(inits), - bin = bin, DIC = DIC, useWINE = useWINE, newWINE = newWINE, WINEPATH = WINEPATH) - bugs.run(n.burnin, bugs.directory, WINE = WINE, useWINE = useWINE, WINEPATH = WINEPATH) - + new.model.file, debug=debug, is.inits=!is.null(inits), + bin=bin, DIC=DIC, useWINE=useWINE, newWINE=newWINE, + WINEPATH=WINEPATH) + bugs.run(n.burnin, bugs.directory, WINE=WINE, useWINE=useWINE, + WINEPATH=WINEPATH) if(codaPkg) return(file.path(getwd(), paste("coda", 1:n.chains, ".txt", sep=""))) - - if (is.R()){ - sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), - model.file = model.file, is.DIC = !is.null(DIC), program = program) - } else { - sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), - model.file = model.file, is.DIC = DIC, program = program) + sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, + n.thin, DIC), + model.file=model.file, is.DIC=DIC, program=program) + if(clearWD) { + file.remove(c("data.txt", "log.odc", "log.txt", "codaIndex.txt", + paste("inits", 1:n.chains, ".txt", sep=""), + paste("coda", 1:n.chains, ".txt", sep=""))) } - if(clearWD) - file.remove(c("data.txt", "log.odc", "log.txt", "codaIndex.txt", - paste("inits", 1:n.chains, ".txt", sep=""), - paste("coda", 1:n.chains, ".txt", sep=""))) class(sims) <- "bugs" - return(sims) + sims } Modified: trunk/R2WinBUGS/man/bugs.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.Rd 2007-06-12 09:57:21 UTC (rev 25) +++ trunk/R2WinBUGS/man/bugs.Rd 2007-06-12 09:58:15 UTC (rev 26) @@ -5,143 +5,172 @@ It automatically writes a WinBUGS script, calls the model, and saves the simulations for easy access in R or S-PLUS.} \usage{ -bugs(data, inits, parameters.to.save, model.file = "model.bug", - 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/", - program = c("winbugs", "openbugs", "WinBugs", "OpenBugs"), - working.directory = NULL, clearWD = FALSE, - useWINE = .Platform$OS.type != "windows", WINE = Sys.getenv("WINE"), - newWINE = FALSE, WINEPATH = NULL) +bugs(data, inits, parameters.to.save, model.file="model.bug", + 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/", + program=c("winbugs", "openbugs", "WinBugs", "OpenBugs"), + working.directory=NULL, clearWD=FALSE, + useWINE=.Platform$OS.type != "windows", WINE=Sys.getenv("WINE"), + newWINE=FALSE, WINEPATH=NULL) } \arguments{ - \item{data}{either a named list (names corresponding to variable names in the \code{model.file}) - of the data for the WinBUGS model, \emph{or} - a vector or list of the names of the data objects used by the model. - If \code{data = "data.txt"}, it is assumed that data have already been written to the working directory - in a file called \file{data.txt}, e.g. by the function \code{\link{bugs.data}}.} - \item{inits}{a list with \code{n.chains} elements; each element of the list is - itself a list of starting values for the WinBUGS model, \emph{or} - a function creating (possibly random) initial values. - Alternatively, if \code{inits = NULL}, initial values are generated by WinBUGS} - \item{parameters.to.save}{character vector of the names of the parameters to save which should be monitored} + \item{data}{either a named list (names corresponding to variable names + in the \code{model.file}) of the data for the WinBUGS model, + \emph{or} a vector or list of the names of the data objects used by + the model. If \code{data="data.txt"}, it is assumed that data have + already been written to the working directory in a file called + \file{data.txt}, e.g. by the function \code{\link{bugs.data}}.} + \item{inits}{a list with \code{n.chains} elements; each element of the + list is itself a list of starting values for the WinBUGS model, + \emph{or} a function creating (possibly random) initial values. + Alternatively, if \code{inits=NULL}, initial values are generated + by WinBUGS.} + \item{parameters.to.save}{character vector of the names of the + parameters to save which should be monitored} \item{model.file}{file containing the model written in WinBUGS code. The extension can be either \file{.bug} or \file{.txt}. - - If the extension is \file{.bug} and \code{program=="winbugs"}, + If the extension is \file{.bug} and \code{program=="winbugs"}, a copy of the file with extension \file{.txt} will be created - in the \code{bugs()} call and removed afterwards. - Note that similarly named \file{.txt} files will be overwritten.} + in the \code{bugs()} call and removed afterwards. Note that + similarly named \file{.txt} files will be overwritten.} \item{n.chains}{number of Markov chains (default: 3)} - \item{n.iter}{number of total iterations per chain (including burn in; default: 2000)} - \item{n.burnin}{length of burn in, i.e. number of iterations to discard at the beginning. - Default is \code{n.iter/2}, that is, discarding the first half of the simulations.} - \item{n.thin}{thinning rate. Must be a positive integer. - Set \code{n.thin} > 1 to save memory and computation time if \code{n.iter} is large. - Default is \code{max(1, floor(n.chains * (n.iter-n.burnin) / 1000))} - which will only thin if there are at least 2000 simulations.} - \item{bin}{number of iterations between saving of results - (i.e. the coda files are saved after each \code{bin} iterations); + \item{n.iter}{number of total iterations per chain (including burn in; + default: 2000)} + \item{n.burnin}{length of burn in, i.e. number of iterations to + discard at the beginning. Default is \code{n.iter/2}, that is, + discarding the first half of the simulations.} + \item{n.thin}{thinning rate. Must be a positive integer. Set + \code{n.thin} > 1 to save memory and computation time if + \code{n.iter} is large. Default is \code{max(1, floor(n.chains * + (n.iter-n.burnin) / 1000))} which will only thin if there are at + least 2000 simulations.} + \item{bin}{number of iterations between saving of results + (i.e. the coda files are saved after each \code{bin} iterations); default is to save only at the end.} \item{debug}{if \code{FALSE} (default), WinBUGS is closed automatically - when the script has finished running, otherwise WinBUGS remains open for further investigation} - \item{DIC}{logical; if \code{TRUE} (default), compute deviance, pD, and DIC. - This is done in WinBUGS directly using the rule \code{pD = Dbar - Dhat}. - If there are less iterations than required for the adaptive phase, - the rule \code{pD = var(deviance) / 2} is used.} - \item{digits}{number of significant digits used for WinBUGS input, see \code{\link{formatC}}} - \item{codaPkg}{logical; if \code{FALSE} (default) a \code{bugs} object is returned, - if \code{TRUE} file names of WinBUGS output are returned for easy access by the \pkg{coda} package - through function \code{\link{read.bugs}}. (not used if \code{program = "openbugs"})} + when the script has finished running, otherwise WinBUGS remains open + for further investigation} + \item{DIC}{logical; if \code{TRUE} (default), compute deviance, pD, + and DIC. This is done in WinBUGS directly using the rule \code{pD = + Dbar - Dhat}. If there are less iterations than required for the + adaptive phase, the rule \code{pD=var(deviance) / 2} is used.} + \item{digits}{number of significant digits used for WinBUGS input, see + \code{\link{formatC}}} + \item{codaPkg}{logical; if \code{FALSE} (default) a \code{bugs} object + is returned, if \code{TRUE} file names of WinBUGS output are + returned for easy access by the \pkg{coda} package through function + \code{\link{read.bugs}} (not used if \code{program="openbugs"}).} \item{bugs.directory}{directory that contains the WinBUGS executable} - \item{program}{the program to use, either \code{winbugs}/\code{WinBugs} or \code{openbugs}/\code{OpenBugs}, - the latter makes use of function \code{\link{openbugs}} and requires the CRAN package \pkg{BRugs}. - The \code{openbugs}/\code{OpenBugs} choice is not available in S-PLUS.} - \item{working.directory}{sets working directory during execution of this function; - WinBUGS' in- and output will be stored in this directory; - if \code{NULL}, the current working directory is chosen.} - \item{clearWD}{logical; indicating whether the files \file{data.txt}, - \file{inits[1:n.chains].txt}, \file{log.odc}, \file{codaIndex.txt}, and - \file{coda[1:nchains].txt} should be removed after WinBUGS has finished. - If set to \code{TRUE}, this argument is only respected if \code{codaPkg = FALSE}. - } - \item{useWINE}{logical; attempt to use the WINE emulator to run WinBUGS, - defaults to \code{TRUE} on Windows, and \code{FALSE} otherwise. - If WINE is used, the arguments \code{bugs.directory} and \code{working.directory} must be given in form of Linux paths - rather than Windows paths (if not \code{NULL}). - The \code{useWINE = TRUE} option is not available in S-PLUS.} + \item{program}{the program to use, either + \code{winbugs}/\code{WinBugs} or \code{openbugs}/\code{OpenBugs}, + the latter makes use of function \code{\link{openbugs}} and requires + the CRAN package \pkg{BRugs}. The \code{openbugs}/\code{OpenBugs} + choice is not available in S-PLUS.} + \item{working.directory}{sets working directory during execution of + this function; WinBUGS' in- and output will be stored in this + directory; if \code{NULL}, the current working directory is chosen.} + \item{clearWD}{logical; indicating whether the files \file{data.txt}, + \file{inits[1:n.chains].txt}, \file{log.odc}, \file{codaIndex.txt}, + and \file{coda[1:nchains].txt} should be removed after WinBUGS has + finished. If set to \code{TRUE}, this argument is only respected if + \code{codaPkg=FALSE}.} + \item{useWINE}{logical; attempt to use the WINE emulator to run + WinBUGS, defaults to \code{FALSE} on Windows, and \code{TRUE} + otherwise. If WINE is used, the arguments \code{bugs.directory} and + \code{working.directory} must be given in form of Linux paths rather + than Windows paths (if not \code{NULL}). The \code{useWINE=TRUE} + option is not available in S-PLUS.} \item{WINE}{character; name of WINE binary file} \item{newWINE}{Set this one to \code{TRUE} for new versions of WINE.} - \item{WINEPATH}{Path the WINE, it is tried hard to get the information automatically if not given.} + \item{WINEPATH}{Path to WINE binary file, it is tried hard to get the + information automatically if not given.} } \details{ -To run: -\enumerate{ -\item Write a WinBUGS model in a ASCII file. -\item Go into R / S-PLUS. -\item Prepare the inputs to the \code{bugs} function and run it (see Example). -\item A WinBUGS window will pop up and R / S-PLUS will freeze up. The model - will now run in WinBUGS. It might take awhile. You will see - things happening in the Log window within WinBUGS. When WinBugs - is done, its window will close and R / S-PLUS will work again. -\item If an error message appears, re-run with \code{debug = TRUE}. -}} + To run: + \enumerate{ + \item Write a WinBUGS model in a ASCII file. + \item Go into R / S-PLUS. + \item Prepare the inputs to the \code{bugs} function and run it (see + Example). + \item A WinBUGS window will pop up and R / S-PLUS will freeze + up. The model will now run in WinBUGS. It might take awhile. You + will see things happening in the Log window within WinBUGS. When + WinBugs is done, its window will close and R / S-PLUS will work + again. + \item If an error message appears, re-run with \code{debug=TRUE}. + } +} + \value{ - If \code{codaPkg = TRUE} the returned values are the names - of coda output files written by WinBUGS containing - the Markov Chain Monte Carlo output in the CODA format. - This is useful for direct access with \code{\link{read.bugs}}. + If \code{codaPkg=TRUE} the returned values are the names + of coda output files written by WinBUGS containing + the Markov Chain Monte Carlo output in the CODA format. + This is useful for direct access with \code{\link{read.bugs}}. - If \code{codaPkg = FALSE}, the following values are returned: + If \code{codaPkg=FALSE}, the following values are returned: \item{n.chains}{see Section \sQuote{Arguments}} \item{n.iter}{see Section \sQuote{Arguments}} \item{n.burnin}{see Section \sQuote{Arguments}} \item{n.thin}{see Section \sQuote{Arguments}} - \item{n.keep}{number of iterations kept per chain (equal to \code{(n.iter-n.burnin) / n.thin})} - \item{n.sims}{number of posterior simulations (equal to \code{n.chains * n.keep})} + \item{n.keep}{number of iterations kept per chain (equal to + \code{(n.iter-n.burnin) / n.thin})} + \item{n.sims}{number of posterior simulations (equal to + \code{n.chains * n.keep})} \item{sims.array}{3-way array of simulation output, with dimensions n.keep, n.chains, and length of combined parameter vector} - \item{sims.list}{list of simulated parameters:\cr - for each scalar parameter, a vector of length n.sims\cr - for each vector parameter, a 2-way array of simulations,\cr + \item{sims.list}{list of simulated parameters: + for each scalar parameter, a vector of length n.sims + for each vector parameter, a 2-way array of simulations, for each matrix parameter, a 3-way array of simulations, etc. - (for convenience, the \code{n.keep * n.chains} simulations in - sims.matrix and sims.list (but NOT sims.array have been randomly permuted)} - \item{sims.matrix}{matrix of simulation output, with \code{n.chains * n.keep} rows and - one column for each element of each saved parameter - (for convenience, the \code{n.keep * n.chains} simulations in - sims.matrix and sims.list (but NOT sims.array have been randomly permuted)} - \item{summary}{summary statistics and convergence information for each - element of each saved parameter.} + (for convenience, the \code{n.keep*n.chains} simulations in + sims.matrix and sims.list (but NOT sims.array) have been randomly + permuted)} + \item{sims.matrix}{matrix of simulation output, with + \code{n.chains*n.keep} rows and one column for each element of + each saved parameter (for convenience, the \code{n.keep*n.chains} + simulations in sims.matrix and sims.list (but NOT sims.array) have + been randomly permuted)} + \item{summary}{summary statistics and convergence information for + each saved parameter.} \item{mean}{a list of the estimated parameter means} \item{sd}{a list of the estimated parameter standard deviations} \item{median}{a list of the estimated parameter medians} - \item{root.short}{names of argument \code{parameters.to.save} and \dQuote{deviance}} + \item{root.short}{names of argument \code{parameters.to.save} and + \dQuote{deviance}} \item{long.short}{indexes; programming stuff} \item{dimension.short}{dimension of \code{indexes.short}} \item{indexes.short}{indexes of \code{root.short}} - \item{last.values}{list of simulations from the most recent iteration; they - can be used as starting points if you wish to run WinBUGS for further iterations} - \item{pD}{an estimate of the effective number of parameters, for calculations see the section \dQuote{Arguments}.} + \item{last.values}{list of simulations from the most recent + iteration; they can be used as starting points if you wish to run + WinBUGS for further iterations} + \item{pD}{an estimate of the effective number of parameters, for + calculations see the section \dQuote{Arguments}.} \item{DIC}{\code{mean(deviance) + pD}} } + \references{ Gelman, A., Carlin, J.B., Stern, H.S., Rubin, D.B. (2003): \emph{Bayesian Data Analysis}, 2nd edition, CRC Press. - Sturtz, S., Ligges, U., Gelman, A. (2005): - R2WinBUGS: A Package for Running WinBUGS from R. + Sturtz, S., Ligges, U., Gelman, A. (2005): + R2WinBUGS: A Package for Running WinBUGS from R. \emph{Journal of Statistical Software} 12(3), 1-16. } - -\author{Andrew Gelman, \email{ge...@st...}, \url{http:/www.stat.columbia.edu/~gelman/bugsR/}; - modifications and packaged by Sibylle Sturtz, \email{st...@st...}, and Uwe Ligges.} -\seealso{\code{\link{print.bugs}}, \code{\link{plot.bugs}}, and the \pkg{coda} package} + +\author{Andrew Gelman, \email{ge...@st...}, + \url{http:/www.stat.columbia.edu/~gelman/bugsR/}; modifications and + packaged by Sibylle Sturtz, \email{st...@st...}, + and Uwe Ligges.} + +\seealso{\code{\link{print.bugs}}, \code{\link{plot.bugs}}, and the + \pkg{coda} package} + \examples{ # An example model file is given in: -model.file <- system.file(package = "R2WinBUGS", "model", "schools.txt") +model.file <- system.file(package="R2WinBUGS", "model", "schools.txt") # Let's take a look: file.show(model.file) @@ -154,17 +183,17 @@ sigma.y <- schools$sd data <- list ("J", "y", "sigma.y") inits <- function(){ - list(theta = rnorm(J, 0, 100), mu.theta = rnorm(1, 0, 100), - sigma.theta = runif(1, 0, 100)) + list(theta=rnorm(J, 0, 100), mu.theta=rnorm(1, 0, 100), + sigma.theta=runif(1, 0, 100)) } ## or alternatively something like: # inits <- list( -# list(theta = rnorm(J, 0, 90), mu.theta = rnorm(1, 0, 90), -# sigma.theta = runif(1, 0, 90)), -# list(theta = rnorm(J, 0, 100), mu.theta = rnorm(1, 0, 100), -# sigma.theta = runif(1, 0, 100)) -# list(theta = rnorm(J, 0, 110), mu.theta = rnorm(1, 0, 110), -# sigma.theta = runif(1, 0, 110))) +# list(theta=rnorm(J, 0, 90), mu.theta=rnorm(1, 0, 90), +# sigma.theta=runif(1, 0, 90)), +# list(theta=rnorm(J, 0, 100), mu.theta=rnorm(1, 0, 100), +# sigma.theta=runif(1, 0, 100)) +# list(theta=rnorm(J, 0, 110), mu.theta=rnorm(1, 0, 110), +# sigma.theta=runif(1, 0, 110))) parameters <- c("theta", "mu.theta", "sigma.theta") @@ -172,9 +201,9 @@ ## You may need to edit "bugs.directory", ## also you need write access in the working directory: schools.sim <- bugs(data, inits, parameters, model.file, - n.chains = 3, n.iter = 5000, - bugs.directory = "c:/Program Files/WinBUGS14/", - working.directory = NULL, clearWD = TRUE) + n.chains=3, n.iter=5000, + bugs.directory="c:/Program Files/WinBUGS14/", + working.directory=NULL, clearWD=TRUE) print(schools.sim) plot(schools.sim) } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gg...@us...> - 2007-06-12 10:09:17
|
Revision: 28 http://svn.sourceforge.net/bugs-r/?rev=28&view=rev Author: ggorjan Date: 2007-06-12 03:09:18 -0700 (Tue, 12 Jun 2007) Log Message: ----------- version bump, merged and reversed ChangeLog files, note about supported BUGS versions Modified Paths: -------------- trunk/R2WinBUGS/Changes trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/man/bugs.Rd Removed Paths: ------------- trunk/R2WinBUGS/Changes0 Modified: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes 2007-06-12 09:58:57 UTC (rev 27) +++ trunk/R2WinBUGS/Changes 2007-06-12 10:09:18 UTC (rev 28) @@ -1,51 +1,37 @@ Changes to R2WinBUGS: ===================== -During the process of packaging R2WinBUGS_0.1 - R2WinBUGS_0.2-1, -quite a lot of changes had been made. Those changes are not -documented anywhere ... +Update 2.1-5 (12 June 2007): +- proper indexing of CODA files also in R version +- fixed useWINE documentation in bugs help page +- note on supported BUGS versions +- as.bugs.array now returns info on used rule for pD i.e. var(deviance)/2 +- some internal fixes related to is.null(DIC) +- merging and reversing ChangeLog files +Update 2.1-4 (20 May 2007): +- Depending on coda now (Namespace issues) +- some more fixes for codetools checks +- Vignette has been updated -Update 0.2-2 (28 Apr 2004): -- schools data: original (see references) instead of the rounded data +Update 2.1-3 (13 May 2007): +- Ported to S-PLUS by Insightful Corp. +- some fixes for codetools checks -Update 0.2-3 (10 Sept 2004): -- bugs.data.inits split to bugs.inits and bugs.data, the latter exported - from the Namespace. - Now we can use already written data files in bugs(). +Update 2.0-4 (01 November 2006): +- print.bugs / plot.bugs documentation fixes +- write.model() fix -Update 0.2-4 (05 Oct 2004): -- bugs.script() did not work for large n.iter values in update step - (no scientific notation allowed) +Update 2.0-3 (06 October 2006): +- \\. -> [.] in regular expressions -Update 0.2-5 (20 Oct 2004): -- bugs() and bugs.script() have a new argument bin that allows to specify - a number of iterations. After each "bin" iterations the coda files are saved. +Update 2.0-2 (26 July 2006): +- changes for DIC, making use of BUGS internal calculations +- some doc fixes -Update 0.2-6 (18 May 2005): -- bugs() changes in order to return a file names of coda output files -- new read.bugs() returns a coda mcmc.list object, if codaPkg=TRUE. +Update 2.0-1 (26 May 2006): +- some wine patches for 2.2-0 by Gregor Gorjanc -Update 0.2-8 (30 May 2005): -- bugs passed DIC to bugs.script in order to be able to disable it - -Update 0.2-9 (26 July 2005): -- bugs has new argument clearWD - -Update 1.0-0 (05 Aug 2005): -- attach.all(), detach.all(), attach.bugs() and detach.bugs() added/changed - more or less according to Andrew Gelman's current bugs.R - -Update 1.0-1 (14 Nov 2005): -- make inits=NULL work (again ?) - -Update 1.1-0 (14 Dec 2005): -- Contribution by Ben Bolker and Yun Yan's rbugs package: - make R2WinBUGS work under WINE - -Update 1.1-1 (17 Feb 2006): -- WINE tweaks (mainly by Gregor Gorjanc) - Update 2.0-0 (08 May 2006): - bugs() doc fix/updates on scrambling - bugs.run() has new arg useWINE (by Gregor Gorjanc) @@ -56,25 +42,188 @@ Jouni Kerman and Andrew Gelman - new function write.model() based on ideas from Jouni Kerman -Update 2.0-1 (26 May 2006): -- some wine patches for 2.2-0 by Gregor Gorjanc +Update 1.1-1 (17 Feb 2006): +- WINE tweaks (mainly by Gregor Gorjanc) -Update 2.0-2 (26 July 2006): -- changes for DIC, making use of BUGS internal calculations -- some doc fixes +Update 1.1-0 (14 Dec 2005): +- Contribution by Ben Bolker and Yun Yan's rbugs package: + make R2WinBUGS work under WINE -Update 2.0-3 (06 October 2006): -- \\. -> [.] in regular expressions +Update 1.0-1 (14 Nov 2005): +- make inits=NULL work (again ?) -Update 2.0-4 (01 November 2006): -- print.bugs / plot.bugs documentation fixes -- write.model() fix +Update 1.0-0 (05 Aug 2005): +- attach.all(), detach.all(), attach.bugs() and detach.bugs() added/changed + more or less according to Andrew Gelman's current bugs.R -Update 2.1-3 (13 May 2007): -- Ported to S-PLUS by Insightful Corp. -- some fixes for codetools checks +Update 0.2-9 (26 July 2005): +- bugs has new argument clearWD -Update 2.1-4 (20 May 2007): -- Depending on coda now (Namespace issues) -- some more fixes for codetools checks -- Vignette has been updated +Update 0.2-8 (30 May 2005): +- bugs passed DIC to bugs.script in order to be able to disable it + +Update 0.2-6 (18 May 2005): +- bugs() changes in order to return a file names of coda output files +- new read.bugs() returns a coda mcmc.list object, if codaPkg=TRUE. + +Update 0.2-5 (20 Oct 2004): +- bugs() and bugs.script() have a new argument bin that allows to specify + a number of iterations. After each "bin" iterations the coda files are saved. + +Update 0.2-4 (05 Oct 2004): +- bugs.script() did not work for large n.iter values in update step + (no scientific notation allowed) + +Update 0.2-3 (10 Sept 2004): +- bugs.data.inits split to bugs.inits and bugs.data, the latter exported + from the Namespace. + Now we can use already written data files in bugs(). + +Update 0.2-2 (28 Apr 2004): +- schools data: original (see references) instead of the rounded data + +During the process of packaging R2WinBUGS_0.1 - R2WinBUGS_0.2-1, +quite a lot of changes had been made. Those changes are not +documented anywhere ... + +During the process of packaging R2WinBUGS_0.1, quite a lot of changes had +been made. Those changes are not documented anywhere ... + +Changes prior to R2WinBUGS_0.1: +=============================== + +Update 30 Oct 2003: + 1. Minor change to a return() statement to be compatible with R 1.8 + 2. Just a warning (from Ben Goodrich): if you are running Bugs.R inside + a loop, you have to be careful with the data() and the inits() + functions since they will not necessarily recognize locally-defined + variables. One workaround is to define the variables used in data() + and inits() using global assignments (<<-), but this can sometimes + make the program run slower. +Update 29 Aug 2003: + 1. Fixed "bugs.data.inits" function so you can use data that have the + same names as R functions. + 2. Changed T and F to TRUE and FALSE everywhere in case the variables + T and F are used as data in the main program + 3. Caution: if you are entering the data as a list of variable names + (see 10 Apr 2003 update, item 1), the data to be input into must + be global variables. This can be relevant if you are running bugs() + inside an R function. + 4. Caution: bugs() has difficulty processing ragged arrays. It is + better to save a whole matrix (e.g., "theta") rather than parts + (e.g., "theta[1:10,1]", "theta[1:5,2]"). If you want to save + part of a vector, you should do it as "theta[1:2]", not "theta[1]", + "theta[2]". +Update 30 Apr 2003: added time monitoring +Update 29 Apr 2003: + 1. The "attach.all" function (no longer called "attach2") overwrites + so that all components of a list or data frame get attached. + 2. Program now looks in the directory /winbug~1/ rather than /winbug~2/ + 3. Graphics parameters for margins are returned to their original state + at the end of the program. + 4. Added "digits.summary" option to the numerical display. + 5. Added "last.values" output: a list that can be input as "inits" + if you want to run the simulations longer. +Update 13 Apr 2003: fixed new bug in round.bugs(). Now all numbers are + saved in scientific notation. +Update 10 Apr 2003: + 1. It is now possible to enter the data as a list of variable names. + For example, before you had to enter data as, + data <- list (n=8, y=c(28,8,-3,7,-1,1,18,12)) + or + n <- 8 + y <- c(28,8,-3,7,-1,1,18,12) + data <- list (n=n, y=y) + Now you can enter the data as, + n <- 8 + y <- c(28,8,-3,7,-1,1,18,12) + data <- list ("n", "y") + The bugs() function will figure out which method you are using (based + on whether "data" is a list of numbers or a vector of character + strings). + This doesn't look like much, but it's convenient when you're entering + lots of data! + 2. It is now possible to enter the initial values as a function, + so as to automatically a random list of inits for each of the chains. + For example, in the 8-schools example below, we can do: + inits <- function() + list (theta=rnorm(J,0,1), mu.theta=rnorm(1,0,100), sigma.theta=runif(1,0,100)) + + to set up the inits as a function (rather than setting up n.chains + sets of specific initial values). Then, the function call, + schools.sim <- bugs (data, inits, parameters, "schools.txt", n.chains=3, n.iter=1000) + automatically sets up 3 sets of initial values (each a list of + theta, mu.theta, sigma.theta). + 3. Bug in the initial rounding (the function round.bugs()) has been fixed. + Thanks for Mark Clements for finding the bug and fixing it! + Also, we have set the default rounding to 5 digits instead of 2. +Update 01 Apr 2003: use layout() rather than split.screen() for graphical + display +Update 18 Mar 2003: + 1. Get the Bugs configuration information from the original file + (Registry_default.odc) rather than overwriting each time. (Fixes a + bug that occurred when R was interrupted in the middle of a Bugs run.) + 2. Display different colored dots in the right panel of the graphical + display, to show the medians from each chain. +Update 13 Mar 2003: fix minor bug in monitor() +Update 10 Mar 2003: fix bug in pD and DIC calculations +Update 7 Mar 2003: + 1. Fix display.parallel=T option by adding min.width so that very + intervals are still visible. + 2. Compute pD separately for each sequence (which gives much more + reasonable estimates before convergence). +Update 8 Feb 2003: minor fixes in graphical display +Update 6 Feb 2003: + 1. Approximate "effective sample size" n.eff given for each parameter. + 2. More explanatory material displayed. + 3. Use bringToTop() to automatically bring up the graphics window. +Update 4 Feb 2003: + 1. Automatically compute the deviance, DIC, and pD. Bugs will not + always compute DIC and pD, so we do so using the definition, + DIC = E(deviance) + pD, using var(deviance)/2 as an estimate of pD. + (This is derived from the chi^2 distribution. We can't use the + Spiegelhalter et al. definition of DIC because we don't have access + to the deviance function.) + 2. Set default for n.thin so that, after thinning, the total number + saved iterations, n.sims, is approximately 1000. +Update 14 Jan 2003 to run with the new WinBugs1.4. You may see an error + message and need to fix the dos.location assignment in bugs(). +Update 6 Jan 2003: + 1. Fix of bug that occurred with uppercase and lowercase variable names + 2. Set default for n.thin so that no more than about 500 iterations + will be saved from each sequence + 3. New option "display.parallel" added to show 80% inferences from + parallel sequences on the right panel of the graphical display. This + can be useful to understand what is going on when there are + convergence problems. +Update 26 Dec 2002: fix of minwidth in bugs.plot.summary +Update 11 Dec 2002: + 1. Automatic fixing of adaptive phases. Now you no longer need to run + for thousands of iterations when slice or Metropolis sampling is used. + 2. Various minor fixes +Update 10 Dec 2002: + 1. Cool graphical display of convergence and inferences! + 2. New "attach2" function that overwrites so that all components of + the list are attached +Update 29 Nov 2002: + 1. Fix of bug in 24 Nov update. + 2. Fix of bug in 16 Nov update. + 3. Length of chains is now pecified in terms of "n.iter" rather than + "n.keep". +Update 24 Nov 2002: improved treatment of "parameters.to.save". For + example, you can now use "alpha" to indicate an entire array of parameters, + whereas before you had to save "alpha[]" or "alpha[,]" or whatever. +Update 16 Nov 2002: mean, sd, median added to outputs +Update 4 Nov 2002: more error-flagging added +Update 26 Oct 2002: + 1. Parameters saved in order of the "parameters.to.save" vector + (not alphabetical order). + 2. Output saved in both matrix and list form. + 3. With the attach.sims=T setting (which is the default), the simulations + for all the saved parameters are saved as R objects. This is + convenient for later use of the simulations. +Updates to 16 Oct 2002: more error-flagging added, mean/sd added to summary, + fixing scientific notation so Bugs can always read data and inits +Update 21 Sept 2002: "quit=F" option changed to "debug=T" +First version written 18 Sept 2002 by Andrew Gelman, + adapted from the EmBedBugs package by Kenneth Rice Deleted: trunk/R2WinBUGS/Changes0 =================================================================== --- trunk/R2WinBUGS/Changes0 2007-06-12 09:58:57 UTC (rev 27) +++ trunk/R2WinBUGS/Changes0 2007-06-12 10:09:18 UTC (rev 28) @@ -1,142 +0,0 @@ -During the process of packaging R2WinBUGS_0.1, quite a lot of changes had -been made. Those changes are not documented anywhere ... - - -Changes prior to R2WinBUGS_0.1: -=============================== - -Update 30 Oct 2003: - 1. Minor change to a return() statement to be compatible with R 1.8 - 2. Just a warning (from Ben Goodrich): if you are running Bugs.R inside - a loop, you have to be careful with the data() and the inits() - functions since they will not necessarily recognize locally-defined - variables. One workaround is to define the variables used in data() - and inits() using global assignments (<<-), but this can sometimes - make the program run slower. -Update 29 Aug 2003: - 1. Fixed "bugs.data.inits" function so you can use data that have the - same names as R functions. - 2. Changed T and F to TRUE and FALSE everywhere in case the variables - T and F are used as data in the main program - 3. Caution: if you are entering the data as a list of variable names - (see 10 Apr 2003 update, item 1), the data to be input into must - be global variables. This can be relevant if you are running bugs() - inside an R function. - 4. Caution: bugs() has difficulty processing ragged arrays. It is - better to save a whole matrix (e.g., "theta") rather than parts - (e.g., "theta[1:10,1]", "theta[1:5,2]"). If you want to save - part of a vector, you should do it as "theta[1:2]", not "theta[1]", - "theta[2]". -Update 30 Apr 2003: added time monitoring -Update 29 Apr 2003: - 1. The "attach.all" function (no longer called "attach2") overwrites - so that all components of a list or data frame get attached. - 2. Program now looks in the directory /winbug~1/ rather than /winbug~2/ - 3. Graphics parameters for margins are returned to their original state - at the end of the program. - 4. Added "digits.summary" option to the numerical display. - 5. Added "last.values" output: a list that can be input as "inits" - if you want to run the simulations longer. -Update 13 Apr 2003: fixed new bug in round.bugs(). Now all numbers are - saved in scientific notation. -Update 10 Apr 2003: - 1. It is now possible to enter the data as a list of variable names. - For example, before you had to enter data as, - data <- list (n=8, y=c(28,8,-3,7,-1,1,18,12)) - or - n <- 8 - y <- c(28,8,-3,7,-1,1,18,12) - data <- list (n=n, y=y) - Now you can enter the data as, - n <- 8 - y <- c(28,8,-3,7,-1,1,18,12) - data <- list ("n", "y") - The bugs() function will figure out which method you are using (based - on whether "data" is a list of numbers or a vector of character - strings). - This doesn't look like much, but it's convenient when you're entering - lots of data! - 2. It is now possible to enter the initial values as a function, - so as to automatically a random list of inits for each of the chains. - For example, in the 8-schools example below, we can do: - inits <- function() - list (theta=rnorm(J,0,1), mu.theta=rnorm(1,0,100), sigma.theta=runif(1,0,100)) - - to set up the inits as a function (rather than setting up n.chains - sets of specific initial values). Then, the function call, - schools.sim <- bugs (data, inits, parameters, "schools.txt", n.chains=3, n.iter=1000) - automatically sets up 3 sets of initial values (each a list of - theta, mu.theta, sigma.theta). - 3. Bug in the initial rounding (the function round.bugs()) has been fixed. - Thanks for Mark Clements for finding the bug and fixing it! - Also, we have set the default rounding to 5 digits instead of 2. -Update 01 Apr 2003: use layout() rather than split.screen() for graphical - display -Update 18 Mar 2003: - 1. Get the Bugs configuration information from the original file - (Registry_default.odc) rather than overwriting each time. (Fixes a - bug that occurred when R was interrupted in the middle of a Bugs run.) - 2. Display different colored dots in the right panel of the graphical - display, to show the medians from each chain. -Update 13 Mar 2003: fix minor bug in monitor() -Update 10 Mar 2003: fix bug in pD and DIC calculations -Update 7 Mar 2003: - 1. Fix display.parallel=T option by adding min.width so that very - intervals are still visible. - 2. Compute pD separately for each sequence (which gives much more - reasonable estimates before convergence). -Update 8 Feb 2003: minor fixes in graphical display -Update 6 Feb 2003: - 1. Approximate "effective sample size" n.eff given for each parameter. - 2. More explanatory material displayed. - 3. Use bringToTop() to automatically bring up the graphics window. -Update 4 Feb 2003: - 1. Automatically compute the deviance, DIC, and pD. Bugs will not - always compute DIC and pD, so we do so using the definition, - DIC = E(deviance) + pD, using var(deviance)/2 as an estimate of pD. - (This is derived from the chi^2 distribution. We can't use the - Spiegelhalter et al. definition of DIC because we don't have access - to the deviance function.) - 2. Set default for n.thin so that, after thinning, the total number - saved iterations, n.sims, is approximately 1000. -Update 14 Jan 2003 to run with the new WinBugs1.4. You may see an error - message and need to fix the dos.location assignment in bugs(). -Update 6 Jan 2003: - 1. Fix of bug that occurred with uppercase and lowercase variable names - 2. Set default for n.thin so that no more than about 500 iterations - will be saved from each sequence - 3. New option "display.parallel" added to show 80% inferences from - parallel sequences on the right panel of the graphical display. This - can be useful to understand what is going on when there are - convergence problems. -Update 26 Dec 2002: fix of minwidth in bugs.plot.summary -Update 11 Dec 2002: - 1. Automatic fixing of adaptive phases. Now you no longer need to run - for thousands of iterations when slice or Metropolis sampling is used. - 2. Various minor fixes -Update 10 Dec 2002: - 1. Cool graphical display of convergence and inferences! - 2. New "attach2" function that overwrites so that all components of - the list are attached -Update 29 Nov 2002: - 1. Fix of bug in 24 Nov update. - 2. Fix of bug in 16 Nov update. - 3. Length of chains is now pecified in terms of "n.iter" rather than - "n.keep". -Update 24 Nov 2002: improved treatment of "parameters.to.save". For - example, you can now use "alpha" to indicate an entire array of parameters, - whereas before you had to save "alpha[]" or "alpha[,]" or whatever. -Update 16 Nov 2002: mean, sd, median added to outputs -Update 4 Nov 2002: more error-flagging added -Update 26 Oct 2002: - 1. Parameters saved in order of the "parameters.to.save" vector - (not alphabetical order). - 2. Output saved in both matrix and list form. - 3. With the attach.sims=T setting (which is the default), the simulations - for all the saved parameters are saved as R objects. This is - convenient for later use of the simulations. -Updates to 16 Oct 2002: more error-flagging added, mean/sd added to summary, - fixing scientific notation so Bugs can always read data and inits -Update 21 Sept 2002: "quit=F" option changed to "debug=T" -First version written 18 Sept 2002 by Andrew Gelman, - adapted from the EmBedBugs package by Kenneth Rice Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2007-06-12 09:58:57 UTC (rev 27) +++ trunk/R2WinBUGS/DESCRIPTION 2007-06-12 10:09:18 UTC (rev 28) @@ -1,7 +1,7 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS Date: 2007-05-20 -Version: 2.1-4 +Version: 2.1-5 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> and Uwe Ligges <li...@st...>. Modified: trunk/R2WinBUGS/man/bugs.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.Rd 2007-06-12 09:58:57 UTC (rev 27) +++ trunk/R2WinBUGS/man/bugs.Rd 2007-06-12 10:09:18 UTC (rev 28) @@ -102,6 +102,12 @@ again. \item If an error message appears, re-run with \code{debug=TRUE}. } + + BUGS version support: + \itemize{ + \item WinBUGS 1.4* (this is the only version that works under Linux) + \item OpenBUGS 2.* (via argument \code{program="OpenBUGS"}) + } } \value{ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gg...@us...> - 2007-08-23 07:44:09
|
Revision: 37 http://bugs-r.svn.sourceforge.net/bugs-r/?rev=37&view=rev Author: ggorjan Date: 2007-08-23 00:44:10 -0700 (Thu, 23 Aug 2007) Log Message: ----------- now default argument values should also work for Linux side using WINE + some code cleanup; hold your breath plase until I test this on Windows machine Modified Paths: -------------- trunk/R2WinBUGS/Changes trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/R/bugs.R trunk/R2WinBUGS/R/bugs.run.R trunk/R2WinBUGS/R/bugs.script.R trunk/R2WinBUGS/R/wineutils.R trunk/R2WinBUGS/man/bugs.Rd trunk/R2WinBUGS/man/bugs.run.Rd trunk/R2WinBUGS/man/bugs.script.Rd Modified: trunk/R2WinBUGS/Changes =================================================================== --- trunk/R2WinBUGS/Changes 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/Changes 2007-08-23 07:44:10 UTC (rev 37) @@ -1,6 +1,13 @@ Changes to R2WinBUGS: ===================== +Update 2.1-6 (23 August 2007): +- reverting back to use thin.updater since it is faster, however CODA files + have "wrong" indexes +- WINE arguments are now set in such a way that defaults work on Windows + and Linux +- minor formatting in the code and help files + Update 2.1-5 (12 June 2007): - proper indexing of CODA files also in R version - fixed useWINE documentation in bugs help page Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/DESCRIPTION 2007-08-23 07:44:10 UTC (rev 37) @@ -1,7 +1,7 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS Date: 2007-05-20 -Version: 2.1-5 +Version: 2.1-6 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> and Uwe Ligges <li...@st...>. Modified: trunk/R2WinBUGS/R/bugs.R =================================================================== --- trunk/R2WinBUGS/R/bugs.R 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/R/bugs.R 2007-08-23 07:44:10 UTC (rev 37) @@ -7,38 +7,30 @@ bugs.directory="c:/Program Files/WinBUGS14/", program=c("WinBUGS", "OpenBUGS", "winbugs", "openbugs"), working.directory=NULL, - clearWD=FALSE, useWINE=.Platform$OS.type != "windows", WINE=Sys.getenv("WINE"), - newWINE=FALSE, WINEPATH=NULL) + clearWD=FALSE, useWINE=.Platform$OS.type != "windows", WINE=NULL, + newWINE=TRUE, WINEPATH=NULL) { program <- match.arg(program) if(program %in% c("openbugs", "OpenBUGS", "OpenBugs")) { - if(is.R()) { - ## If OpenBUGS, we only call openbugs() and exit... - return(openbugs(data, inits, parameters.to.save, model.file, - n.chains, n.iter, n.burnin, n.thin, DIC=DIC, - bugs.directory, working.directory, digits)) - } else { - stop ("OpenBUGS is not yet available in S-PLUS") - } + if(!is.R()) stop("OpenBUGS is not yet available in S-PLUS") + ## If OpenBUGS, we only call openbugs() and exit... + return(openbugs(data, inits, parameters.to.save, model.file, + n.chains, n.iter, n.burnin, n.thin, DIC=DIC, + bugs.directory, working.directory, digits)) } ## 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") + + ## Wine if(useWINE) { if(!is.R()) - stop ("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") - ## attempt to find wine and winepath - if(!nchar(WINE)) { - WINE <- system("locate wine | grep bin/wine$", intern=TRUE) - WINE <- WINE[length(WINE)] - } - if(!length(WINE)) stop("couldn't locate WINE binary") - if(!nchar(WINEPATH)) { - WINEPATH <- system("locate winepath | grep bin/winepath$", intern=TRUE) - WINEPATH <- WINEPATH[length(WINEPATH)] - } - if(!length(WINEPATH)) stop("couldn't locate WINEPATH binary") + stop("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") + ## Attempt to find wine and winepath + if(is.null(WINE)) WINE <- findUnixBinary(x="wine") + if(is.null(WINEPATH)) WINEPATH <- findUnixBinary(x="winepath") } + if(!is.null(working.directory)) { savedWD <- getwd() setwd(working.directory) @@ -50,8 +42,9 @@ 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.") + } 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" @@ -67,7 +60,7 @@ bin=bin, DIC=DIC, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) bugs.run(n.burnin, bugs.directory, WINE=WINE, useWINE=useWINE, - WINEPATH=WINEPATH) + newWINE=newWINE, WINEPATH=WINEPATH) if(codaPkg) return(file.path(getwd(), paste("coda", 1:n.chains, ".txt", sep=""))) sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, Modified: trunk/R2WinBUGS/R/bugs.run.R =================================================================== --- trunk/R2WinBUGS/R/bugs.run.R 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/R/bugs.run.R 2007-08-23 07:44:10 UTC (rev 37) @@ -1,51 +1,55 @@ "bugs.run" <- - function(n.burnin, bugs.directory, WINE = "", - useWINE = .Platform$OS.type != "windows", - newWINE = TRUE, WINEPATH = NULL){ - -if(useWINE && !is.R()) - stop ("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") -if(useWINE && !newWINE) bugs.directory <- win2native(bugs.directory) + function(n.burnin, bugs.directory, + useWINE=.Platform$OS.type != "windows", WINE=NULL, + newWINE=TRUE, WINEPATH=NULL) +{ -## Update the lengths of the adaptive phases in the Bugs updaters + if(useWINE && !is.R()) + stop("Non-Windows platforms not yet supported in R2WinBUGS for S-PLUS") + + ## Is bugs.directory defined in Windows (where second character is : + ## i.e. C:\Program...) or Unix style path? + test <- substr(bugs.directory, start=2, stop=2) == ":" + if(useWINE && test) { + bugs.directory <- win2native(bugs.directory, newWINE=newWINE, WINEPATH=WINEPATH) + } + + ## Update the lengths of the adaptive phases in the Bugs updaters try(bugs.update.settings(n.burnin, bugs.directory)) -## Return the lengths of the adaptive phases to their original settings - if (is.R()){ - on.exit(try(file.copy(file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), - file.path(bugs.directory, "System/Rsrc/Registry.odc"), - overwrite = TRUE))) - } else { - on.exit(try(splus.file.copy(file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), - file.path(bugs.directory, "System/Rsrc/Registry.odc"), - overwrite = TRUE))) - } - -## Search Win*.exe (WinBUGS executable) within bugs.directory - dos.location <- file.path(bugs.directory, - grep("^Win[[:alnum:]]*[.]exe$", list.files(bugs.directory), value = TRUE)[1]) - if(!file.exists(dos.location)) + + ## Return the lengths of the adaptive phases to their original settings + if(is.R()) { + .fileCopy <- file.copy + } else { + .fileCopy <- splus.file.copy + } + on.exit(try(.fileCopy(file.path(bugs.directory, "System/Rsrc/Registry_Rsave.odc"), + file.path(bugs.directory, "System/Rsrc/Registry.odc"), + overwrite=TRUE))) + + ## Search Win*.exe (WinBUGS executable) within bugs.directory + dos.location <- file.path(bugs.directory, + grep("^Win[[:alnum:]]*[.]exe$", + list.files(bugs.directory), value=TRUE)[1]) + if(!file.exists(dos.location)) stop(paste("WinBUGS executable does not exist in", bugs.directory)) -## Call Bugs and have it run with script.txt + + ## Call Bugs and have it run with script.txt bugsCall <- paste("\"", dos.location, "\" /par \"", - native2win(file.path(getwd(), "script.txt"), newWINE = newWINE, WINEPATH = WINEPATH), - "\"", sep = "") - if (useWINE) - bugsCall <- paste(WINE, bugsCall) + native2win(file.path(getwd(), "script.txt"), + useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH), + "\"", sep="") + if(useWINE) bugsCall <- paste(WINE, bugsCall) temp <- system(bugsCall) + if(temp == -1) + stop("Error in bugs.run().\nCheck that WinBUGS is in the specified directory.") - if(temp == -1) - stop("Error in bugs.run().\nCheck that WinBUGS is in the specified directory.") -## Stop and print an error message if Bugs did not run correctly + ## Stop and print an error message if Bugs did not run correctly if(is.R()) { - if(length(grep("Bugs did not run correctly", - scan("coda1.txt", character(), quiet=TRUE, sep="\n"))) > 0) - stop("Look at the log file and\ntry again with debug=TRUE and figure out what went wrong within Bugs.") + tmp <- scan("coda1.txt", character(), quiet=TRUE, sep="\n") } else { - if (length(grep("Bugs did not run correctly", - scan("coda1.txt", character(), sep="\n"))) > 0) - stop("Look at the log file and\ntry again with debug=TRUE and figure out what went wrong within Bugs.") + tmp <- scan("coda1.txt", character(), sep="\n") } + if(length(grep("Bugs did not run correctly", tmp)) > 0) + stop("Look at the log file and\ntry again with 'debug=TRUE' to figure out what went wrong within Bugs.") } - - - Modified: trunk/R2WinBUGS/R/bugs.script.R =================================================================== --- trunk/R2WinBUGS/R/bugs.script.R 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/R/bugs.script.R 2007-08-23 07:44:10 UTC (rev 37) @@ -1,50 +1,65 @@ "bugs.script" <- -function (parameters.to.save, n.chains, n.iter, n.burnin, - n.thin, model.file, debug=FALSE, is.inits, bin, - DIC = FALSE, useWINE = FALSE, newWINE = FALSE, WINEPATH = NULL){ -### Write file script.txt for Bugs to read + function(parameters.to.save, n.chains, n.iter, n.burnin, + n.thin, model.file, debug=FALSE, is.inits, bin, + DIC=FALSE, useWINE=.Platform$OS.type != "windows", + newWINE=TRUE, WINEPATH=NULL) +{ + ## Write file script.txt for Bugs if((ceiling(n.iter/n.thin) - ceiling(n.burnin/n.thin)) < 2) stop ("(n.iter-n.burnin)/n.thin must be at least 2") working.directory <- getwd() script <- "script.txt" - model <- if(length(grep("\\\\", model.file)) || length(grep("/", model.file))){ - model.file - } else file.path(working.directory, model.file) + + test <- length(grep("\\\\", model.file)) || length(grep("/", model.file)) + model <- ifelse(test, model.file, file.path(working.directory, model.file)) + model <- native2win(model, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) + data <- file.path(working.directory, "data.txt") + data <- native2win(data, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) + coda <- file.path(working.directory, "coda") - logfile <- file.path(working.directory, "log.odc") - logfileTxt <- file.path(working.directory, "log.txt") - inits <- sapply(paste(working.directory, "/inits", 1:n.chains, ".txt", sep=""), - function(x) native2win(x, , WINEPATH = WINEPATH)) + coda <- native2win(coda, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) + + logFile <- file.path(working.directory, "log.odc") + logFile <- native2win(logFile, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) + logFileTxt <- file.path(working.directory, "log.txt") + logFileTxt <- native2win(logFileTxt, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) + + inits <- paste(working.directory, "/inits", 1:n.chains, ".txt", sep="") + inits <- sapply(inits, function(x) native2win(x, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH)) + initlist <- paste("inits (", 1:n.chains, ", '", inits, "')\n", sep="") + savelist <- paste("set (", parameters.to.save, ")\n", sep="") redo <- ceiling((n.iter-n.burnin)/(n.thin*bin)) - thinUpdateCommand <- paste("thin.updater (", n.thin, ")\n", - "update (", ceiling(n.burnin/n.thin), ")\n", sep = "") + thinUpdate <- paste("thin.updater (", n.thin, ")\n", + "update (", ceiling(n.burnin/n.thin), ")\n", sep="") cat( "display ('log')\n", - "check ('", native2win(model, WINEPATH=WINEPATH), "')\n", - "data ('", native2win(data, WINEPATH=WINEPATH), "')\n", + "check ('", model, "')\n", + "data ('", data, "')\n", "compile (", n.chains, ")\n", if(is.inits) initlist, "gen.inits()\n", - thinUpdateCommand, + thinUpdate, savelist, if(DIC) "dic.set()\n", rep( - c("update (", formatC(ceiling(bin), format = "d"), ")\n", - "coda (*, '", native2win(coda, WINEPATH=WINEPATH), "')\n"),redo), + c("update (", formatC(ceiling(bin), format="d"), ")\n", + "coda (*, '", coda, "')\n"),redo), "stats (*)\n", if(DIC) "dic.stats()\n", "history (*)\n", - "save ('", native2win(logfile, WINEPATH=WINEPATH), "')\n", - "save ('", native2win(logfileTxt, WINEPATH=WINEPATH), "')\n", + "save ('", logFile, "')\n", + "save ('", logFileTxt, "')\n", file=script, sep="", append=FALSE) - if (!debug) cat ("quit ()\n", file=script, append=TRUE) - sims.files <- paste ("coda", 1:n.chains, ".txt", sep="") - for (i in 1:n.chains) cat ("WinBUGS did not run correctly.\n", - file=sims.files[i], append=FALSE) + + if(!debug) cat("quit ()\n", file=script, append=TRUE) + + sims.files <- paste("coda", 1:n.chains, ".txt", sep="") + for(i in 1:n.chains) + cat("WinBUGS did not run correctly.\n", file=sims.files[i], append=FALSE) } Modified: trunk/R2WinBUGS/R/wineutils.R =================================================================== --- trunk/R2WinBUGS/R/wineutils.R 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/R/wineutils.R 2007-08-23 07:44:10 UTC (rev 37) @@ -1,65 +1,110 @@ -## from Jun Yan's rbugs package, extended +findUnixBinary <- function(x) +{ + ## --- Environmental variable --- + tmp <- Sys.getenv(toupper(x)) + if(nchar(tmp) != 0) return(tmp) + ## else -if (is.R()){ + ## --- Standard place --- + tmp <- paste("/usr/bin", x, sep="") + if(file.exists(tmp)) return(tmp) + ## else ... -## get drive mapping table from ~/.wine/config -winedriveMap <- function(config="~/.wine/config") { - if (!file.exists(config)) return (NULL); - con <- readLines(config) - con <- con[- grep("^;", con)] - drive <- con[grep("^\\[Drive ", con)] - drive <- substr(drive, 8, 8) - drive <- paste(drive, ":", sep="") - path <- con[grep("Path", con)] - len <- length(drive) - path <- path[1:len] - dir <- sapply(path, - function(x) { - foo <- unlist(strsplit(x, "\"")) - foo[length(foo)] - }) - dir <- sub("%HOME%",tools::file_path_as_absolute("~"),dir) - data.frame(drive = I(drive), path = I(dir), row.names=NULL) -} + ## --- Which --- + tmp <- system(paste("which ", x, sep=""), intern=TRUE) + if(length(tmp) != 0) return(tmp) + ## else .. -## translate windows dir to native dir -winedriveTr <- function(windir, DriveTable=winedriveMap()) { - win.dr <- substr(windir, 1, 2) - ind <- pmatch(toupper(win.dr), DriveTable$drive) - native.dr <- DriveTable$path[ind] - sub(win.dr, native.dr, windir) + ## --- Locate --- + tmp <- system(paste("locate ", x, " | grep bin/", x, "$", sep=""), intern=TRUE) + tmp <- tmp[length(tmp)] ## keep only last hit + if(length(tmp) > 0) return(tmp) + + stop(paste("couldn't find", x, "binary file")) } -## translate full Unix path to Wine path -winedriveRTr <- function(unixpath, DriveTable=winedriveMap()) { - blocks <- strsplit(unixpath,"/")[[1]] - cblocks <- c("/",sapply(1+seq(along=blocks[-1]), - function(n) paste(blocks[1:n],collapse="/"))) - path <- match(cblocks,DriveTable$path) - if (any(!is.na(path))) { - unixdir <- cblocks[which.min(path)] - windrive <- paste(DriveTable$drive[min(path,na.rm=TRUE)],"/",sep="") - winpath <- sub("//","/",sub(unixdir,windrive,unixpath)) ## kluge - } else stop("can't find equivalent Windows path: file may be inaccessible") - winpath +native2win <- function(x, useWINE=.Platform$OS.type != "windows", + newWINE=TRUE, WINEPATH=NULL) +{ + ## Translate Unix path to Windows (wine) path + if(useWINE) { + if(newWINE) { + if(is.null(WINEPATH)) WINEPATH <- findUnixBinary(x="winepath") + x <- system(paste(WINEPATH, "-w", x), intern=TRUE) + gsub("\\\\", "/", x) ## under wine BUGS cannot use \ or \\ + } else { + winedriveRTr(x) + } + } else { + x + } } +## TODO: why are we masking these functions in S-PLUS -win2native <- function(x, useWINE=.Platform$OS.type != "windows") { # win -> native - if (useWINE) winedriveTr(x) - else x -} +if(is.R()) { -} # end if (is.R()) + win2native <- function(x, useWINE=.Platform$OS.type != "windows", + newWINE=TRUE, WINEPATH=NULL) + { + ## Translate Windows path to native (unix) path + if(useWINE) { + if(newWINE) { + if(is.null(WINEPATH)) WINEPATH <- findUnixBinary(x="winepath") + system(paste(WINEPATH, " \"", x, "\"", sep=""), intern=TRUE) + } else { + winedriveTr(x) + } + } else { + x + } + } -native2win <- function(x, useWINE=.Platform$OS.type != "windows", newWINE=TRUE, WINEPATH=NULL) { # native -> win - if(is.R()){ - if (useWINE && !newWINE) return(winedriveRTr(x)) - if (useWINE && newWINE) { - x <- system(paste(WINEPATH, "-w", x), intern = TRUE) - return(gsub("\\\\", "/", x)) ## under wine BUGS cannot use \ or \\ - } else x - } else { #S-PLUS - gsub("\\\\", "/", x) + winedriveMap <- function(config="~/.wine/config") + { + ## Get drive mapping table from ~/.wine/config + if(!file.exists(config)) return(NULL); + con <- readLines(config) + con <- con[- grep("^;", con)] + drive <- con[grep("^\\[Drive ", con)] + drive <- substr(drive, 8, 8) + drive <- paste(drive, ":", sep="") + path <- con[grep("Path", con)] + len <- length(drive) + path <- path[1:len] + dir <- sapply(path, + function(x) { + foo <- unlist(strsplit(x, "\"")) + foo[length(foo)] + }) + dir <- sub("%HOME%",tools::file_path_as_absolute("~"),dir) + data.frame(drive=I(drive), path=I(dir), row.names=NULL) } -} + + winedriveTr <- function(windir, DriveTable=winedriveMap()) + { + ## Translate Windows path to native (Unix) path + win.dr <- substr(windir, 1, 2) + ind <- pmatch(toupper(win.dr), DriveTable$drive) + native.dr <- DriveTable$path[ind] + sub(win.dr, native.dr, windir) + } + + winedriveRTr <- function(unixpath, DriveTable=winedriveMap()) + { + ## Translate Unix path to Windows (wine) path + blocks <- strsplit(unixpath,"/")[[1]] + cblocks <- c("/",sapply(1+seq(along=blocks[-1]), + function(n) paste(blocks[1:n],collapse="/"))) + path <- match(cblocks,DriveTable$path) + if(any(!is.na(path))) { + unixdir <- cblocks[which.min(path)] + windrive <- paste(DriveTable$drive[min(path,na.rm=TRUE)],"/",sep="") + winpath <- sub("//","/",sub(unixdir,windrive,unixpath)) ## kludge + } else { + stop("can't find equivalent Windows path: file may be inaccessible") + } + winpath + } + +} # end of if(is.R()) Modified: trunk/R2WinBUGS/man/bugs.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.Rd 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/man/bugs.Rd 2007-08-23 07:44:10 UTC (rev 37) @@ -3,8 +3,8 @@ \title{Run WinBUGS and OpenBUGS from R or S-PLUS} \description{The \code{bugs} function takes data and starting values as - input. It automatically writes a WinBUGS script, calls the model, and - saves the simulations for easy access in R or S-PLUS.} + input. It automatically writes a \pkg{WinBUGS} script, calls the model, and + saves the simulations for easy access in \R or S-PLUS.} \usage{ bugs(data, inits, parameters.to.save, model.file="model.bug", @@ -15,25 +15,25 @@ bugs.directory="c:/Program Files/WinBUGS14/", program=c("WinBUGS", "OpenBUGS", "winbugs", "openbugs"), working.directory=NULL, clearWD=FALSE, - useWINE=.Platform$OS.type != "windows", WINE=Sys.getenv("WINE"), - newWINE=FALSE, WINEPATH=NULL) + useWINE=.Platform$OS.type != "windows", WINE=NULL, + newWINE=TRUE, WINEPATH=NULL) } \arguments{ \item{data}{either a named list (names corresponding to variable names - in the \code{model.file}) of the data for the WinBUGS model, + in the \code{model.file}) of the data for the \pkg{WinBUGS} model, \emph{or} a vector or list of the names of the data objects used by the model. If \code{data="data.txt"}, it is assumed that data have already been written to the working directory in a file called \file{data.txt}, e.g. by the function \code{\link{bugs.data}}.} \item{inits}{a list with \code{n.chains} elements; each element of the - list is itself a list of starting values for the WinBUGS model, + list is itself a list of starting values for the \pkg{WinBUGS} model, \emph{or} a function creating (possibly random) initial values. Alternatively, if \code{inits=NULL}, initial values are generated - by WinBUGS.} + by \pkg{WinBUGS}.} \item{parameters.to.save}{character vector of the names of the parameters to save which should be monitored} - \item{model.file}{file containing the model written in WinBUGS code. + \item{model.file}{file containing the model written in \pkg{WinBUGS} code. The extension can be either \file{.bug} or \file{.txt}. If the extension is \file{.bug} and \code{program=="WinBUGS"}, a copy of the file with extension \file{.txt} will be created @@ -53,69 +53,76 @@ \item{bin}{number of iterations between saving of results (i.e. the coda files are saved after each \code{bin} iterations); default is to save only at the end.} - \item{debug}{if \code{FALSE} (default), WinBUGS is closed automatically - when the script has finished running, otherwise WinBUGS remains open + \item{debug}{if \code{FALSE} (default), \pkg{WinBUGS} is closed automatically + when the script has finished running, otherwise \pkg{WinBUGS} remains open for further investigation} \item{DIC}{logical; if \code{TRUE} (default), compute deviance, pD, - and DIC. This is done in WinBUGS directly using the rule \code{pD = + and DIC. This is done in \pkg{WinBUGS} directly using the rule \code{pD = Dbar - Dhat}. If there are less iterations than required for the adaptive phase, the rule \code{pD=var(deviance) / 2} is used.} - \item{digits}{number of significant digits used for WinBUGS input, see + \item{digits}{number of significant digits used for \pkg{WinBUGS} input, see \code{\link{formatC}}} \item{codaPkg}{logical; if \code{FALSE} (default) a \code{bugs} object - is returned, if \code{TRUE} file names of WinBUGS output are + is returned, if \code{TRUE} file names of \pkg{WinBUGS} output are returned for easy access by the \pkg{coda} package through function \code{\link{read.bugs}} (not used if \code{program="OpenBUGS"}).} - \item{bugs.directory}{directory that contains the WinBUGS executable} + \item{bugs.directory}{directory that contains the \pkg{WinBUGS} executable} \item{program}{the program to use, either \code{winbugs}/\code{WinBUGS} or \code{openbugs}/\code{OpenBUGS}, the latter makes use of function \code{\link{openbugs}} and requires the CRAN package \pkg{BRugs}. The \code{openbugs}/\code{OpenBUGS} choice is not available in S-PLUS.} \item{working.directory}{sets working directory during execution of - this function; WinBUGS' in- and output will be stored in this + this function; \pkg{WinBUGS}' in- and output will be stored in this directory; if \code{NULL}, the current working directory is chosen.} \item{clearWD}{logical; indicating whether the files \file{data.txt}, \file{inits[1:n.chains].txt}, \file{log.odc}, \file{codaIndex.txt}, - and \file{coda[1:nchains].txt} should be removed after WinBUGS has + and \file{coda[1:nchains].txt} should be removed after \pkg{WinBUGS} has finished. If set to \code{TRUE}, this argument is only respected if \code{codaPkg=FALSE}.} + \item{useWINE}{logical; attempt to use the WINE emulator to run - WinBUGS, defaults to \code{FALSE} on Windows, and \code{TRUE} - otherwise. If WINE is used, the arguments \code{bugs.directory} and - \code{working.directory} must be given in form of Linux paths rather - than Windows paths (if not \code{NULL}). The \code{useWINE=TRUE} - option is not available in S-PLUS.} - \item{WINE}{character; name of WINE binary file} - \item{newWINE}{Set this one to \code{TRUE} for new versions of WINE.} - \item{WINEPATH}{Path to WINE binary file, it is tried hard to get the - information automatically if not given.} + \pkg{WinBUGS}, defaults to \code{FALSE} on Windows, and \code{TRUE} + otherwise. Not available in S-PLUS.} + \item{WINE}{character, path to \file{wine} binary file, it is + tried hard to get the information automatically if not given.} + \item{newWINE}{Use new versions of WINE that have \file{winepath} + utility} + \item{WINEPATH}{character, path to \file{winepath} binary file, it is + tried hard to get the information automatically if not given.} } \details{ To run: \enumerate{ - \item Write a WinBUGS model in a ASCII file. - \item Go into R / S-PLUS. - \item Prepare the inputs to the \code{bugs} function and run it (see - Example). - \item A WinBUGS window will pop up and R / S-PLUS will freeze - up. The model will now run in WinBUGS. It might take awhile. You - will see things happening in the Log window within WinBUGS. When - WinBUGS is done, its window will close and R / S-PLUS will work + \item Write a \pkg{WinBUGS} model in an ASCII file (hint: use + \code{\link{write.model}}). + \item Go into \R / S-PLUS. + \item Prepare the inputs for the \code{bugs} function and run it (see + Example section). + \item A \pkg{WinBUGS} window will pop up and \R / S-PLUS will freeze + up. The model will now run in \pkg{WinBUGS}. It might take awhile. You + will see things happening in the Log window within \pkg{WinBUGS}. When + \pkg{WinBUGS} is done, its window will close and \R / S-PLUS will work again. \item If an error message appears, re-run with \code{debug=TRUE}. } BUGS version support: \itemize{ - \item WinBUGS 1.4* (this is the only version that works under Linux) - \item OpenBUGS 2.* (via argument \code{program="OpenBUGS"}) + \item \pkg{WinBUGS} 1.4* (this is the only version that works under Linux) + \item \pkg{OpenBUGS} 2.* (via argument \code{program="OpenBUGS"}) } + + If \code{useWINE=TRUE} is used, all paths (such as + \code{working.directory} and \code{model.file}, must be given in + native (Unix) style, but \code{working.directory} can be given as + native(Unix) or Windows path style. This is done to achieve greatest + portability with default argument value for \code{working.directory}. } \value{ If \code{codaPkg=TRUE} the returned values are the names - of coda output files written by WinBUGS containing + of coda output files written by \pkg{WinBUGS} containing the Markov Chain Monte Carlo output in the CODA format. This is useful for direct access with \code{\link{read.bugs}}. @@ -154,7 +161,7 @@ \item{indexes.short}{indexes of \code{root.short}} \item{last.values}{list of simulations from the most recent iteration; they can be used as starting points if you wish to run - WinBUGS for further iterations} + \pkg{WinBUGS} for further iterations} \item{pD}{an estimate of the effective number of parameters, for calculations see the section \dQuote{Arguments}.} \item{DIC}{\code{mean(deviance) + pD}} Modified: trunk/R2WinBUGS/man/bugs.run.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.run.Rd 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/man/bugs.run.Rd 2007-08-23 07:44:10 UTC (rev 37) @@ -2,30 +2,27 @@ \alias{bugs.run} \title{Calling WinBUGS} -\description{Updates the lengths of the adaptive phases in the WinBUGS - registry (using \code{\link{bugs.update.settings}}), calls WinBUGS and - runs it with \file{script.txt} - intended for internal use} +\description{Updates the lengths of the adaptive phases in the + \pkg{WinBUGS} registry (using \code{\link{bugs.update.settings}}), + calls \pkg{WinBUGS} and runs it with \file{script.txt} - intended for + internal use} \usage{ -bugs.run(n.burnin, bugs.directory, WINE = "", - useWINE = .Platform$OS.type != "windows", - newWINE = TRUE, WINEPATH = NULL) +bugs.run(n.burnin, bugs.directory, + useWINE=.Platform$OS.type != "windows", WINE=NULL, + newWINE=TRUE, WINEPATH=NULL) } \arguments{ \item{n.burnin}{length of burn in} - \item{bugs.directory}{directory that contains the WinBUGS executable} - \item{WINE}{name of WINE binary, for Windows emulation} - \item{useWINE}{logical; attempt to use the WINE emulator to run WinBUGS, - defaults to \code{TRUE} on Windows, and \code{FALSE} otherwise. - The \code{useWINE = TRUE} option is not available in S-PLUS.} - \item{newWINE}{set this one to \code{TRUE} for new versions of WINE.} - \item{WINEPATH}{Path the WINE, \code{bugs} tries hard to get the - information automatically and pass it to this function, if not - given.} + \item{bugs.directory}{directory that contains the \pkg{WinBUGS} executable} + \item{useWINE}{as in \code{\link{bugs}} meta function} + \item{WINE}{as in \code{\link{bugs}} meta function} + \item{newWINE}{as in \code{\link{bugs}} meta function} + \item{WINEPATH}{as in \code{\link{bugs}} meta function} } \value{Nothing, but has side effects as documented in - \code{\link{bugs.update.settings}} and calls WinBUGS.} + \code{\link{bugs.update.settings}} and calls \pkg{WinBUGS}.} \seealso{The main function to be called by the user is \code{\link{bugs}}.} \keyword{internal} Modified: trunk/R2WinBUGS/man/bugs.script.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.script.Rd 2007-08-08 11:31:43 UTC (rev 36) +++ trunk/R2WinBUGS/man/bugs.script.Rd 2007-08-23 07:44:10 UTC (rev 37) @@ -3,13 +3,13 @@ \title{Writes script for running WinBUGS} -\description{Write file \file{script.txt} for WinBUGS to read - intended - for internal use} +\description{Write file \file{script.txt} for \pkg{WinBUGS} to read - + intended for internal use} \usage{ bugs.script(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, - model.file, debug = FALSE, is.inits, bin, DIC = FALSE, - useWINE = FALSE, newWINE = FALSE, WINEPATH = NULL) + model.file, debug=FALSE, is.inits, bin, DIC=FALSE, + useWINE=.Platform$OS.type != "windows", newWINE=TRUE, WINEPATH=NULL) } \arguments{ \item{parameters.to.save}{parameters that should be monitored} @@ -17,20 +17,17 @@ \item{n.iter}{number of total iterations (including burn in)} \item{n.burnin}{length of burn in} \item{n.thin}{size of thinning parameter} - \item{model.file}{file containing the model written in WinBUGS code} - \item{debug}{if \code{FALSE}, WinBUGS is closed automatically, - otherwise WinBUGS remains open for further investigation} + \item{model.file}{file containing the model written in \pkg{WinBUGS} code} + \item{debug}{if \code{FALSE}, \pkg{WinBUGS} is closed automatically, + otherwise \pkg{WinBUGS} remains open for further investigation} \item{is.inits}{logical; whether initial values are given by the user - (\code{TRUE}) or have to be generated by WinBUGS} + (\code{TRUE}) or have to be generated by \pkg{WinBUGS}} \item{bin}{number of iterations between saving of results} \item{DIC}{logical; if \code{TRUE}, compute deviance, pD, and DIC - automatically in WinBUGS} - \item{useWINE}{logical; use WINE to run WinBUGS under Linux. - The \code{useWINE = TRUE} option is not available in S-PLUS.} - \item{newWINE}{set this one to \code{TRUE} for new versions of WINE.} - \item{WINEPATH}{Path the WINE, \code{bugs} tries hard to get the - information automatically and pass it to this function, if not - given.} + automatically in \pkg{WinBUGS}} + \item{useWINE}{as in \code{\link{bugs}} meta function} + \item{newWINE}{as in \code{\link{bugs}} meta function} + \item{WINEPATH}{as in \code{\link{bugs}} meta function} } \value{Nothing, but as a side effect, the script file \file{script.txt} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <li...@us...> - 2007-09-17 09:18:10
|
Revision: 65 http://bugs-r.svn.sourceforge.net/bugs-r/?rev=65&view=rev Author: ligges Date: 2007-09-17 02:18:14 -0700 (Mon, 17 Sep 2007) Log Message: ----------- as for BRugs: allow model.file to be a function containing a BUGS model that is written to a temporary model file Modified Paths: -------------- trunk/R2WinBUGS/R/bugs.R trunk/R2WinBUGS/man/bugs.Rd Modified: trunk/R2WinBUGS/R/bugs.R =================================================================== --- trunk/R2WinBUGS/R/bugs.R 2007-09-17 09:08:38 UTC (rev 64) +++ trunk/R2WinBUGS/R/bugs.R 2007-09-17 09:18:14 UTC (rev 65) @@ -36,6 +36,17 @@ setwd(working.directory) on.exit(setwd(savedWD)) } + if(is.function(model.file)){ + temp <- + if(is.R()){ + paste(tempfile("model"), "txt", sep=".") + } else { + gsub(".tmp$", ".txt", tempfile("model")) + } + write.model(model.file, con = temp) + model.file <- gsub("\\\\", "/", temp) + if(!is.R()) on.exit(file.remove(model.file), add = TRUE) + } if(!file.exists(model.file)) stop(paste(model.file, "does not exist.")) if(file.info(model.file)$isdir) Modified: trunk/R2WinBUGS/man/bugs.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.Rd 2007-09-17 09:08:38 UTC (rev 64) +++ trunk/R2WinBUGS/man/bugs.Rd 2007-09-17 09:18:14 UTC (rev 65) @@ -38,7 +38,9 @@ If the extension is \file{.bug} and \code{program=="WinBUGS"}, a copy of the file with extension \file{.txt} will be created in the \code{bugs()} call and removed afterwards. Note that - similarly named \file{.txt} files will be overwritten.} + similarly named \file{.txt} files will be overwritten. + Alternatively, \code{model.file} can be an R function that contains a BUGS model that is written to a + temporary model file (see \code{\link{tempfile}}) using \code{\link{write.model}}.} \item{n.chains}{number of Markov chains (default: 3)} \item{n.iter}{number of total iterations per chain (including burn in; default: 2000)} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <li...@us...> - 2008-01-06 18:17:53
|
Revision: 77 http://bugs-r.svn.sourceforge.net/bugs-r/?rev=77&view=rev Author: ligges Date: 2008-01-06 10:17:57 -0800 (Sun, 06 Jan 2008) Log Message: ----------- Some major changes contributed by Jouni Kerman, citing from his message the relevant changes: (In function bugs:) - Add bugs.seed parameter and the corresponding bugs script entry to specify random seed in WinBUGS - Add summary.only parameter (default FALSE) which prevents outputting the coda and only returns the parameter summary - Add save.history parameter (default TRUE) which controls whether the trace plots are generated at end - Add n.sims parameter (number of simulations to keep). I often specify this and n.iter only. - Fix definition of n.thin which assumes output of 1000 simulations (divide by n.sims instead) - If the global option 'R2WinBUGS.bugs.directory' exists, bugs uses it for the R2WinBUGS directory - Allow specification of data file that can be named something else than data.txt - Allow specification of inits file names I have also rewritten the bugs.log routine so it does not crash if DIC is not specified; it also does not make assumptions about the column names. Modified Paths: -------------- trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/R/bugs.R trunk/R2WinBUGS/R/bugs.data.R trunk/R2WinBUGS/R/bugs.inits.R trunk/R2WinBUGS/R/bugs.log.R trunk/R2WinBUGS/R/bugs.script.R trunk/R2WinBUGS/R/openbugs.R trunk/R2WinBUGS/man/bugs.Rd trunk/R2WinBUGS/man/bugs.data.Rd trunk/R2WinBUGS/man/bugs.inits.Rd trunk/R2WinBUGS/man/bugs.log.Rd trunk/R2WinBUGS/man/bugs.script.Rd trunk/R2WinBUGS/man/openbugs.Rd Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/DESCRIPTION 2008-01-06 18:17:57 UTC (rev 77) @@ -1,6 +1,6 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS -Date: 2007-09-25 +Date: 2008-01-06 Version: 2.1-7 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> Modified: trunk/R2WinBUGS/R/bugs.R =================================================================== --- trunk/R2WinBUGS/R/bugs.R 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/R/bugs.R 2008-01-06 18:17:57 UTC (rev 77) @@ -1,21 +1,26 @@ "bugs" <- function(data, inits, parameters.to.save, model.file="model.bug", n.chains=3, n.iter=2000, n.burnin=floor(n.iter / 2), - n.thin=max(1, floor(n.chains * (n.iter - n.burnin) / 1000)), + n.thin=max(1, floor(n.chains * (n.iter - n.burnin) / n.sims)), n.sims = 1000, bin=(n.iter - n.burnin) / n.thin, debug=FALSE, DIC=TRUE, digits=5, codaPkg=FALSE, bugs.directory="c:/Program Files/WinBUGS14/", program=c("WinBUGS", "OpenBUGS", "winbugs", "openbugs"), working.directory=NULL, clearWD=FALSE, useWINE=.Platform$OS.type != "windows", WINE=NULL, - newWINE=TRUE, WINEPATH=NULL) + newWINE=TRUE, WINEPATH=NULL, bugs.seed=NULL, summary.only=FALSE, + save.history=!summary.only) { program <- match.arg(program) + if (missing(bugs.directory) && + !is.null(bugs.dir <- getOption("R2WinBUGS.bugs.directory"))) { # requested by Jouni Kerman + bugs.directory <- bugs.dir + } if(program %in% c("openbugs", "OpenBUGS", "OpenBugs")) { if(!is.R()) stop("OpenBUGS is not yet available in S-PLUS") ## If OpenBUGS, we only call openbugs() and exit... return(openbugs(data, inits, parameters.to.save, model.file, - n.chains, n.iter, n.burnin, n.thin, DIC=DIC, + n.chains, n.iter, n.burnin, n.thin, n.sims, DIC=DIC, bugs.directory, working.directory, digits)) } ## Checking number of inits, which is NOT save here: @@ -52,12 +57,32 @@ 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) + if (!(length(data) == 1 && is.vector(data) && is.character(data) && + (regexpr("\\.txt$", data) > 0))) { + bugs.data.file <- bugs.data(data, dir = getwd(), digits) } else { - if(!file.exists(data)) stop("File data.txt does not exist.") + if(!file.exists(data)) + stop("File", data, "does not exist.") + bugs.data.file <- data } - bugs.inits(inits, n.chains, digits) + + + if (is.character(inits)) { + if (!all(file.exists(inits))) { + stop("One or more inits files are missing") + } + if (length(inits)!=n.chains) { + stop("Need one inits file for each chain") + } + bugs.inits.files <- inits + } else { + if (!is.function(inits) && !is.null(inits) && (length(inits) != n.chains)) { + stop("Number of initialized chains (length(inits)) != n.chains") + } + bugs.inits.files <- 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))) { @@ -70,17 +95,24 @@ bugs.script(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, new.model.file, debug=debug, is.inits=!is.null(inits), bin=bin, DIC=DIC, useWINE=useWINE, newWINE=newWINE, - WINEPATH=WINEPATH) + WINEPATH=WINEPATH, bugs.seed=bugs.seed, + summary.only=summary.only, save.history=save.history, + bugs.data.file = bugs.data.file, + bugs.inits.files = bugs.inits.files) bugs.run(n.burnin, bugs.directory, WINE=WINE, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) if(codaPkg) return(file.path(getwd(), paste("coda", 1:n.chains, ".txt", sep=""))) + if (summary.only) { + return(log("log.txt")) + } + sims <- c(bugs.sims(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, DIC), model.file=model.file, program=program) if(clearWD) { - file.remove(c("data.txt", "log.odc", "log.txt", "codaIndex.txt", - paste("inits", 1:n.chains, ".txt", sep=""), + file.remove(c(bugs.data.file, "log.odc", "log.txt", "codaIndex.txt", + bugs.inits.files, paste("coda", 1:n.chains, ".txt", sep=""))) } class(sims) <- "bugs" Modified: trunk/R2WinBUGS/R/bugs.data.R =================================================================== --- trunk/R2WinBUGS/R/bugs.data.R 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/R/bugs.data.R 2008-01-06 18:17:57 UTC (rev 77) @@ -1,26 +1,27 @@ "bugs.data" <- -function(data, dir = getwd(), digits = 5){ +function(data, dir = getwd(), digits = 5, data.file = "data.txt"){ if(is.numeric(unlist(data))) if(is.R()) { write.datafile(lapply(data, formatC, digits = digits, format = "E"), - file.path(dir, "data.txt")) + file.path(dir, data.file)) } else { - writeDatafileS4(data, towhere = "data.txt") + writeDatafileS4(data, towhere = data.file) } else { if(is.R()) { data.list <- lapply(as.list(data), get, pos = parent.frame(2)) names(data.list) <- as.list(data) write.datafile(lapply(data.list, formatC, digits = digits, format = "E"), - file.path(dir, "data.txt")) + file.path(dir, data.file)) } else { data.list <- lapply(as.list(data), get, where = parent.frame(2)) names(data.list) <- unlist(data) - writeDatafileS4(data.list, towhere = "data.txt") + writeDatafileS4(data.list, towhere = data.file) } - } + } + return(data.file) } Modified: trunk/R2WinBUGS/R/bugs.inits.R =================================================================== --- trunk/R2WinBUGS/R/bugs.inits.R 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/R/bugs.inits.R 2008-01-06 18:17:57 UTC (rev 77) @@ -1,22 +1,22 @@ "bugs.inits" <- -function (inits, n.chains, digits){ +function (inits, n.chains, digits, + inits.files = paste("inits", 1:n.chains, ".txt", sep = "")){ if(!is.null(inits)) { for(i in 1:n.chains) { if(is.function(inits)) if(is.R()) { write.datafile(lapply(inits(), formatC, digits = digits, format = "E"), - paste("inits", i, ".txt", sep = "")) + inits.files[i]) } else { - writeDatafileS4(inits(), towhere = - paste("inits", i, ".txt", sep = "")) + writeDatafileS4(inits(), towhere = inits.files[i]) } else if(is.R()) { write.datafile(lapply(inits[[i]], formatC, digits = digits, format = "E"), - paste("inits", i, ".txt", sep = "")) + inits.files[i]) } else { - writeDatafileS4(inits[[i]], towhere = paste( - "inits", i, ".txt", sep = "")) + writeDatafileS4(inits[[i]], towhere = inits.files[i]) } } } + return(inits.files) } Modified: trunk/R2WinBUGS/R/bugs.log.R =================================================================== --- trunk/R2WinBUGS/R/bugs.log.R 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/R/bugs.log.R 2008-01-06 18:17:57 UTC (rev 77) @@ -1,49 +1,34 @@ -bugs.log <- function(file) +bugs.log <- function (file) { - if(!file.exists(file)) - stop("Log file", file, "does not exist") - logfile <- readLines(file) + # Extracts the summary statistics from log.txt written by WinBUGS. + # Jouni Kerman 2007-01-30 + # does essentially the same thing as bugs.log() but + # - won't crash if DIC is not there + # - makes fewer assumptions about the structure of the matrix - ## --- Stats --- - - statsStart <- which(logfile == "Node statistics") + 2 - if(!length(statsStart)) - stop("Log file", file, "does not contain node statistics.") - ## + 2 to remove - ## "Node statistics" - ## "\t node\t mean\t sd\t MC error\t2.5%\tmedian\t97.5%\tstart\tsample" - - statsEnd <- which(logfile == "dic.stats()") - 1 - ## - 1 to remove - ## "dic.stats()" - - statsTable <- logfile[statsStart:statsEnd] - statsTable <- sub("\t", "", statsTable) - statsTable <- sapply(strsplit(statsTable, "\t"), "[") - colnames(statsTable) <- statsTable[1,] - statsTable <- t(apply(statsTable[-1,], 2, as.numeric)) - colnames(statsTable) <- c("mean", "sd", "MC error", "2.5%", "median", "97.5%", "start", "sample") - - ## --- DIC --- - - DICStart <- which(logfile == "DIC") + 3 - ## + 3 to remove - ## "DIC" - ## "Dbar = post.mean of -2logL; Dhat = -2LogL at post.mean of stochastic nodes" - ## "\tDbar\tDhat\tpD\tDIC\t" - - DICEnd <- grep("history(", logfile, fixed=TRUE) - 1 - ## - 1 to remove - ## "history(..." - - if(!length(DICEnd) || !length(DICStart) || (DICEnd < DICStart)){ - DICTable <- NA - } else { - DICTable <- logfile[DICStart:DICEnd] - DICTable <- sapply(strsplit(DICTable, "\t"), "[") - colnames(DICTable) <- DICTable[1,] - DICTable <- t(apply(DICTable[-1,], 2, as.numeric)) - colnames(DICTable) <- c("Dbar", "Dhat", "pD", "DIC") - } - list(stats=statsTable, DIC=DICTable) + if(!file.exists(file)) + stop("Log file", file, "does not exist") + log.txt <- readLines(file) + extract <- function (m, line.match, skip=0, empty.left.col=TRUE) { + start <- (skip + which(log.txt == line.match)[1]) + if(is.na(start)) return(NULL) + if(length(start) < 1) return(NULL) + mx <- strsplit(m[-(1:start)], "\t") + n.cols <- length(mx[[1]]) + mxlen <- sapply(mx, length) + end <- which(mxlen!=n.cols)[1] - 1 + mx <- mx[1:end] + cm <- matrix(unlist(mx), ncol=n.cols, byrow=TRUE) # character format + if(empty.left.col) cm <- cm[,-1] # empty column + col.names <- cm[1, -1] # first column is just "node" + row.names <- cm[,1][-1] # first row is just "" + col.names <- gsub("[[:space:]]+", "", col.names) # get rid of spaces + cm <- cm[-1,-1] # delete dimname row/columns + m <- matrix(as.numeric(cm), nrow=nrow(cm)) # convert to numeric + dimnames(m) <- list(row.names, col.names) + return(m) + } + stats <- extract(log.txt, "Node statistics") + DIC <- extract(log.txt, "DIC", skip=1, empty.left.col=FALSE) + list(stats=stats, DIC=DIC) } Modified: trunk/R2WinBUGS/R/bugs.script.R =================================================================== --- trunk/R2WinBUGS/R/bugs.script.R 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/R/bugs.script.R 2008-01-06 18:17:57 UTC (rev 77) @@ -2,20 +2,28 @@ function(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, model.file, debug=FALSE, is.inits, bin, DIC=FALSE, useWINE=.Platform$OS.type != "windows", - newWINE=TRUE, WINEPATH=NULL) + newWINE=TRUE, WINEPATH=NULL, bugs.seed=NULL, summary.only=FALSE, + save.history=TRUE, bugs.data.file, bugs.inits.files) { + + output.coda <- (!summary.only) ## Write file script.txt for Bugs - if((ceiling(n.iter/n.thin) - ceiling(n.burnin/n.thin)) < 2) stop ("(n.iter-n.burnin)/n.thin must be at least 2") working.directory <- getwd() script <- "script.txt" - test <- length(grep("\\\\", model.file)) || length(grep("/", model.file)) - model <- ifelse(test, model.file, file.path(working.directory, model.file)) + model <- + if (length(grep("\\\\", model.file)) || length(grep("/", model.file))) { + model.file + } + else file.path(working.directory, model.file) model <- native2win(model, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) - data <- file.path(working.directory, "data.txt") + history <- file.path(working.directory, "history.odc") + history <- native2win(history, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) + + data <- file.path(working.directory, bugs.data.file) data <- native2win(data, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) coda <- file.path(working.directory, "coda") @@ -26,16 +34,21 @@ logFileTxt <- file.path(working.directory, "log.txt") logFileTxt <- native2win(logFileTxt, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH) - inits <- paste(working.directory, "/inits", 1:n.chains, ".txt", sep="") + inits <- paste(working.directory, "/", bugs.inits.files, sep="") inits <- sapply(inits, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH, - function(x, useWINE, newWINE, WINEPATH) - {native2win(x, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH)}) + function(x, useWINE, newWINE, WINEPATH) + {native2win(x, useWINE=useWINE, newWINE=newWINE, WINEPATH=WINEPATH)}) initlist <- paste("inits (", 1:n.chains, ", '", inits, "')\n", sep="") savelist <- paste("set (", parameters.to.save, ")\n", sep="") redo <- ceiling((n.iter-n.burnin)/(n.thin*bin)) + bugs.seed.cmd <- "" + if (!is.null(bugs.seed)) { + bugs.seed.cmd <- paste("set.seed(", bugs.seed, ")\n", sep="") + } + thinUpdate <- paste("thin.updater (", n.thin, ")\n", "update (", ceiling(n.burnin/n.thin), ")\n", sep="") @@ -43,18 +56,19 @@ "display ('log')\n", "check ('", model, "')\n", "data ('", data, "')\n", + bugs.seed.cmd, "compile (", n.chains, ")\n", if(is.inits) initlist, "gen.inits()\n", thinUpdate, - savelist, + savelist, if(DIC) "dic.set()\n", - rep( - c("update (", formatC(ceiling(bin), format="d"), ")\n", - "coda (*, '", coda, "')\n"),redo), + rep(c("update (", formatC(ceiling(bin), format = "d"), ")\n", + if (output.coda) c("coda (*, '", coda, "')\n")), + redo), "stats (*)\n", if(DIC) "dic.stats()\n", - "history (*)\n", + if (save.history) c("history (*, '", history, "')\n"), "save ('", logFile, "')\n", "save ('", logFileTxt, "')\n", file=script, sep="", append=FALSE) Modified: trunk/R2WinBUGS/R/openbugs.R =================================================================== --- trunk/R2WinBUGS/R/openbugs.R 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/R/openbugs.R 2008-01-06 18:17:57 UTC (rev 77) @@ -3,7 +3,7 @@ openbugs <- 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)), + n.thin = max(1, floor(n.chains *(n.iter - n.burnin) / n.sims)), n.sims = 1000, DIC = TRUE, bugs.directory = "c:/Program Files/OpenBUGS/", working.directory=NULL, digits = 5) { Modified: trunk/R2WinBUGS/man/bugs.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.Rd 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/man/bugs.Rd 2008-01-06 18:17:57 UTC (rev 77) @@ -9,28 +9,31 @@ \usage{ bugs(data, inits, parameters.to.save, model.file="model.bug", 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, + n.thin=max(1, floor(n.chains * (n.iter - n.burnin) / n.sims)), + n.sims = 1000, bin=(n.iter - n.burnin) / n.thin, debug=FALSE, DIC=TRUE, digits=5, codaPkg=FALSE, bugs.directory="c:/Program Files/WinBUGS14/", program=c("WinBUGS", "OpenBUGS", "winbugs", "openbugs"), working.directory=NULL, clearWD=FALSE, useWINE=.Platform$OS.type != "windows", WINE=NULL, - newWINE=TRUE, WINEPATH=NULL) + newWINE=TRUE, WINEPATH=NULL, bugs.seed=NULL, summary.only=FALSE, + save.history=!summary.only) } \arguments{ \item{data}{either a named list (names corresponding to variable names in the \code{model.file}) of the data for the \pkg{WinBUGS} model, \emph{or} a vector or list of the names of the data objects used by - the model. If \code{data="data.txt"}, it is assumed that data have - already been written to the working directory in a file called - \file{data.txt}, e.g. by the function \code{\link{bugs.data}}.} + the model. If \code{data} is a one element character vector (such as \code{"data.txt"}), + it is assumed that data have already been written to the working directory into that file, + e.g. by the function \code{\link{bugs.data}}.} \item{inits}{a list with \code{n.chains} elements; each element of the list is itself a list of starting values for the \pkg{WinBUGS} model, \emph{or} a function creating (possibly random) initial values. Alternatively, if \code{inits=NULL}, initial values are generated - by \pkg{WinBUGS}.} + by \pkg{WinBUGS}. If \code{inits} is a character vector with \code{n.chains} elements, + it is assumed that inits have already been written to the working directory into those files, + e.g. by the function \code{\link{bugs.inits}}.} \item{parameters.to.save}{character vector of the names of the parameters to save which should be monitored} \item{model.file}{file containing the model written in \pkg{WinBUGS} code. @@ -52,6 +55,7 @@ \code{n.iter} is large. Default is \code{max(1, floor(n.chains * (n.iter-n.burnin) / 1000))} which will only thin if there are at least 2000 simulations.} + \item{n.sims}{The approximate number of simulations to keep after thinning.} \item{bin}{number of iterations between saving of results (i.e. the coda files are saved after each \code{bin} iterations); default is to save only at the end.} @@ -68,7 +72,9 @@ is returned, if \code{TRUE} file names of \pkg{WinBUGS} output are returned for easy access by the \pkg{coda} package through function \code{\link{read.bugs}} (not used if \code{program="OpenBUGS"}).} - \item{bugs.directory}{directory that contains the \pkg{WinBUGS} executable} + \item{bugs.directory}{directory that contains the \pkg{WinBUGS} executable. + If the global option \code{R2WinBUGS.bugs.directory} is not \code{NULL}, + it will be used as the default.} \item{program}{the program to use, either \code{winbugs}/\code{WinBUGS} or \code{openbugs}/\code{OpenBUGS}, the latter makes use of function \code{\link{openbugs}} and requires @@ -92,6 +98,10 @@ utility} \item{WINEPATH}{character, path to \file{winepath} binary file, it is tried hard to get the information automatically if not given.} + \item{bugs.seed}{random seed for \pkg{WinBUGS} (default is no seed)} + \item{summary.only}{If \code{TRUE}, only a parameter summary for very quick analyses is given, + temporary created files are not removed in that case.} + \item{save.history}{If \code{TRUE} (the default), trace plots are generated at the end.} } \details{ Modified: trunk/R2WinBUGS/man/bugs.data.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.data.Rd 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/man/bugs.data.Rd 2008-01-06 18:17:57 UTC (rev 77) @@ -2,11 +2,11 @@ \alias{bugs.data} \title{Writing input for WinBUGS} -\description{Write file \file{data.txt} for \pkg{WinBUGS} to read - for +\description{Write file for \pkg{WinBUGS} to read - for internal use.} \usage{ -bugs.data(data, dir = getwd(), digits = 5) +bugs.data(data, dir = getwd(), digits = 5, data.file = "data.txt") } \arguments{ \item{data}{either a named list (names corresponding to variable names @@ -16,10 +16,11 @@ \item{dir}{the directory to write the file \file{data.txt} to} \item{digits}{number of significant digits used for \pkg{WinBUGS} input, see \code{\link{formatC}}} + \item{data.file}{name for the file R writes the data into.} } -\value{Nothing, but as a side effect, the data file \file{data.txt} is - written} +\value{The name of \code{data.file} is returned and as a side effect, + the data file is written} \seealso{The main function to be called by the user is \code{\link{bugs}}.} \keyword{internal} Modified: trunk/R2WinBUGS/man/bugs.inits.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.inits.Rd 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/man/bugs.inits.Rd 2008-01-06 18:17:57 UTC (rev 77) @@ -6,7 +6,8 @@ the working directory for \pkg{WinBUGS} to read - for internal use.} \usage{ -bugs.inits(inits, n.chains, digits) +bugs.inits(inits, n.chains, digits, + inits.files = paste("inits", 1:n.chains, ".txt", sep = "")) } \arguments{ \item{inits}{a list with \code{n.chains} elements; each element of the @@ -15,9 +16,10 @@ \item{n.chains}{number of Markov chains} \item{digits}{number of significant digits used for \pkg{WinBUGS} input, see \code{\link{formatC}}} + \item{inits.file}{name for the inits files R write the inits into.} } -\value{Nothing, but as a side effect, the inits files \file{inits*.txt} +\value{Vector of names of \code{inits.files}; as a side effect, the inits files \file{inits*.txt} are written} \seealso{The main function to be called by the user is \code{\link{bugs}}.} Modified: trunk/R2WinBUGS/man/bugs.log.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.log.Rd 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/man/bugs.log.Rd 2008-01-06 18:17:57 UTC (rev 77) @@ -17,11 +17,6 @@ \item{DIC}{A matrix containing the DIC statistics as returned from \pkg{WinBUGS}.} } - -\details{ In later releases of \pkg{R2WinBUGS}, this function is -considered to read the relevant data from the log file rather than -analysing and calculating the relevant data in \R / S-PLUS again. } - \seealso{The main function that generates the log file is \code{\link{bugs}}.} \keyword{IO} \keyword{file} Modified: trunk/R2WinBUGS/man/bugs.script.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.script.Rd 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/man/bugs.script.Rd 2008-01-06 18:17:57 UTC (rev 77) @@ -9,7 +9,9 @@ \usage{ bugs.script(parameters.to.save, n.chains, n.iter, n.burnin, n.thin, model.file, debug=FALSE, is.inits, bin, DIC=FALSE, - useWINE=.Platform$OS.type != "windows", newWINE=TRUE, WINEPATH=NULL) + useWINE=.Platform$OS.type != "windows", newWINE=TRUE, WINEPATH=NULL, + bugs.seed=NULL, summary.only=FALSE, save.history=TRUE, + bugs.data.file, bugs.inits.files) } \arguments{ \item{parameters.to.save}{parameters that should be monitored} @@ -28,6 +30,12 @@ \item{useWINE}{as in \code{\link{bugs}} meta function} \item{newWINE}{as in \code{\link{bugs}} meta function} \item{WINEPATH}{as in \code{\link{bugs}} meta function} + \item{bugs.seed}{random seed for \pkg{WinBUGS} (default is no seed)} + \item{summary.only}{If \code{TRUE}, only a parameter summary for very quick analyses is given, + temporary created files are not removed in that case.} + \item{save.history}{If \code{TRUE} (the default), trace plots are generated at the end.} + \item{bugs.data.file}{character name of the data file} + \item{bugs.inits.files}{character vector of names of the inits files} } \value{Nothing, but as a side effect, the script file \file{script.txt} Modified: trunk/R2WinBUGS/man/openbugs.Rd =================================================================== --- trunk/R2WinBUGS/man/openbugs.Rd 2007-10-17 10:09:41 UTC (rev 76) +++ trunk/R2WinBUGS/man/openbugs.Rd 2008-01-06 18:17:57 UTC (rev 77) @@ -10,8 +10,9 @@ openbugs(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)), - DIC = TRUE, bugs.directory = "c:/Program Files/OpenBUGS/", + n.thin = max(1, floor(n.chains * (n.iter - n.burnin) / n.sims)), + n.sims = 1000, DIC = TRUE, + bugs.directory = "c:/Program Files/OpenBUGS/", working.directory = NULL, digits = 5) } @@ -19,9 +20,9 @@ \item{data}{either a named list (names corresponding to variable names in the \code{model.file}) of the data for the \pkg{OpenBUGS} model, \emph{or} a vector or list of the names of the data objects used by - the model. If \code{data = "data.txt"}, it is assumed that data - have already been written to the working directory in a file called - \file{data.txt}, e.g. by the function \code{\link{bugs.data}}.} + the model. If \code{data} is a one element character vector (such as \code{"data.txt"}), + it is assumed that data have already been written to the working directory into that file, + e.g. by the function \code{\link{bugs.data}}.} \item{inits}{a list with \code{n.chains} elements; each element of the list is itself a list of starting values for the \pkg{OpenBUGS} model, \emph{or} a function creating (possibly random) initial values. @@ -45,7 +46,8 @@ \code{n.iter} is large. Default is \code{max(1, floor(n.chains * (n.iter-n.burnin) / 1000))} which will only thin if there are at least 2000 simulations.} - \item{DIC}{logical; if \code{TRUE} (default), compute deviance, pD, and + \item{n.sims}{The approximate number of simulations to keep after thinning.} + \item{DIC}{logical; if \code{TRUE} (default), compute deviance, pD, and DIC. This is done in \pkg{BRugs} directly.} \item{digits}{number of significant digits used for \pkg{OpenBUGS} input, see \code{\link{formatC}}} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <li...@us...> - 2008-01-08 21:12:15
|
Revision: 79 http://bugs-r.svn.sourceforge.net/bugs-r/?rev=79&view=rev Author: ligges Date: 2008-01-08 13:11:35 -0800 (Tue, 08 Jan 2008) Log Message: ----------- bugs in monitor() reported by Andrew Gelman: - exported from NS (for making code in his book work) - extracts the default number of chains from the object by default Modified Paths: -------------- trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/NAMESPACE trunk/R2WinBUGS/R/monitor.R trunk/R2WinBUGS/man/bugs.log.Rd trunk/R2WinBUGS/man/monitor.Rd Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2008-01-06 18:56:18 UTC (rev 78) +++ trunk/R2WinBUGS/DESCRIPTION 2008-01-08 21:11:35 UTC (rev 79) @@ -1,6 +1,6 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS -Date: 2008-01-06 +Date: 2008-01-08 Version: 2.1-7 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> Modified: trunk/R2WinBUGS/NAMESPACE =================================================================== --- trunk/R2WinBUGS/NAMESPACE 2008-01-06 18:56:18 UTC (rev 78) +++ trunk/R2WinBUGS/NAMESPACE 2008-01-08 21:11:35 UTC (rev 79) @@ -8,6 +8,7 @@ bugs.data, read.bugs, bugs.log, + monitor, openbugs, as.bugs.array, write.model) Modified: trunk/R2WinBUGS/R/monitor.R =================================================================== --- trunk/R2WinBUGS/R/monitor.R 2008-01-06 18:56:18 UTC (rev 78) +++ trunk/R2WinBUGS/R/monitor.R 2008-01-08 21:11:35 UTC (rev 79) @@ -1,15 +1,15 @@ "monitor" <- -function (a, n.chains, trans=NULL, keep.all=FALSE, Rupper.keep=FALSE) { +function (a, n.chains=dim(a)[2], trans=NULL, keep.all=FALSE, Rupper.keep=FALSE) { -# If keep.all=T: a is a n x m x k array: -# m sequences of length n, k variables measured -# If keep.all=F: a is a 2n x m x k array (first half will be discarded) -# -# trans is a vector of length k: "" if no transformation, or "log" or "logit" -# (If trans is not defined, it will be set to "log" for parameters that -# are all-positive and 0 otherwise.) -# -# If Rupper.keep=T: keep Rupper. (Otherwise don't display it.) +## If keep.all=T: a is a n x m x k array: +## m sequences of length n, k variables measured +## If keep.all=F: a is a 2n x m x k array (first half will be discarded) +## +## trans is a vector of length k: "" if no transformation, or "log" or "logit" +## (If trans is not defined, it will be set to "log" for parameters that +## are all-positive and 0 otherwise.) +## +## If Rupper.keep=TRUE: keep Rupper. (Otherwise don't display it.) invlogit <- function (x) {1 / (1 + exp(-x))} nparams <- if(length(dim(a)) < 3) 1 else dim(a)[length(dim(a))] # Calculation and initialization of the required matrix "output" @@ -30,9 +30,9 @@ confshrink = conv.p$confshrink, n.eff = conv.p$n.eff) } else if (trans[i]=="logit"){ - if (!is.R()){ - logit <- function (x) { log(x /(1- x)) } - } + if (!is.R()){ + logit <- function (x) { log(x /(1- x)) } + } conv.p <- conv.par(logit(ai), n.chains, Rupper.keep=Rupper.keep) conv.p <- list(quantiles = invlogit(conv.p$quantiles), confshrink = conv.p$confshrink, n.eff = conv.p$n.eff) Modified: trunk/R2WinBUGS/man/bugs.log.Rd =================================================================== --- trunk/R2WinBUGS/man/bugs.log.Rd 2008-01-06 18:56:18 UTC (rev 78) +++ trunk/R2WinBUGS/man/bugs.log.Rd 2008-01-08 21:11:35 UTC (rev 79) @@ -18,5 +18,6 @@ \pkg{WinBUGS}.} } \seealso{The main function that generates the log file is \code{\link{bugs}}.} +\author{Jouni Kerman} \keyword{IO} \keyword{file} Modified: trunk/R2WinBUGS/man/monitor.Rd =================================================================== --- trunk/R2WinBUGS/man/monitor.Rd 2008-01-06 18:56:18 UTC (rev 78) +++ trunk/R2WinBUGS/man/monitor.Rd 2008-01-08 21:11:35 UTC (rev 79) @@ -3,15 +3,13 @@ \alias{conv.par} \title{Special summary statistics} -\description{Special summary statistics of the WinBUGS output - intended - for internal use} +\description{Special summary statistics of the WinBUGS output.} \usage{ -monitor(a, n.chains, trans = NULL, keep.all = FALSE, Rupper.keep = FALSE) +monitor(a, n.chains = dim(a)[2], trans = NULL, keep.all = FALSE, Rupper.keep = FALSE) conv.par(x, n.chains, Rupper.keep = TRUE) } \arguments{ - \item{x}{} \item{a}{a \code{n * m * k} array: \code{m} sequences of length \code{n}, \code{k} variables measured} \item{n.chains}{number of Markov chains} @@ -21,9 +19,10 @@ \item{keep.all}{if \code{FALSE} (default), first half of \code{a} will be discarded} \item{Rupper.keep}{if \code{FALSE}, don't return \code{Rupper}} + \item{x}{for internal use only} } -\details{See the well documented code for details.} +\details{\code{conv.par} is intended for internal use only.} \value{ for \code{monitor}: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <li...@us...> - 2008-01-18 16:59:59
|
Revision: 82 http://bugs-r.svn.sourceforge.net/bugs-r/?rev=82&view=rev Author: ligges Date: 2008-01-18 09:00:00 -0800 (Fri, 18 Jan 2008) Log Message: ----------- - make scan() in openbugs() quiet - add note on option BRugsVerbose - increase date for release Modified Paths: -------------- trunk/R2WinBUGS/DESCRIPTION trunk/R2WinBUGS/R/openbugs.R trunk/R2WinBUGS/man/openbugs.Rd Modified: trunk/R2WinBUGS/DESCRIPTION =================================================================== --- trunk/R2WinBUGS/DESCRIPTION 2008-01-17 18:24:14 UTC (rev 81) +++ trunk/R2WinBUGS/DESCRIPTION 2008-01-18 17:00:00 UTC (rev 82) @@ -1,6 +1,6 @@ Package: R2WinBUGS Title: Running WinBUGS and OpenBUGS from R / S-PLUS -Date: 2008-01-08 +Date: 2008-01-18 Version: 2.1-7 Author: originally written by Andrew Gelman <ge...@st...>; changes and packaged by Sibylle Sturtz <st...@st...> Modified: trunk/R2WinBUGS/R/openbugs.R =================================================================== --- trunk/R2WinBUGS/R/openbugs.R 2008-01-17 18:24:14 UTC (rev 81) +++ trunk/R2WinBUGS/R/openbugs.R 2008-01-18 17:00:00 UTC (rev 82) @@ -7,7 +7,7 @@ DIC = TRUE, bugs.directory = "c:/Program Files/OpenBUGS/", working.directory=NULL, digits = 5) { - if(!require(BRugs)) + if(!require("BRugs")) stop("BRugs is required") ## switching from bugs() to BRugsFit() notation modelFile <- model.file @@ -55,7 +55,7 @@ ## set the adaptive phases adaptivelines <- scan(system.file("OpenBUGS", "Bugs", "Rsrc", "Registry.txt", package="BRugs"), - what="character") + what="character", quiet = TRUE) factories <- sub(".adaptivePhase", "", adaptivelines[grep("adaptivePhase",adaptivelines)]) sapply(factories, BRugs::modelSetAP, max(0, nBurnin-1)) Modified: trunk/R2WinBUGS/man/openbugs.Rd =================================================================== --- trunk/R2WinBUGS/man/openbugs.Rd 2008-01-17 18:24:14 UTC (rev 81) +++ trunk/R2WinBUGS/man/openbugs.Rd 2008-01-18 17:00:00 UTC (rev 82) @@ -67,6 +67,11 @@ and Uwe Ligges. } +\note{By default, BRugs (and hence \code{openbugs()}) is quite verbose. + This can be controlled for the whole BRugs package by by the option \sQuote{BRugsVerbose} (see \code{\link{options}}) + which is set to \code{TRUE} by default. +} + \seealso{\code{\link{bugs}} and the \pkg{BRugs} package} \examples{ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |