Tutorials › Pharmacometric Workflows and Reproducible Modeling › Time-Varying Covariates

Data Specification and Dataset Preparation · Tutorial 27 of 399

Time-Varying Covariates

Learn how to align changing covariate measurements with analysis times, document the alignment rule, and identify when a rule would use information from the future.

Intermediate 9 min read

What You'll Learn

  • Distinguish a covariate’s measurement time from the event time at which its value is needed.
  • Apply a documented last-observation-carried-forward rule with a maximum permitted time gap.
  • Calculate a linearly interpolated value and identify the assumptions it requires.
  • Prevent future measurements or results unavailable at the analysis time from entering a covariate record.
  • Diagnose stale, misordered, missing, and ambiguously timed covariate data.
  • Compare carry-forward, interpolation, and leaving values unavailable for an analysis time.

Why Time-Varying Covariates Need a Time Rule

A static covariate, such as a baseline characteristic, can be attached to multiple records without changing its value. A time-varying covariate is different: its recorded value can change during follow-up, so a model row at a particular time needs a rule for choosing which measurement represents the covariate at that time. Without an explicit rule, two analysts can produce different datasets from the same measurements.

For example, a laboratory value measured on day 4 may be used in a record on day 6 under a carry-forward policy. A value measured on day 9 cannot be treated as if it were already known on day 6 when the purpose is to represent information available at day 6. Timestamping and alignment therefore affect both reproducibility and the meaning of the model input.

Definition: For individual \(i\), let \(x_{ij}\) be the \(j\)th recorded measurement of a covariate, measured at time \(t_{ij}\). Let \(t\) be the analysis time at which a covariate value is required. An alignment rule maps the dated measurements to a value \(x_i(t)\), or specifies that no value is available. The rule must state how it handles measurements before and after \(t\), ties at \(t\), and gaps between measurements.

The time origin and units must be defined consistently. For example, times might be days since the first dose, with timestamps recorded to the nearest hour. A measurement’s time should refer to when it was taken or collected, not automatically to when it was entered into a database. When the distinction matters, record both the measurement time and the time the result became available to the people or process that would use it.

Carry-Forward Alignment

A common policy is to use the most recent measurement taken at or before the analysis time. This is often called last observation carried forward (LOCF). It treats the covariate as unchanged between that measurement and the next one. That is a data-alignment assumption, not a claim that the underlying biological quantity truly stayed constant.

Definition: Under carry-forward alignment, the selected measurement time is \(t_{ij^*}=\max\{t_{ij}:t_{ij}\leq t\}\), and \(x_i(t)=x_{ij^*}\). If there is no measurement at or before \(t\), or if the selected measurement is more than a prespecified maximum age \(G\) at time \(t\), the value is unavailable under this policy.

A maximum age \(G\) prevents a very old measurement from being treated as representative indefinitely. The threshold should be justified for the covariate and analysis and stated in the data specification. If the analysis time is day 12, the selected measurement is from day 5, and \(G=10\) days, the measurement is 7 days old and passes the age rule. If the analysis time is day 20, that same measurement is 15 days old and fails.

Worked Example: Carrying Forward Dated Measurements

Suppose a hypothetical participant has weight measurements of 70.0 kg on day 0, 68.0 kg on day 14, and 66.5 kg on day 35. Analysis records are needed on days 10, 21, 35, and 40. The prespecified rule is to carry forward the latest measurement at or before each record, provided it is no more than 21 days old. A measurement on the same day is eligible.

Day 10. The latest prior measurement is 70.0 kg at day 0. Its age is \(10-0=10\) days, which is within 21 days. Assign 70.0 kg.

Day 21. The latest prior measurement is 68.0 kg at day 14. Its age is \(21-14=7\) days, which is within the limit. Assign 68.0 kg.

Day 35. A measurement of 66.5 kg is available on day 35. The same-day rule makes it eligible, so assign 66.5 kg. A project should specify same-time ordering if an event and measurement share a timestamp but it is not known whether the result was available for that event.

Day 40. The latest prior measurement remains 66.5 kg at day 35. Its age is \(40-35=5\) days, so assign 66.5 kg.

The resulting values are 70.0 kg, 68.0 kg, 66.5 kg, and 66.5 kg, respectively. They are assignments under the stated policy, not additional weight measurements. Each assigned value should remain traceable to its source measurement and timestamp.

Interpolation and Its Information Boundary

When the intended representation is a smoothly changing covariate, linear interpolation between two measurements is another possible rule. It assumes the value changes linearly over the interval between the measurements. Unlike carry-forward, interpolation at time \(t\) generally uses a measurement after \(t\). That can be appropriate for a retrospective description of a trajectory under a stated assumption, but it can introduce future information when the covariate is meant to represent what was known at time \(t\).

Definition: Given measurements \(x_{ij}\) and \(x_{i,j+1}\) at \(t_{ij}\) and \(t_{i,j+1}\), with \(t_{ij}\leq t\leq t_{i,j+1}\) and \(t_{i,j+1}>t_{ij}\), linear interpolation assigns \(x_i(t)=x_{ij}+\frac{t-t_{ij}}{t_{i,j+1}-t_{ij}}(x_{i,j+1}-x_{ij})\). This rule requires a bracketing pair of measurements and an assumption that linear change between them is an adequate representation.

For a predictor intended to reflect information available at time \(t\), impose a point-in-time restriction: no measurement taken after \(t\) may contribute to the assigned value. If results are not available immediately on collection, the restriction may need to use the result-availability time as well. A measurement collected before \(t\) but reported after \(t\) was not necessarily usable at \(t\).

Worked Example: Interpolation Versus a Point-in-Time Value

In a hypothetical dataset, a biomarker is 100 units at day 0 and 120 units at day 10. A model record is needed at day 4. First calculate the linear interpolation, then compare it with a point-in-time carry-forward rule.

The interpolation fraction is \((4-0)/(10-0)=4/10=0.4\). The change across the full interval is \(120-100=20\) units. Therefore,

$$ x_i(4)=100+\frac{4-0}{10-0}(120-100) =100+0.4(20)=108\ \text{units}. $$

The interpolated value is 108 units. However, it uses the day-10 measurement, which is after the day-4 analysis time. If the record is intended to represent information available on day 4, the day-10 measurement must not be used. Under carry-forward alignment, the latest eligible measurement is the day-0 value, so the point-in-time value is 100 units. These values answer different questions: 108 is a retrospective interpolation under a linearity assumption; 100 is the latest measured value available by day 4 under the stated policy.

Justifying the No-Future Rule

The point-in-time restriction can be expressed directly. Let \(\mathcal{I}_i(t)\) denote the measurements permitted as inputs for an analysis record at time \(t\). For an information-at-time-\(t\) predictor, every contributing measurement must satisfy \(t_{ij}\leq t\), and, when availability is relevant, its result must also have been available by \(t\). This condition is not met by interpolation if the right-hand measurement occurs after \(t\).

The reason is straightforward: if the value assigned at \(t\) depends on an observation made at a later time, then changing that future observation can change the record at \(t\). A value that is supposed to represent only information available at \(t\) should not change merely because a later measurement was added. This check provides a practical diagnostic for future-information leakage.

1
Set the analysis time.
Identify the exact event or record time and its units and time origin.
2
Apply the eligibility rule.
For a point-in-time predictor, exclude measurements taken after the analysis time, and exclude results not yet available if availability time is part of the policy.
3
Assign or flag.
Apply the declared carry-forward or other eligible rule, check its gap limit, and record the source timestamp. If no eligible value passes the rule, mark the covariate unavailable rather than silently borrowing a future value.

Worked Example: Collection Time and Result Availability

A hypothetical laboratory measurement is collected on day 5, but its result becomes available on day 7. A model record is required on day 6. An earlier valid measurement of 4.2 mmol/L was collected and reported on day 0. The policy is to carry forward the latest result available by the analysis time, with a maximum age of 10 days.

The day-5 measurement was collected before day 6, but its result was not available until day 7. It therefore fails the availability condition for the day-6 record. The day-0 result is available by day 6 and is \(6-0=6\) days old, within the 10-day limit. The value assigned on day 6 is 4.2 mmol/L.

If the analysis instead used only collection timestamps, it might assign the day-5 result to day 6. That would be valid only if the analysis specification defines the covariate using collected specimens regardless of when results became known. For a predictor intended to represent an available result, that choice would introduce information that was not yet available at the record time.

Documenting the Rule and Checking the Output

A reproducible specification should identify the individual and covariate, the measurement timestamp and units, the analysis-time field, the time origin, and the rule used to select or calculate a value. It should also state the maximum carry-forward age, the treatment of same-time records, whether result-availability time matters, and what happens when no eligible measurement exists. Store the source measurement time or times used for each assigned value so that an assignment can be checked against its origin.

Useful checks include confirming that measurement times are ordered within individual, detecting duplicate timestamps with conflicting values, and verifying that every assigned value has an eligible source. For point-in-time values, check that each source time is no later than its analysis time and that any required result-availability time also passes. For carry-forward values, calculate the age of the selected measurement and flag values that exceed the declared limit. Unexpected gaps, abrupt jumps, or long runs of an identical carried value may indicate a source or alignment issue; they are reasons to investigate, not automatic proof of an error.

ApproachWhat it assumes or requiresImportant limitation
Carry forwardThe last eligible value represents the covariate until another measurement is available; define an age limit.Can become stale and does not represent changes between measurements.
Linear interpolationMeasurements bracket the analysis time and linear change between them is adequate.Usually uses a later measurement, so it is unsuitable for a strictly point-in-time predictor.
Leave unavailableNo eligible measurement or permitted derivation is available at the analysis time.Leaves gaps for downstream handling, but avoids disguising uncertainty with an unsupported value.

No single rule is correct for every covariate or purpose. Carry-forward is simple and respects the no-future restriction when applied to eligible observations, but a maximum age is important. Interpolation can provide a smooth retrospective trajectory, but its assumptions should be plausible and its use of later observations explicit. Leaving a value unavailable is often more defensible than inventing a value when measurements do not support the required time point. The choice should reflect the intended interpretation of the model input, not merely which option produces the fewest missing records.

Common Mistakes and Limitations

  • Using the nearest measurement without regard to direction. A day-9 measurement may be closer to a day-6 record than a day-0 measurement, but it is still future information. For a point-in-time predictor, only eligible measurements at or before the record time may be used.
  • Carrying a value forward without an age limit. A value from months earlier may no longer be a credible representation. Specify a justified maximum age and leave the value unavailable when it is exceeded.
  • Treating collection time as result availability. If the analysis concerns information known at the time, a result reported later cannot be used merely because its specimen was collected earlier.
  • Assuming interpolation is a neutral calculation. It imposes a trajectory assumption and may use later data. State both facts and keep it out of point-in-time predictors when future measurements are prohibited.
  • Confusing an assigned value with a measurement. A carried or interpolated value is derived from recorded measurements. Retain the source timestamps and alignment method so its provenance is clear.

These rules govern dataset representation; they do not establish that the covariate is measured without error or that its relationship with an outcome is causal. Sparse measurements can leave the true trajectory uncertain even when the alignment procedure is implemented exactly. The resulting records should be interpreted according to the measurement schedule and the stated alignment assumptions.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions below.

  1. A covariate is 8 units on day 2 and 11 units on day 8. Under carry-forward alignment, what value is assigned on day 5, before applying any age limit?
  2. Using the measurements in question 1, calculate the linear interpolation at day 5. Which measurement makes that value unsuitable for a strictly point-in-time predictor?
  3. A result collected on day 3 is not available until day 6. Can it contribute to a value defined as information available on day 5? Explain.
  4. The most recent eligible measurement is 12 days old, and the documented maximum carry-forward age is 7 days. What should happen to the covariate value?
  5. Name two timestamps or timing rules that should be documented to make time-varying covariate alignment reproducible.

Solutions. (1) The latest measurement at or before day 5 is the day-2 value, so carry forward 8 units, provided the age rule permits it. (2) The fraction is \((5-2)/(8-2)=3/6=0.5\); interpolation gives \(8+0.5(11-8)=9.5\) units. It uses the day-8 measurement, which is after day 5. (3) No. Although collection occurred on day 3, the result was unavailable until day 6, so it was not available on day 5 under the stated definition. (4) Mark the value unavailable under that policy because \(12>7\) days; do not carry it forward anyway. (5) Appropriate answers include the measurement timestamp, analysis-time field and time origin, result-availability time when relevant, maximum carry-forward age, and same-time ordering rule.

Clinical Biostats