One of the most attractive properties of trees is that they can be
graphically displayed. We use the plot()
function to display the tree structure,
and the text()
function to display the node labels. The argument
pretty=0
instructs R to include the category names for any qualitative predictors,
rather than simply displaying a letter for each category.
plot(tree.carseats)
text(tree.carseats, pretty = 0)
(Click on the image to see the full tree.)
The most important indicator of Sales
appears to be shelving location,
since the first branch differentiates Good
locations from Bad
and Medium
locations.
tree.carseats
node), split, n, deviance, yval, (yprob)
* denotes terminal node
1) root 400 541.500 No ( 0.59000 0.41000 )
2) ShelveLoc: Bad,Medium 315 390.600 No ( 0.68889 0.31111 )
4) Price < 92.5 46 56.530 Yes ( 0.30435 0.69565 )
8) Income < 57 10 12.220 No ( 0.70000 0.30000 )
...
If we just type the name of the tree object, R
prints output corresponding
to each branch of the tree. R
displays the split criterion (e.g. Price<92.5
), the
number of observations in that branch, the deviance, the overall prediction
for the branch (Yes
or No
), and the fraction of observations in that branch
that take on values of Yes
and No
. Branches that lead to terminal nodes are
indicated using asterisks.
Purchase
as dependent variable and all other variables as independent variables.
Store the model in the variable tree.oj
.Purchase
?
PriceDiff
ListPriceDiff
LoyalCH
PriceCH
tree.oj
object.
When building the tree, how many of the training observations were classified following the path LoyalCH < 0.5036
, LoyalCH > 0.276142
, and PriceDiff < 0.05
. n
in the output)
n
= 75n
= 40n
= 145n
= 101Assume that: