Path from root to leaf node in mvpart

I was recently asked by a R user about how one could extract the “rule” in a classification/regression tree. The requirement was to obtain the path traced from the root node to the leaf nodes and obtain all the paths or “rules”

path.rpart() function in the mvpart package provides this convenience


# Create a classification tree
ozone <- mvpart(Ozone ~ ., data=airquality)
print(ozone) # Gives you the various splits in the tree

# Issue the two commands below to see the graphical representation

# To obtain the summary of the created tree

# To obtain the path to the leaf nodes
leafnodeRows <- grepl("leaf",ozone$frame$var)
nodevals <- as.numeric(rownames(ozone$frame)[leafnodeRows])
rules <- path.rpart(ozone,nodevals)
rulesdf <-"rbind",lapply(rules,function(x)paste(x,collapse = " -AND- ")))
rulesdf <- data.frame(nodeNumber=rownames(rulesdf),rule=rulesdf[,1],stringsAsFactors=FALSE)

About indiacrunchin
Any sufficiently advanced technology is indistinguishable from magic -- Arthur C. Clarke

One Response to Path from root to leaf node in mvpart

  1. sayan dasgupta says:

    Great Stuff !! My guess it will be even more interesting if you put the decision/probability in a column in the the rulesdf data.frame corresponding to the rule

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: