Skip to content
Prev 59505 / 398502 Next

Help with ooplot(gplots) and error bars

Jean-Louis Abitbol wrote:
Jean-Louis,

Here is an example.

# Plot proportions and their Wilson confidence limits
set.seed(3)
d <- expand.grid(continent=c('USA','Europe'), year=1999:2001,
                  reps=1:100)
# Generate binary events from a population probability of 0.2
# of the event, same for all years and continents
d$y <- ifelse(runif(6*100) <= .2, 1, 0)
s <- with(d,
           summarize(y, llist(continent,year),
                     function(y) {
                      n <- sum(!is.na(y))
                      s <- sum(y, na.rm=T)
                      binconf(s, n)
                     }, type='matrix')
)

Dotplot(year ~ Cbind(y) | continent,  data=s, ylab='Year',
         xlab='Probability')

I did have to temporarily override a function in Hmisc to fix a problem. 
  This will be corrected in an upcoming release of Hmisc:

mApply <- function(X, INDEX, FUN=NULL, ..., simplify=TRUE) {
## Matrix tapply
## X: matrix with n rows; INDEX: vector or list of vectors of length n
## FUN: function to operate on submatrices of x by INDEX
## ...: arguments to FUN; simplify: see sapply
## Modification of code by Tony Plate <tplate at blackmesacapital.com> 10Oct02
## If FUN returns more than one number, mApply returns a matrix with
## rows corresponding to unique values of INDEX
nr <- nrow(X)
if(!length(nr)) {  ## X not a matrix
   r <- tapply(X, INDEX, FUN, ..., simplify=simplify)
   if(is.matrix(r)) r <- drop(t(r)) else
   if(simplify && is.list(r))
     r <- drop(matrix(unlist(r), nrow=length(r),
                      dimnames=list(names(r),names(r[[1]])), byrow=TRUE))
} else {
   idx.list <- tapply(1:nr, INDEX, c)
   r <- sapply(idx.list, function(idx,x,fun,...) 
fun(x[idx,,drop=FALSE],...),
               x=X, fun=FUN, ..., simplify=simplify)
   if(simplify) r <- drop(t(r))
}
dn <- dimnames(r)
if(length(dn) && !length(dn[[length(dn)]])) {
   fx <- FUN(X,...)
   dnl <- if(length(names(fx))) names(fx) else dimnames(fx)[[2]]
   dn[[length(dn)]] <- dnl
   dimnames(r) <- dn
}

if(simplify && is.list(r) && is.array(r)) {

   ll <- sapply(r, length)
   maxl <- max(ll)
   empty <- (1:length(ll))[ll==0]
   for(i in empty) r[[i]] <- rep(NA, maxl)
   ## unlist not keep place for NULL entries for nonexistent categories
   first.not.empty <- ((1:length(ll))[ll > 0])[1]
   nam <- names(r[[first.not.empty]])
   dr <- dim(r)

   r <- aperm(array(unlist(r), dim=c(maxl,dr),
                    dimnames=c(list(nam),dimnames(r))),
              c(1+seq(length(dr)), 1))
}
r
}



Frank