I have generated a simple tree using the rpart()
function, however I would like to be able to stop the second split at Petal.Length < 4.9
before it splits by Petal.Width
, however I would not like to alter anything else in the tree. The only thing that I have found is that I can use the subset function in order to manually grow the tree, but this process can be very tedious. Any suggestions on a function that could possibly be used? The code used to generate the tree is:
library(rpart)
library(datasets)
data("iris")
library(rpart.plot)
Sample <-sample.int(n = nrow(iris), size = floor(.7*nrow(iris)), replace = F)
train <- iris[Sample, ]
test <- iris[-Sample, ]
m1 <- rpart(Species~Sepal.Width + Sepal.Length + Petal.Length + Petal.Width,
data = train, control = rpart.control(cp = 0.005), method = "anova")
rpart.plot(m1, type = 3, fallen.leaves = TRUE)
One approach is to use the snip
argument of rpart.plot
:
trimmed.tree <- rpart.plot(m1, snip=TRUE))$obj # manually trim the tree
rpart.plot(trimmed.tree) # display the trimmed tree
This puts the tree on the screen, which you can manually prune with the mouse. For details, see Chapter 9 "Trimming a tree with the mouse" of the rpart.plot package vignette http://www.milbo.org/doc/prp.pdf .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.