Desktop Survival Guide
by Graham Williams


A traditional decision tree can be built from the wine dataset provided with the rattle package, again using the rpart function.

> library(rattle)
> train <- c(sample(nrow(wine), 0.5*nrow(wine)))
> wi.rpart <- rpart(Type ~ ., data=wine[train,])
> wi.rpart

We are able to display some possibly useful information about the decision tree with a mixture of functions and direct access to the decision tree object. The where element of the rpart object lists the terminal branch that each observation in the training dataset ends up in.

> printcp(wi.rpart)
> formula(wine.rpart)
> wi.rpart$where

We can interact with the plot of the decision tree using the function path.rpart. After we have plot the decision tree we can left click on a node of the tree to interactively have information about that node displayed in the R Console. We exit the interactive mode with a right click of the mouse button.

> plot(wi.rpart)
> text(wi.rpart)
> path.rpart(wi.rpart)

Display an error matrix.

> table(predict(wine.rpart, wine[-train,], type="class"), 
        wine[-train, "Type"])

The mvpart function of the mvpart package provides some additional functionality and displays a different plot of the decision tree after building it.

> library(mvpart)
> wi.mvpart <- mvpart(Type ~ ., data=wine[train,])

Recent research has lead to the development of the conditional tree using ctree from the party package:

> library(party)
> wi.ctree <- ctree(Type ~ ., data=wine[train,])
> plot(wi.ctree)

Copyright © Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
The PDF version is a formatted comprehensive draft book (with over 800 pages).
Brought to you by Togaware. This page generated: Sunday, 22 August 2010