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