Friday, March 2, 2007

plot with confidence intervals

## modified version of the code I found on the web.
## I forgot who wrote the origial version

plotCI <- function (x, y = NULL, uiw, liw = uiw, ylim=NULL,..., sfrac = 0.01) {
col2 = 'gray'
if (is.list(x)) {
y <- x$y
x <- x$x
}
if (is.null(y)) {
if (is.null(x))
stop("both x and y NULL")
y <- as.numeric(x)
x <- seq(along = x)
}
ui <- y + uiw
li <- y - liw
if(is.null(ylim)) {
plot(x, y, ylim = range(c(y, ui, li),na.rm=T), ...)} else
{plot(x, y, ylim = ylim, ...)}
smidge <- diff(par("usr")[1:2]) * sfrac
segments(x, li, x, ui,col=col2)
x2 <- c(x, x)
ul <- c(li, ui)
segments(x2 - smidge, ul, x2 + smidge, ul,col=col2)
if(is.null(ylim)) ylim=range(c(y, ui, li),na.rm=T)
points(x, y, ...)
invisible(list(x = x, y = y))
}

No comments:

Contributors

google