Tutorials › Multiple Imputation by Chained Equations (MICE) › Nonlinear Effects Within And Between Clusters

Nested and Multilevel Data · Tutorial 354 of 497

Nonlinear Effects Within And Between Clusters

Represent within-cluster and between-cluster nonlinear associations separately, and construct their polynomial or spline terms consistently during imputation.

Advanced 12 min read

What You'll Learn

  • Decompose a predictor into within-cluster deviations and between-cluster means.
  • Specify separate polynomial effects for within-cluster and between-cluster associations.
  • Construct piecewise-linear spline bases for each component.
  • Recalculate transformed terms when imputed predictor values change.
  • Recognize circularity from cluster means and incompatibility risks in chained equations.
  • Compare polynomial, spline, and linear specifications using diagnostics.

Why Separate Nonlinear Effects?

A curved association between an outcome and a predictor can arise in two distinct ways in clustered data. Individuals who differ from others in their own cluster may show one pattern, while clusters with different average predictor values may show another. A single curve fitted to the raw predictor can mix these patterns, because it does not distinguish an individual's position within a cluster from the cluster's position in the overall sample.

This distinction matters during imputation as well as analysis. If an incomplete predictor is represented by a cluster mean, a within-cluster deviation, and nonlinear transformations of those quantities, the transformed terms must reflect the current imputed values. Treating a square or spline basis as an unrelated variable can produce completed data in which the mathematical relationships among variables do not hold.

Definition: For predictor \(x_{ij}\), observed for individual \(i\) in cluster \(j\), define the cluster mean \(\bar{x}_{.j}\), the within-cluster deviation \(x_{ij}-\bar{x}_{.j}\), and the grand mean \(\bar{x}_{..}\). The deviation represents an individual's position relative to their cluster mean; the centered cluster mean \(\bar{x}_{.j}-\bar{x}_{..}\) represents the cluster's position relative to the grand mean.

The decomposition is useful only when the cluster mean has an appropriate interpretation and is estimated from the available or currently imputed values. For the examples below, assume clusters are correctly identified, the predictor is measured on a meaningful numerical scale, and the intended analysis concerns associations conditional on the included covariates. The examples are hypothetical; they do not describe actual studies.

Separate Polynomial Terms

A quadratic model can give within- and between-cluster components different slopes and curvature. Let \(y_{ij}\) be an outcome and define \(w_{ij}=x_{ij}-\bar{x}_{.j}\) and \(b_j=\bar{x}_{.j}-\bar{x}_{..}\). One possible mean model is:

$$ E(y_{ij}\mid x)=\beta_0+\beta_{1W}w_{ij}+\beta_{2W}w_{ij}^2+\beta_{1B}b_j+\beta_{2B}b_j^2. $$

Here \(\beta_{2W}\) describes within-cluster curvature and \(\beta_{2B}\) describes between-cluster curvature. The squared terms are squares of the separately centered components, not simply the raw square \(x_{ij}^2\). This specification does not force the within- and between-cluster relationships to be the same. If the scientific model requires a random intercept or random slope, the corresponding random-effects structure is added separately; the fixed nonlinear terms do not by themselves specify that structure.

Worked Example: Building Within- and Between-Cluster Quadratics

Suppose a hypothetical dataset has two clusters. In cluster A, predictor values are 2, 4, and 6; in cluster B, they are 8 and 10. Calculate the centered components and their squares.

The cluster means are \(\bar{x}_{.A}=(2+4+6)/3=4\) and \(\bar{x}_{.B}=(8+10)/2=9\). The grand mean across the five individuals is \(\bar{x}_{..}=(2+4+6+8+10)/5=6\). Thus the between-cluster components are \(-2\) for A and \(3\) for B.

For an individual with \(x_{ij}=6\) in A, the within component is \(6-4=2\), with square \(2^2=4\). The between component is \(4-6=-2\), with square \((-2)^2=4\). For the individual with \(x_{ij}=10\) in B, the within component is \(10-9=1\), with square \(1^2=1\), and the between component is \(9-6=3\), with square \(3^2=9\).

For illustration, suppose the fitted fixed-effect coefficients are \(\beta_{1W}=1.2\), \(\beta_{2W}=0.3\), \(\beta_{1B}=0.8\), and \(\beta_{2B}=-0.1\). The covariate contribution for \(x=6\) in A is \(1.2(2)+0.3(4)+0.8(-2)-0.1(4)=0.8\). For \(x=10\) in B it is \(1.2(1)+0.3(1)+0.8(3)-0.1(9)=2.4\). These are contributions to the fitted mean, excluding the intercept and any other predictors; they are not outcome values by themselves.

The arithmetic also shows why raw squares do not automatically provide this decomposition. Since \(x_{ij}=\bar{x}_{.j}+w_{ij}\),

$$ x_{ij}^2=w_{ij}^2+2\bar{x}_{.j}w_{ij}+\bar{x}_{.j}^2. $$

A raw-square term therefore contains a cross-product and a cluster-mean component. It is not equivalent to including distinct squared within and between terms, and it imposes a different structure on the association.

Flexible Curves With Spline Bases

A polynomial is compact, but higher-degree polynomials can behave erratically near the ends of the observed range. A spline represents a curve using basis functions joined at knots. The knots and basis must be chosen consistently with the scientific question and the range of the predictor. A simple piecewise-linear basis uses \(u\) and hinge terms \(h_k(u)=\max(0,u-k)\), where \(k\) is a knot. Separate bases can be applied to the within component and the between component.

$$ E(y_{ij}\mid x)=\beta_0+\sum_{r=1}^{R}\gamma_{rW}B_r(w_{ij})+ \sum_{s=1}^{S}\gamma_{sB}C_s(b_j). $$

Here \(B_r\) and \(C_s\) are chosen basis functions; they need not be the same. For example, the within-cluster curve may need a knot at a different value from the between-cluster curve. A spline with a linear term and one hinge term is still a restricted, piecewise-linear model, not an unrestricted curve.

Worked Example: Constructing a Piecewise-Linear Within-Cluster Basis

Consider a hypothetical cluster with mean predictor value 5 and an individual with \(x_{ij}=6\), so \(w_{ij}=6-5=1\). Use a piecewise-linear basis with a knot at \(w=0\): the basis terms are \(w\) and \(h_0(w)=\max(0,w)\).

For this individual, \(w=1\) and \(h_0(1)=\max(0,1)=1\). For another individual with \(x_{ij}=3\) in the same cluster, \(w=3-5=-2\), and \(h_0(-2)=\max(0,-2)=0\). The hinge allows the slope above the knot to differ from the slope below it. For example, with coefficients 0.5 for \(w\) and 0.4 for \(h_0(w)\), the within-component contribution is \(0.5(1)+0.4(1)=0.9\) for the first individual and \(0.5(-2)+0.4(0)=-1.0\) for the second.

To represent a between-cluster curve, apply a separately chosen basis to \(b_j=\bar{x}_{.j}-\bar{x}_{..}\). Do not substitute the within value \(w_{ij}\) into that basis: it describes a different association.

Constructing Terms During Imputation

In chained equations, a transformed predictor such as \(w_{ij}^2\) is mathematically determined by the underlying predictor and cluster means. If \(x_{ij}\) is incomplete, first draw or update its imputed value, then recompute the cluster means and all derived deviations, squares, and spline bases according to the chosen construction. The transformations should not be imputed as free-standing variables: independently imputing \(x\) and \(x^2\) can yield an impossible pair, such as \(x=3\) and \(x^2=12\).

For the conditional model that imputes \(x\), the outcome and other relevant covariates generally contribute information about the missing value. However, a quadratic substantive relationship between \(x\) and the outcome is not automatically represented by an ordinary linear imputation regression for \(x\) on the outcome. A conditional model that is too simple can fail to preserve the nonlinear relationship. The imputation specification must be chosen with the intended substantive model in view; chained equations do not guarantee that every set of conditional models corresponds to a coherent joint distribution.

Cluster means require particular care. When imputing an individual \(x_{ij}\), the ordinary cluster mean includes that same value. Using a mean that is calculated from the target value as if it were an independent predictor can create circularity. One practical construction for the conditional update is a leave-one-out mean,

$$ \bar{x}_{.-i,j}=\frac{\sum_{k\ne i}x_{kj}}{n_j-1}, $$

when \(n_j>1\) and the other cluster values are available or currently imputed. After updating \(x_{ij}\), recompute the full cluster mean for the substantive analysis and its derived terms. The leave-one-out choice is not a universal substitute for a correctly specified joint conditional model; it is a way to avoid inserting the target value into its own predictor in a simple conditional update.

Worked Example: Updating a Missing Value Without Reusing It in Its Cluster Mean

Suppose a hypothetical cluster has four predictor values \(2,4,\text{missing},8\). At one imputation iteration, the missing value currently has a provisional draw of 6. The full current mean is \((2+4+6+8)/4=5\), so the provisional within deviation for that record is \(6-5=1\) and its square is \(1\).

When forming a conditional predictor for this record's update, its leave-one-out mean is \((2+4+8)/3=14/3\approx4.667\), which does not use the record's current value. Relative to that mean, the provisional value 6 has deviation \(6-14/3=4/3\approx1.333\), with square \((4/3)^2=16/9\approx1.778\). If the next draw is 7, the leave-one-out mean remains \(14/3\) for this update, while the completed cluster mean becomes \((2+4+7+8)/4=5.25\). The new full-data deviation is \(7-5.25=1.75\), and its square is \(1.75^2=3.063\).

The distinction is deliberate: the leave-one-out quantity is used to construct a conditional predictor for the target update, while the full cluster mean and derived terms are rebuilt from the completed values for analysis. The assumed construction must be applied consistently across iterations and clusters.

Justification and Practical Checks

The reason for rebuilding deterministic terms follows directly from their definitions. If the completed predictor changes from \(x_{ij}^{(t)}\) to \(x_{ij}^{(t+1)}\), then its cluster mean, deviation, and square are functions of that updated value and of the other current values. Thus the transformed terms at iteration \(t+1\) must equal those functions evaluated at the new completed data. Leaving the old terms in place would make them inconsistent with the data used in the next conditional model.

$$ w_{ij}^{(t+1)}=x_{ij}^{(t+1)}-\bar{x}_{.j}^{(t+1)},\qquad \left(w_{ij}^{2}\right)^{(t+1)}=\left(w_{ij}^{(t+1)}\right)^2. $$

This is a deterministic update, not an additional stochastic draw. Uncertainty in the missing \(x\) values is carried through because each imputation produces different completed values, which in turn produce different cluster means and nonlinear terms.

Useful checks include verifying that the within deviations sum to zero within each cluster when calculated from the same full set of values, checking that each squared term is nonnegative, and confirming that spline bases change at the intended knots. Inspect observed and imputed predictor distributions by cluster, and compare the fitted nonlinear patterns with the observed data. Sparse clusters, extreme imputed values, and knots placed where there are few observations can make nonlinear terms unstable.

A linear model is simpler and often more stable, but can miss meaningful curvature. A polynomial is compact and easy to interpret locally, but its shape can be overly constrained and high-degree forms can be unstable. Splines allow more local flexibility, at the cost of selecting knots and estimating more parameters. Whichever representation is used, within- and between-cluster terms should be compared separately rather than treating a single transformation of raw \(x\) as a general solution.

Common pitfall: Independently imputing a predictor, its square, and its cluster mean treats quantities with exact mathematical relationships as unrelated. Instead, impute the underlying predictor, recompute its deterministic transformations after each update, and use a conditional specification that reflects the nonlinear substantive relationship. Recalculation alone does not fix a misspecified conditional model or guarantee compatibility.

Check Your Understanding

Answer each question, then compare with the short solutions below.

  1. A cluster mean is 10 and an individual's value is 13. What are the within deviation and its square?
  2. Why does a raw \(x^2\) term not, by itself, distinguish within- from between-cluster curvature?
  3. For a hinge basis \(h_2(u)=\max(0,u-2)\), calculate the basis value at \(u=1\) and \(u=4\).
  4. A missing predictor is imputed as 5, and its square is also independently imputed as 30. What is wrong, and what value should the square have?

Solutions. (1) The deviation is \(13-10=3\); its square is \(3^2=9\). (2) Expanding \(x_{ij}^2=(w_{ij}+\bar{x}_{.j})^2\) gives \(w_{ij}^2+2w_{ij}\bar{x}_{.j}+\bar{x}_{.j}^2\), so the raw square mixes within, between, and cross-product components. (3) \(h_2(1)=\max(0,-1)=0\), and \(h_2(4)=\max(0,2)=2\). (4) The terms violate the deterministic relationship; the square must be recomputed as \(5^2=25\), not drawn independently as 30.

Clinical Biostats