Tutorials › Exam PA - Predictive Analytics › Class Errors Across Terminal Nodes

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

Class Errors Across Terminal Nodes

Use terminal-node class proportions and error counts to locate where a classification tree makes systematic mistakes and assess whether those patterns persist on validation data.

Intermediate 11 min read

What You'll Learn

  • Calculate a terminal node’s class proportions and majority-class prediction.
  • Find the number and rate of misclassified observations in each node.
  • Separate class-specific errors from the overall error rate.
  • Distinguish leaves with the most errors from leaves with the highest error rates.
  • Use held-out observations to check whether apparent error patterns persist.
  • Avoid misleading comparisons caused by unequal leaf sizes or changing predictions after validation.

Why Examine Errors by Terminal Node?

A classification tree assigns a predicted class to each terminal node, also called a leaf. An overall error rate summarizes how often the tree is wrong, but it does not show where those errors occur. Errors concentrated in one leaf may indicate that a particular path through the tree leaves important variation unexplained. Errors spread across leaves may point to a broader limitation of the model. Either way, examining the terminal nodes provides a more useful diagnosis than accuracy alone.

For a professional analysis, the distinction between training and held-out data matters. Counts calculated from the observations used to fit a tree describe its apparent, or training, errors. They can help explain the fitted model, but they do not by themselves establish how well it will classify new observations. Held-out observations can show whether a pattern continues beyond the fitting data.

Definition: In a classification tree, a terminal node is a final region reached by following the tree’s split rules. For terminal node \(j\), let \(n_{jk}\) be the number of observations in that node whose actual class is \(k\), and let \(n_j=\sum_k n_{jk}\). The node’s empirical class proportion for class \(k\) is \(n_{jk}/n_j\). Under equal misclassification costs, the usual node prediction is a class with the largest count. The number misclassified in the node is the node total minus the largest class count.

The empirical class proportions describe the composition of the observations reaching a leaf. Under the usual interpretation, they estimate the conditional class probabilities for observations with predictor values in that region. They are not guarantees that a future observation in the same region belongs to the most common class. A node can have a majority class and still have a substantial error rate.

Calculating Node Errors

Suppose there are \(K\) possible classes and the tree uses equal cost for every incorrect classification. The predicted class in node \(j\) is the class with the largest observed count. If there is a tie, the prediction depends on the software’s tie-breaking convention; the calculations below assume there is a unique largest count.

$$ \widehat{p}_{jk}=\frac{n_{jk}}{n_j}, \qquad \widehat{c}_j=\mathop{\arg\max}_{k} n_{jk}, \qquad e_j=n_j-\max_k n_{jk}. $$

Here, \(\widehat{p}_{jk}\) is the observed proportion of class \(k\) in node \(j\), \(\widehat{c}_j\) is the node’s predicted class, and \(e_j\) is its number of errors. The node’s apparent error rate is \(e_j/n_j\). Across all terminal nodes, the overall apparent error rate is the total number of errors divided by the total number of observations.

$$ \widehat{R}_{\mathrm{overall}} = \frac{\sum_j e_j}{\sum_j n_j} = \frac{\sum_j e_j}{N}. $$

This overall rate weights each node according to its size. That is appropriate when the goal is the fraction of all observations classified incorrectly. It differs from the unweighted average of the node error rates, which gives a small leaf the same influence as a large one. Neither summary replaces the node-by-node counts: a leaf with the largest error rate need not contribute the largest number of errors.

Worked Example: Finding Which Leaf Contributes More Errors

A hypothetical claims-screening tree has two terminal nodes. In leaf 1, 18 cases are actually class A and 6 are class B. In leaf 2, 5 are class A and 15 are class B. Using equal costs for the two types of error, calculate each leaf’s prediction, error count, and error rate, then find the overall apparent error rate.

Method. In each leaf, predict the class with the larger count. Subtract that count from the leaf total to obtain its error count.

Leaf 1. The total is \(18+6=24\). The class proportions are \(18/24=0.75\) for A and \(6/24=0.25\) for B. The prediction is A, so the 6 actual B cases are misclassified. The error rate is \(6/24=0.25\).

Leaf 2. The total is \(5+15=20\). The class proportions are \(5/20=0.25\) for A and \(15/20=0.75\) for B. The prediction is B, so the 5 actual A cases are misclassified. The error rate is \(5/20=0.25\).

Overall. There are \(24+20=44\) cases and \(6+5=11\) errors. Thus the apparent error rate is \(11/44=0.25\), or 25%.

Both leaves have the same error rate, but leaf 1 contributes 6 errors while leaf 2 contributes 5. The method avoids confusing the number of errors with the fraction of a leaf that is wrong. As a check, each leaf’s predicted-class count plus its error count equals its total: \(18+6=24\) and \(15+5=20\).

Class-Specific Errors Reveal the Direction of Mistakes

An overall error count treats all incorrect classifications alike. For a two-class problem, it can also be useful to identify the direction of errors. If the tree predicts A in a leaf, observations actually in B are errors of the B-to-A type. If it predicts B, observations actually in A are errors of the A-to-B type. These errors can have different practical consequences, so report them separately when the decision context makes that distinction important.

For several terminal nodes, add the relevant counts across the leaves. For example, the number of actual B observations predicted as A is the sum of \(n_{jB}\) over leaves whose prediction is A. A class-specific error rate uses the number of actual observations of that class as its denominator. State the denominator clearly: a rate among actual B cases is not the same as the fraction of predictions of A that are wrong.

Worked Example: Locating Errors Across Three Leaves

A hypothetical service team uses a tree to classify whether an account needs review (Yes) or does not need review (No). The training counts in three leaves are shown below. Treat the two error types equally. Calculate the predictions, leaf error rates, total apparent error rate, and the class-specific error rates among actual Yes and actual No cases.

LeafActual NoActual Yes
12812
2911
3416

Leaf 1. Its total is \(28+12=40\), and No is the majority class. The tree predicts No and misclassifies 12 Yes cases. Its error rate is \(12/40=0.30\).

Leaf 2. Its total is \(9+11=20\), and Yes is the majority class. The prediction is Yes, with 9 No cases misclassified. Its error rate is \(9/20=0.45\).

Leaf 3. Its total is \(4+16=20\), and Yes is the majority class. The prediction is Yes, with 4 No cases misclassified. Its error rate is \(4/20=0.20\).

Overall error. The total number of observations is \(40+20+20=80\). The total number of errors is \(12+9+4=25\). Therefore the overall apparent error rate is \(25/80=0.3125\), or 31.25%.

Class-specific errors. There are \(12+11+16=39\) actual Yes cases. The tree predicts No in leaf 1, so 12 of these Yes cases are missed; the error rate among actual Yes cases is \(12/39\approx0.3077\). There are \(28+9+4=41\) actual No cases. The tree predicts Yes in leaves 2 and 3, so \(9+4=13\) No cases are incorrectly flagged; the error rate among actual No cases is \(13/41\approx0.3171\).

Leaf 2 has the highest error rate, 45%, but leaf 1 contributes the most errors, 12. The class-specific calculations also show that the errors include both missed Yes cases and incorrectly flagged No cases. This prevents an overall rate from concealing which actual class is being misclassified.

Why the Majority-Count Formula Works

The node error formula follows directly from the equal-cost classification rule. Consider a fixed terminal node containing \(n_j\) observations. If the tree predicts class \(k\), then every observation in that node whose actual class is not \(k\) is an error. The number of errors is therefore \(n_j-n_{jk}\). To minimize that count, choose the class with the largest \(n_{jk}\).

$$ \min_k (n_j-n_{jk}) = n_j-\max_k n_{jk}. $$

Dividing by \(n_j\) gives the node’s minimum apparent error rate under this rule:

$$ \frac{n_j-\max_k n_{jk}}{n_j} = 1-\max_k \widehat{p}_{jk}. $$

The same reasoning applies to conditional probabilities: if the true class probabilities within a region were \(p_{jk}\), predicting the most probable class would have conditional error probability \(1-\max_k p_{jk}\), when every wrong class prediction has the same cost. The observed proportions replace those unknown probabilities when describing training data. This result does not apply unchanged when different errors have different costs or when the prediction rule uses a threshold other than the majority-class rule.

Check Whether the Pattern Persists on Held-Out Data

A tree’s training counts are shaped by the data used to construct it. To assess whether a node-level pattern generalizes, send held-out observations through the already fitted tree and count actual classes and predictions in each leaf. Keep the tree’s fitted split rules and predicted class labels fixed during this evaluation. Re-estimating the leaf labels from the held-out outcomes would use the evaluation data to alter the model being evaluated.

For example, suppose the fitted tree predicts No in leaf 1 and Yes in leaves 2 and 3. A held-out count table can reveal whether leaf 1 still contains many actual Yes cases, or whether its training errors were not repeated in the new data. Compare rates as well as counts, and note the sample size behind each rate. A high rate based on very few held-out cases is less stable than a similar rate based on many cases.

A node pattern is evidence of a localized weakness, not proof of its cause. The predictor path may leave relevant differences unrepresented, the observed sample may be variable, or the relationship may differ between the fitting and evaluation data. Describe what the counts show before proposing an explanation. In particular, do not infer that a predictor causes the error merely because the leaf is defined by that predictor’s split rules.

Worked Example: Auditing Held-Out Errors Without Changing the Tree

A fitted account-review tree predicts No in leaf 1 and Yes in leaf 2. Its held-out observations have the following counts. Calculate the error rate in each leaf and overall. Then identify the class-specific error counts. The fitted predictions must remain unchanged during this audit.

LeafFitted predictionActual NoActual Yes
1No1812
2Yes515

Leaf 1. The held-out total is \(18+12=30\). Because the fitted tree predicts No, the 12 actual Yes observations are errors. The leaf error rate is \(12/30=0.40\).

Leaf 2. The held-out total is \(5+15=20\). The fitted prediction is Yes, so the 5 actual No observations are errors. The leaf error rate is \(5/20=0.25\).

Overall. There are \(30+20=50\) held-out observations and \(12+5=17\) errors. The held-out error rate is \(17/50=0.34\), or 34%.

There are \(12+15=27\) actual Yes cases, of which 12 are predicted No. The error rate among actual Yes cases is \(12/27\approx0.4444\). There are \(18+5=23\) actual No cases, of which 5 are predicted Yes. The error rate among actual No cases is \(5/23\approx0.2174\). Thus this held-out set shows more errors among actual Yes cases than among actual No cases.

Common mistake and correction. A tempting but incorrect approach is to relabel leaf 1 as Yes because its held-out counts contain 12 Yes and 18 No? In fact, those counts still have No as the majority, so that proposed relabeling would not even follow the data. More importantly, held-out outcomes must not be used to revise fitted predictions during an evaluation. Use the stated fitted predictions, count their errors, and report the result as an assessment of that fixed tree. The check is \(12+5=17\) errors among \(50\) cases, matching the overall rate of 34%.

Practical Reading and Common Traps

A concise terminal-node error audit should make it possible to answer four questions: what class the tree predicts in each leaf, what actual classes occur there, how many observations are misclassified, and whether the same pattern appears on held-out data. A compact table is often clearer than a single accuracy statistic.

  • Do not equate the most errors with the highest error rate. A large leaf can contribute many errors even when its error rate is moderate; a small leaf can have a high rate but few errors.
  • Do not average leaf rates without considering their sizes. The overall error rate is total errors divided by total observations. An unweighted average answers a different question.
  • Do not treat a majority prediction as certainty. A leaf with proportions 55% and 45% has a majority class, but the observed composition is close to a tie.
  • Do not confuse training and held-out errors. Training errors describe the fitted sample; held-out errors evaluate fixed predictions on observations not used to fit the tree.
  • State the error direction and denominator. “Errors among actual Yes cases” and “the fraction of Yes predictions that are wrong” are different quantities.

In R, the essential task is to compare each observation’s actual class with its predicted class and group those comparisons by the terminal node it reached. For example, if a data frame called audit contains the columns leaf, actual, and predicted, a grouped cross-tabulation such as with(audit, table(leaf, actual, predicted)) displays the counts needed for this audit. Ensure that the predictions were produced by the fitted tree and, for a held-out assessment, that the held-out outcomes were not used to change the tree.

Key takeaway: Read a classification tree’s performance leaf by leaf. The majority class determines the usual equal-cost prediction; the remaining class counts are errors. Compare counts, rates, error direction, and held-out results before deciding whether the tree’s mistakes are localized or widespread.

Check Your Understanding

For each problem, use the stated fitted prediction rule and show how the counts determine the requested error summary.

  1. A terminal node has 14 actual A cases and 6 actual B cases. With equal misclassification costs, what class is predicted, how many cases are misclassified, and what is the node error rate? (Answer: Predict A; 6 errors; error rate \(6/20=0.30\).)
  2. A tree has two leaves. Leaf 1 contains 7 actual A and 13 actual B cases; leaf 2 contains 12 actual A and 8 actual B cases. Find each prediction and the overall apparent error rate. (Answer: Predict B in leaf 1 and A in leaf 2; errors \(7+8=15\) of \(40\), for an overall error rate of 0.375.)
  3. A fitted tree predicts Red in leaf 1 and Blue in leaf 2. Held-out counts are: leaf 1, 10 actual Red and 5 actual Blue; leaf 2, 4 actual Red and 11 actual Blue. Find both leaf error rates and the overall held-out error rate. (Answer: Leaf rates \(5/15=0.3333\) and \(4/15=0.2667\); overall \(9/30=0.3000\).)
  4. Three leaves have actual class counts (A, B) of (16, 4), (6, 14), and (9, 11), respectively. Find the total apparent error rate and the number of actual B observations misclassified as A. (Answer: Predictions A, B, B; errors \(4+6+9=19\) of \(60\), so \(19/60\approx0.3167\); 4 actual B observations are misclassified as A.)
  5. Two leaves have error counts and sizes of 8 out of 40, and 3 out of 10. Which has the higher error rate, which contributes more errors, and what is the overall error rate? (Answer: The second leaf has the higher rate, \(3/10=0.30\) versus \(8/40=0.20\); the first contributes more errors, 8 versus 3; overall error rate \(11/50=0.22\).)
Clinical Biostats