Tutorials › Marginal Structural Models › Encoding Time-Varying Covariate Histories

Longitudinal Targeted Maximum Likelihood · Tutorial 387 of 501

Encoding Time-Varying Covariate Histories

Build time-indexed histories that give each longitudinal regression the information available at its decision point—without accidentally using future covariates.

Advanced 12 min read

What You'll Learn

  • Define the history available immediately before each treatment decision.
  • Arrange repeated measurements in wide or long form without changing their temporal order.
  • Construct histories for treatment and sequential outcome regressions.
  • Substitute an intervention into the treatment sequence while retaining the appropriate covariate history.
  • Check encodings for future-information leakage, discarded history, and ambiguous time indices.

Why the Order of a History Matters

A longitudinal regression does not receive “the patient’s covariates” in the abstract. It receives measurements available at a particular time. A covariate recorded after a treatment decision cannot be used as a predictor of that decision, even if it appears in the same row of a wide data set. Encoding the history correctly therefore means preserving both which variables were observed and when they were observed.

This matters in longitudinal TMLE because treatment nuisance regressions and sequential outcome regressions are evaluated at different points in the treatment sequence. Intervention prediction also proceeds forward through time: assign a treatment according to the intervention, then use the covariate history that is available at the next decision. The introductory material on longitudinal TMLE establishes the role of these nuisance functions; here the focus is the information each function is allowed to use.

Definition: Suppose measurements are ordered as \(W,L_1,A_1,L_2,A_2,\ldots,L_K,A_K,Y\). Here \(W\) is baseline information, \(L_t\) is the covariate measurement available before treatment \(A_t\), and \(Y\) is the final outcome. Write \(\bar L_t=(L_1,\ldots,L_t)\) and \(\bar A_t=(A_1,\ldots,A_t)\). The pre-treatment history at decision \(t\) is $$ H_t=(W,\bar L_t,\bar A_{t-1}), $$ where \(\bar A_0\) is empty. Thus a treatment regression at time \(t\) uses \(H_t\), not measurements recorded after \(A_t\).

This convention places \(L_t\) before \(A_t\): \(L_t\) may reflect earlier treatment, but it is available for the current decision. For \(t=1\), \(H_1=(W,L_1)\); for \(t=2\), \(H_2=(W,L_1,A_1,L_2)\). If a study uses a different measurement schedule, define the ordering from the actual data-collection protocol rather than assuming that variable subscripts alone settle the timing.

From the Data Layout to the Histories

In wide form, one row represents a person and repeated measurements occupy separate columns. In long form, one row represents a person at a decision time. Either layout can encode the same history, provided the time index and the pre-decision rule are explicit. A long-form row for decision \(t\) should contain the variables in \(H_t\), the current treatment \(A_t\), and any outcome or follow-up information appropriate to that row. It should not silently include \(L_{t+1}\) as a predictor of \(A_t\).

A useful construction is to write down the available columns before fitting any nuisance regression. For each \(t\), list \(W\), \(L_1,\ldots,L_t\), and \(A_1,\ldots,A_{t-1}\). Keep a fixed meaning for every subscript. If the same clinical measurement is recorded several times, distinguish its value at each time; if a variable is genuinely constant over time, identify it as baseline information rather than copying it into the history under ambiguous names.

Worked Example: Building Histories for Two Decisions

Suppose a study records baseline \(W\), a symptom score \(L_1\), treatment \(A_1\), a later lab value \(L_2\), treatment \(A_2\), and final outcome \(Y\). For one participant, the recorded values are \(W=1\), \(L_1=2\), \(A_1=1\), \(L_2=3\), \(A_2=0\), and \(Y=1\).

At the first decision, the history is \(H_1=(1,2)\): baseline value and symptom score. Neither \(L_2\) nor \(A_2\) belongs in the regression for \(A_1\), since both occur later.

At the second decision, \(H_2=(1,2,1,3)\): baseline, the first symptom score, the first treatment, and the later lab value. The regression for \(A_2\) may use all four entries because each is available by that decision.

For illustration, suppose a fitted treatment model at decision two is \(\operatorname{logit}P(A_2=1\mid H_2)=-1+0.5W+0.8L_1+0.4A_1\), with no \(L_2\) term in this deliberately simplified model. Substitution gives \(-1+0.5(1)+0.8(2)+0.4(1)=1.5\). Therefore the fitted probability is \(1/(1+\exp(-1.5))\), approximately \(0.818\). This calculation uses the specified model; it does not imply that omitting \(L_2\) is generally appropriate. The full available history and the chosen regression formula are distinct decisions.

A history is the information set; a regression formula is a way of representing that information to a statistical model. In particular, writing \(H_t\) does not require entering every variable as a separate linear term. It does require not accidentally replacing the intended history with a different one. The subsequent material on interactions and splines concerns ways of modeling relationships once the history has been constructed.

Histories in Intervention Prediction

For an intervention that sets \(A_t=g_t(H_t)\), prediction follows the same temporal order as data collection. The intervention selects a treatment using the current pre-treatment history. The next covariate measurement is then predicted conditional on the history and treatment that came before it. It is not appropriate to predict an earlier treatment using a later covariate merely because that covariate is observed in the data used to fit the model.

One way to express the recursive prediction is to define \(Q_{K+1}=E[Y\mid W,\bar L_K,\bar A_K]\). For a deterministic regime \(g=(g_1,\ldots,g_K)\), set \(Q_K(H_K,a_K)=E[Y\mid H_K,A_K=a_K]\). For earlier times, define $$ Q_t(H_t,a_t)=E\!\left[Q_{t+1}\{H_{t+1},g_{t+1}(H_{t+1})\}\mid H_t,A_t=a_t\right], \qquad t=1,\ldots,K-1. $$ The intervention prediction at the first decision is \(Q_1\{H_1,g_1(H_1)\}\), averaged over the baseline and first pre-treatment covariate distribution. This recursion makes the history update explicit: \(H_{t+1}\) contains \(H_t\), the assigned or conditioned-on treatment \(A_t\), and the newly measured \(L_{t+1}\). The treatment assigned at the next step is then evaluated using that updated history.

Worked Example: Predicting Under a Two-Decision Regime

Fix a baseline stratum and suppose a regime sets \(A_1=1\) and \(A_2=0\). For simplicity, let the intermediate covariate \(L_2\) be binary. Assume the covariate model gives \(P(L_2=1\mid A_1=1)=0.6\). Under \(A_2=0\), suppose the outcome regression gives mean \(0.2\) when \(L_2=0\) and mean \(0.5\) when \(L_2=1\).

After assigning \(A_1=1\), average the next-stage outcome predictions over the covariate distribution induced by that treatment: \(0.4(0.2)+0.6(0.5)=0.08+0.30=0.38\). Thus the predicted mean under this regime in the fixed baseline stratum is \(0.38\).

For comparison, suppose \(P(L_2=1\mid A_1=0)=0.2\), and under \(A_2=0\) the corresponding outcome means are \(0.1\) for \(L_2=0\) and \(0.3\) for \(L_2=1\). The prediction for the regime \(A_1=0,A_2=0\) is \(0.8(0.1)+0.2(0.3)=0.08+0.06=0.14\). The calculations differ both because the covariate distribution changes with the first treatment and because the outcome predictions differ. Using the \(L_2\) distribution from one first-treatment regime to predict the other would break the sequential history update.

Two Properties of a Correct Encoding

Result 1 (equivalent representations of a history): Let \(Z_t=\phi_t(H_t)\) be a one-to-one measurable encoding with a measurable inverse. Then conditioning on \(Z_t\) gives the same conditional distribution as conditioning on \(H_t\). In particular, for any integrable random variable \(X\), \(E[X\mid Z_t]=E[X\mid H_t]\) almost surely.

Proof. Since \(Z_t\) is a function of \(H_t\), every event determined by \(Z_t\) is determined by \(H_t\). Conversely, the measurable inverse makes \(H_t\) a function of \(Z_t\), so every event determined by \(H_t\) is determined by \(Z_t\). Their generated sigma-fields are equal: \(\sigma(Z_t)=\sigma(H_t)\). Conditional expectation is defined, up to almost-sure equality, by the conditioning sigma-field. Equal sigma-fields therefore give \(E[X\mid Z_t]=E[X\mid H_t]\). The same argument applies to conditional distributions. \(\square\)

A merely convenient summary need not have this property. For example, replacing two distinct prior treatments by their sum loses which treatment occurred at which time. Such a summary may still be useful under additional modeling assumptions, but it is not automatically an equivalent encoding of the ordered history.

Result 2 (no future measurement in a pre-treatment predictor): Under the stated ordering, a valid predictor for the treatment mechanism at time \(t\) is measurable with respect to \(\sigma(H_t)\). A function that uses \(L_{t+1}\) is generally measurable with respect to a larger information set, not the pre-treatment history. It represents the same treatment mechanism only if its dependence on \(L_{t+1}\) has no effect on the conditional probability given \(H_t\).

Justification. By definition, \(H_t\) contains exactly the recorded baseline and covariate information through \(L_t\), together with treatments through \(A_{t-1}\). Thus a function of \(H_t\) is available before \(A_t\). The next measurement \(L_{t+1}\) is recorded after \(A_t\) in the specified sequence, so it is not a function of \(H_t\) in general. A regression using \(L_{t+1}\) conditions on information outside the pre-treatment history. If, exceptionally, the fitted conditional probability does not actually vary with \(L_{t+1}\) once \(H_t\) is fixed, it may reduce to a function of \(H_t\); absent that condition, it is a different conditional model. \(\square\)

Worked Example: Detecting Future-Information Leakage

Consider two records with the same \(W=0\) and \(L_1=1\): person A has \(A_1=0,L_2=0\), and person B has \(A_1=1,L_2=1\). The valid first-decision history for both is \(H_1=(0,1)\). A regression for \(A_1\) that includes \(L_2\) can distinguish these two records using a measurement taken after the decision it is intended to predict.

The correction is to construct the first-decision predictors from \(W,L_1\) only. At decision two, \(L_2\) is available, so the history for \(A_2\) includes \(W,L_1,A_1,L_2\). The fact that \(L_2\) is a valid predictor at time two does not make it a valid predictor at time one. This is a timing error, not something fixed merely by fitting a more flexible regression.

Practical Checks, Alternatives, and Limits

Before fitting a nuisance regression or generating intervention predictions, check each decision time against the data-collection schedule. Diagnostics for encoding errors are often structural rather than statistical:

  • Print the predictor names for every \(t\) and verify that no variable measured after \(A_t\) appears.
  • Check that prior treatments are present in later histories when they are part of the observed information.
  • Verify that time labels have a single meaning, and that a row for decision \(t\) uses \(L_t\), not a value shifted from another visit.
  • Compare a few constructed histories against the original participant records by hand, including records with missing visits or irregular timing.
  • When making intervention predictions, verify that each updated history contains the intervention’s earlier treatment and the covariate value predicted or observed at the next step.

Wide data are convenient for checking one participant’s complete sequence; long data are convenient for fitting a separate decision-time regression. Neither format is intrinsically safer. A manually shortened summary is another option, but it changes the information supplied to the regression unless it is an equivalent encoding or is justified by substantive assumptions. No encoding alone resolves incorrect measurement timing, unrecorded confounding, or lack of support for histories required by an intervention. It also cannot determine how missing covariates should be handled: missingness and dropout must be represented according to the study’s observation process rather than silently treated as ordinary history values.

A common mistake is to treat every column in a participant’s row as eligible for every time-specific regression. That reasoning confuses “observed somewhere in the study” with “available before this decision.” The correction is to build \(H_t\) separately at each time point from the ordered protocol, and then use that history consistently in the relevant nuisance regression and intervention prediction.

Check Your Understanding

Answer each question using the ordering \(W,L_1,A_1,L_2,A_2,Y\).

  1. Write \(H_1\) and \(H_2\).
  2. Can \(L_2\) be a predictor in a regression for \(A_1\)? Explain.
  3. If \(Z_2=(W,L_1+A_1,L_2)\), is \(Z_2\) necessarily an equivalent encoding of \(H_2\)?
  4. Under a regime setting \(A_1=1\), what covariate distribution should be used to average predictions after the first decision?

Solutions. 1. \(H_1=(W,L_1)\) and \(H_2=(W,L_1,A_1,L_2)\). 2. No. \(L_2\) is measured after \(A_1\), so it is not part of the pre-treatment history \(H_1\). 3. No, not necessarily: the sum \(L_1+A_1\) can combine distinct values of \(L_1\) and \(A_1\), so the encoding may not recover the ordered history. 4. Use the distribution of the next covariate \(L_2\) conditional on the history and the assigned treatment \(A_1=1\), then average the subsequent predictions over that distribution.

Clinical Biostats