Building, plotting, and evaluating – classification trees

You can use a couple of R packages to build classification trees. Under the hood, they all do the same thing.

Getting ready

If you do not already have the rpart, rpart.plot, and caret packages, install them now. Download the data files for this chapter from the book's website and place the banknote-authentication.csv file in your R working directory.

How to do it...

This recipe shows you how you can use the rpart package to build classification trees and the rpart.plot package to generate nice-looking tree diagrams:

  1. Load the rpart, rpart.plot, and caret packages:
    > library(rpart)
    > library(rpart.plot)
    > library(caret)
  2. Read the data:
    > bn <- read.csv("banknote-authentication.csv")
  3. Create data partitions. ...

Get R Data Analysis Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.