User Tools

Site Tools


r-credplot

Table of Contents

Credible Interval Plot

A type of forest plot for credible (and other) intervals

Code

# credplot - a type of forest plot
# for credible (and other) intervals 
# m   - estimates
# mlo - lower bound
# mhi - upper bound
# cen - if(!is.na(cen)) abline(v=cen)
 
credplot <-
function (m, mlo, mhi, cen = NA, ...) 
{
    require(lattice)
    if(is.null(names(m)))
        names(m) <- as.character(1:length(m))
    rge <- range(c(mlo, mhi))
    rge[1] <- rge[1] - 0.05 * diff(rge)
    rge[2] <- rge[2] + 0.05 * diff(rge)
    dotplot(m, mlo = mlo, mhi = mhi, cen = cen, xlim = rge, ..., 
        panel = function(x, y, mlo = mlo, mhi = mhi, cen = cen, 
            horizontal, levels.fos, ...) {
            if (missing(levels.fos)) {
                if (horizontal) 
                    levels.fos <- unique(y)
                else 
                    levels.fos <- unique(x)
            }
            if (horizontal) 
                panel.segments(x0 = mlo, y0 = levels.fos,
                  x1 = mhi, y1 = levels.fos)
            else 
                panel.segments(x0 = levels.fos, y0 = mlo, 
                  x1 = levels.fos, y1 = mhi)
            if (!is.na(cen)) {
                if (horizontal) 
                    panel.abline(v = cen, lty = 2)
                else 
                    panel.abline(h = cen, lty = 2)
            }
            panel.dotplot(x, y, horizontal, 
                    levels.fos = levels.fos, lty=0,...)
        })
}

Example

The following code generates a plot similar to that below.

m <- rnorm(50, 0, 1/10)
mlo <- m - 1/10
mhi <- m + 1/10
credplot(m, mlo, mhi, cen=0)

credplot.png

r-credplot.txt · Last modified: 2013/03/19 16:56 by matt