Tutorials › Joint Modeling of Longitudinal and Time-to-Event Data › Adding Random Effects Within Trajectory Classes

Latent Class Joint Models · Tutorial 450 of 697

Adding Random Effects Within Trajectory Classes

Learn to specify class-specific trajectory means alongside within-class random effects, and distinguish between-subject deviations from residual measurement error.

Advanced 12 min read

What You'll Learn

  • Write a longitudinal submodel with latent-class-specific mean trajectories and subject-level random effects.
  • Distinguish systematic differences between class means from individual variation within a class.
  • Derive the marginal within-class covariance induced by random intercepts and slopes.
  • Identify the assumptions behind a standard Gaussian random-effects specification.
  • Recognize overfitting, confounding, and inadequate within-class trajectory structure.

Why Add Random Effects Within Trajectory Classes?

A latent class describes a subgroup with its own average longitudinal pattern. But subjects assigned to the same class need not follow identical trajectories. Some may begin above the class average; others may change faster or more slowly. A model with class-specific means alone leaves these individual differences to the measurement-error term, which can be an inadequate representation when deviations persist across visits or follow a systematic pattern over time.

Within-class random effects add subject-level departures from the class mean. The two parts answer different questions: class-specific means describe how the average trajectory differs between classes, while random effects describe how subjects vary around their own class's trajectory. In a joint model, these components can also provide a way to relate a subject's longitudinal deviation to their event risk. The central task here is to specify the longitudinal part and understand the heterogeneity it represents.

Definition: Let \(C_i\in\{1,\ldots,G\}\) denote the latent class for subject \(i\), and let \(Y_{ij}\) be the longitudinal measurement for subject \(i\) at time \(t_{ij}\). A class-specific mean with within-class random effects has the form $$ Y_{ij}=m_{C_i}(t_{ij},x_i;\beta_{C_i})+z(t_{ij},x_i)^\top b_i+\epsilon_{ij}. $$ Here \(m_g\) is the mean trajectory for class \(g\), \(\beta_g\) its parameters, \(z\) the random-effects design vector, \(b_i\) the subject-specific random-effects vector, and \(\epsilon_{ij}\) the occasion-specific residual.

Model Components and Assumptions

For a standard Gaussian specification, conditional on class \(C_i=g\) and observed covariates, assume \(b_i\mid C_i=g\sim N(0,D_g)\), where \(D_g\) is a positive semidefinite covariance matrix. Assume also that \(\epsilon_{ij}\mid C_i=g\sim N(0,\sigma_g^2)\), with errors independent across visits and independent of \(b_i\). These assumptions imply that the class-specific trajectory is the conditional mean: \(E(Y_{ij}\mid C_i=g,x_i)=m_g(t_{ij},x_i;\beta_g)\). The zero mean for \(b_i\) is important: the class-specific mean is the average trajectory in that class, rather than a mean shifted by an unidentified average random effect.

For example, a linear class-specific mean and a random intercept and slope can be written

$$ Y_{ij}=\beta_{0g}+\beta_{1g}t_{ij}+x_i^\top\gamma_g+b_{0i}+b_{1i}t_{ij}+\epsilon_{ij},\qquad C_i=g. $$

In this model, \(\beta_{0g}\) and \(\beta_{1g}\) describe the class's average starting level and rate of change, while \(b_{0i}\) and \(b_{1i}\) describe subject \(i\)'s deviations from those class averages. The covariate effect \(\gamma_g\) may be shared across classes or allowed to differ; that is a modeling choice, not a consequence of including random effects. The event-time submodel can be specified separately by class and may include a function of \(b_i\) when subject-specific longitudinal deviations are intended to relate to event risk. The precise event-time specification is a separate modeling decision.

Random-effects distributions and covariance structures need not differ by class. A common \(D\) can be used when similar within-class variability is plausible and the data cannot support separate covariance matrices. Class-specific \(D_g\) allows different patterns of within-class variability, but requires more information. In either case, a covariance matrix must be valid: its diagonal elements are variances, and its off-diagonal elements must be consistent with a positive semidefinite matrix.

Worked Example: Separating a Class Mean From a Subject's Trajectory

Suppose a hypothetical two-class model uses time in years. In class 1, the mean biomarker trajectory is \(10+2t\) units. A subject has random intercept \(b_{0i}=1\) unit and random slope \(b_{1i}=-0.5\) units per year. At year 3, the occasion-specific residual is \(\epsilon_{ij}=0.2\) units. Calculate the predicted mean and the subject's modeled measurement.

The class-specific mean at year 3 is \(10+2(3)=16\) units. The subject's random-effects departure at year 3 is \(b_{0i}+b_{1i}t=1+(-0.5)(3)=-0.5\) units. Thus the conditional subject-specific expectation, given these random effects, is \(16-0.5=15.5\) units. Including the stated residual gives \(Y_{ij}=16-0.5+0.2=15.7\) units.

The class mean remains 16 units at year 3: the random-effects departure does not redefine the class trajectory. It describes this subject's deviation from that trajectory. The residual is a further occasion-specific difference.

What Variation Do the Components Capture?

The random effects induce dependence between repeated measurements from the same subject. Conditional on class \(g\), the model gives a useful decomposition. Let \(Z_i\) be the matrix whose rows are \(z(t_{ij},x_i)^\top\), and let \(R_i\) be the covariance matrix of the occasion-specific residuals. Under the assumptions above, \(R_i=\sigma_g^2 I\) for independent, equal-variance residuals. The law of total covariance yields the marginal covariance of the repeated measurements within class \(g\).

$$ \begin{aligned} \operatorname{Var}(Y_i\mid C_i=g) &=\operatorname{Var}\{E(Y_i\mid b_i,C_i=g)\mid C_i=g\} +E\{\operatorname{Var}(Y_i\mid b_i,C_i=g)\mid C_i=g\}\\ &=Z_iD_gZ_i^\top+R_i. \end{aligned} $$

The first term is persistent, structured between-subject variation: the same random intercept or slope affects multiple visits. The second term is residual variation that remains after class and random effects are accounted for. With independent residuals, it contributes only to the diagonal. This distinction is a feature of the specified model; it does not establish that the assumptions are correct for a particular data set.

Worked Example: Covariance From a Random Intercept and Slope

Consider a hypothetical class with random intercept variance \(D_{00}=4\), random slope variance \(D_{11}=1\), and intercept-slope covariance \(D_{01}=D_{10}=0.5\). Suppose the residual variance is \(1\) unit squared at each visit. For measurements at years 0 and 2, calculate the marginal variances and covariance. Use \(z(t)=(1,t)^\top\).

At year 0, the random-effects variance is \(z(0)^\top D z(0)=4\). Adding the residual variance gives \(\operatorname{Var}(Y(0)\mid C=g)=4+1=5\) units squared.

At year 2, the random-effects variance is \(4+2(2)(0.5)+2^2(1)=4+2+4=10\) units squared. Including the residual gives \(\operatorname{Var}(Y(2)\mid C=g)=10+1=11\) units squared.

The covariance between year 0 and year 2 is \(z(0)^\top D z(2)=4+2(0.5)=5\) units squared. Independent occasion-specific residuals contribute nothing to this off-diagonal covariance. Thus the same subject-level effects generate correlated measurements, even though the residual errors are independent.

Between-class and within-class heterogeneity can coexist. To see the distinction, imagine two classes with different average levels, and subjects who also vary around their respective class averages. The class contrast is a difference in \(m_g\); the within-class spread is governed by \(D_g\) and \(R_i\). If one reports only the overall spread, these sources can be conflated. In particular, pooling classes without accounting for their different means can produce apparent variation that is not within-class subject variability.

Worked Example: Comparing Between-Class and Within-Class Variation

Suppose a hypothetical measurement at a fixed visit has class means of 10 and 14 units. Each class has probability \(0.5\), and within either class the total variance from random effects and residual error is 4 units squared. Calculate the overall mean and variance, and identify the two sources of variance.

The overall mean is \(0.5(10)+0.5(14)=12\) units. By the law of total variance, \(\operatorname{Var}(Y)=E\{\operatorname{Var}(Y\mid C)\}+\operatorname{Var}\{E(Y\mid C)\}\). The within-class contribution is \(0.5(4)+0.5(4)=4\) units squared. The between-class contribution is \(0.5(10-12)^2+0.5(14-12)^2=0.5(4)+0.5(4)=4\) units squared. Therefore the overall variance is \(4+4=8\) units squared.

The value 8 is not the within-class variance; that component is 4. The other 4 arises because the class means differ. This calculation illustrates why adding random effects within classes does not make class-specific means redundant: they represent distinct parts of the variation.

Specification, Estimation, and Failure Modes

The model's flexibility depends on the random-effects design. A random intercept allows subjects to differ in level while preserving a common trajectory shape. Adding a random slope allows their rates of change to vary. Higher-order random effects or class-specific covariance matrices can represent richer variation, but also add parameters and estimation demands. The design should reflect scientifically plausible individual departures and the measurement schedule, not simply maximize complexity.

In a joint model, class membership remains latent, so the longitudinal observations and event-time information contribute to uncertainty about class and random effects through the model. Estimation must account for that uncertainty rather than treating a subject's most likely class assignment as known. The random effects are also integrated over their distribution, rather than treated as known subject-level constants. The resulting computation can be more demanding than a class-specific mean model with no random effects.

Several warning signs merit attention. A random-slope variance estimated near zero may indicate that the data do not support slope heterogeneity, although numerical and design limitations should also be considered. A correlation estimate near \(1\) or \(-1\) between random effects can signal a weakly identified or overly elaborate covariance structure. Estimates can be especially unstable when visits are sparse, follow-up times are similar for everyone, or a class contains little information. Patterns in within-class residuals that change over time can indicate a misspecified mean, variance, or residual structure. These checks assess model adequacy; they do not by themselves identify a unique replacement model.

A key comparison is between three representations. A class-specific mean model without random effects permits distinct subgroup averages but no additional persistent subject-level trajectory departures. A model with random effects but no classes permits a continuous distribution of subject deviations around one population mean trajectory. The combined model allows both discrete differences in average trajectory and continuous subject variation within each class. They answer different scientific questions; one is not automatically superior to the others.

There are limitations. A Gaussian random-effects distribution may be a poor description of within-class variation, and normality can affect estimation and inference. A class-specific mean can be too rigid if its shape is wrong; random effects should not be used to disguise a systematic mean misspecification. Conversely, adding many random effects may leave too little information to estimate their covariance. If the class structure and random-effects distribution both have substantial flexibility, their roles can become difficult to distinguish. Results should therefore be interpreted as features of a specified model, not as proof that the population consists of exactly those classes or that all individual differences follow the chosen distribution.

Common Pitfall

A frequent mistake is to say, “The random intercept accounts for the class difference because people in a class start at different levels.” That reasoning confuses variation around a class mean with differences between class means. In the model, the random intercept has mean zero within class \(g\); it describes subject deviations around \(m_g\). If two classes have different average starting levels, those differences belong in their class-specific mean parameters. Allowing arbitrary nonzero class-specific random-effect means without constraints would blur the distinction and make the mean and random-effect components difficult to identify.

Key takeaway: Class-specific means represent systematic average trajectory differences across latent classes. Within-class random effects represent persistent subject-level departures from those means, while occasion-specific residuals represent remaining measurement-level variation under the assumed residual model.

Check Your Understanding

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

  1. In a random-intercept and random-slope model, which component changes the class-average slope, and which allows a subject's slope to deviate from that average?
  2. For \(Y_{ij}=\beta_{0g}+\beta_{1g}t_{ij}+b_{0i}+b_{1i}t_{ij}+\epsilon_{ij}\), what does the zero-mean assumption on \(b_i\mid C_i=g\) ensure?
  3. If residual errors are independent across visits but a random intercept is present, can two measurements from the same subject be correlated? Explain.
  4. At a fixed visit, within-class variance is 3 units squared and the between-class contribution is 5 units squared. What is the total variance under the law of total variance?
  5. Name one reason a model with class-specific means and random effects may be difficult to estimate.

Solutions. (1) \(\beta_{1g}\) determines the class-average slope; \(b_{1i}\) gives subject \(i\)'s slope deviation. (2) It makes the class-specific mean the average trajectory in class \(g\), rather than shifting that mean by an average random effect. (3) Yes. The random intercept is shared across visits, so it induces positive covariance even when the occasion-specific residuals are independent. (4) The total variance is \(3+5=8\) units squared. (5) For example, sparse visits, little follow-up-time variation, or a small class may provide insufficient information for the class means and random-effects covariance to be estimated reliably.

Clinical Biostats