Tutorials › Exam PA - Predictive Analytics › Splitting Categorical Predictors Into Groups

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

Splitting Categorical Predictors Into Groups

See how a tree groups factor levels into two child nodes, evaluates candidate groupings, and manages the difference between ordered and unordered categories.

Intermediate 10 min read

What You'll Learn

  • Define a binary split of a categorical predictor as a partition of its levels into two nonempty groups
  • Count the distinct partitions of an unordered factor and explain why complement groups are duplicates
  • Calculate child-node squared error and Gini impurity for candidate category groupings
  • Distinguish adjacent splits of an ordered factor from unrestricted subsets of an unordered factor
  • Recognize when numeric codes for categories produce invalid candidate splits
  • Explain why exhaustive category-split search becomes difficult as the number of levels increases

How a Categorical Predictor Splits a Node

A tree split on a categorical predictor does not usually separate levels by a numerical threshold. Instead, it assigns the predictor's levels to two groups. One child receives observations whose level is in the first group; the other receives observations whose level is in the second. The split is binary even when the predictor has many categories.

For example, a hypothetical insurance model might split a node according to whether a policy's distribution channel is in the group {agent, broker} or the group {direct, online}. The labels themselves have no numerical order. What matters is whether that partition improves the tree's chosen prediction criterion.

Definition: For a categorical predictor with \(K\) observed levels, a binary category split is a partition of those levels into two nonempty, disjoint groups. Each observation goes to the child corresponding to the group containing its level. A split is evaluated using the tree's criterion, such as total child squared error for regression or weighted child impurity for classification.

The word “observed” matters: a level with no observations in the node cannot contribute evidence to that node's split. In practice, candidate splits must also respect any restrictions imposed by the tree-growing procedure, such as minimum child size. The calculations below assume the stated levels have observations and that no additional restrictions rule out a candidate.

Unordered Factors: Subsets, Not Thresholds

For an unordered factor, there is no meaningful sequence of levels. A candidate split can use any nonempty proper subset on one side, with all remaining levels on the other. If the levels are A, B, C, and D, for instance, {A, C} versus {B, D} is a valid split even though its groups are not consecutive in the alphabet.

A partition and its complement describe the same split: {A, C} versus {B, D} is identical to {B, D} versus {A, C}. The number of distinct binary partitions is therefore smaller than the number of subsets.

$$ \text{Number of distinct splits} =\frac{2^K-2}{2} =2^{K-1}-1 $$

There are \(2^K\) subsets of \(K\) levels. Excluding the empty set and the full set leaves \(2^K-2\) nonempty, proper subsets. Each split is counted twice in that total, once for each side chosen as the subset, so division by two gives \(2^{K-1}-1\). With 4 levels this is 7 splits; with 10 levels it is 511; with 20 levels it is 524,287.

This count explains why a direct exhaustive search can become impractical for a factor with many levels. Tree methods may use restrictions or computational shortcuts, but those choices can change which candidate groupings are considered. A fitted tree's split should therefore be interpreted as the grouping selected under its search procedure, not necessarily as the result of checking every possible partition.

Evaluating a Regression Split

For a regression tree using squared error, each child predicts the mean response among observations in that child. A candidate category partition is scored by adding the squared errors in the two children. The preferred candidate, among those considered, has the smallest total child squared error.

$$ \operatorname{SSE}_{\text{children}} = \sum_{g\in\{L,R\}} \sum_{i\in g}(y_i-\bar{y}_g)^2 $$

Here \(L\) and \(R\) denote the two groups of observations created by the split, \(y_i\) is an observed response, and \(\bar{y}_g\) is the mean response in child \(g\). This comparison concerns the observations in the node being split; the parent-node error is the same for every candidate at that node.

Worked Example: Comparing Two Unordered Regression Splits

A hypothetical model predicts a claim-related response using an unordered four-level service-region factor. In a node, each level has two observations. The responses are A: 0 and 2; B: 2 and 4; C: 7 and 9; D: 9 and 11. Compare the split {A, B} versus {C, D} with {A, D} versus {B, C}, using total child SSE. State which is better and identify the calculation trap.

Method. For each candidate, calculate each child mean and sum squared deviations from that mean. The calculation is exact for the stated squared-error criterion.

First candidate: {A, B} versus {C, D}. The first child contains 0, 2, 2, and 4, so its mean is \((0+2+2+4)/4=2\). Its SSE is \((0-2)^2+(2-2)^2+(2-2)^2+(4-2)^2=4+0+0+4=8\). The second child contains 7, 9, 9, and 11, so its mean is \((7+9+9+11)/4=9\). Its SSE is \((7-9)^2+(9-9)^2+(9-9)^2+(11-9)^2=4+0+0+4=8\). Thus the total child SSE is \(8+8=16\).

Second candidate: {A, D} versus {B, C}. The first child contains 0, 2, 9, and 11, with mean \((0+2+9+11)/4=5.5\). Its SSE is \(30.25+12.25+12.25+30.25=85\). The second child contains 2, 4, 7, and 9, also with mean \(5.5\). Its SSE is \(12.25+2.25+2.25+12.25=29\). The total is \(85+29=114\).

The first candidate is better because \(16<114\). The common mistake is to compare only the category means or to omit a category's within-category SSE. The method requires the squared deviations of every observation from its own child mean. As a check, the first candidate's two child sums, 16 in total, are also the SSEs obtained directly from the listed observations.

Why the Regression Calculation Can Be Decomposed

A useful identity explains how category-level summaries relate to the child SSE. Suppose a child contains categories \(j\), with \(n_j\) observations, category mean \(\bar{y}_j\), and within-category SSE \(W_j=\sum_{i\in j}(y_i-\bar{y}_j)^2\). Its overall mean is \(\bar{y}\). Then

$$ \sum_j\sum_{i\in j}(y_i-\bar{y})^2 = \sum_j W_j+\sum_j n_j(\bar{y}_j-\bar{y})^2 $$

To see why, write each response's deviation as \(y_i-\bar{y}=(y_i-\bar{y}_j)+(\bar{y}_j-\bar{y})\), square it, and sum within category \(j\). The cross term is \(2(\bar{y}_j-\bar{y})\sum_{i\in j}(y_i-\bar{y}_j)\), which is zero because deviations from the category mean sum to zero. The remaining terms are the within-category SSE and \(n_j(\bar{y}_j-\bar{y})^2\). Summing over categories gives the identity. It shows that child error includes both variation within levels and variation between their means.

For the first child in the example, each category has within-category SSE 2, so the within-category total is \(2+2=4\). The category means are 1 and 3, each based on 2 observations, and the child mean is 2. The between-category term is \(2(1-2)^2+2(3-2)^2=4\). The child SSE is therefore \(4+4=8\), agreeing with the direct calculation.

Classification Splits and Weighted Impurity

For classification, a split is assessed by the impurity of each child, weighted by that child's share of the observations. For a binary response, Gini impurity is \(2p(1-p)\), where \(p\) is the proportion of one class in the node. It is zero when all observations belong to one class and is largest at \(p=0.5\). For a multiclass response, Gini impurity is \(1-\sum_c p_c^2\), where \(p_c\) is the proportion in class \(c\).

$$ I_{\text{children}} = \frac{n_L}{n}I_L+\frac{n_R}{n}I_R $$

Here \(n_L\) and \(n_R\) are the child sizes, \(n=n_L+n_R\), and \(I_L,I_R\) are their impurities. When the parent node is fixed, the split with the largest reduction from parent impurity has the smallest weighted child impurity. The same weighted-comparison principle applies when using another impurity measure, such as entropy.

Worked Example: Finding a Useful Unordered Classification Group

A hypothetical retention model uses four unordered service-channel levels to predict whether a customer renews. The node contains four customers per level. The renewal counts are A: 4, B: 3, C: 1, and D: 0. Compare {A, B} versus {C, D} with {A, D} versus {B, C}, using weighted binary Gini impurity.

Method. Calculate each child’s renewal proportion and Gini impurity, then weight by the child size. Each candidate here creates two children of size 8 from a parent of size 16.

For {A, B}, there are \(4+3=7\) renewals among 8 customers. Its Gini impurity is \(2(7/8)(1/8)=14/64=0.21875\). For {C, D}, there is \(1+0=1\) renewal among 8, so its impurity is \(2(1/8)(7/8)=0.21875\). The weighted child impurity is \((8/16)(0.21875)+(8/16)(0.21875)=0.21875\).

For {A, D}, there are \(4+0=4\) renewals among 8, giving impurity \(2(4/8)(4/8)=0.5\). For {B, C}, there are \(3+1=4\) renewals among 8, also giving impurity \(0.5\). Its weighted child impurity is \(0.5\).

The first candidate is better: \(0.21875<0.5\). The parent has 8 renewals among 16, so its Gini impurity is \(2(8/16)(8/16)=0.5\); the first split reduces it by \(0.5-0.21875=0.28125\). A common mistake is to average the two child impurities without weighting when child sizes differ. They happen to be equal here, but the general criterion uses the size weights.

Ordered Factors: Adjacent Groups

An ordered factor has a meaningful sequence, such as low, moderate, high, and very high. If the split is required to respect that order, each candidate divides the sequence at a cut point: levels below the cut go left and levels above it go right. With \(K\) levels, there are \(K-1\) such adjacent splits, rather than \(2^{K-1}-1\) unrestricted partitions.

Whether to require adjacent groups is a modeling decision. An order does not imply equal spacing: the difference between “low” and “moderate” need not equal the difference between “high” and “very high.” Treating the labels as consecutive ranks for candidate splits is also not the same as treating their codes as a quantitative predictor with meaningful numerical distances. If nonadjacent groupings are allowed, an ordered factor can still have the full set of subset partitions.

Worked Example: Choosing a Cut in an Ordered Risk Band

A hypothetical classification tree uses four ordered bands—low, guarded, elevated, and severe—to predict whether a specified event occurs. Each band has four observations. The event counts, in order, are 0, 1, 3, and 4. Restrict candidates to adjacent cuts and select the cut with the smallest weighted Gini impurity.

Method. Evaluate the three possible cuts, using \(2p(1-p)\) in each child and weighting by the child size. The parent has 8 events among 16 observations, so its Gini impurity is \(2(8/16)(8/16)=0.5\).

Cut after low. The left child is pure, with Gini 0. The right child has \(1+3+4=8\) events among 12, so its Gini is \(2(8/12)(4/12)=4/9\). The weighted impurity is \((4/16)(0)+(12/16)(4/9)=1/3\).

Cut after guarded. The left child has 1 event among 8, so its Gini is \(2(1/8)(7/8)=7/32\). The right child has \(3+4=7\) events among 8, giving the same Gini, \(7/32\). The weighted impurity is \((8/16)(7/32)+(8/16)(7/32)=7/32=0.21875\).

Cut after elevated. The left child has 4 events among 12, so its Gini is \(2(4/12)(8/12)=4/9\). The right child is pure, with Gini 0. The weighted impurity is \((12/16)(4/9)+(4/16)(0)=1/3\).

The best adjacent cut is after guarded, with weighted impurity \(0.21875\), lower than \(1/3\) for either other cut. Its reduction from parent impurity is \(0.5-0.21875=0.28125\). The check is that the selected cut groups the lower two bands against the upper two while retaining the stated order.

Common Trap: Treating Category Codes as Measurements

Suppose four unordered levels happen to be stored as codes 1, 2, 3, and 4. Those codes identify levels; they do not establish a meaningful order or distance. A numerical threshold search would consider only the contiguous partitions {1} versus {2, 3, 4}, {1, 2} versus {3, 4}, and {1, 2, 3} versus {4}. It would miss valid noncontiguous subsets.

For a concrete check, suppose each level has four observations, with event counts 4, 0, 4, and 0, respectively. The unordered split {1, 3} versus {2, 4} makes both children pure, so weighted Gini is 0. A threshold search on the codes cannot make that split: its best candidate has weighted Gini \(1/3\) (the first or last cut). Thus coding labels as numbers can exclude a substantially better grouping. For a genuinely ordered factor, adjacent cuts may be appropriate; for an unordered factor, do not impose an order merely because software stores labels using integers.

Category-split search matters because a factor can represent meaningful groups that are not captured by one numerical threshold. At the same time, the number of candidate partitions grows rapidly, so the search method and any restrictions affect the result. Interpret the selected grouping in context, verify that its levels are genuinely categorical, and distinguish an ordered cut from an arbitrary subset split. These considerations are aligned with the SOA Exam PA (Predictive Analytics) syllabus.

Check Your Understanding

For each problem, identify the candidate split logic and check the stated final result.

  1. An unordered factor has 6 observed levels. How many distinct binary partitions are possible if every nonempty proper subset is eligible and a split and its complement count as the same partition? (Answer: \(2^{5}-1=31\) distinct splits.)
  2. A regression child combines two categories, each with 3 observations and within-category SSE 3. Their means are 2 and 6. Find the combined child mean and SSE. (Answer: Mean \(4\); SSE \(3+3+3(2-4)^2+3(6-4)^2=30\).)
  3. A binary classification node has three unordered levels, each with four observations. Event counts are A: 4, B: 0, C: 4. Compare the split {A, C} versus {B} with the parent Gini impurity. (Answer: Child weighted Gini \(0\); parent Gini \(2(8/12)(4/12)=4/9\); reduction \(4/9\).)
  4. An ordered four-level factor has three observations per level, with event counts 0, 1, 2, and 3 in order. If only adjacent cuts are allowed, what is the smallest weighted Gini impurity and where is the cut? (Answer: \(5/18\), at the cut after level 2; the two child event proportions are \(1/6\) and \(5/6\).)
Clinical Biostats