3
iris <- read.csv("iris.csv") #iris data available in R
library(rpart)
iris.rpart <- rpart(Species~Sepal.length+Sepal.width+Petal.width+Petal.length, 
                 data=iris)
plotcp(iris.rpart)
printcp(iris.rpart)
iris.rpart1 <- prune(iris.rpart, cp=0.047)
plot(iris.rpart1,uniform=TRUE)
text(iris.rpart1, use.n=TRUE, cex=0.6)

I have tried to get the rpart done on the iris data. However, is it possible by using some function in R to get the rules applied by rpart for this current tree preparation so that we know how the classifications are made when we add further new points to the data set?

jbaums
  • 27,115
  • 5
  • 79
  • 119

1 Answers1

1

The rpart.plot package has a function rpart.rules for generating a set of rules for a tree. For example

library(rpart.plot)
iris.rpart <- rpart(Species~., data=iris)
rpart.rules(iris.rpart)

gives

   Species  seto vers virg
    setosa [1.00  .00  .00] when Petal.Length <  2.5
versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8
 virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8

And

options(width=1000)
rpart.predict(iris.rpart, newdata=iris[50:52,], rules=TRUE)

gives you the rule used to make each prediction:

   setosa versicolor virginica
50      1    0.00000  0.000000 because Petal.Length <  2.5
51      0    0.90741  0.092593 because Petal.Length >= 2.5 & Petal.Width <  1.8
52      0    0.90741  0.092593 because Petal.Length >= 2.5 & Petal.Width <  1.8

For more examples see Chapter 4 of the rpart.plot vignette.

Stephen Milborrow
  • 976
  • 10
  • 14