We discuss recursive partitioning, a technique for classification and regression using a decision tree in section 6.7.3 of the book. Support for these methods is available within the rpart package. Torsten Hothorn and Achim Zeileis have extended the support for these methods with the partykit package, which provides a toolkit with infrastructure for representing, summarizing, and visualizing tree-structured regression and classification models.

In this entry, we revisit the example from the book, which worked to classify predictors of homelessness in the HELP study.

**R**

ds = read.csv("http://www.math.smith.edu/r/data/help.csv")

library(rpart); library(partykit)

ds$sub = as.factor(ds$substance)

homeless.rpart = rpart(homeless ~ female + i1 + sub + sexrisk + mcs +

pcs, method="class", data=ds)

plot(homeless.rpart)

text(homeless.rpart)

This reproduces Figure 6.2 (p. 236) from the book, while we can display the output from the classification tree using the `printcp()` command.

> printcp(homeless.rpart)

Classification tree:

rpart(formula = home ~ female + i1 + sub + sexrisk + mcs + pcs,

data = ds, method = "class")

Variables actually used in tree construction:

[1] female i1 mcs pcs sexrisk

Root node error: 209/453 = 0.5

n= 453

CP nsplit rel error xerror xstd

1 0.10 0 1.0 1.0 0.05

2 0.05 1 0.9 1.1 0.05

3 0.03 4 0.8 1.1 0.05

4 0.02 5 0.7 1.0 0.05

5 0.01 7 0.7 0.9 0.05

6 0.01 9 0.7 0.9 0.05

Using the partykit package, we can make a nice graphic describing these results. We’ll use the `plot.party()` function on a party object (coerced from the rpart object generated above using `as.party()`). This provides more information about the tree (as seen in the Figure above).

plot(as.party(homeless.rpart), type="simple")

More information as well as a lovely vignette can be found here.

**SAS**

Recursive partitioning is available through SAS Enterprise Miner, a module not always included in SAS installations.

*Related*

To

**leave a comment** for the author, please follow the link and comment on their blog:

** SAS and R**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as:

Data science,

Big Data, R jobs, visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...

**Tags:** HELP data set, partykit package, Recursive Partitioning, regression trees, rpart package