Tutorials › Joint Modeling of Longitudinal and Time-to-Event Data › Continuity Constraints for Spline Trajectories

Flexible Trajectories and Non-Gaussian Responses · Tutorial 474 of 697

Continuity Constraints for Spline Trajectories

Learn how continuity requirements translate into coefficient equations for piecewise cubic trajectories, and how to verify the resulting spline at its knots.

Advanced 11 min read

What You'll Learn

  • Write value, first-derivative, and second-derivative matching conditions at an interior knot.
  • Translate a polynomial between local knot-centered coordinates without losing units.
  • Distinguish continuous, continuously differentiable, and twice-continuously differentiable cubic trajectories.
  • Count the free coefficients remaining after continuity constraints.
  • Diagnose a spline whose value or derivatives jump at a knot.

Why Coefficients Must Match at Knots

A piecewise cubic trajectory can adapt its shape over time by using a separate cubic polynomial on each interval between knots. But fitting a polynomial on each interval does not, by itself, ensure a coherent trajectory: the pieces could disagree at a knot, or meet while having sharply different slopes or curvature. In longitudinal models, such behavior can create an abrupt change in the modeled biomarker mean that comes only from the chosen parameterization.

The remedy is to impose equations on the polynomial coefficients. These equations express exactly what it means for adjacent pieces to agree in value, first derivative, and second derivative. The resulting constraints are algebraic and apply whether the trajectory is used as a mean or as a component of a longitudinal model’s linear predictor. They do not, on their own, make assumptions about the residual distribution or the event-time submodel.

Definition: A piecewise cubic trajectory is continuous at a knot if its left- and right-hand values agree there (it is \(C^0\)). It is continuously differentiable there if the values and first derivatives agree (\(C^1\)). It is twice continuously differentiable there if the values, first derivatives, and second derivatives agree (\(C^2\)). These conditions are imposed at every interior knot where the specified smoothness is required.

Set Up the Local Polynomials

Let the ordered knots be \(\kappa_0<\kappa_1<\cdots<\kappa_{K+1}\), with \(\kappa_1,\ldots,\kappa_K\) interior knots. On interval \(j\), from \(\kappa_{j-1}\) to \(\kappa_j\), write the trajectory using time measured from that interval’s left endpoint:

$$ m_j(t)=a_{j0}+a_{j1}(t-\kappa_{j-1})+ a_{j2}(t-\kappa_{j-1})^2+ a_{j3}(t-\kappa_{j-1})^3,\qquad j=1,\ldots,K+1. $$

The local coordinate makes the matching equations especially transparent. At the interior knot \(\kappa_j\), the left-hand polynomial has coordinate \(h_j=\kappa_j-\kappa_{j-1}\), while the right-hand polynomial has coordinate zero. The interval widths \(h_j\) are positive because the knots are strictly ordered. We assume each piece is an ordinary cubic polynomial on its interval and that the same trajectory is being joined on both sides of each knot.

If \(m_j(t)\) is measured in marker units and time is measured in years, then \(a_{j0}\) has units of marker units, \(a_{j1}\) has units of marker units per year, \(a_{j2}\) has units of marker units per year squared, and \(a_{j3}\) has units of marker units per year cubed. Each term in the polynomial therefore has marker units. The constraints below also match quantities with the same units on both sides.

Deriving the Matching Constraints

Differentiate the polynomial on interval \(j\), using \(u=t-\kappa_{j-1}\):

$$ m_j'(t)=a_{j1}+2a_{j2}u+3a_{j3}u^2,\qquad m_j''(t)=2a_{j2}+6a_{j3}u. $$

At \(\kappa_j\), the left piece has \(u=h_j\). The right piece, \(m_{j+1}\), starts at its local coordinate zero. Equating the corresponding quantities gives the following conditions.

$$ \begin{aligned} C^0:\quad &a_{j0}+a_{j1}h_j+a_{j2}h_j^2+a_{j3}h_j^3=a_{j+1,0},\\ C^1:\quad &a_{j1}+2a_{j2}h_j+3a_{j3}h_j^2=a_{j+1,1},\\ C^2:\quad &2a_{j2}+6a_{j3}h_j=2a_{j+1,2}. \end{aligned} $$

The \(C^0\) equation is the condition for continuity alone. Requiring \(C^1\) means imposing both the \(C^0\) and first-derivative equations. Requiring \(C^2\) means imposing all three. There is no matching condition on third derivatives for a \(C^2\) cubic spline: a jump in the third derivative is allowed.

These equations can also be used constructively. Given the coefficients on the left piece, choose the next piece’s constant, linear, and quadratic coefficients to satisfy the three equations. Its cubic coefficient remains free. Solving for the right-hand coefficients gives:

$$ \begin{aligned} a_{j+1,0}&=a_{j0}+a_{j1}h_j+a_{j2}h_j^2+a_{j3}h_j^3,\\ a_{j+1,1}&=a_{j1}+2a_{j2}h_j+3a_{j3}h_j^2,\\ a_{j+1,2}&=a_{j2}+3a_{j3}h_j. \end{aligned} $$

This is a short derivation of the constraint form: evaluate the left polynomial and its first two derivatives at the knot, evaluate the right polynomial and its derivatives at local coordinate zero, and equate each pair. The matching equations are linear in the coefficients when the knot locations are fixed.

Worked Example: Constructing a \(C^2\) Join

Consider two hypothetical marker-mean pieces, with time in years and marker values in marker units. The first piece is \(m_1(t)=2+t+0.5t^2+0.1t^3\) on \(0\leq t\leq2\). We want the second piece to be \(C^2\) at the knot \(\kappa_1=2\), and we choose its cubic coefficient to be \(-0.05\).

Here \(h_1=2\). The left-hand value at the knot is \(2+1(2)+0.5(2^2)+0.1(2^3)=2+2+2+0.8=6.8\) marker units. The left-hand slope is \(1+2(0.5)(2)+3(0.1)(2^2)=1+2+1.2=4.2\) marker units per year. The left-hand second derivative is \(2(0.5)+6(0.1)(2)=1+1.2=2.2\) marker units per year squared.

Write the next piece in \(u=t-2\) as \(m_2(t)=a_{20}+a_{21}u+a_{22}u^2-0.05u^3\). Matching value gives \(a_{20}=6.8\). Matching slope gives \(a_{21}=4.2\). Matching second derivative gives \(2a_{22}=2.2\), so \(a_{22}=1.1\). Thus \(m_2(t)=6.8+4.2u+1.1u^2-0.05u^3\).

At \(t=3\), \(u=1\), so the trajectory is \(6.8+4.2+1.1-0.05=12.05\) marker units. Its slope there is \(4.2+2(1.1)-3(0.05)=6.25\) marker units per year. The knot conditions were used to determine the first three right-piece coefficients; the chosen cubic coefficient controls how that piece bends away from the knot.

How Many Coefficients Remain?

With \(K\) interior knots, there are \(K+1\) intervals and \(4(K+1)\) local polynomial coefficients before matching. At each interior knot, a \(C^2\) join imposes three scalar equations. For distinct knots, these equations are independent: starting with the first interval’s four coefficients and adding one new interval at a time, each new interval has three coefficients fixed by matching and one cubic coefficient still free. Consequently, the dimension is \(4+K=K+4\).

This count explains the equivalence with a familiar cubic-spline representation from the earlier material on truncated power spline bases. A \(C^2\) piecewise cubic with \(K\) interior knots can be written using four global cubic terms and one truncated cubic term per knot:

$$ m(t)=\beta_0+\beta_1t+\beta_2t^2+\beta_3t^3+ \sum_{j=1}^{K}\delta_j(t-\kappa_j)_+^3, \qquad (x)_+=\max(x,0). $$

Each added term is zero to the left of its knot and is a cubic to the right. Its value, first derivative, and second derivative are zero at the knot, so it changes the cubic piece to the right without disrupting \(C^2\) matching. The representation has \(4+K\) coefficients, agreeing with the constraint count. The local-coefficient equations and this basis representation describe the same class of trajectories, though their coefficients have different meanings.

Worked Example: Checking a Join That Is Only \(C^1\)

Use the left piece from the previous example, whose value, slope, and second derivative at \(t=2\) are \(6.8\), \(4.2\), and \(2.2\), respectively. Suppose the proposed right piece is \(m_2(t)=6.8+4.2u+1.3u^2-0.05u^3\), where \(u=t-2\).

At \(u=0\), its value is \(6.8\), matching the left value. Its derivative at \(u=0\) is \(4.2\), matching the left slope. But its second derivative at \(u=0\) is \(2(1.3)=2.6\), not \(2.2\). The curvature jump is \(2.6-2.2=0.4\) marker units per year squared.

Therefore the trajectory is \(C^1\), but not \(C^2\), at the knot. This may be an acceptable model if only value and slope continuity are required. It is not a valid \(C^2\) cubic spline with the stated coefficients. A graph alone may make the discrepancy hard to see, so the derivative check is useful even when the trajectory appears smooth.

Equivalent Constraints in Global Coordinates

Some software or derivations express every piece in powers of \(t\), rather than powers of time since the interval began. If the left piece is \(A_0+A_1t+A_2t^2+A_3t^3\) and the right piece is \(B_0+B_1t+B_2t^2+B_3t^3\), matching at \(\kappa\) requires:

$$ \begin{aligned} A_0+A_1\kappa+A_2\kappa^2+A_3\kappa^3 &=B_0+B_1\kappa+B_2\kappa^2+B_3\kappa^3,\\ A_1+2A_2\kappa+3A_3\kappa^2 &=B_1+2B_2\kappa+3B_3\kappa^2,\\ 2A_2+6A_3\kappa&=2B_2+6B_3\kappa. \end{aligned} $$

These are the same value, slope, and curvature conditions, evaluated in a different coordinate system. Local coordinates often simplify interpretation because a segment’s value at its left endpoint is exactly its constant coefficient. Global coordinates can be convenient for algebra, but it is easy to confuse coefficients from the two coordinate systems. Translating a polynomial between coordinates before comparing coefficients avoids that mistake.

Worked Example: Reading a Truncated-Cubic Coefficient

Suppose a hypothetical trajectory has one knot at \(t=2\) and is written as \(m(t)=2+t+0.5t^2+0.1t^3-0.15(t-2)_+^3\). Before the knot, the truncated term is zero, so the trajectory is \(m_1(t)=2+t+0.5t^2+0.1t^3\). After the knot, set \(u=t-2\). The base cubic, translated to \(u\), is \(6.8+4.2u+1.1u^2+0.1u^3\), as follows by expanding the powers of \(u+2\).

The added term is \(-0.15u^3\) for \(u\geq0\). Thus the right-hand piece is \(6.8+4.2u+1.1u^2+(0.1-0.15)u^3 =6.8+4.2u+1.1u^2-0.05u^3\). Its constant, linear, and quadratic coefficients agree with those required in the first worked example. The truncated-cubic term changes the third-order coefficient on the right, while leaving the value, slope, and second derivative at the knot unchanged.

Diagnostics, Alternatives, and Limitations

A practical check should evaluate each adjacent pair at every interior knot. Compare the two fitted values for \(C^0\), the two first derivatives for \(C^1\), and the two second derivatives for \(C^2\). A nonzero discrepancy indicates either a violated constraint, a coding error, or a different smoothness requirement than intended. If coefficients are estimated numerically, tiny discrepancies may be due to rounding; assess them at the precision of the computation rather than treating every floating-point difference as a substantive jump.

There are several common failure modes:

  • Matching values but not slopes. This gives a continuous trajectory, but a visible corner may remain. Continuity alone is not differentiability.
  • Matching slopes while forgetting values. Derivative equality does not force the two pieces to meet at the same height; all lower-order conditions required for the target smoothness must be imposed.
  • Using the wrong interval width. In local coordinates, the left piece is evaluated at \(h_j\), not at \(\kappa_j\). Substituting the absolute knot time in place of \(h_j\) generally produces incorrect constraints.
  • Mixing coefficient units. The coefficient of a squared-time term has marker units per year squared, not merely per year squared. Unit checks can reveal a missing time scale or a mismatched derivative.
  • Constraining only a population-average trajectory. If subject-specific trajectories are also intended to be \(C^2\), their coefficients must satisfy the matching conditions too. Constraining fixed effects alone does not generally guarantee smooth individual trajectories.

The main alternative is to parameterize the trajectory directly in a basis that enforces the desired smoothness, such as the truncated-cubic form above, rather than fit separate local polynomials and impose explicit equalities. B-spline bases provide another basis representation with local support, introduced in the earlier material on B-spline bases. Explicit local constraints are useful when segment-specific coefficients are needed or when the smoothness order must be tailored. A basis that builds continuity in can be more compact and reduces the risk of accidentally fitting unconstrained jumps.

Neither approach guarantees that the chosen trajectory is scientifically adequate. Smoothness is a structural restriction, not evidence that the mean path is correctly specified. A \(C^2\) spline can still be too rigid, too wiggly for the data, or poorly supported near the ends of follow-up. The present coefficient constraints also do not choose knot locations or determine how much flexibility is appropriate; those are separate modeling decisions.

Key takeaway: At each knot, evaluate each neighboring polynomial and its required derivatives on the correct side. For a \(C^2\) cubic trajectory, equate the values, first derivatives, and second derivatives; the third derivatives need not agree.

Check Your Understanding

Answer the questions, then compare your reasoning with the short solutions.

  1. For two local cubic pieces meeting at a knot with interval width \(h\), write the equation that enforces continuity of value.
  2. A left piece has second derivative \(3.0\) marker units per year squared at a knot. What quadratic coefficient must a right-hand local polynomial have for a \(C^2\) join?
  3. With four interior knots, how many free coefficients does a \(C^2\) piecewise cubic have?
  4. Why can the third derivative jump at a knot in a \(C^2\) cubic spline?

Solutions. (1) If the left coefficients are \(a_0,\ldots,a_3\) and the right constant coefficient is \(b_0\), the equation is \(a_0+a_1h+a_2h^2+a_3h^3=b_0\). (2) At the right piece’s local origin its second derivative is twice its quadratic coefficient, so \(2b_2=3.0\) and \(b_2=1.5\) marker units per year squared. (3) The dimension is \(K+4=4+4=8\) coefficients. (4) \(C^2\) requires agreement only in value and the first two derivatives. A piecewise cubic’s third derivative is constant within each interval, but no condition in \(C^2\) requires those constants to agree across a knot.

Clinical Biostats