aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKen Kellner <kenkellner@users.noreply.github.com>2020-10-06 11:49:54 -0400
committerGitHub <noreply@github.com>2020-10-06 11:49:54 -0400
commit329bd6ced8ebcd40fbbc269149cd554f0d6c70b5 (patch)
treebc7ccb867737969c8a5994753fc2d4b36a6d01a1
parent275108c25c922c17110730e3d1efcd6a4ee51c62 (diff)
parentb6ace244ec61bd9c95ff71dd47681c48c0694fc0 (diff)
Merge pull request #41 from mikemeredith/master
Update plots, remove View
-rw-r--r--DESCRIPTION7
-rw-r--r--NAMESPACE15
-rw-r--r--R/densityplot.R66
-rw-r--r--R/get_plot_info.R35
-rw-r--r--R/jags_View.R (renamed from R/View.R)23
-rw-r--r--R/mcmc_tools.R70
-rw-r--r--R/plot.R27
-rw-r--r--R/ppcheck.R38
-rw-r--r--R/traceplot.R88
-rw-r--r--R/utils.R8
-rw-r--r--R/whiskerplot.R74
-rw-r--r--R/xyplot.R5
-rw-r--r--man/View.Rd32
-rw-r--r--man/densityplot.Rd23
-rw-r--r--man/jags_View.Rd27
-rw-r--r--man/ppcheck.Rd10
-rw-r--r--man/traceplot.Rd17
-rw-r--r--man/whiskerplot.Rd7
18 files changed, 382 insertions, 190 deletions
diff --git a/DESCRIPTION b/DESCRIPTION
index 3e850c5..da1c450 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,12 +1,11 @@
Package: jagsUI
-Version: 1.5.1
-Date: 2019-07-30
+Version: 1.5.1.5000
+Date: 2020-10-06
Title: A Wrapper Around 'rjags' to Streamline 'JAGS' Analyses
Author: Ken Kellner <contact@kenkellner.com>
Maintainer: Ken Kellner <contact@kenkellner.com>
Depends:
R (>= 2.14.0),
- lattice
Imports:
rjags (>= 3-13),
coda (>= 0.13),
@@ -16,7 +15,7 @@ Imports:
graphics,
utils
SystemRequirements: JAGS (http://mcmc-jags.sourceforge.net)
-Description: A set of wrappers around 'rjags' functions to run Bayesian analyses in 'JAGS' (specifically, via 'libjags'). A single function call can control adaptive, burn-in, and sampling MCMC phases, with MCMC chains run in sequence or in parallel. Posterior distributions are automatically summarized (with the ability to exclude some monitored nodes if desired) and functions are available to generate figures based on the posteriors (e.g., predictive check plots, traceplots). Function inputs, argument syntax, and output format are nearly identical to the 'R2WinBUGS'/'R2OpenBUGS' packages to allow easy switching between MCMC samplers.
+Description: A set of wrappers around 'rjags' functions to run Bayesian analyses in 'JAGS' (specifically, via 'libjags'). A single function call can control adaptive, burn-in, and sampling MCMC phases, with MCMC chains run in sequence or in parallel. Posterior distributions are automatically summarized (with the ability to exclude some monitored nodes if desired) and functions are available to generate figures based on the posteriors (e.g., predictive check plots, traceplots). Function inputs, argument syntax, and output format are nearly identical to the 'R2WinBUGS'/'R2OpenBUGS' packages to allow easy switching between MCMC samplers.
License: GPL-3
URL: https://github.com/kenkellner/jagsUI
NeedsCompilation: no
diff --git a/NAMESPACE b/NAMESPACE
index 4b52685..c21a735 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -1,27 +1,22 @@
-import(lattice)
+# import(lattice) # no longer needed?
importFrom(rjags, "jags.model", "adapt", "coda.samples", "list.modules",
"load.module","unload.module","list.factories","set.factory")
importFrom(coda, "gelman.diag", "as.mcmc.list", "thin", "mcmc", "as.mcmc")
-importFrom(parallel, "detectCores", "makeCluster", "clusterExport",
+importFrom(parallel, "detectCores", "makeCluster", "clusterExport",
"clusterSetRNGStream", "clusterApply", "stopCluster", "clusterEvalQ")
importFrom(stats, "runif", "time", "start", "end", "quantile", "var", "sd")
importFrom(grDevices, "devAskNewPage", "rainbow")
importFrom(graphics, "plot", "abline", "axis", "box", "segments", "lines", "par", "legend")
importFrom(utils, "capture.output")
-export("jags", "jagsUI","jags.basic","autojags","pp.check", "traceplot",
- "whiskerplot", "View", "jags.View")
+export("jags", "jagsUI","jags.basic","autojags","pp.check", "traceplot",
+ "densityplot", "whiskerplot", "jags.View")
S3method("plot", "jagsUI")
-S3method("xyplot", "jagsUI")
-S3method("densityplot", "jagsUI")
+# S3method("xyplot", "jagsUI")
S3method("print","jagsUI")
S3method("update","jagsUI")
S3method("update","jagsUIbasic")
S3method("summary","jagsUI")
-S3method("traceplot", "jagsUI")
-S3method("traceplot", "default")
-S3method("View", "jagsUI")
-S3method("View","default")
diff --git a/R/densityplot.R b/R/densityplot.R
index 1ca9e24..7d346e1 100644
--- a/R/densityplot.R
+++ b/R/densityplot.R
@@ -1,4 +1,64 @@
+#Get density plots for series of parameters
+densityplot <- function(x, parameters=NULL, per_plot=9, ask=NULL){
+
+ #Check input class and get basic plot settings
+ check_class(x)
+ if(is.null(ask))
+ ask <- grDevices::dev.interactive(orNone = TRUE)
+ plot_info <- get_plot_info(x, parameters, per_plot, ask)
+
+ #Handle par()
+ old_par <- graphics::par(plot_info$new_par)
+ on.exit(graphics::par(old_par))
+
+ #Generate plot
+ n <- length(plot_info$params)
+ for (i in 1:n){
+ m_labels <- (i %% plot_info$per_plot == 0) || (i==n)
+ param_density(x, plot_info$params[i], m_labels=m_labels)
+ }
+}
-densityplot.jagsUI <- function(x,...){
- densityplot(x$samples)
-} \ No newline at end of file
+#Density plot for single parameter
+param_density <- function(x, parameter, m_labels=FALSE){
+
+ #Get samples
+ vals <- mcmc_to_mat(x$samples, parameter)
+
+ # Get bandwidth, one value for all chains
+ bw <- mean(apply(vals, 2, stats::bw.nrd0))
+
+ from <- min(vals) - 3*bw # these are 'density's defaults...
+ to <- max(vals) + 3*bw # ... use these if no constraints
+ xx <- vals
+ mult <- 1
+
+ # Check for non-negative constraint or probability
+ if (min(vals) >= 0 && min(vals) < 2 * bw) { # it's non-negative
+ from <- 0
+ xx <- rbind(vals, -vals)
+ mult <- 2
+ }
+ if (min(vals) >= 0 && max(vals) <= 1 &&
+ (min(vals) < 2 * bw || 1 - max(vals) < 2 * bw)) { # it's a probability
+ xx <- rbind(vals, -vals, 2-vals)
+ mult <- 3
+ to <- 1
+ }
+
+ # Get densities
+ dens <- apply(xx, 2, function(x) stats::density(x, bw=bw, from=from, to=to)$y) * mult
+
+ # Draw plot
+ x <- seq(from, to, length=nrow(dens))
+ cols <- grDevices::rainbow(ncol(vals))
+ graphics::matplot(x, dens, type='l', lty=1, col=cols,
+ xlab='Value', ylab='Density', main=paste('Density of',parameter))
+
+ #Add margin labels if necessary
+ if(m_labels){
+ graphics::mtext("Value", side=1, line=1.5, outer=TRUE)
+ graphics::mtext("Density", side=2, line=1.5, outer=TRUE)
+ }
+
+}
diff --git a/R/get_plot_info.R b/R/get_plot_info.R
new file mode 100644
index 0000000..edc6c7a
--- /dev/null
+++ b/R/get_plot_info.R
@@ -0,0 +1,35 @@
+
+#General function for setting up plots
+get_plot_info <- function(x, parameters, per_plot, ask, Rhat_min=NULL){
+
+ #Expand non-scalar parameters and check they exist
+ all_params <- param_names(x$samples)
+ if(!is.null(parameters)){
+ parameters <- match_params(parameters, all_params)
+ if(is.null(parameters)){
+ stop("None of the provided parameters were found in the output")
+ }
+ } else{
+ parameters <- all_params
+ }
+
+ #If rhat_min, check parameters against it
+ if(!is.null(Rhat_min)){
+ Rhats <- x$summary[parameters, 'Rhat']
+ parameters <- parameters[Rhats >= Rhat_min]
+ if(length(parameters)==0) stop("No parameters > Rhat_min")
+ }
+
+ #Reduce max panels per plot if larger than number of parameters
+ if(length(parameters) <= per_plot){
+ per_plot <- length(parameters)
+ ask=FALSE
+ }
+
+ #Set up new par settings
+ new_par <- list(mar=c(1.5,1.5,2.5,1), oma=c(3,3,0,0), ask=ask)
+ if(per_plot > 1)
+ new_par$mfrow <- c(ceiling(sqrt(per_plot)), round(sqrt(per_plot)))
+
+ list(params=parameters, new_par=new_par, per_plot=per_plot)
+}
diff --git a/R/View.R b/R/jags_View.R
index 0a49435..0bae3e0 100644
--- a/R/View.R
+++ b/R/jags_View.R
@@ -1,21 +1,7 @@
-View <- function(x, title, ...) UseMethod("View")
+# Removed View.jagsUI (was already deprecated)
-View.default <- function(x, title, ...){
-
- if(missing(title)){
- title <- deparse(substitute(x))
- }
-
- utils::View(x, title)
-}
-
-View.jagsUI <- function(x,title,digits=3, ...){
- .Deprecated("jags.View")
- jags.View(x, title, digits=3, ...)
-}
-
-jags.View <- function(x,title,digits=3, ...){
+jags.View <- function(x, title, digits=3){
# grab object name
if(missing(title)){
title <- paste("jagsUI:", deparse(substitute(x)))
@@ -29,13 +15,12 @@ jags.View <- function(x,title,digits=3, ...){
#row.names(z) <- rownames(x$summary)
}
z[,6] <- z[,6]==1
-
+
view.out <- cbind(parameter=rownames(x$summary),z)
row.names(view.out) <- NULL
-
+
# View the output
utils::View(view.out, title=title)
invisible(view.out)
-
}
diff --git a/R/mcmc_tools.R b/R/mcmc_tools.R
new file mode 100644
index 0000000..205631d
--- /dev/null
+++ b/R/mcmc_tools.R
@@ -0,0 +1,70 @@
+#Functions for manipulating and extracting info from mcmc.list-class objects
+#from package rjags/coda
+
+# This is a subset of the functions in mcmc_tools in devel version 1.5.1.9024
+
+###------------------------------------------------------------------------------
+#Remove brackets and indices from parameter names in mcmc.list
+strip_params <- function(params_raw, unique=FALSE){
+ params_strip <- sapply(strsplit(params_raw,'[', fixed=T),'[',1)
+ if(unique) return( unique(params_strip) )
+ params_strip
+}
+#------------------------------------------------------------------------------
+
+###------------------------------------------------------------------------------
+#Identify which columns in mcmc.list object correspond to a given
+#parameter name (useful for non-scalar parameters)
+which_params <- function(param, params_raw){
+ params_strip <- strip_params(params_raw)
+ if( ! param %in% params_strip ){
+ return(NULL)
+ }
+ which(params_strip == param)
+}
+#------------------------------------------------------------------------------
+
+###------------------------------------------------------------------------------
+#Get names of parameters from an mcmc.list
+#If simplify=T, also drop brackets/indices
+param_names <- function(mcmc_list, simplify=FALSE){
+ raw <- colnames(mcmc_list[[1]])
+ if(!simplify) return(raw)
+ strip_params(raw, unique=T)
+}
+#------------------------------------------------------------------------------
+
+###------------------------------------------------------------------------------
+#Match parameter name to scalar or array versions of parameter name
+match_params <- function(params, params_raw){
+ unlist(lapply(params, function(x){
+ if(x %in% params_raw) return(x)
+ if(!x %in% strip_params(params_raw)) return(NULL)
+ params_raw[which_params(x, params_raw)]
+ }))
+}
+#------------------------------------------------------------------------------
+
+###------------------------------------------------------------------------------
+#Subset cols of mcmc.list (simple version of [.mcmc.list method)
+select_cols <- function(mcmc_list, col_inds){
+ out <- lapply(1:length(mcmc_list), FUN=function(x){
+ mcmc_element <- mcmc_list[[x]][,col_inds,drop=FALSE]
+ attr(mcmc_element,'mcpar') <- attr(mcmc_list[[x]], 'mcpar')
+ class(mcmc_element) <- 'mcmc'
+ mcmc_element
+ })
+ class(out) <- 'mcmc.list'
+ out
+}
+#------------------------------------------------------------------------------
+
+###------------------------------------------------------------------------------
+#Convert one parameter in mcmc.list to matrix, n_iter * n_chains
+mcmc_to_mat <- function(samples, param){
+ psamples <- select_cols(samples, param)
+ n_chain <- length(samples)
+ n_iter <- nrow(samples[[1]])
+ matrix(unlist(psamples), nrow=n_iter, ncol=n_chain)
+}
+#------------------------------------------------------------------------------
diff --git a/R/plot.R b/R/plot.R
index 6e66ac5..79a256e 100644
--- a/R/plot.R
+++ b/R/plot.R
@@ -1,6 +1,23 @@
-plot.jagsUI <- function(x,...){
- devAskNewPage(ask=FALSE)
- plot(x$samples,ask=TRUE)
- devAskNewPage(ask=FALSE)
-} \ No newline at end of file
+#Plot method for jagsUI objects
+plot.jagsUI <- function(x, parameters=NULL, per_plot=4, ask=NULL, ...){
+
+ if(is.null(ask))
+ ask <- grDevices::dev.interactive(orNone = TRUE)
+ plot_info <- get_plot_info(x, parameters, per_plot, ask)
+ dims <- c(min(length(plot_info$params), per_plot), 2)
+ if(length(plot_info$params) <= per_plot)
+ ask <- FALSE
+ new_par <- list(mfrow = dims, mar = c(4,4,2.5,1), oma=c(0,0,0,0), ask=ask)
+
+ #Handle par()
+ old_par <- graphics::par(new_par)
+ on.exit(graphics::par(old_par))
+
+
+ #Make plot
+ for (i in plot_info$params){
+ param_trace(x, i)
+ param_density(x, i)
+ }
+}
diff --git a/R/ppcheck.R b/R/ppcheck.R
index 4ecfbe4..8229b7b 100644
--- a/R/ppcheck.R
+++ b/R/ppcheck.R
@@ -1,35 +1,25 @@
-pp.check <- function(x, observed, simulated, xlab=NULL, ylab=NULL, main=NULL, ...){
- if(class(x)!="jagsUI"){stop('Requires jagsUI object as input')}
- devAskNewPage(ask=FALSE)
- obs <- eval(parse(text=paste('x$sims.list$',observed,sep="")))
- if(is.null(obs))
- stop("MCMC chain ", deparse(substitute(observed)), " not found.", call.=FALSE)
- sim <- eval(parse(text=paste('x$sims.list$',simulated,sep="")))
- if(is.null(sim))
- stop("MCMC chain ", deparse(substitute(simulated)), " not found.", call.=FALSE)
+pp.check <- function(x, observed, simulated,
+ xlab='Observed data', ylab='Simulated data',
+ main='Posterior Predictive Check', ...){
- bpval <- mean(sim > obs)
-
- if(is.null(xlab)){
- xlab <- 'Observed Data'
- }
- if(is.null(ylab)){
- ylab <- 'Simulated Data'
+ check_class(x)
+ if(is.null(match_params(observed, param_names(x$samples)))){
+ stop("Observed parameter not found in output")
}
- if(is.null(main)){
- main <- paste('Posterior Predictive Check')
+ if(is.null(match_params(simulated, param_names(x$samples)))){
+ stop("Simulated parameter not found in output")
}
- # minval <- min(c(observed,simulated))
- # maxval <- max(c(observed,simulated))
- # plotrange <- c(minval,maxval)
+ obs <- c(mcmc_to_mat(x$samples, observed))
+ sim <- c(mcmc_to_mat(x$samples, simulated))
+
+ bpval <- mean(sim > obs)
plotrange <- range(obs, sim)
- plot(x = obs, y = sim, xlab=xlab, ylab=ylab, main=main,
+ graphics::plot(x = obs, y = sim, xlab=xlab, ylab=ylab, main=main,
xlim=plotrange,ylim=plotrange, ...)
- # abline(1,1)
- abline(0,1, lwd=2)
+ graphics::abline(0,1, lwd=2)
where <- if(bpval < 0.5) 'topleft' else 'bottomright'
graphics::legend(where, paste("P =", round(bpval,2)), bty='n', cex=1.5)
diff --git a/R/traceplot.R b/R/traceplot.R
index d3fbc1e..e029d18 100644
--- a/R/traceplot.R
+++ b/R/traceplot.R
@@ -1,39 +1,53 @@
+#Get traceplots for series of parameters
+traceplot <- function(x, parameters=NULL, Rhat_min=NULL,
+ per_plot=9, ask=NULL){
-traceplot <- function(x, ...) UseMethod("traceplot")
-
-traceplot.default <- function(x, ...) coda::traceplot(x, ...)
-
-traceplot.jagsUI <- function (x, parameters=NULL, ...) {
-
- samples <- x$samples
-
- if(is.null(parameters)){params <- names(as.data.frame(samples[[1]]))
- } else {params <- translate.params(x,parameters)}
-
- nparams <- length(params)
- nchains <- x$mcmc.info[[1]]
- rhat <- x$Rhat
-
- sep <- par()$ask
- on.exit(par(ask=sep))
- par(ask=TRUE)
-
- xmax <- x$mcmc.info$n.samples / x$mcmc.info$n.chains
-
- col=c('red','blue','green','yellow','orange','violet')
- if(nchains>6){col=rainbow(nchains)}
-
- for (i in 1:nparams){
- if(nchains>1){
- rhat <- gelman.diag(samples[,params[i]],autoburnin=FALSE)$psrf[1]
- title <- paste('Trace of ',params[i],', Rhat = ',round(rhat,2),sep="")
- } else {title <- paste('Trace of ',params[i],sep="") }
- plot(x = 1:xmax, y = samples[,params[i]][[1]], main = title, xlab="Iterations", ylab="Value",type="l", col=col[1],
- ylim=range(samples[,params[i]]))
- if(nchains>1){
- for (j in 2:nchains){
- lines(x = 1:xmax, y = samples[,params[i]][[j]],type="l", col=col[j])
- }}
+ #Check input class and get basic plot settings
+ check_class(x)
+ if(is.null(ask))
+ ask <- grDevices::dev.interactive(orNone = TRUE)
+ plot_info <- get_plot_info(x, parameters, per_plot, ask, Rhat_min)
+
+ #Handle par()
+ old_par <- graphics::par(plot_info$new_par)
+ on.exit(graphics::par(old_par))
+
+ #Generate plot
+ n <- length(plot_info$params)
+ for (i in 1:n){
+ m_labels <- (i %% plot_info$per_plot == 0) || (i==n)
+ param_trace(x, plot_info$params[i], m_labels=m_labels)
+ }
+
+}
+
+#Traceplot for single parameter
+param_trace <- function(x, parameter, m_labels=FALSE){
+
+ #Get samples and Rhat values
+ vals <- mcmc_to_mat(x$samples, parameter)
+ Rhat <- sprintf("%.3f",round(x$summary[parameter, 'Rhat'],3))
+
+ #Draw plot
+ cols <- grDevices::rainbow(ncol(vals))
+ graphics::matplot(1:nrow(vals), vals, type='l', lty=1, col=cols,
+ xlab='Iterations', ylab='Value',
+ main=paste('Trace of',parameter))
+ # graphics::plot(1:nrow(vals), vals[,1], type='l', col=cols[1],
+ # ylim=range(vals), xlab='Iterations', ylab='Value',
+ # main=paste('Trace of',parameter))
+ # for (i in 2:ncol(vals)) graphics::lines(1:nrow(vals), vals[,i], col=cols[i]) # this fails with 1 chain
+
+ #Add Rhat value
+ graphics::legend('bottomright', legend=bquote(hat(R) == .(Rhat)),
+ bty='o', bg='white', cex=1.2)
+
+ #Add margin labels if necessary
+ if(m_labels){
+ graphics::mtext("Iteration", side=1, line=1.5, outer=TRUE)
+ graphics::mtext("Value", side=2, line=1.5, outer=TRUE)
}
-
-} \ No newline at end of file
+}
+
+#General function for setting up plots
+# get_plot_info now has its own file \ No newline at end of file
diff --git a/R/utils.R b/R/utils.R
new file mode 100644
index 0000000..9458c63
--- /dev/null
+++ b/R/utils.R
@@ -0,0 +1,8 @@
+
+
+#--- from process_output ---------------------------------------------------------------------------
+#Check that an object is the right class
+check_class <- function(output){
+ if(!inherits(output, "jagsUI")) stop("Requires jagsUI object")
+}
+#------------------------------------------------------------------------------
diff --git a/R/whiskerplot.R b/R/whiskerplot.R
index f9068b8..5b2bda9 100644
--- a/R/whiskerplot.R
+++ b/R/whiskerplot.R
@@ -1,40 +1,46 @@
-whiskerplot <- function(x,parameters,quantiles=c(0.025,0.975),zeroline=TRUE){
- if(class(x)!="jagsUI"){stop('Requires jagsUI object as input')}
- devAskNewPage(ask=FALSE)
+whiskerplot <- function(x,parameters,quantiles=c(0.025,0.975),
+ zeroline=TRUE, ...){
- #Generate a list of all specified output parameters
- #Expand from shorthand if necessary
- parameters <- translate.params(x,parameters)
+ #Check input object class
+ check_class(x)
- n <- length(parameters)
-
- xstructure <- c(1:n)
-
- qs <- function(x,y){as.numeric(quantile(x,y))}
-
- means <- tops <- bottoms <-ymin <- ymax <- vector(length=n)
- for (i in 1:n){
- hold <- unlist(x$samples[,parameters[i]])
- means[i] <- mean(hold)
- tops[i] <- qs(hold,quantiles[2])
- bottoms[i] <- qs(hold,quantiles[1])
+ #Check parameters given
+ all_params <- param_names(x$samples)
+ parameters <- match_params(parameters, all_params)
+ if(is.null(parameters)){
+ stop("None of the provided parameters were found in the output")
}
- ymin <- min(bottoms)
- ymax <- max(tops)
-
- plot(xstructure,means,xaxt="n",ylim=c(ymin,ymax),xlim=c(0,n+1),xlab="Parameters",ylab="Parameter Values",pch=19,cex=1.5,
- main=paste('Whisker plot, quantiles (',quantiles[1],' - ',quantiles[2],')',sep=""))
- axis(side=1, at=c(1:n), labels=parameters)
- box()
-
- if(zeroline){abline(h=0)}
-
- for (i in 1:n){
- segments(x0=xstructure[i],y0=bottoms[i],x1=xstructure[i],y1=tops[i], lwd=2)
- segments(x0=xstructure[i]-0.2,y0=bottoms[i],x1=xstructure[i]+0.2,y1=bottoms[i])
- segments(x0=xstructure[i]-0.2,y0=tops[i],x1=xstructure[i]+0.2,y1=tops[i])
+ #Check quantile argument
+ if((length(quantiles)!=2) | (quantiles[2] <= quantiles[1])){
+ stop("Incompatible quantile values provided")
}
-
-} \ No newline at end of file
+
+ #Calculate means and CIs
+ post_stats <- sapply(parameters,
+ function(i){
+ sims <- mcmc_to_mat(x$samples, i)
+ c(mean(sims,na.rm=TRUE),
+ stats::quantile(sims,na.rm=TRUE,quantiles))
+ })
+
+ #Plot parameter means
+ n <- length(parameters)
+ graphics::plot(1:n, post_stats[1,], xaxt="n",
+ ylim=range(post_stats), xlim=c(0,n+1),
+ xlab="Parameters",
+ ylab=paste0('Parameter mean and quantiles (',quantiles[1],
+ ' - ',quantiles[2],')'), pch=19, cex=1.5, ...)
+ graphics::axis(side=1, at=1:n, labels=parameters)
+ graphics::box()
+
+ #Draw line at zero
+ if(zeroline) graphics::abline(h=0)
+
+ #Draw error bars
+ wd <- (n+2)/40
+ graphics::segments(1:n, post_stats[2,], 1:n, post_stats[3,], lwd=2)
+ graphics::segments(1:n-wd, post_stats[2,], 1:n+wd, post_stats[2,])
+ graphics::segments(1:n-wd, post_stats[3,], 1:n+wd, post_stats[3,])
+}
diff --git a/R/xyplot.R b/R/xyplot.R
deleted file mode 100644
index e7052eb..0000000
--- a/R/xyplot.R
+++ /dev/null
@@ -1,5 +0,0 @@
-
-xyplot.jagsUI <- function(x, ...){
- devAskNewPage(ask=FALSE)
- xyplot(x$samples)
-} \ No newline at end of file
diff --git a/man/View.Rd b/man/View.Rd
deleted file mode 100644
index 7246a87..0000000
--- a/man/View.Rd
+++ /dev/null
@@ -1,32 +0,0 @@
-\name{View}
-% functions
-\alias{View}
-\alias{View.jagsUI}
-\alias{jags.View}
-
-\title{View a jagsUI output object in a separate window}
-
-\usage{
-
- View(x, title, ...)
-
- \method{View}{jagsUI}(x, title, digits=3, ...)
-}
-
-\arguments{
- \item{x}{A jagsUI object}
- \item{title}{Specify a title for the window.}
- \item{digits}{Number of digits to display after the decimal.}
- \item{...}{Further arguments pass to or from other methods.}
-}
-
-\description{
-Show an R object in a separate, spreadsheet-style window. This is an S3 generic version of \code{\link[utils]{View}}.
-
-The method will be deprecated in the next version due to constant issues with IDEs especially RStudio.
-It will be replacted with a standalone function \code{jags.View()}, which you can use now.
-}
-
-\author{
- Ken Kellner \email{contact@kenkellner.com} and Mike Meredith.
-}
diff --git a/man/densityplot.Rd b/man/densityplot.Rd
new file mode 100644
index 0000000..efe5631
--- /dev/null
+++ b/man/densityplot.Rd
@@ -0,0 +1,23 @@
+\name{densityplot}
+\alias{densityplot}
+
+\title{Density plots of JAGS output}
+
+\usage{
+ densityplot(x, parameters=NULL, per_plot=9, ask=NULL)
+}
+
+\arguments{
+ \item{x}{A jagsUI object}
+ \item{parameters}{A vector of names (as characters) of parameters to plot. Parameter names must match parameters included in the model. Calling non-scalar parameters without subsetting (e.g. \code{alpha}) will plot all values of \code{alpha}. If \code{parameters=NULL}, all parameters will be plotted.}
+ \item{per_plot}{Maximum number of parameters to include on each plot.}
+ \item{ask}{If \code{TRUE}, ask user for confirmation before generating each new plot; the default is to ask when output is going to the screen, not when it is going to a file.}
+}
+
+\description{
+ Displays a series of density plots for posteriors of monitored parameters in a JAGS analysis.
+}
+
+\author{
+ Ken Kellner \email{contact@kenkellner.com}.
+}
diff --git a/man/jags_View.Rd b/man/jags_View.Rd
new file mode 100644
index 0000000..e69e5a8
--- /dev/null
+++ b/man/jags_View.Rd
@@ -0,0 +1,27 @@
+\name{jags.View}
+% functions
+\alias{jags.View}
+
+\title{View a jagsUI output object in a separate window}
+
+\usage{
+
+ jags.View(x, title, digits=3)
+
+}
+
+\arguments{
+ \item{x}{A jagsUI object}
+ \item{title}{Specify a title for the window.}
+ \item{digits}{Number of digits to display after the decimal.}
+}
+
+\description{
+Show an R object in a separate, spreadsheet-style window via a call to \code{\link[utils]{View}}.
+
+This function replaces the \code{View} method for class \code{jagsUI}, which caused problems with RStudio.
+}
+
+\author{
+ Ken Kellner \email{contact@kenkellner.com} and Mike Meredith.
+}
diff --git a/man/ppcheck.Rd b/man/ppcheck.Rd
index cb64938..f0c3591 100644
--- a/man/ppcheck.Rd
+++ b/man/ppcheck.Rd
@@ -5,12 +5,14 @@
\title{Posterior Predictive Checks for Bayesian Analyses fit in JAGS}
\usage{
- pp.check(x, observed, simulated, xlab=NULL, ylab=NULL, main=NULL, ...)}
+ pp.check(x, observed, simulated, xlab='Observed data', ylab='Simulated data',
+ main='Posterior Predictive Check', ...)
+}
\arguments{
\item{x}{A jagsUI object generated using the \code{jags} function}
- \item{observed}{The name of the parameter (as a string, in the JAGS model) representing the fit of the observed data (e.g. residuals)}
- \item{simulated}{The name of the corresponding parameter (as a string, in the JAGS model) representing the fit of the new simulated data}
+ \item{observed}{The name of the parameter (as a string) representing the fit of the observed data (e.g. residuals)}
+ \item{simulated}{The name of the corresponding parameter (as a string) representing the fit of the new simulated data}
\item{xlab}{Customize x-axis label}
\item{ylab}{Customize y-axis label}
\item{main}{Customize plot title}
@@ -18,7 +20,7 @@
}
\description{
- A simple interface for generating a posterior predictive check plot for a JAGS analysis fit using jagsUI, based on the posterior distributions of discrepency metrics specified by the user and calculated and returned by JAGS (for example, sums of residuals). The user supplies the name of the discrepancy metric calculated for the real data in the argument \code{actual}, and the corresponding discrepancy for data simulated by the model in argument \code{new}. The posterior distributions of the two parameters will be plotted in X-Y space and a Bayesian p-value calculated.
+ A simple interface for generating a posterior predictive check plot for a JAGS analysis fit using jagsUI, based on the posterior distributions of discrepency metrics specified by the user and calculated and returned by JAGS (for example, sums of residuals). The user supplies the name of the discrepancy metric calculated for the real data in the argument \code{observed}, and the corresponding discrepancy for data simulated by the model in argument \code{simulated}. The posterior distributions of the two parameters will be plotted in X-Y space and a Bayesian p-value calculated.
}
\author{
diff --git a/man/traceplot.Rd b/man/traceplot.Rd
index 8fc103a..444940e 100644
--- a/man/traceplot.Rd
+++ b/man/traceplot.Rd
@@ -1,27 +1,24 @@
\name{traceplot}
-% functions
\alias{traceplot}
-\alias{traceplot.jagsUI}
\title{Traceplots of JAGS output}
\usage{
-
- traceplot(x, ...)
-
- \method{traceplot}{jagsUI}(x, parameters=NULL, ...)
+ traceplot(x, parameters=NULL, Rhat_min=NULL, per_plot=9, ask=NULL)
}
\arguments{
\item{x}{A jagsUI object}
- \item{parameters}{A vector of names (as characters) of parameters to plot. Parameter names must match parameters included in the model. Non-scalar parameters with multiple values (e.g. \code{alpha} where \code{alpha} is a vector of length 5) can be selected/subsetted (e.g. \code{alpha[1:3]}). Calling non-scalar parameters without subsetting (e.g. \code{alpha}) will plot all values of \code{alpha}. If \code{parameters=NULL}, all nodes will be plotted.}
- \item{...}{Further arguments pass to or from other methods.}
+ \item{parameters}{A vector of names (as characters) of parameters to plot. Parameter names must match parameters included in the model. Calling non-scalar parameters without subsetting (e.g. \code{alpha}) will plot all values of \code{alpha}. If \code{parameters=NULL}, all parameters will be plotted.}
+ \item{Rhat_min}{If provided, only plot parameters with Rhat values that exceed the provided value. A good min value to start with is 1.05.}
+ \item{per_plot}{Maximum number of parameters to include on each plot.}
+ \item{ask}{If \code{TRUE}, ask user for confirmation before generating each new plot; the default is to ask when output is going to the screen, not when it is going to a file.}
}
\description{
- Displays a series of MCMC iteration plots for each monitored parameter in a JAGS analysis. The calculated Rhat value for each parameter is given in the plot title if there is >1 chains.
+ Displays a series of MCMC iteration plots for monitored parameter in a JAGS analysis, along with the calculated Rhat value.
}
\author{
Ken Kellner \email{contact@kenkellner.com}.
-} \ No newline at end of file
+}
diff --git a/man/whiskerplot.Rd b/man/whiskerplot.Rd
index fef0cc8..a61feed 100644
--- a/man/whiskerplot.Rd
+++ b/man/whiskerplot.Rd
@@ -5,14 +5,15 @@
\title{Whisker plots of parameter posterior distributions}
\usage{
- whiskerplot(x, parameters, quantiles=c(0.025,0.975), zeroline=TRUE)
+ whiskerplot(x, parameters, quantiles=c(0.025,0.975), zeroline=TRUE, ...)
}
\arguments{
\item{x}{A jagsUI object}
- \item{parameters}{A vector of names (as characters) of parameters to include in the plot. Parameter names must match parameters included in the model. Non-scalar parameters with multiple values (e.g. \code{alpha} where \code{alpha} is a vector of length 5) can be selected/subsetted (e.g. \code{alpha[1:3]}). Calling non-scalar parameters without subsetting (e.g. \code{alpha}) will plot all values of \code{alpha}.}
+ \item{parameters}{A vector of names (as characters) of parameters to include in the plot. Parameter names must match parameters included in the model. Calling non-scalar parameters without subsetting (e.g. \code{alpha}) will plot all values of \code{alpha}.}
\item{quantiles}{A vector with two values specifying the quantile values (lower and upper).}
\item{zeroline}{If TRUE, a horizontal line at zero is drawn on the plot.}
+ \item{\dots}{Additional arguments passed to \link{plot.default}}
}
\description{
@@ -89,7 +90,7 @@ whiskerplot(out,parameters='mu')
#Plot a subset of mu
-whiskerplot(out,parameters='mu[c(1:3,7)]')
+whiskerplot(out,parameters=c('mu[1]','mu[7]'))
#Plot mu and alpha together