[fbf06f]: / partyMod / R / Print.R

Download this file

152 lines (131 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# $Id$
prettysplit <- function(x, inames = NULL, ilevels = NULL) {
if (length(x) == 4)
names(x) <- c("variableID", "ordered", "splitpoint", "splitstatistics")
if (length(x) == 5)
names(x) <- c("variableID", "ordered", "splitpoint", "splitstatistics",
"toleft")
if (length(x) == 6)
names(x) <- c("variableID", "ordered", "splitpoint", "splitstatistics",
"toleft", "table")
if (x$ordered) {
class(x) <- "orderedSplit"
} else {
class(x) <- "nominalSplit"
}
if (!is.null(ilevels)) {
if (!is.null(ilevels[x[["variableID"]]]))
attr(x$splitpoint, "levels") <- ilevels[[x[["variableID"]]]]
}
if (!is.null(inames)) x$variableName <- inames[x[["variableID"]]]
return(x)
}
prettytree <- function(x, inames = NULL, ilevels = NULL) {
names(x) <- c("nodeID", "weights", "criterion", "terminal",
"psplit", "ssplits", "prediction", "left", "right")
if (is.null(inames) && extends(class(x), "BinaryTree"))
inames <- names(x@data@get("input"))
names(x$criterion) <- c("statistic", "criterion", "maxcriterion")
names(x$criterion$criterion) <- inames
names(x$criterion$statistic) <- inames
if (x$terminal) {
class(x) <- "TerminalNode"
return(x)
}
x$psplit <- prettysplit(x$psplit, inames = inames, ilevels = ilevels)
if (length(x$ssplit) > 0)
x$ssplit <- lapply(x$ssplit, prettysplit, inames = inames,
ilevels = ilevels)
class(x) <- "SplittingNode"
x$left <- prettytree(x$left, inames = inames, ilevels = ilevels)
x$right <- prettytree(x$right, inames = inames, ilevels = ilevels)
return(x)
}
print.TerminalNode <- function(x, n = 1, ...) {
cat(paste(paste(rep(" ", n - 1), collapse = ""), x$nodeID, ")* ",
sep = "", collapse = ""),
"weights =", sum(x$weights), "\n")
}
print.SplittingNode <- function(x, n = 1, ...) {
cat(paste(paste(rep(" ", n - 1), collapse = ""), x$nodeID, ") ", sep=""))
print(x$psplit, left = TRUE)
cat(paste("; criterion = ", round(x$criterion$maxcriterion, 3),
", statistic = ", round(max(x$criterion$statistic), 3), "\n",
collapse = "", sep = ""))
print(x$left, n + 2)
cat(paste(paste(rep(" ", n - 1), collapse = ""), x$nodeID, ") ", sep=""))
print(x$psplit, left = FALSE)
cat("\n")
print(x$right, n + 2)
}
print.orderedSplit <- function(x, left = TRUE, ...) {
if (!is.null(attr(x$splitpoint, "levels"))) {
sp <- attr(x$splitpoint, "levels")[x$splitpoint]
} else {
sp <- x$splitpoint
}
if (!is.null(x$toleft)) left <- as.logical(x$toleft) == left
if (left) {
cat(x$variableName, "<=", sp)
} else {
cat(x$variableName, ">", sp)
}
}
print.nominalSplit <- function(x, left = TRUE, ...) {
levels <- attr(x$splitpoint, "levels")
### is > 0 for levels available in this node
tab <- x$table
if (left) {
lev <- levels[as.logical(x$splitpoint) & (tab > 0)]
} else {
lev <- levels[!as.logical(x$splitpoint) & (tab > 0)]
}
txt <- paste("{", paste(lev, collapse = ", "), "}", collapse = "", sep = "")
cat(x$variableName, "==", txt)
}
print.BinaryTreePartition <- function(x, ...)
print(x@tree)
print.BinaryTree <- function(x, ...) {
cat("\n")
cat("\t Conditional inference tree with", length(unique(where(x))),
"terminal nodes\n\n")
y <- x@responses
if (y@ninputs > 1) {
cat("Responses:", paste(names(y@variables),
collapse = ", "), "\n")
} else {
cat("Response: ", names(y@variables), "\n")
}
inames <- names(x@data@get("input"))
if (length(inames) > 1) {
cat("Inputs: ", paste(inames, collapse = ", "), "\n")
} else {
cat("Input: ", inames, "\n")
}
cat("Number of observations: ", x@responses@nobs, "\n\n")
print(x@tree)
}
print.RandomForest <- function(x, ...) {
cat("\n")
cat("\t Random Forest using Conditional Inference Trees\n")
cat("\n")
cat("Number of trees: ", length(x@ensemble), "\n")
cat("\n")
y <- x@responses
if (y@ninputs > 1) {
cat("Responses:", paste(names(y@variables),
collapse = ", "), "\n")
} else {
cat("Response: ", names(y@variables), "\n")
}
inames <- names(x@data@get("input"))
if (length(inames) > 1) {
cat("Inputs: ", paste(inames, collapse = ", "), "\n")
} else {
cat("Input: ", inames, "\n")
}
cat("Number of observations: ", x@responses@nobs, "\n\n")
invisible(x)
}
setMethod("show", "BinaryTree", function(object) print(object))
setMethod("show", "RandomForest", function(object) print(object))