R/precplot.R

#' replacement for rethinking::plot(precis(model)) which doesn't always work
#'
#' \code{precplot(model,prob=0.95,...)}
#'
#' @param model name of the map or map2stan model
#' @param prob probability level for interval. Default is 0.95
#' @param ... additional arguments for plot
#'
#' @details plot doesn't seem to work consistently with precis objects in the
#' rethinking package. This is a rough plot to achieve essentially the same.
#'
#' @examples
#' X = data.frame(x = rnorm(100, mean=10, sd = 2))
#' M1 = map(alist(x~dnorm(mu,sigma),
#'                mu <- a,
#'                a ~ dnorm(0,10),
#'                sigma ~ dcauchy(0,2)),
#'                data=X)
#' precplot(M1, prob=0.9,...)

precplot = function(mapmodel,prob=0.95){
  par(ask=FALSE,mar=c(4,6,2,1),lend="square")
  Params = precis(mapmodel)@output[,]
  Mult = qnorm(prob/2+.5)
  N = nrow(Params)
  Params = Params[rev(rownames(Params)),]
  Upper = Params$Mean + Params$StdDev*Mult
  Lower = Params$Mean - Params$StdDev*Mult
  plot(1:N~Params[,1],yaxt="n", ylab="", xlab="parameter value",
       xlim=c(min(Lower),max(Upper)),ylim=c(0.5,N+0.5))
  axis(2, at=1:N, labels = rownames(Params),las=1)
  abline(h=1:N, lty=2, col="grey80")
  abline(v=0, lty=2)
  for(a in 1:N){
    lines(c(Lower[a],Upper[a]),c(a,a),lwd=2)
  }
  title(main=bquote(.(prob*100)*"%   interval"),cex.main=0.8)
}
helophilus/ColsTools documentation built on May 30, 2019, 4:03 p.m.