要绘制树的每个节点的标准差,您可以使用 rpart.plot 和
node.fun,如第 6 章所述
rpart.plot package vignette。
例如
library(rpart.plot)
data(iris)
tree = rpart(Sepal.Length~., data=iris, cp=.05) # example tree
# Calculate the standard deviation at each node of the tree.
sd <- sqrt(tree$frame$dev / (tree$frame$n-1))
# Append the standard deviation as an extra column to the tree frame.
tree$frame <- cbind(tree$frame, sd)
# Create a node.fun to print the standard deviation at each node.
# See Chapter 6 of the rpart.plot vignette http://www.milbo.org/doc/prp.pdf.
node.fun.sd <- function(x, labs, digits, varlen)
{
s <- round(x$frame$sd, 2) # round sd to 2 digits
paste(labs, "\n\nsd", s)
}
# Plot the tree, using the node.fun to add the standard deviation to each node
rpart.plot(tree, type=4, node.fun=node.fun.sd)
给了
如果您只想要叶节点的标准偏差(而不是
内部节点),您可以这样做:
library(rpart.plot)
data(iris)
tree = rpart(Sepal.Length~., data=iris, cp=.05)
sd <- sqrt(tree$frame$dev / (tree$frame$n-1))
is.leaf <- tree$frame$var == "<leaf>" # logical vec, indexed on row in frame
sd[!is.leaf] <- NA # change sd of non-leaf nodes to NA
tree$frame <- cbind(tree$frame, sd)
node.fun2 <- function(x, labs, digits, varlen)
{
s <- paste("\n\nsd", round(x$frame$sd, 2)) # round sd to 2 digits
s[is.na(x$frame$sd)] <- "" # delete NAs
paste(labs, s)
}
rpart.plot(tree, type=4, node.fun=node.fun2)
给了