|
Version 1.5 (October 2016)
wb.stats
[R] Scanning node statistics from WinBUGS output
Menu
Top Syntax
wb.stats(file, nodes=character(0), node.statistics=c("mean", "sd", "MC error", "2.5%", "median", "97.5%", "start", "sample"), return.as.list=length(node.statistics)>1)
Top wb.stats arguments
Top Example
The code below reproduces the input and output of an R session involving calls to wb.stats.
> source('c:/users/pbelisle/My Documents/Home/bin/R/fcts/WBStats.R') # load file where wb.stats R function is defined
> u <- wb.stats('c:/users/pbelisle/My Documents/Home/MyProject/WinBUGS/log/mywinbugsoutput.txt', return.as.list=F) # read (all) the node statistics from the cited WinBUGS text output file > u
# Read the same WinBUGS output file as above, but select only a subset of nodes found therein u <- wb.stats('c:/users/pbelisle/My Documents/Home/MyProject/WinBUGS/log/mywinbugsoutput.txt', nodes=c('x.mudiff', 'prev'), return.as.list=F) > u
# Read the same WinBUGS output file as above, but select only a subset of nodes found therein and only a few node statistics AND RETURN OUTPUT AS A LIST u <- wb.stats('c:/users/pbelisle/My Documents/Home/MyProject/WinBUGS/log/mywinbugsoutput.txt', nodes=c('prev', 'x.mu'), node.statistics=c('mean', 'median')) > u
Note that the first line of code in the block above reads in the wb.stats function code, if it was previously saved to the file referenced by that command. Alternatively, you can cut and paste the function (see code in section below) directly into R. Of course, if you save your session, this step needs to be done only the first time you use wb.stats. Top Code
wb.stats <- function(file, nodes=character(0), node.statistics=c("mean", "sd", "MC error", "2.5%", "median", "97.5%", "start", "sample"), return.as.list=length(node.statistics)>1)
{ wblog <- scan(file, sep='\n', what="", quiet=T) tab.found <- grep('\t', wblog) wblog <- wblog[tab.found] # Remove node stats header line(s) -- if found twice, ignore lines below second occurence median.found <- grep('\\smedian', wblog, perl=T) if (length(median.found) > 1) wblog <- wblog[seq(median.found[2]-1)] wblog <- wblog[-median.found[1]] # Exhaustive list of node stats returned by WinBUGS wb.node.statistics <- c("mean", "sd", "MC error", "2.5%", "median", "97.5%", "start", "sample") # remove lines with comments (possible if output was modified) found.comment <- grep('#', wblog) if (length(found.comment)) wblog <- wblog[-found.comment] wblog <- gsub('\\s+', ' ', wblog, perl=T) wblog <- sub('\\s', '', wblog, perl=T) # remove leading space wblog <- wblog[nchar(wblog)>0] # drop empty lines # drop lines with /odds ratio/ slashes <- grep("/", wblog) if (length(slashes)) wblog <- wblog[-slashes] wblog <- matrix(unlist(strsplit(wblog, ' ')), byrow=T, nrow=length(wblog)) nodes.names <- wblog[,1] wblog <- matrix(suppressWarnings(as.numeric(wblog[,-1])), nrow=nrow(wblog)) colnames(wblog) <- wb.node.statistics rownames(wblog) <- nodes.names # Keep only required node statistics w <- match(tolower(node.statistics), tolower(wb.node.statistics)) wblog <- wblog[,w,drop=F] if (length(nodes)) { nodes.names0 <- sub('\\[.*', '', nodes.names, perl=T) w <- !is.na(match(nodes.names0, nodes)) | !is.na(match(nodes.names, nodes)) wblog <- wblog[w,,drop=F] } if (return.as.list) { stats <- sort(colnames(wblog)) n.stats <- length(stats) dim <- rownames(wblog) dim <- sub("\\[.*", "", dim, perl=T) dimensions <- unique(sort(dim)) n.dim <- length(dimensions) out <- list() for (i in seq(n.dim)) { d <- dimensions[i] w <- which(dim==d) my.out <- list() my.i <- 0 for (s in stats) { my.i <- my.i + 1 my.out[[my.i]] <- wblog[w, s] } if (n.stats == 1) { my.out <- unlist(my.out) } else { names(my.out) <- stats } out[[i]] <- my.out } names(out) <- dimensions } else { out <- wblog } out } # end of wb.stats Top Download
wb.stats is a free R function. Download version 1.5 now. |