Tutorials › Exam PA - Predictive Analytics › Plotting Trees And Displaying Decision Rules

Exam PA · Single Trees and Rule-Based Prediction · Tutorial 501 of 966

Plotting Trees And Displaying Decision Rules

Use tree plots and rule displays to trace observations to terminal nodes and interpret the information shown at each node.

Intermediate 12 min read

What You'll Learn

  • Plot an rpart tree and add readable split labels and node counts
  • Distinguish internal-node split conditions from terminal-node predictions
  • Interpret regression fitted values and classification class distributions
  • Trace a terminal node back to its root-to-leaf decision rule
  • Check plotted labels against the fitted tree object in R
  • Recognize what node counts and displayed summaries do not establish

Why Plot a Fitted Tree?

A fitted tree is a set of successive decisions, but its printed structure can be difficult to scan. A plot makes the sequence visible: each internal node applies a condition, each branch represents an outcome of that condition, and each terminal node gives a prediction for the observations reaching it. A decision-rule display expresses the same route as a list of conditions.

The plot is an inspection tool, not a replacement for the fitted object. Its labels may show a subset of the available information, and display options can change what appears. In particular, a terminal-node prediction is not the same thing as the node's sample count or, for a classification tree, the full distribution of classes within that node.

Definition: A node is a group of observations that reach the same point in a tree. An internal node applies a split condition and sends observations down branches. A terminal node (or leaf) applies no further split and supplies the fitted prediction for observations that reach it. A decision rule for a terminal node is the conjunction of the split conditions along the path from the root to that node.

Plotting an rpart Tree

For a model fitted with rpart, base R can draw the tree with plot() and add labels with text(). The use.n=TRUE argument asks for the number of observations at nodes; all=TRUE asks for labels at internal as well as terminal nodes; and pretty=0 avoids abbreviating factor labels.

$$ \texttt{plot(fit)} \qquad \texttt{text(fit, use.n=TRUE, all=TRUE, pretty=0)} $$

Here fit is an already fitted rpart object. For a regression tree, the node label typically includes the fitted response value, and use.n=TRUE adds the node count. For a classification tree, the node label typically identifies the predicted class; do not assume that it also shows every class probability. Inspect the object or use a suitable display option when the full class distribution matters.

The optional rpart.plot package provides alternatives for arranging labels and showing additional node information. For example, rpart.plot(fit, type=2, extra=1) requests a plot with split labels placed below nodes and the number of observations displayed. The available extras and label formats depend on the function and its options. Always verify what a displayed value represents rather than inferring its meaning from its position.

Worked Example: Reading Regression Values and Counts

A hypothetical insurer fits an unweighted regression tree to predict a claim-cost index, measured in thousands of dollars. The displayed portion of the fitted tree has a root node with 40 observations and prediction 12.0. It splits on vehicle age: age less than 5 years goes left, and age at least 5 goes right. The left child has 16 observations and prediction 8.0; the right child has 24 observations and prediction 14.6667. The left child splits again on annual mileage: under 10,000 miles has 10 observations and prediction 6.0, while at least 10,000 miles has 6 observations and prediction 11.3333. Identify the rule and prediction for a 4-year-old vehicle with 12,000 annual miles, and check the displayed summaries.

Method. Start at the root and evaluate each split condition in order. The vehicle age is 4, so \(4 < 5\): follow the left branch. At that node, annual mileage is 12,000, so it is not under 10,000: follow the right branch. The observation reaches the terminal node with 6 observations and fitted prediction 11.3333 thousand dollars.

The decision rule is therefore: vehicle age is less than 5 years and annual mileage is at least 10,000 miles. The prediction in dollars is \(11.3333 \times 1{,}000 = \$11{,}333.30\) if the displayed value is treated as exact to four decimal places. The tree itself has made its prediction on the stated thousands-of-dollars scale; the dollar conversion is only a change of units.

Check. The child counts add to the parent counts: \(16+24=40\), and \(10+6=16\). For an unweighted squared-error regression tree, a parent fitted value is the mean of the response values in that node. The child predictions give a weighted mean of \( (16 \times 8.0 + 24 \times 14.6667)/40 = 480.0008/40 = 12.00002\), which agrees with the displayed root value 12.0 to the shown precision. The slight difference is rounding in the displayed child value.

Common mistake. Choosing the left terminal node just because age followed the left branch stops the traversal too soon. The left child is an internal node, so its mileage condition must also be applied. The final prediction comes from the terminal node, not the first child reached.

What Node Labels Mean

A useful reading order is to identify the response scale, read the split condition, check the branch direction, and then interpret the destination node. Node numbers, when displayed, are identifiers rather than measurements. A label such as \(n=18\) is a count of observations reaching that node, while a regression value such as 7.4 is a fitted response value. These quantities answer different questions.

In an unweighted regression tree using squared-error loss, the terminal-node fitted value is the mean response among the training observations in that leaf. This gives a simple check on a plot if the response values or their summaries are supplied. It does not mean that every observation in the leaf had that value. The prediction is constant throughout the leaf, even when the individual responses differ.

For a classification tree, a terminal node contains observations from one or more response classes. The predicted class is generally the class with the largest estimated proportion in that node, according to the model's classification rule. A plot that shows only the predicted class hides the size and composition of the other classes. For example, a predicted class of “Renew” could come from a node where 9 of 10 observations renew or from one where 51 of 100 renew. The class label alone does not distinguish those situations.

Interpretation rule: Read a node's predicted class, fitted value, count, and class distribution as distinct pieces of information. A count describes how many observations reached the node; a fitted value or predicted class summarizes the model's prediction; class probabilities or proportions describe the estimated distribution across classes.

Worked Example: Interpreting a Classification Node

A hypothetical retention model predicts whether a policy will renew. One terminal node contains 20 training policies: 12 renewed and 8 did not. Assume the tree assigns the class with the larger within-node proportion, with no special misclassification costs. Determine the class prediction and the estimated class proportions. Then interpret what a plot that displays only the predicted class would omit.

Method. Divide each class count by the total node count. The estimated proportion that renewed is \(12/20=0.60\). The estimated proportion that did not renew is \(8/20=0.40\). These proportions sum to \(0.60+0.40=1.00\), as they must when the two classes exhaust the outcomes in the node.

Because \(0.60 > 0.40\), the predicted class is “renewed.” Under the stated assumptions, the estimated class distribution is 60% renewed and 40% not renewed. A display showing only “renewed” omits the fact that 8 of the 20 training policies in the node did not renew. It also omits the node count unless the count is separately displayed.

Common mistake. Reporting the predicted class as a 100% renewal rate confuses the model's assigned class with the observed class distribution. The predicted class is one label; the within-node proportions are \(0.60\) and \(0.40\), not \(1.00\) and \(0.00\).

From a Plot to an Explicit Decision Rule

A path to a terminal node is formed by combining the conditions encountered from the root down. If the path first requires \(X_1 < c\), then \(X_2 \geq d\), then \(X_3\) to belong to one of several factor levels, the rule is the conjunction of all three conditions. Every condition must be satisfied for an observation to reach that leaf.

For an rpart fit, path.rpart() can list the path to a specified node. The node identifiers are available as row names of the fitted frame, so a practical inspection workflow is to examine those identifiers and then request the path. The printed path should still be read in the context of the branch conditions and factor levels in the fitted tree.

$$ \texttt{row.names(fit\$frame)} \qquad \texttt{path.rpart(fit, nodes = node\_id)} $$

The expression above is schematic: replace node_id with the node identifier of interest. In R code, the dollar sign selects a component of an object; the displayed form here indicates the frame component of fit. For factor predictors, a branch may correspond to a set of levels rather than a single level. Preserve the full set shown by the model; abbreviating it in a report can change the rule.

Worked Example: Checking a Factor-Based Rule

A hypothetical tree predicts whether a small-business account requires manual review. The root split sends “North” and “Central” regions left and “South” and “West” regions right. The right branch splits on annual premium: less than \$5,000 goes left and at least \$5,000 goes right. The right terminal node has 15 accounts, of which 11 were classified as requiring review and 4 as not requiring review. An account is in the West region with an annual premium of \$5,000. State its rule, predicted class, and within-node class proportions.

Method. First evaluate the factor split. West is in the right-hand group, so follow the right branch. The premium is exactly \$5,000. Since the split condition for the left branch is “less than \$5,000,” equality does not satisfy it; the account follows the right branch to the terminal node.

The rule is: region is South or West and annual premium is at least \$5,000. The class proportions are \(11/15=0.7333\) for “requires review” and \(4/15=0.2667\) for “does not require review,” to four decimal places. Since \(11>4\), the predicted class is “requires review.” The proportions sum to \(0.7333+0.2667=1.0000\), subject to rounding.

Common mistake. Treating “less than \$5,000” as “less than or equal to \$5,000” sends the account to the wrong branch. Read the inequality literally, especially for values equal to a numeric cut point. Also retain both factor levels on the West-side branch: this rule does not say that West alone is sufficient.

A Reliable Inspection Sequence

Use a consistent sequence when reading a tree plot or checking a displayed rule. This separates the routing logic from the node summaries and reduces the chance of reporting an incomplete condition.

1
Identify the response and scale.
Confirm whether the model is a regression tree or a classification tree, and check the units or class labels.
2
Trace each split literally.
At each internal node, evaluate the condition and follow the branch it specifies. Pay attention to strict versus inclusive inequalities and complete factor-level groups.
3
Stop at a terminal node.
Record its fitted value or predicted class. Do not treat an internal-node summary as the final prediction for an observation that continues to a leaf.
4
Read counts and distributions separately.
Check what the plotting function actually displays, and consult the fitted object when class probabilities or other node details are needed.
5
State the complete rule.
Combine every condition from the root to the selected leaf, preserving branch direction, threshold equality, and factor levels.

R Display Checks and Limitations

A plot is a compact summary, so confirm important interpretations against the fitted object. For example, inspect print(fit), summary(fit), and the model frame when a plotted label is unclear. A plotting option may suppress counts, show only a predicted class, round a fitted value, or abbreviate factor labels. Rounded display values are for readability; do not use them as if they were unrounded model calculations.

Node counts also need context. In an unweighted fit, they indicate how many training observations reached the node. If case weights were used, the fitted summaries may be weight-dependent while the displayed count still counts records. Thus a count of 20 does not necessarily mean that the node contributes total weight 20. Neither count nor class distribution alone establishes how well the tree will perform on new observations.

A readable plot helps communicate a model, but it does not establish that the model is appropriate, stable, or useful. Those questions require evaluation beyond the display. For this task, the practical objective is narrower: accurately recover the fitted tree's conditions and summaries, and make clear which information the selected display does and does not show.

Key takeaway: A tree plot shows routing and selected node summaries. Trace all branches to a terminal node, read its prediction separately from its count, and inspect the fitted object when the plot does not show the complete class distribution or rule.

Check Your Understanding

For each problem, trace the conditions carefully and distinguish predictions from node summaries.

  1. A regression-tree leaf contains 8 unweighted observations and has displayed fitted value 3.25 on a scale measured in hundreds of dollars. What is the leaf's prediction in dollars, and what does the count of 8 represent? (Answer: \$325; eight training observations reached the leaf.)
  2. A classification leaf contains 18 cases: 10 “late” and 8 “on time.” Under a largest-proportion rule, what class is predicted, and what are the two class proportions to four decimal places? (Answer: “late”; 0.5556 late and 0.4444 on time.)
  3. A split sends observations left when score is less than 70 and right otherwise. Where does a score of exactly 70 go, and why? (Answer: Right; 70 is not less than 70.)
  4. A path requires product type to be “A” or “C,” then balance to be at least \$2,400. State the full rule for the corresponding leaf and identify whether a “C” product with balance \$2,399 reaches it. (Answer: Product type is A or C and balance is at least \$2,400; no, the balance condition fails.)
  5. A classification node has 14 observations, of which 9 are “accept” and 5 are “refer.” A plot shows only “accept.” Give the class proportions and state one fact hidden by the class label alone. (Answer: 0.6429 accept and 0.3571 refer; the label alone hides that 5 of 14 observations are “refer.”)
Clinical Biostats