\begin{Schunk}
\begin{Sinput}
 ##' Summary stats table-maker for regression users
 ##'
 ##' rockchalk::summarize does the numerical calculations
 ##' 
 ##' This is, roughly speaking, doing the right thing, but
 ##' not in a clever way. I need to think harder on that.
 ##'
 ##' @param object A fitted regression or an R data.frame, or any
 ##'      other object type that does not fail in code{model.frame(object)}.
 ##' @param stats Default is a vector c("mean", "sd", "min", "max"). Other
 ##'      stats reported by rockchalk::summarize should work fine as well
 ##' @param digits 2 decimal points is default
 ##' @param ... Other arguments passed to rockchalk::summarizeNumerics and
 ##'      summarizeFactors.
 ##' @return a character matrix
 ##' @author Paul Johnson
 descriptiveTable <- function(object, stats = c("mean", "sd", "min", "max"), 
                              digits = 2, probs = c(0, .5, 1), ...){
      mc <- match.call(expand.dots = TRUE)
      dots <- list(...)
      
      dat <- model.frame(object)    
      arglist <- list(dat = dat, stats = stats, digits = digits)
      arglist <- modifyList(arglist, dots)
      summ.dat <- do.call(rockchalk::summarize, arglist)
      reslt <- data.frame(variable = rownames(summ.dat[["numerics"]]), 
                                      summ.dat[["numerics"]][stats[stats %in% names(summ.dat[["numerics"]])]],
                          stringsAsFactors = FALSE)
      numbers <- names(which(sapply(reslt, is.numeric)))
      for(j in numbers) reslt[ , j] <- formatC(reslt[ , j], digits = digits)
      
      reslt2 <- vector("list", length = length(summ.dat[["factors"]]))
      names(reslt2) <- names(summ.dat[["factors"]])
      for(j in names(summ.dat[[2]])){
          tab <- summ.dat[[2]][[j]]
          tab.prop <- tab[["table"]]/tab[["table"]]["nobs"]
          ## remove elements after nobs
          nobs.col <- which(names(tab.prop) == "nobs")
          tab.prop <- tab.prop[1:(nobs.col - 1)]
          reslt2[[j]] <- data.frame(variable=names(tab.prop), 
                                    mean = formatC(tab.prop, digits = digits), 
                                    stringsAsFactors = FALSE)
          reslt2[[j]] <- rbind(data.frame(variable = j, mean = "", 
                                          stringsAsFactors = FALSE), 
                               reslt2[[j]])
      }
     
      reslt3 <- do.call(rbind, reslt2)
      
      reslt4 <- plyr::rbind.fill(reslt, reslt3)
      reslt4[is.na(reslt4)] <- ""
      reslt4
  }
\end{Sinput}
\end{Schunk}
