R/theta_y_plot_tau.R

Defines functions theta_y_plot_tau

Documented in theta_y_plot_tau

#' @title Plot tau in theta_y across Metropolis samples
#'
#' @description \code{theta_y_plot_tau} plots tau in theta_y for a given ordinal index across Metropolis samples given a bundle of relevant variables generated by \code{bundle_theta_y_sample}.
#'
#' @param bundle The bundle of variables (see \code{bundle_theta_y_sample})
#' @param j Index for which set of tau variables to plot
#' @param decim An integer for sampling (decimating) data to reduce the number of points for very large samples [optional; default = 1 for all samples]
#' @param fileName Filename for pdf save file [optional]
#'
# @keywords
#' @export
#'
#' @author Michael Holton Price <MichaelHoltonPrice@gmail.com>

theta_y_plot_tau <- function(bundle,j,decim=1,fileName=NA) {
  if(!bundle$haveNames) {
    varName <- paste('ord',as.character(j),sep='')
  } else {
    varName <- bundle$varNames[j]
  }
  ind <- seq(1,length(bundle$theta_yList),by=decim)
  toFile <- !is.na(fileName)
  options(warn=-1) # in case M < 3
  colVect <- RColorBrewer::brewer.pal(n=bundle$hp$M[j],name='Set1')
  options(warn=0)

  if(length(colVect) < bundle$hp$M[j]) {
    colVect <- colVect[0:(bundle$hp$M[j]-1) %% length(colVect) + 1]
  }

  valList <- lapply(bundle$theta_yList,function(x){x$tau[[j]]})

  yMin <- Inf
  yMax <- -Inf
  yMin <- min(yMin,unlist(valList))
  yMax <- max(yMax,unlist(valList))

  if(bundle$haveKnown) {
    v <- bundle$known$theta_y$tau[[j]]
    yMin <- min(yMin,v)
    yMax <- max(yMax,v)
  }
  if(toFile) {
    pdf(fileName)
  }
  plot(1,type="n",xlab="Sample", ylab='tau',xlim=c(0,max(ind)),ylim=c(yMin,yMax),main=varName)
  for(m in 1:bundle$hp$M[j]) {
    points(ind,sapply(valList,function(x){x[m]})[ind],col=colVect[m],pch=20)
    if(bundle$haveKnown) {
      lines(c(0,max(ind)),c(1,1)*v[m],col=colVect[m],lwd=3)
    }
  }

  legend('left',legend=as.character(1:bundle$hp$M[j]),col=colVect,lty=1,lwd=3)
  if(toFile) {
    dev.off()
  }
}
eehh-stanford/yada documentation built on June 18, 2020, 8:05 p.m.