Tutorials › Multiple Imputation by Chained Equations (MICE) › Within-Between Decomposition

Nested and Multilevel Data · Tutorial 337 of 497

Within-Between Decomposition

Learn to split a level-1 predictor into its cluster mean and within-cluster deviation, and verify the decomposition with unbalanced data.

Advanced 10 min read

What You'll Learn

  • Define a cluster mean and a within-cluster deviation for each observation.
  • Derive the sum-of-squares partition into within-cluster and between-cluster components.
  • Calculate the overall mean correctly when cluster sizes are unequal.
  • Check the decomposition by comparing the two components with the total sum of squares.
  • Distinguish within-between decomposition from grand-mean centering and cluster indicators.
  • Recognize when missing values or inconsistent cluster definitions make the calculation unreliable.

Why Split a Predictor by Cluster?

In nested data, a level-1 predictor varies across observations and may also differ systematically from one cluster to another. For example, patients within hospitals may have different ages, while the average age may also vary across hospitals. A single raw predictor combines these two sources of variation. Within-between decomposition separates them arithmetically: each observation is represented by its cluster mean and its deviation from that mean.

This split is useful in multilevel analyses because it makes clear which part of a predictor represents differences among observations in the same cluster and which part represents differences among cluster averages. The decomposition itself is an identity, not a model and not a causal claim. It does not, by itself, determine how either component should enter an analysis.

Definition: Let \(x_{ij}\) be a level-1 predictor for observation \(i\) in cluster \(j\), where cluster \(j\) contains \(n_j\) observations. Its cluster mean is \(\bar{x}_j=\frac{1}{n_j}\sum_{i=1}^{n_j}x_{ij}\). The within-cluster deviation is \(x^W_{ij}=x_{ij}-\bar{x}_j\). Thus \(x_{ij}=\bar{x}_j+x^W_{ij}\), with the cluster mean constant for all observations in cluster \(j\).

The grand mean across all observations is \(\bar{x}=\frac{1}{N}\sum_j\sum_i x_{ij}\), where \(N=\sum_j n_j\). When cluster sizes differ, this is a size-weighted average of the cluster means: \(\bar{x}=\frac{1}{N}\sum_j n_j\bar{x}_j\). It is generally not the unweighted average of those means.

The Sum-of-Squares Decomposition

Suppose the clusters form a disjoint and exhaustive partition of the observations, and the predictor is available for every observation included in the calculation. The total sum of squared deviations from the grand mean can then be partitioned into a within-cluster sum and a between-cluster sum. The between-cluster component weights each squared difference between a cluster mean and the grand mean by that cluster’s size.

$$ \sum_j\sum_i (x_{ij}-\bar{x})^2 = \sum_j\sum_i (x_{ij}-\bar{x}_j)^2 + \sum_j n_j(\bar{x}_j-\bar{x})^2. $$

The first term on the right is the within-cluster sum of squares: it measures variation among observations around their own cluster means. The second is the between-cluster sum of squares: it measures variation in cluster means around the observation-weighted grand mean. These are sums of squares in the original predictor’s units squared.

Why the Identity Holds

For each observation, add and subtract its cluster mean. Then square the resulting two-part expression and sum it over observations:

$$ x_{ij}-\bar{x}=(x_{ij}-\bar{x}_j)+(\bar{x}_j-\bar{x}), $$
$$ \begin{aligned} \sum_j\sum_i(x_{ij}-\bar{x})^2 &=\sum_j\sum_i(x_{ij}-\bar{x}_j)^2 +\sum_j\sum_i(\bar{x}_j-\bar{x})^2\\ &\quad+2\sum_j\sum_i(x_{ij}-\bar{x}_j)(\bar{x}_j-\bar{x}). \end{aligned} $$

Within each cluster, \(\bar{x}_j-\bar{x}\) is constant. Also, the deviations from the cluster mean sum to zero: \(\sum_i(x_{ij}-\bar{x}_j)=0\). Therefore, the cross-product term is zero in every cluster. The remaining middle term repeats \((\bar{x}_j-\bar{x})^2\) exactly \(n_j\) times, giving \(n_j(\bar{x}_j-\bar{x})^2\). This proves the stated identity.

No normality or independence assumption is needed for this arithmetic identity. The requirements are about the data being partitioned and measured consistently: each observation belongs to one cluster, every included observation has a predictor value, and each cluster mean is calculated from the observations designated as belonging to that cluster. Statistical models built using these components may require additional assumptions.

Worked Example: Checking the Decomposition in Unequal-Size Clusters

Consider a hypothetical predictor measured in three clusters. Cluster A has values 1, 4, and 7; cluster B has values 8 and 12; cluster C has values 2, 3, 5, and 6. The cluster sizes are 3, 2, and 4, so the data are unbalanced.

Find the cluster means and grand mean. The means are \(\bar{x}_A=12/3=4\), \(\bar{x}_B=20/2=10\), and \(\bar{x}_C=16/4=4\). The total is 48 across 9 observations, so \(\bar{x}=48/9=16/3\), or about 5.333 units.

Calculate the within-cluster sum of squares. For A, the squared deviations from 4 sum to \((1-4)^2+(4-4)^2+(7-4)^2=18\). For B, the squared deviations from 10 sum to \((8-10)^2+(12-10)^2=8\). For C, the squared deviations from 4 sum to \((2-4)^2+(3-4)^2+(5-4)^2+(6-4)^2=10\). Thus the within-cluster sum is \(18+8+10=36\) squared units.

Calculate the between-cluster sum of squares. Weight each squared difference from the grand mean by its cluster size:

$$ \begin{aligned} \sum_j n_j(\bar{x}_j-\bar{x})^2 &=3(4-16/3)^2+2(10-16/3)^2+4(4-16/3)^2\\ &=16/3+392/9+64/9\\ &=504/9=56. \end{aligned} $$

A and C have the same mean but different sizes, so their contributions are different: A contributes \(16/3\), while C contributes \(64/9\). The total sum of squares must therefore be \(36+56=92\) squared units. The within and between components add exactly to that total.

Calculating the Components Observation by Observation

The decomposition can also be used to construct two columns for each observation. The within-cluster column contains \(x^W_{ij}=x_{ij}-\bar{x}_j\). A between-cluster column can contain \(\bar{x}_j\), repeated for each observation in cluster \(j\), or the centered cluster mean \(\bar{x}_j-\bar{x}\). The former keeps the cluster mean on the predictor’s original scale; the latter expresses it as a difference from the grand mean.

For the previous example, observation A’s value 1 has a within-cluster deviation of \(1-4=-3\), and its cluster mean is 4. Its decomposition is \(1=4+(-3)\). Observation B’s value 12 has a deviation of \(12-10=2\), so \(12=10+2\). The within deviations sum to zero within each cluster; that is a useful check on the calculations.

Worked Example: Decomposing Values in Three Clusters

A second hypothetical dataset has cluster A values 2 and 6, cluster B values 5, 7, and 9, and cluster C has a single value, 13. Calculate the components and verify the sum-of-squares identity.

Cluster means and grand mean. The means are 4, 7, and 13. The grand mean is \((2+6+5+7+9+13)/6=42/6=7\). The cluster sizes are 2, 3, and 1.

Within component. Cluster A contributes \((2-4)^2+(6-4)^2=8\). Cluster B contributes \((5-7)^2+(7-7)^2+(9-7)^2=8\). Cluster C contributes 0 because its only value equals its cluster mean. The within sum is \(8+8+0=16\) squared units.

Between component. The weighted sum is \(2(4-7)^2+3(7-7)^2+1(13-7)^2=18+0+36=54\) squared units. The total is \(16+54=70\). Directly, the squared deviations from 7 are \(25+1+4+0+4+36=70\), confirming the identity.

A singleton cluster has a within deviation of zero by construction; this does not mean its value is known precisely or that the cluster has no substantive variability. It means only that there is no second observation in that cluster with which to calculate a within-cluster difference.

Unequal Sizes and Alternative Centering Choices

Unequal cluster sizes matter whenever the target is an average across observations. The grand mean weights each cluster mean by \(n_j\), because a cluster with more observations contributes more values to the overall average. An unweighted average of cluster means instead gives each cluster equal influence, regardless of size; it answers a different averaging question.

Worked Example: Weighted Versus Unweighted Cluster Means

Suppose a hypothetical predictor has values 1 and 3 in cluster A, and values 6, 8, 8, and 10 in cluster B. Their means are 2 and 8, and their sizes are 2 and 4.

The observation-level grand mean is \((1+3+6+8+8+10)/6=36/6=6\). Equivalently, it is the weighted mean \((2(2)+4(8))/6=36/6=6\). The unweighted mean of the two cluster means is \((2+8)/2=5\), which is not the overall mean across observations.

Using the correct grand mean, the between-cluster sum is \(2(2-6)^2+4(8-6)^2=32+16=48\). The within sum is 2 for cluster A and 8 for cluster B, totaling 10. Thus the total sum is 58 squared units; directly summing squared deviations from 6 gives \(25+9+0+4+4+16=58\).

Subtracting the grand mean from every observation, \(x_{ij}-\bar{x}\), is called grand-mean centering. It shifts the origin but leaves the predictor as a mixture of within- and between-cluster variation. Cluster-mean centering, \(x_{ij}-\bar{x}_j\), isolates within-cluster deviations, while the cluster means themselves retain between-cluster variation. Cluster indicators, discussed in the earlier material on Cluster Indicators and Their Limits, represent clusters with separate indicator variables; they are a different way to represent cluster membership, not a replacement definition for these two components.

Checks, Pitfalls, and Limits

Several quick checks catch common arithmetic or data-definition errors:

  • Within each cluster, the deviations should sum to zero, subject to small rounding error.
  • The weighted cluster means should reproduce the overall mean: \(\sum_j n_j\bar{x}_j/N=\bar{x}\).
  • The within and between sums of squares should add to the directly calculated total sum of squares.
  • When using rounded means, retain adequate precision during intermediate calculations and round only the reported result.

A common mistake is to treat each cluster mean as one equally weighted observation when computing the grand mean or the between-cluster sum of squares. That ignores cluster size. In the first worked example, A and C both have mean 4, but C has four observations and A has three; their between contributions are \(64/9\) and \(16/3\), respectively. The correct weighting is by \(n_j\), not by the number of clusters alone.

Another mistake is to conclude that a large within or between sum of squares identifies an important effect. These quantities describe how the observed predictor’s variation is partitioned; their size depends on the predictor’s scale and the number of observations. They do not establish an association, a causal effect, or the suitability of a particular model. Nor does decomposition alone account for uncertainty in estimated cluster means.

For incomplete data, the arithmetic requires a defined set of predictor values for each calculation. Do not silently calculate some cluster means from observed values and others from completed values, or change the cluster membership rule partway through. In an imputation workflow, components derived from a predictor must be calculated consistently with the analysis dataset and the intended definition of the cluster mean. This tutorial defines the decomposition; it does not prescribe a model for imputing missing values or resolve the inferential consequences of estimating cluster means.

Key takeaway: Every level-1 value has the exact decomposition \(x_{ij}=\bar{x}_j+(x_{ij}-\bar{x}_j)\). Use observation-weighted grand means and cluster-size weights for the sum-of-squares identity, especially when cluster sizes differ.

Check Your Understanding

Answer each question, then compare your work with the solutions.

  1. A cluster contains values 2, 4, and 6. Find its mean and the within-cluster deviation for each observation.
  2. Two clusters have means 3 and 9, with sizes 2 and 4. Find the observation-level grand mean and the between-cluster sum of squares.
  3. Why must the cross-product term vanish when deriving the sum-of-squares decomposition?
  4. In one sentence, distinguish grand-mean centering from cluster-mean centering.

Solutions. (1) The mean is \((2+4+6)/3=4\); the deviations are \(-2, 0, 2\), which sum to zero. (2) The grand mean is \((2(3)+4(9))/6=42/6=7\). The between sum is \(2(3-7)^2+4(9-7)^2=32+16=48\) squared units. (3) Within each cluster the sum of deviations from its cluster mean is zero, and the difference between that cluster mean and the grand mean is constant, so their cross-product sum is zero. (4) Grand-mean centering subtracts the same overall mean from every observation, whereas cluster-mean centering subtracts each observation’s own cluster mean.

Clinical Biostats