text.tree.EJN <- function (x, splits = TRUE, label = "yval", all = FALSE, pretty = NULL, digits = getOption("digits") - 3, adj = par("adj"), xpd = TRUE, dig = 7, ...) { oldxpd <- par(xpd = xpd) on.exit(par(oldxpd)) if (inherits(x, "singlenode")) stop("cannot plot singlenode tree") if (!inherits(x, "tree")) stop("not legitimate tree") frame <- x$frame column <- names(frame) if (!is.null(ylevels <- attr(x, "ylevels"))) column <- c(column, ylevels) if (!is.null(label) && is.na(match(label, column))) stop("label must be a column label of the frame component of the tree") charht <- par("cxy")[2] if (!is.null(srt <- list(...)$srt) && srt == 90) { if (missing(adj)) adj <- 0 ladj <- 1 - adj } else ladj <- adj xy <- treeco(x) if (splits) { node <- as.numeric(row.names(frame)) left.child <- match(2 * node, node) rows <- labels.tree.EJN(x, pretty = pretty, digits = dig)[left.child] ind <- !is.na(rows) text(xy$x[ind], xy$y[ind] + 0.5 * charht, rows[ind], adj = adj, ...) } if (!is.null(label)) { leaves <- if (all) rep(TRUE, nrow(frame)) else frame$var == "" if (label == "yval" & !is.null(ylevels)) { # EJN>>> Get in the class counts W.tr.f <- cbind(frame$yprob, frame$n) # Class label stat <- as.character(frame$yval[leaves]) classes <- dim(W.tr.f)[2] - 1 stat <- paste("\n", stat, "\n", W.tr.f[,1]*W.tr.f[,classes+1], sep="") for (i in (2:classes)) { stat <- paste(stat, "/", W.tr.f[,i]*W.tr.f[,classes+1], sep="") } #< 1) { if (length(dimnames(stat)[[2]])) stat[1, ] <- paste(sep = ":", dimnames(stat)[[2]], stat[1, ]) stat <- do.call("paste", c(list(sep = "\n"), split(stat, col(stat)))) } text(xy$x[leaves], xy$y[leaves] - 0.5 * charht, labels = stat, adj = ladj, ...) } invisible() } labels.tree.EJN <- function (object, pretty = TRUE, collapse = TRUE, digits = 7, ...) { if (!inherits(object, "tree")) stop("not legitimate tree") frame <- object$frame xlevels <- attr(object, "xlevels") var <- as.character(frame$var) # EJN>>> # Put spaces around < and > tmp <-sub("^>", " > ", sub("^<", " < ", frame$splits)) # Replace > - with >- and < - with <-. This lines up the first digits tmp <-sub("> -", ">-", sub("< -", "<-", tmp)) # Chop after the appropriate number of digits. (can't round without converting) tmp <- substr(tmp, 1, 5 + digits) # Put the space back after the - splits <- sub(">-", "> -", sub("<-", "< -", tmp)) #<", " > ", sub("^<", " < ", frame$splits)), , 2) if (!is.null(pretty)) { if (pretty) xlevels <- lapply(xlevels, abbreviate, minlength = pretty) for (i in grep("^:", splits[, 1])) for (j in 1:2) { sh <- splits[i, j] nc <- nchar(sh) sh <- substring(sh, 2:nc, 2:nc) xl <- xlevels[[var[i]]][match(sh, letters)] splits[i, j] <- paste(": ", paste(as.vector(xl), collapse = ","), sep = "") } } if (!collapse) return(array(paste(var, splits, sep = ""), dim(splits))) node <- as.numeric(row.names(frame)) parent <- match((node%/%2), node) odd <- as.logical(node%%2) node[odd] <- paste(var[parent[odd]], splits[parent[odd], 2], sep = "") node[!odd] <- paste(var[parent[!odd]], splits[parent[!odd], 1], sep = "") node[1] <- "root" node }