Why Nested Categories Need Careful Encoding
A branch belongs to a region, a clinic belongs to a health network, and a product may belong to a product line. In each case, the smaller unit is identified in the context of its parent. The branch name “Central,” for example, might be used in several regions, without referring to the same physical branch. Treating that name as a globally shared category can combine distinct groups and distort fitted comparisons.
The key modeling decision is whether the categories are nested or crossed. With nested categories, each child category belongs to one parent, and a child label may be reused under another parent. With crossed categories, the factors have separate meanings, and combinations of their levels are conceptually possible. The distinction affects which group indicators represent the data and how fitted differences can be interpreted.
A group indicator is a variable equal to 1 for observations in a specified group and 0 otherwise. To represent nested categories, define indicators for the distinct observed parent-child groups. With an intercept in the model, one group is usually selected as the reference group, and its indicator is omitted. Each remaining indicator coefficient is a fitted difference from that reference group, holding any other predictors fixed.
Nested Group Indicators and Their Interpretation
Suppose a response \(Y\) is modeled using an intercept and indicators for the non-reference groups. With four observed groups and the first group as the reference, a linear model can be written as
Here \(G\) labels the combined parent-child group, and \(I(G=j)\) is 1 when an observation belongs to group \(j\), and 0 otherwise. The fitted mean for group 1 is \(\beta_0\); the fitted mean for group 2 is \(\beta_0+\beta_1\), and likewise for the other groups. Thus, \(\beta_j\) is the fitted difference between group \(j+1\) and the reference group.
This parameterization is one way to fit separate means for the observed groups. It does not, by itself, separate a parent-category effect from a child-category effect. If every branch occurs in just one region, the data do not provide a repeated branch level across regions that would directly support a comparison of “the same branch” under different regions. A separate regional effect requires an additional modeling assumption or a design that makes that effect identifiable.
Worked Example: Comparing Branches Within and Across Regions
A hypothetical insurer models average monthly service cost using four observed region-and-branch groups. The North region has branches Pine and Lake; the South region has branches Cedar and Hill. Pine-North is the reference group. The fitted model is \(E[Y\mid G]=100-8I(G=\text{Lake-North})+15I(G=\text{Cedar-South})+4I(G=\text{Hill-South})\), where costs are measured in dollars. Find each fitted group mean and interpret the contrasts.
Method. Treat each region-and-branch pair as a distinct group, then add the indicator coefficient to the reference mean for each non-reference group.
For Pine-North, all three indicators are zero, so its fitted mean is \(100\) dollars.
For Lake-North, only its indicator is 1: \(100-8=92\) dollars. For Cedar-South, the fitted mean is \(100+15=115\) dollars. For Hill-South, it is \(100+4=104\) dollars.
The Lake-North group is fitted at \(8\) dollars less than Pine-North. Cedar-South is fitted at \(15\) dollars more than Pine-North, and Hill-South at \(4\) dollars more. Cedar-South and Hill-South differ by \(115-104=11\) dollars. These are comparisons of the named groups. They do not establish that the South region itself adds \(15\) dollars: the South branches are not the same branches observed in the North.
Check. The four fitted means are \(100\), \(92\), \(115\), and \(104\). Subtracting the reference mean from each non-reference mean recovers the stated coefficients: \(-8\), \(15\), and \(4\). A common mistake is to describe the \(15\)-dollar coefficient as a pure region effect, despite it comparing two different region-branch groups.
Why the Indicator Coefficients Are Group Differences
The interpretation follows directly from substituting the indicator values into the model. For the reference group, every non-reference indicator is zero, so its fitted mean is \(\beta_0\). For a non-reference group \(j\), its own indicator equals 1 and all other group indicators equal 0, giving a fitted mean of \(\beta_0+\beta_j\). Subtracting the reference mean gives
This also explains why the reference group matters: changing the reference changes the coefficient values and their comparisons, while leaving the fitted group means unchanged. This is the same reference-level principle used for ordinary categorical predictors. The distinctive step for nested categories is defining the groups correctly before choosing a reference.
For an ordinary least-squares model containing only an intercept and group indicators, the fitted mean for each group is that group’s sample average. To see why, let a group have observations \(y_1,\ldots,y_n\) and fitted constant \(m\). Least squares chooses \(m\) to minimize \(\sum_{i=1}^{n}(y_i-m)^2\). Differentiating with respect to \(m\) and setting the derivative to zero gives
Thus, each group’s fitted mean is its observed average in this model. Adding other predictors changes the interpretation: an indicator coefficient is then a difference in fitted response at the same values of those predictors, rather than necessarily a difference between raw group averages.
Worked Example: Group Differences at a Fixed Predictor Value
A hypothetical service center models the amount of a claim adjustment, in dollars, using a numeric complexity score \(x\) and nested service-center groups. The Central region’s Maple center is the reference group. The fitted model is \(E[Y\mid x,G]=40+3x+12I(G=\text{Central-Park})-5I(G=\text{Coastal-Reef})+9I(G=\text{Coastal-Bay})\). Calculate the fitted amount for each group when \(x=4\), then compare the two Coastal centers.
Method. First calculate the reference prediction at the specified score, then add the appropriate group-indicator coefficient.
The shared predictor contribution is \(40+3(4)=40+12=52\) dollars. Therefore, Maple-Central has fitted amount \(52\) dollars. Park-Central has fitted amount \(52+12=64\) dollars. Reef-Coastal has fitted amount \(52-5=47\) dollars. Bay-Coastal has fitted amount \(52+9=61\) dollars.
At \(x=4\), Bay-Coastal is fitted at \(61-47=14\) dollars more than Reef-Coastal. Equivalently, their coefficients relative to the same reference differ by \(9-(-5)=14\) dollars. This is a comparison at the same complexity score, not an unadjusted comparison of the centers’ observed average claim amounts.
Check. Subtracting the reference prediction of \(52\) from the four predictions gives \(0\), \(12\), \(-5\), and \(9\), exactly the model’s group contributions. A common mistake is to omit the \(3x\) term for non-reference groups; the numeric predictor applies to every group in this model.
Nested Groups Versus Crossed-Factor Interactions
An interaction between two crossed factors allows the effect of one factor to vary across levels of the other. For example, region and distribution channel might be crossed if each channel has the same meaning in every region and each region-channel combination is meaningful. By contrast, branch-within-region groups are formed from parent-child pairs: a branch label reused in another region may identify a different unit.
A crossed-factor interaction may include combinations with no observations. Such combinations have no data to estimate their group-specific means without additional assumptions. A nested group factor focuses on the observed parent-child combinations and avoids treating an unobserved pairing as if it were an observed group. The choice is not merely a matter of which model has more terms; it reflects how the categories are defined in the application.
Worked Example: Avoiding a False Crossed-Category Comparison
A hypothetical utility records response times for four observed office groups: Central-East, Harbor-East, Central-West, and Ridge-West. “Central” is a local office label reused in both regions; the two Central offices are distinct. Their observed group averages are, respectively, \(70\), \(62\), \(81\), and \(75\) minutes. Use Central-East as the reference to encode the groups and compare the equal-weighted average of the two observed office means in each region.
Method. Encode each observed region-office pair as its own group. The reference fitted mean is \(70\) minutes. The Harbor-East indicator coefficient is \(62-70=-8\) minutes. The Central-West coefficient is \(81-70=11\) minutes. The Ridge-West coefficient is \(75-70=5\) minutes.
The fitted group means are therefore \(70\), \(70-8=62\), \(70+11=81\), and \(70+5=75\) minutes, matching the supplied group averages. The equal-weighted East average is \((70+62)/2=132/2=66\) minutes. The equal-weighted West average is \((81+75)/2=156/2=78\) minutes. Their difference is \(78-66=12\) minutes.
This \(12\)-minute difference compares the two regions’ averages across the listed offices, giving each office equal weight. It is not automatically a region effect adjusted for office composition. If the numbers of observations differed across offices, an observation-weighted regional average could be different.
Check. The indicator differences reproduce all four group means, and the region-average calculation uses the stated equal weights. A crossed model using the office labels as if “Central” were one shared office would incorrectly merge Central-East and Central-West. It could also imply comparisons involving, for example, Ridge-East, even though that group is not among the observed combinations. The two Central offices must remain distinct because their parent regions identify different units.
Implementing the Group Structure in R
When a child label can recur under different parents, build a group identifier from both factors rather than using the child factor alone. In R, a factor such as interaction(region, branch, drop = TRUE) forms a combined factor for observed region-branch combinations. The drop = TRUE option omits combinations that do not occur in the data. A model using that combined group factor estimates differences among the represented groups, subject to the selected contrast coding and reference level.
Before fitting the model, check that each child unit has one parent in the data definition, and inspect the observed parent-child combinations. A repeated label can be either a genuine shared category or a locally reused name; the data dictionary and problem context determine which. Encoding cannot resolve an ambiguous definition.
Determine whether the child categories are unique across parents or are local labels that can repeat.
For nested factors, treat each parent-child pair as a distinct group.
State the reference group so every reported indicator coefficient has an unambiguous comparison.
Describe coefficients as fitted group differences, conditional on other predictors when they are included.
Common Exam Traps and Practical Limits
- Using the child label alone. If “Central” occurs in two regions as a local office name, a single Central indicator merges distinct units. Use the parent-child pair.
- Calling a group contrast a parent effect. A coefficient comparing one branch in one region with a branch in another region is a group comparison. It does not isolate a region effect unless the model and design support that interpretation.
- Assuming every combination is observed. A crossed-factor model can refer to combinations that are absent from the data. Without observations or explicit assumptions, their group-specific fitted values are not determined by observed group means alone.
- Ignoring the reference and other predictors. A group coefficient is relative to the stated reference. In a model with covariates, it compares fitted responses at the same covariate values.
- Overstating causal meaning. Fitted differences describe the specified model and data. They do not establish that changing a region or assigning an observation to a branch would cause the response to change.
Check Your Understanding
For each problem, identify the relevant group structure before calculating or interpreting a fitted difference.
- A hypothetical insurer uses Lakeside-North as the reference, with fitted mean cost \(120\) dollars. The indicator coefficients are \( -6\) dollars for Summit-North and \(10\) dollars for Lakeside-South. Find both fitted means and their difference. (Answer: Summit-North is \(114\) dollars; Lakeside-South is \(130\) dollars; Lakeside-South is \(16\) dollars higher.)
- A model for processing time is \(E[Y\mid x,G]=25+2x+7I(G=\text{Ridge-East})-4I(G=\text{Ridge-West})\), where Valley-East is the reference group. Find the predictions for Valley-East, Ridge-East, and Ridge-West at \(x=3\). (Answer: \(31\), \(38\), and \(27\), respectively; Ridge-East is \(11\) higher than Ridge-West at that score.)
- A nested group model has reference mean \(54\) units and indicator coefficients \(-5\) for Bay-North and \(9\) for Bay-South. Find the three fitted means and state what the coefficient \(9\) compares. (Answer: \(54\), \(49\), and \(63\); the \(9\) compares Bay-South with the reference group.)
- Two regions each have an office locally named Central, but they are distinct offices. Their fitted group means are \(68\) minutes in the East and \(80\) minutes in the West. What is the fitted difference, and should these offices share one child-level group indicator? (Answer: West Central is \(12\) minutes higher; no, use distinct East-Central and West-Central groups.)
- A model gives group means of \(50\) and \(64\) for the two observed clinics in Region A, and \(70\) and \(76\) for the two observed clinics in Region B. Calculate the equal-weighted average for each region and the difference. Explain why it is not necessarily a population-weighted regional difference. (Answer: Region A is \(57\), Region B is \(73\), and the equal-weighted difference is \(16\); unequal numbers of observations per clinic could produce different observation-weighted averages.)