Converting "rule lists" into "tree objects"

Using the famous "iris" data set, I was able to make a decision tree and plot the results:

#load libraries

#load data

# make tree   
r <- rpart(Species ~ .,data = iris)
#plot tree

enter image description here

From the following website (Data Mining Survivor: Tuning_Parameters - Convert Tree to Rules), I found a way to neatly extract the "rules list" associated with this tree:

# extract "rules list"

list.rules.rpart <- function(model)
    if (!inherits(model, "rpart")) stop("Not a legitimate rpart tree")
    # Get some information.
    frm     <- model$frame
    names   <- row.names(frm)
    ylevels <- attr(model, "ylevels")
    ds.size <- model$frame[1,]$n
    # Print each leaf node as a rule.
    for (i in 1:nrow(frm))
        if (frm[i,1] == "<leaf>")
            # The following [,5] is hardwired - needs work!
            cat(sprintf(" Rule number: %s ", names[i]))
            cat(sprintf("[yval=%s cover=%d (%.0f%%) prob=%0.2f]\n",
                        ylevels[frm[i,]$yval], frm[i,]$n,
                        round(100*frm[i,]$n/ds.size), frm[i,]$yval2[,5]))
            pth <- path.rpart(model, nodes=as.numeric(names[i]),
            cat(sprintf("   %s\n", unlist(pth)[-1]), sep="")

#view rules
 my_rules = list.rules.rpart(r)

This looks as follows:

Rule number: 2 [yval=setosa cover=50 (33%) prob=1.00]
   Petal.Length< 2.45

 Rule number: 6 [yval=versicolor cover=54 (36%) prob=0.00]
   Petal.Width< 1.75

 Rule number: 7 [yval=virginica cover=46 (31%) prob=0.00]

My Question:

Suppose I only had the "my_rules" file - e.g. let's suppose I manually take the rules and put them into a data frame:

# manually place rules into a data frame

rules_frame <- data.frame("class" = c("setosa", "versicolor", "virginica"), 
"condition" = c("Petal.Length < 2.45", " Petal.Length>= 2.45 & Petal.Width < 1.75", "Petal.Length >=2.45 & Petal.Width>=1.75"))

#view result

       class                                 condition
1     setosa                       Petal.Length < 2.45
2 versicolor  Petal.Length>= 2.45 & Petal.Width < 1.75
3  virginica   Petal.Length >=2.45 & Petal.Width>=1.75

Is it somehow possible to take "rules_frame" and pass it to the "rpart.plot()" command? This would effectively allow me to take a set of "rules" generated by any tree library in R (e.g. "party", "C50", etc.) and plot them using the "rpart.plot" library.

Can anyone please show me how to do this?


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.