##Two classification trees for the Iris dataset are used

library(DMwR2)
## Warning: package 'DMwR2' was built under R version 4.2.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Using set.seed() function to make sure the same trees are processed each time for setting up the model form and training data.
set.seed(1234)
data(iris)
ct1 <- rpartXse(Species ~., iris)
ct2 <- rpartXse(Species ~., iris, se=0)

##Visualization of the two objects (ct1 and ct2)

library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 4.2.2
## Loading required package: rpart
## Warning: package 'rpart' was built under R version 4.2.2

Pruning for the first tree produced on the left of the graphics. Pruning for the second tree produced graphics on the right.

prp(ct1, type = 0, extra=101)
## Warning: Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).
## To silence this warning:
##     Call prp with roundint=FALSE,
##     or rebuild the rpart model with model=TRUE.
prp(ct2, type=0,extra=101)
## Warning: Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).
## To silence this warning:
##     Call prp with roundint=FALSE,
##     or rebuild the rpart model with model=TRUE.

Predict the results

set.seed(1234)
rndSample <- sample(1:nrow(iris),100)
## Train the iris data set
tr <- iris[rndSample, ]
## Test the iris data set
ts <- iris[- rndSample, ]
ct <- rpartXse(Species ~., tr, se=0.5)
## predict function generates a matrix of the possible output from test data and generated model 
psi <- predict(ct, ts)
head(psi)
##    setosa versicolor virginica
## 1       1          0         0
## 7       1          0         0
## 11      1          0         0
## 12      1          0         0
## 15      1          0         0
## 16      1          0         0

##predict another

ps2 <- predict(ct, ts, type="class")
head(ps2)
##      1      7     11     12     15     16 
## setosa setosa setosa setosa setosa setosa 
## Levels: setosa versicolor virginica

Results

(cm <- table(ps2, ts$Species))
##             
## ps2          setosa versicolor virginica
##   setosa         18          0         0
##   versicolor      0         15         1
##   virginica       0          3        13

Calculate the error rate

100*(1-sum(diag(cm))/sum(cm))
## [1] 8

Conclusion:

#33% of the Species Setosa (all 50) have a petal length of less than 2.5. 32% of Versicolor (47) have a petal length less than 5 and a petal width less than 1.8, with 2% with a petal width around 1.6. 31% of Virginica has a petal width less than 1.8 and 2% with petal length less than 5, but 2% petal width around 1.6.