From: <gg...@us...> - 2007-06-12 10:10:25
|
Revision: 29 http://svn.sourceforge.net/bugs-r/?rev=29&view=rev Author: ggorjan Date: 2007-06-12 03:10:26 -0700 (Tue, 12 Jun 2007) Log Message: ----------- some code formatting and removal of return() Modified Paths: -------------- trunk/R2WinBUGS/R/bugs.sims.R Modified: trunk/R2WinBUGS/R/bugs.sims.R =================================================================== --- trunk/R2WinBUGS/R/bugs.sims.R 2007-06-12 10:09:18 UTC (rev 28) +++ trunk/R2WinBUGS/R/bugs.sims.R 2007-06-12 10:10:26 UTC (rev 29) @@ -1,9 +1,11 @@ "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 +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") # 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, + ## read in the names of the parameters and the indices of their samples + index <- read.table("codaIndex.txt", header = FALSE, sep = "\t") + ## 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]) @@ -35,19 +37,19 @@ 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){ + 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]]]) + 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")) + 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]]] @@ -58,9 +60,11 @@ rank.long <- unlist(long.short) for (i in 1:n.chains){ - 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))] + 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 } @@ -118,12 +122,11 @@ 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){ + if(DIC) { LOG <- bugs.log("log.txt")$DIC - if(any(is.na(LOG))){ + if(any(is.na(LOG))) { deviance <- all$sims.array[, , dim(sims.array)[3], drop = FALSE] - if(!is.R()) - dimnames(deviance) <- NULL + if(!is.R()) dimnames(deviance) <- NULL dim(deviance) <- dim(deviance)[1:2] pD <- numeric(n.chains) DIC <- numeric(n.chains) @@ -138,11 +141,7 @@ all <- c(all, list(pD = pD, DIC = DIC, DICbyR = FALSE)) } } - return(all) + all } -if (!is.R()){ - .subset <- function(x, index){ - return (x[index]) - } -} +if(!is.R()) .subset <- function(x, index) x[index] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |