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.
1 comment:
When I was in college I had the opportunity to use regression trees through the SAS Enterprise Miner. I actually have in one of my computers SAS EG along with the Miner.
I have been following you for about a year and a half now and because of your examples I was able to self-study (with the SAS book as well) and pass the SAS certification.
Would you please in the future post Regression trees using the SAS Enterprise Miner?
Best Regards,
Alexander Hayes
Post a Comment