规则存储在fit$FittedTrees[[1]]
采用相对难以解释的表格格式。
我为您构建了一个相当长的函数,它将提取规则作为数据框,并根据要求另外将树绘制为 ggplot。
RLT_tree <- function(RLT_obj, plot = TRUE)
{
tree <- as.data.frame(t(RLT_obj$FittedTrees[[1]]))
tree <- tree[c(2, 3, 5, 6, 8, 9, grep("Class\\d", names(tree)))]
class_cols <- grep("Class\\d", names(tree))
names(tree)[class_cols] <-
RLT_obj$ylevels[1 + as.numeric(sub("Class(\\d+)", "\\1",
names(tree)[class_cols]))]
tree$variable <- RLT_obj$variablenames[tree$SplitVar1]
tree$variable[is.na(tree$variable)] <- "(Leaf node)"
tree$rule <- tree$variable
tree$depth <- numeric(nrow(tree))
tree$rightness <- numeric(nrow(tree))
tree$group <- character(nrow(tree))
walk_tree <- function(node, depth, rightness, node_label = "Start", group = "S")
{
new_row <- tree[which(tree$Node == node),]
new_row$depth <- depth
new_row$rightness <- rightness
left_label <- paste(new_row$variable, new_row$SplitValue, sep = " < ")
right_label <- paste(new_row$variable, new_row$SplitValue, sep = " > ")
new_row$variable <- paste(node_label, "\nn = ", new_row$NumObs)
new_row$rule <- node_label
if(is.nan(new_row$SplitValue)) {
n_objs <- round(new_row[,class_cols] * new_row$NumObs)
classify <- paste((names(tree)[class_cols])[n_objs != 0],
n_objs[n_objs != 0],
collapse = "\n")
new_row$variable <- paste(new_row$variable, classify, sep = "\n")
}
new_row$group <- group
tree[which(tree$Node == node),] <<- new_row
if(!is.nan(new_row$SplitValue)){
walk_tree(new_row$NextLeft, depth + 1, rightness - 2/(depth/2 + 1),
left_label, paste(group, "L"))
walk_tree(new_row$NextRight, depth + 1, rightness + 2/(depth/2 + 1),
right_label, paste(group, "R"))
}
}
walk_tree(0, 0, 0)
tree$depth <- max(tree$depth) - tree$depth
tree$type <- is.nan(tree$NextLeft)
tree$group <- substr(tree$group, 1, nchar(tree$group) - 1)
if(plot)
{
print(ggplot(tree, aes(rightness, depth)) +
geom_segment(aes(x = rightness, xend = rightness,
y = depth, yend = depth - 1, alpha = type)) +
geom_line(aes(group = group)) +
geom_label(aes(label = variable, fill = type), size = 4) +
theme_void() +
scale_x_continuous(expand = c(0, 1)) +
suppressWarnings(scale_alpha_discrete(range = c(1, 0))) +
theme(legend.position = "none"))
}
tree$isLeaf <- is.nan(tree$NextLeft)
tree[c(match(c("Node", "rule", "depth", "isLeaf"), names(tree)), class_cols)]
}
这允许:
df <- RLT_tree(fit, plot = TRUE)
and
df
#> Node rule depth isLeaf setosa versicolor virginica
#> 1 0 Start 6 FALSE 0.3111111 0.34814815 0.3407407
#> 2 1 Sepal.Width < 3.2 5 FALSE 0.1573034 0.51685393 0.3258427
#> 3 2 Sepal.Width > 3.2 5 FALSE 0.6086957 0.02173913 0.3695652
#> 4 3 Sepal.Length < 5.4 4 FALSE 0.7000000 0.30000000 0.0000000
#> 5 4 Sepal.Length > 5.4 4 TRUE 0.0000000 0.57971014 0.4202899
#> 6 5 Petal.Length < 1.3 3 TRUE 1.0000000 0.00000000 0.0000000
#> 7 6 Petal.Length > 1.3 3 FALSE 0.6000000 0.40000000 0.0000000
#> 8 7 Petal.Length < 1.4 2 TRUE 1.0000000 0.00000000 0.0000000
#> 9 8 Petal.Length > 1.4 2 FALSE 0.5000000 0.50000000 0.0000000
#> 10 9 Petal.Length < 3.9 1 FALSE 0.7500000 0.25000000 0.0000000
#> 11 10 Petal.Length > 3.9 1 TRUE 0.0000000 1.00000000 0.0000000
#> 12 11 Sepal.Length < 4.9 0 TRUE 1.0000000 0.00000000 0.0000000
#> 13 12 Sepal.Length > 4.9 0 TRUE 0.0000000 1.00000000 0.0000000
#> 14 13 Petal.Width < 0.2 4 TRUE 1.0000000 0.00000000 0.0000000
#> 15 14 Petal.Width > 0.2 4 FALSE 0.3793103 0.03448276 0.5862069
#> 16 15 Sepal.Length < 5.7 3 TRUE 1.0000000 0.00000000 0.0000000
#> 17 16 Sepal.Length > 5.7 3 FALSE 0.0000000 0.05555556 0.9444444
#> 18 17 Sepal.Width < 3.3 2 TRUE 0.0000000 0.00000000 1.0000000
#> 19 18 Sepal.Width > 3.3 2 FALSE 0.0000000 0.08333333 0.9166667
#> 20 19 Petal.Length < 6.1 1 FALSE 0.0000000 0.11111111 0.8888889
#> 21 20 Petal.Length > 6.1 1 TRUE 0.0000000 0.00000000 1.0000000
#> 22 21 Sepal.Length < 6.3 0 TRUE 0.0000000 0.16666667 0.8333333
#> 23 22 Sepal.Length > 6.3 0 TRUE 0.0000000 0.00000000 1.0000000
为了在更一般的情况下展示这一点,我们还可以这样做:
fit2 = RLT(mtcars[,1:3], factor(rownames(mtcars)), model = "classification", ntrees = 1)
df <- RLT_tree(fit2)