Tutorials › Joint Modeling of Longitudinal and Time-to-Event Data › Translating Trajectory Choices Into Longitudinal Model Formulas

Software and Workflow · Tutorial 657 of 697

Translating Trajectory Choices Into Longitudinal Model Formulas

Translate a proposed biomarker trajectory into fixed- and random-effects terms, then verify the resulting paths by expanding the formula and calculating predictions.

Intermediate 10 min read

What You'll Learn

  • Map linear, quadratic, and piecewise trajectories to longitudinal model terms.
  • Distinguish population-level fixed effects from subject-specific random effects.
  • Check whether an interaction allows a group-specific trajectory or only a level difference.
  • Calculate subject-specific fitted values and slopes from model coefficients.
  • Diagnose formula mismatches, unsupported complexity, and time-coding problems.

From a Trajectory to a Formula

A longitudinal formula is more than software syntax: it encodes a claim about how a biomarker changes over time and how subjects may differ in that change. A formula can fit successfully while representing a different trajectory from the one intended. The reliable approach is to write the mathematical mean first, translate each term into the software formula, and then expand the formula back into a subject-specific path.

Let \(y_{ij}\) be the biomarker measurement for subject \(i\) at visit \(j\), and let \(t_{ij}\) be time measured from a stated origin. A useful basic longitudinal model is

$$ y_{ij}=m_i(t_{ij})+\epsilon_{ij}, \qquad m_i(t)=\text{subject-specific mean trajectory}. $$

The formula specifies the mean trajectory \(m_i(t)\), including its fixed and random effects. In the simple Gaussian version used for illustration, random effects \(b_i\) have mean zero and covariance matrix \(D\), and measurement errors \(\epsilon_{ij}\) have mean zero and variance \(\sigma^2\), independently across measurements and independently of \(b_i\). These distributional assumptions are not guaranteed merely because a formula has been written; they are part of the model and should be assessed in context. Software syntax and supported random-effect structures also vary, so the formula must be checked against the chosen fitting tool.

Definition: Fixed effects describe the average trajectory and systematic differences, such as a group contrast. Random effects describe subject-specific deviations from that average. A subject-specific trajectory is obtained by adding that subject’s random-effect contributions to the fixed-effects trajectory.

For example, an intercept represents the expected biomarker level at the time origin when other predictors are at their reference values. A time coefficient represents a rate of change per unit of time only when the trajectory is linear in that time variable. In nonlinear models, the instantaneous rate depends on time.

Linear Trajectories and Formula Expansion

A linear trajectory with a group-level difference in biomarker level, but a common average time slope, can be written as

$$ m_i(t)=\beta_0+\beta_1t+\beta_2G_i+b_{0i}+b_{1i}t, $$

where \(G_i=0\) denotes the comparison group and \(G_i=1\) the treatment group. Here \(b_{0i}\) is subject \(i\)'s deviation in starting level, and \(b_{1i}\) is the subject's deviation in slope. If the software uses the familiar mixed-model formula syntax, the corresponding formula is y ~ time + group + (1 + time | id). The group variable should be coded or treated as a factor so that its coefficient has the intended contrast interpretation.

Worked Example: A Linear Path With a Group Level Difference

Suppose, in a hypothetical biomarker analysis, the fitted fixed effects are \(\beta_0=40\) biomarker units, \(\beta_1=-2\) units per year, and \(\beta_2=1.5\) units. For a treatment-group subject with \(b_{0i}=3\) units and \(b_{1i}=0.5\) units per year, calculate the fitted mean at \(t=2\) years.

The formula is y ~ time + group + (1 + time | id), corresponding to the trajectory above. Substitution gives

$$ m_i(2)=40+(-2)(2)+(1.5)(1)+3+(0.5)(2)=41.5\text{ units}. $$

The fixed-effects prediction for the treatment group at two years is \(40-4+1.5=37.5\) units; this subject's random intercept and slope add \(3+1=4\) units. The treatment-versus-comparison difference is \(1.5\) units at every time because this model has no time-by-group interaction. The value \(41.5\) is the subject-specific mean, not necessarily the observed measurement: an observation also includes its residual error.

A different scientific claim—that groups have different average slopes—requires an interaction. The formula y ~ time * group + (1 + time | id) expands to time, group, and time-by-group fixed effects, plus the stated random effects. Its mean includes an additional term \(\beta_3tG_i\). The group contrast is then \(\beta_2+\beta_3t\), rather than a constant. The formula must match the intended contrast, not just look plausible at a glance.

1
Write the intended mean.
State which terms vary with time, group, and subject. Specify the time origin and reference group.
2
Translate each term.
Use fixed-effect terms for the average pattern and random-effect terms for the subject-specific deviations you intend to allow.
3
Expand and test.
Expand shorthand such as an interaction, then calculate fitted paths at selected times to confirm that the coded formula has the intended meaning.

Nonlinear Trajectories: Curvature and Knots

A quadratic trajectory allows curvature while keeping the model relatively simple. With centered time \(t_c=t-c\), where \(c\) is a chosen reference time, a possible model is

$$ m_i(t_c)=\beta_0+\beta_1t_c+\beta_2t_c^2+b_{0i}+b_{1i}t_c. $$

The corresponding formula is y ~ time_c + I(time_c^2) + (1 + time_c | id). In R-style formulas, I(...) requests arithmetic on the expression inside it. Without that protection, some formula systems interpret the caret as an operator rather than as a request to square the variable. This model allows subject-specific intercepts and linear slopes but gives all subjects the same quadratic curvature. Adding a random quadratic term is a stronger and more data-demanding choice.

Worked Example: Interpreting a Quadratic Path

In a hypothetical model, suppose \(\beta_0=100\) units, \(\beta_1=-4\) units per year, and \(\beta_2=0.5\) units per year squared. For one subject let \(b_{0i}=-2\) units and \(b_{1i}=1\) unit per year. Calculate the fitted mean and instantaneous slope at \(t_c=2\) years.

The formula is y ~ time_c + I(time_c^2) + (1 + time_c | id). The fitted mean is

$$ m_i(2)=100+(-4)(2)+(0.5)(2^2)-2+(1)(2)=94\text{ units}. $$

Differentiating the subject-specific mean with respect to \(t_c\) gives \(\beta_1+2\beta_2t_c+b_{1i}\). At \(t_c=2\), this is \(-4+2(0.5)(2)+1=-1\) unit per year. Thus the fitted path is decreasing at that time, even though the quadratic term is positive. The curvature means the slope changes over time; \(\beta_1\) alone is not the slope at every time.

A piecewise-linear trajectory is useful when a prespecified time point \(k\) marks a change in slope. Define the hinge term \(h(t)=\max(0,t-k)\). A model containing \(t\) and \(h(t)\) is continuous at \(k\): before the knot, the hinge is zero; after it, the hinge increases with time. For subject-specific changes in both the initial slope and the slope change, one possible formula is y ~ time + hinge + (1 + time + hinge | id). This can require substantial data because it estimates several random effects and their covariances.

Worked Example: Checking a Piecewise-Linear Path

Suppose a hypothetical model has a knot at \(k=3\) years and fitted fixed effects \(\beta_0=50\) units, \(\beta_1=-1.2\) units per year, and \(\beta_2=0.4\) units per year for the hinge term. For one subject, let \(b_{0i}=2\) units, \(b_{1i}=0.2\) units per year, and \(b_{2i}=-0.1\) units per year. At \(t=5\), \(h(5)=\max(0,5-3)=2\) years, so

$$ m_i(5)=50+(-1.2)(5)+(0.4)(2)+2+(0.2)(5)+(-0.1)(2)=47.6\text{ units}. $$

Before the knot, the subject-specific slope is \(\beta_1+b_{1i}=-1.2+0.2=-1.0\) unit per year. After the knot, it is \(\beta_1+\beta_2+b_{1i}+b_{2i}=-1.2+0.4+0.2-0.1=-0.7\) unit per year. The hinge is zero at the knot, so the two line segments meet rather than introducing an abrupt jump in the fitted level.

Time-Varying Terms and Practical Checks

A biomarker trajectory is time-varying by definition: its fitted value changes with \(t\). Adding a time-varying predictor is a separate modeling choice. For example, if treatment status changes across visits, a row-specific treatment variable can enter the mean as a covariate. That does not automatically give treatment a time interaction, nor does it imply that the biomarker's slope differs by treatment. To specify a different slope by a binary group, include the corresponding interaction with time. To specify a nonlinear time pattern, include the chosen nonlinear time basis. Keep these roles distinct when constructing the data table and formula.

After fitting, inspect the formula's implied design: confirm the time variable and units, the reference levels, the included powers or basis functions, and the random effects attached to each subject. Then calculate or plot fixed and subject-specific fitted trajectories across the observed time range. Check that the number and timing of observations can support the chosen complexity, and examine residual patterns against fitted values and time. Curvature left in residuals can indicate an inadequate mean trajectory; implausible subject paths or unstable random-effect estimates can signal an overcomplicated random structure. Sparse observations near a knot, or near one end of the time range, make the shape there especially uncertain.

These checks do not establish that the model is correct. They help detect mismatches between intent and implementation and reveal some failures of fit. A linear model is easy to interpret but may miss curvature. A quadratic model is compact but can behave implausibly outside the observed time range. Piecewise linear models allow a prespecified slope change but depend on the knot choice. Spline bases can represent more flexible smooth patterns, though their basis and boundary behavior need careful checking. More flexibility can improve description while making estimates less stable, especially when subjects have few measurements.

Common pitfall: Treating time * group as a group level difference only. In formula syntax, the asterisk expands to time, group, and time-by-group terms, so it allows the average slope to differ by group. If the intended model has a group level difference and a common slope, use time + group instead. Conversely, omitting the interaction when group-specific slopes are intended imposes a common-slope assumption.

A formula describes the longitudinal mean and the chosen random-effect structure; it does not by itself guarantee that software has fitted that exact model or that the assumptions are adequate. In a joint model, the longitudinal trajectory is one component of a larger specification. Checking the trajectory here is therefore important, but it does not replace checking the other model components or their implementation.

Check Your Understanding

For each item, identify what the formula permits. Brief solutions follow the questions.

  1. For y ~ time + group + (1 | id), does the fixed average slope vary by group?
  2. What fixed-effects terms are included by time * group?
  3. For a quadratic model with \(\beta_1=-3\), \(\beta_2=0.25\), and no random slope, what is the fixed instantaneous slope at \(t_c=4\)?
  4. With a hinge at \(k=2\), what are \(h(1)\) and \(h(5)\), and what does the hinge term change?
  5. Name one check that can reveal a mismatch between an intended trajectory and its software formula.

Solutions. (1) No. The fixed slope is common across groups; the group term allows a level contrast. (2) Time, group, and time-by-group. (3) The slope is \(\beta_1+2\beta_2t_c=-3+2(0.25)(4)=-1\) unit per time unit. (4) \(h(1)=0\) and \(h(5)=3\); the hinge adds a slope change after the knot while retaining continuity. (5) Expand the formula's terms and calculate fitted values at selected times and group levels, or plot those implied trajectories.

Clinical Biostats