0

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)

Decision Tree

OTStats
  • 1,820
  • 1
  • 13
  • 22
user1234
  • 1
  • 1
  • 1
    You should also learn to build reproducible examples with `set.seed()`. When I run your code I don't even get a split on "Petal.Width". – IRTFM Jan 20 '19 at 19:12
  • Thank you I added that. That split was just an example though, I would ideally like to be able to apply this logic to a much larger tree – user1234 Jan 20 '19 at 19:15

1 Answers1

0

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.

Stephen Milborrow
  • 976
  • 10
  • 14