"adjust" <- function(x, adjust = "right") { nr <- nchar(x) mnr <- max(nr) blanks <- paste(character(mnr + 1.), collapse = " ") # adj.no <- pmatrch(adjust,c("right","left","centre","center")) pad <- mnr - nr + 1. lpad <- switch(adjust, right = pad, left = rep(0., length(pad)) + 1., centre = , center = 1. + pad %/% 2.) rpad <- pad - lpad + 1. nm <- paste(substring(blanks, 1., lpad), x, substring(blanks, 1., rpad), sep = "") nm <- substring(nm, 2., mnr + 1.) names(nm) <- names(x) nm } "clear" <- function(nrow, ncol) { if(missing(nrow) || missing(ncol)) mfrow <- unlist(par(mfrow = c(1., 1.))) else mfrow <- c(1., 1.) if(!missing(nrow)) mfrow[1.] <- nrow if(!missing(ncol)) mfrow[2.] <- ncol invisible(par(mfrow = mfrow)) } "na.ignore" <- function(frame) frame "percents" <- function(tabl, denom, units = "%" ,pretty = T, print.denom = T) { if(is.null(dim(tabl)) | (length(dim(tabl)) == 1)) tabl <- array(tabl, c(1., length(tabl)), dimnames = list(NULL, names(tabl))) dim.tb <- dim(tabl) if (is.numeric(units)) { real.units <- units } else { real.units <- switch(units, "%"=100,"K"=1000,"10K"=1e4, "100K"=1e5,"M"=1e6,"10M"=1e7,"100M"=1e8, "B"=1e9) } if(missing(denom)) { tb.n <- sum(tabl) pcts <- (real.units * tabl)/tb.n if(pretty | (!is.numeric(units))) { if (units !="%") units <- paste("/",units,sep="") if(print.denom) rep <- paste(adjust(paste(tabl, "/", tb.n, sep = ""), "right"), "(", format(round( pcts, 1.)), units, ")", sep = "") else rep <- paste(adjust(paste(tabl, sep = ""), "right"), "(", format(round(pcts, 1.)), units,")", sep = "") if(!is.null(dim(tabl))) rep <- array(rep, dim = dim(tabl), dimnames = dimnames(tabl)) else rep <- array(rep, dim = length(tabl), dimnames = list(names(tabl))) attr(rep, "class") <- c("table", "array") } else rep <- pcts } else { nms.tb <- dimnames(tabl) if(length(denom) == length(tabl)) { if(is.null(dim(denom)) | (length(dim(denom)) == 1)) denom <- array(denom, c(1., length(denom)), dimnames = list(NULL, names(denom))) pcts <- (real.units * tabl)/denom if (!is.integer(denom)) denom <- round(denom,1) if(pretty | !is.numeric(units)) { if (units !="%") units <- paste("/",units,sep="") if(print.denom) rep <- array(paste(adjust(paste(tabl, "/", denom, sep = ""), "right"), "(", format(round(pcts, 1.)), units, ")", sep = ""), dim = dim.tb, dimnames = nms.tb) else rep <- array(paste(adjust(paste(tabl, sep = ""), "right"), "(", format(round(pcts, 1.)), units,")", sep = ""), dim = dim.tb, dimnames = nms.tb) attr(rep, "class") <- c("table", "array") } else rep <- pcts } else { tb.n <- apply(tabl, denom, sum) pcts <- real.units * sweep(tabl, denom, tb.n, "/") if(pretty) { tb.n <- sweep(array(1., dim = dim(tabl)), denom, tb.n, "*") mode(tb.n) <- "integer" if(print.denom) rep <- array(paste(adjust(paste(tabl, "/", tb.n, sep = ""), "right"), "(", format(round(pcts, 1.)), units, ")", sep = ""), dim = dim.tb, dimnames = nms.tb) else rep <- array(paste(adjust(paste(tabl, sep = ""), "right"), "(", format(round(pcts, 1.)),units, ")", sep = ""), dim = dim.tb, dimnames = nms.tb) attr(rep, "class") <- c("table", "array") } else rep <- pcts } } rep } "prelim.plot" <- function(formula, data, subset, na.action = na.ignore, smooth = T, plot.resp = T, clear.screen = T, flip = F) { call <- match.call() m <- match.call(expand = F) m$plot.resp <- m$clear.screen <- m$smooth <- m$flip <- NULL m$na.action <- na.action m[[1.]] <- as.name("model.frame") m <- eval(m, sys.parent()) Terms <- attr(m, "terms") y.col <- attr(Terms, "response") y <- m[[y.col]] y.name <- names(m)[y.col] y.class <- attr(y, "class") predictors <- names(m)[ - y.col] if(clear.screen) clear() if(plot.resp) { if(is.factor(y)) barplot(table(y), xlab = "Count", main = y.name) else if(!is.null(y.class)) if(y.class == "Surv") plot(survfit(y ~ 1)) else hist(y, main = y.name, xlab = y.name) } for(term in predictors) { xv <- m[, term] if(flip) quick.plot(y, xv, y.name, term, smooth) else quick.plot(xv, y, term, y.name, smooth) } invisible() } "quick.plot" <- function(x, y, xname="x", yname="y", smooth = F) { y.class <- attr(y, "class") if(is.factor(x)) { if(is.factor(y)) { mt <- paste(yname, "by", xname) tab <- percents(table(x, y), 1, pretty = F) dotchart(tab, group = array.index(tab, 1), labels = as.character(array.index(tab, 2)), xlim = c(0., 100.), main = mt,xlab="Percent") return(invisible()) } if(!is.null(y.class)) if((y.class == "Surv")) { plot(survfit(y ~ x), xlab = xname, lty=seq(along=levels(x))) legend("bottomleft", lty=seq(along=levels(x)), levels(x)) return(invisible()) } mt <- paste(yname, "vs.", xname) ns <- table(x[!is.na(y)]) boxplot(split(y, x), main = mt, xlab = xname, ylab = yname) xs <- 1:length(unique(x)) usr <- par()$usr cex <- par()$cex text(xs, usr[4.] + (usr[4.] - usr[3.])/20., paste("n=", ns), cex = cex) } else { if(is.factor(y)) { mt <- paste(yname, "vs.", xname) boxplot(split(x,y),horizontal=TRUE,xlab="",ylab="") # cat.smooth(x,y) title(main = mt, xlab = xname, ylab = yname) return(invisible()) } if(!is.null(y.class)) if(y.class == "Surv") { gp.x <- qcut(x) plot(survfit(y ~ gp.x), lty=seq(along=levels(gp.x)), xlab = xname) legend("bottomleft", lty=seq(along=levels(gp.x)), levels(gp.x)) return(invisible()) } mt <- paste(yname, "vs.", xname) plot(x, y, ylab = yname, main = mt, xlab = xname) if(smooth) { kp <- !(is.na(x) | is.na(y)) lines(supsmu(x[kp], y[kp])) } } invisible() } uniplot <- function(data, labels = dimnames(data)[[2.]], ...) { num.var <- dim(data)[2.] if(!is.character(labels)) stop("labels must be a character vector") if(!is.null(names(labels))) labels <- labels[dimnames(data)[[2.]]] for(i in seq(length = num.var)) { var <- data[, i] var <- var[!is.na(var)] if(is.factor(var)) { tb <- table(var) barplot(tb, xlab = "", main=labels[i] , ...) } else hist(var, xlab = labels[i],main=labels[i]) } invisible() }