Learn R Programming

itree (version 0.1)

plot.itree: Plot an itree Object

Description

Plots an itree object on the current graphics device. This is based on the plotting function from rpart but modifies/extends it in various ways to deal with some of itree's capabilities.

Usage

"plot"(x, uniform=FALSE, branch=1, compress=FALSE, nspace, margin=0, minbranch=.3, highlight.color="black",do_node_re=FALSE, ...)

Arguments

x
a fitted object of class itree, containing a classification, regression, or rate tree.
uniform
if TRUE, uniform vertical spacing of the nodes is used; this may be less cluttered when fitting a large plot onto a page. The default is to use a non-uniform spacing proportional to the error in the fit.
branch
controls the shape of the branches from parent to child node. Any number from 0 to 1 is allowed. A value of 1 gives square shouldered branches, a value of 0 give V shaped branches, with other values being intermediate.
compress
if FALSE, the leaf nodes will be at the horizontal plot coordinates of 1:nleaves. If TRUE, the routine attempts a more compact arrangement of the tree. The compaction algorithm assumes uniform=TRUE; surprisingly, the result is usually an improvement even when that is not the case.
nspace
the amount of extra space between a node with children and a leaf, as compared to the minimal space between leaves. Applies to compressed trees only. The default is the value of branch.
margin
an extra fraction of white space to leave around the borders of the tree. (Long labels sometimes get cut off by the default computation).
minbranch
set the minimum length for a branch to minbranch times the average branch length. This parameter is ignored if uniform=TRUE. Sometimes a split will give very little improvement, or even (in the classification case) no improvement at all. A tree with branch lengths strictly proportional to improvement leaves no room to squeeze in node labels.
highlight.color
If the itree object to be plotted is one-sided, setting highlight.color="RED", for example, will highlight the path to the node for which the split was chosen. For instance if method="purity", the highlights outline the pure nodes.
do_node_re
Set to TRUE if you wish to call text.itree() with risk estimates for each leaf node (default is FALSE). This matters in the plot function because space must be left to accommodate writing the risk estimates for each terminal node.
...
arguments to be passed to or from other methods.

Value

The coordinates of the nodes are returned as a list, with components x and y.

Side Effects

An unlabeled plot is produced on the current graphics device.

Details

This function is a method for the generic function plot, for objects of class itree. The y-coordinate of the top node of the tree will always be 1.

See Also

itree, text.itree

Examples

Run this code
#the rpart example:
fit <- itree(Price ~ Mileage + Type + Country, cu.summary)
plot(fit, compress=TRUE)
text(fit, use.n=TRUE)

### new to itree, plotting node risk:
require(mlbench); data(BostonHousing)
#fit a tree:
cart <- itree(medv~.,BostonHousing,minsplit=25,minbucket=25,cp=0)

#generate theta-hat values by computing average out-of-bag loss:
## Not run: 
# theta_hats <- getOOBLoss(model_tree.obj=cart.bh,data=bh,nboot=100)
# 
# # Then for each leaf we estimate local risk by the mean in-node theta-hat.
# lre <- estNodeRisk(tree.obj=cart.bh,est_observation_loss=theta_hats$avgOOBloss)
# 
# # to add the lre to the plot:
# plot(cart.bh, do_node_re= TRUE, uniform=TRUE)
# text(cart.bh, est_node_risk = lre)
# ## End(Not run)

#plot using highlighting for one-sided methods:
purity.tree <- itree(medv~.,BostonHousing,minsplit=25,minbucket=25,cp=0,method="purity")
plot(purity.tree,highlight.color="blue")
text(purity.tree)

Run the code above in your browser using DataLab