Tutorials › Marginal Structural Models › Debugging Time Indices and Treatment Histories

Longitudinal Targeted Maximum Likelihood · Tutorial 402 of 501

Debugging Time Indices and Treatment Histories

Learn to align covariates, treatments, and outcomes in a longitudinal TMLE, and to diagnose implementations that condition on future data or apply interventions at the wrong time.

Advanced 13 min read

What You'll Learn

  • Define a pre-treatment history at every decision time and distinguish it from the history generated under an intervention.
  • Trace which covariates are available for each treatment and outcome regression.
  • Check the longitudinal TMLE recursion against the actual data-collection order.
  • Detect future-information leakage and off-by-one errors in treatment regimes.
  • Use consistency, sequential exchangeability, and positivity to justify the intervention-world recursion.

Why Time Indices Need an Audit

A longitudinal TMLE can use appropriate nuisance estimators and still target the wrong quantity if its time indices are misaligned. A covariate measured after a treatment cannot serve as information available before that treatment; an intervention intended for the second decision can accidentally be applied at the first. These errors can produce plausible-looking estimates, so the implementation must be checked against the data-collection timeline, not just against the names of its variables.

The audit begins by stating precisely when each covariate is observed, when each treatment is assigned, and when the outcome is measured. It then checks that the histories in the sequential regressions, the imposed treatment values, and the intervention-world conditional means all refer to those same times.

A Timing Convention for the Audit

Suppose there are treatment decisions \(A_0,\ldots,A_K\), with baseline covariate \(L_0\). At decision time \(t\), \(L_t\) has been measured but \(A_t\) has not yet been assigned. For \(t<K\), \(L_{t+1}\) is measured after \(A_t\) and before \(A_{t+1}\). The outcome \(Y\) is measured after the final decision \(A_K\). This is one explicit timing convention; an implementation should document its own convention just as explicitly.

Definition: The pre-treatment history at decision time \(t\) is \(H_t=(L_0,A_0,L_1,A_1,\ldots,A_{t-1},L_t)\), with \(H_0=L_0\). It contains only information available immediately before \(A_t\). For \(t<K\), write \(H_{t+1}=(H_t,A_t,L_{t+1})\); after the final treatment, write \(H_{K+1}=(H_K,A_K)\). Under an intervention \(g=(g_0,\ldots,g_K)\), \(H_t^g\) denotes the history generated under that intervention, not the factual history \(H_t\).

A regime specifies the treatment to be applied at each decision: \(A_t=g_t(H_t)\). A fixed regime is a special case in which \(g_t\) is constant. When checking code, write down the regime as an indexed sequence, such as \((g_0,g_1)=(0,1)\), before interpreting any variable called `treatment_1` or `treatment_2`. Those names do not by themselves establish whether the first column means \(A_0\) or \(A_1\).

The key distinction is between a factual history and an intervention-world history. The recursion below describes the conditional mean of \(Y^g\) given \(H_t^g\). Its observed-data regressions are fitted using records with the relevant treatment set to the regime value. At later times, the history being considered is the one that would have arisen under the earlier regime assignments; it need not equal the history observed for a person whose earlier treatments differed.

The Sequential Regression Recursion

For a deterministic regime \(g\), define the observed-data regressions recursively. At the terminal time, set \(q_{K+1}(h_{K+1})=E[Y\mid H_{K+1}=h_{K+1}]\). Then, for \(t=K,\ldots,0\), define

$$ q_t(h_t)=E\!\left[q_{t+1}(H_{t+1})\mid H_t=h_t,\ A_t=g_t(h_t)\right]. $$

For \(t=K\), \(H_{K+1}\) includes \(A_K\) but no \(L_{K+1}\); thus this last regression step is simply the conditional mean of \(Y\) at the regime-assigned final treatment. Earlier steps average over the next covariate distribution among records with the treatment set to the regime value. The recursion is a statement about conditional expectations on the relevant support; in continuous histories, these are understood as regular conditional expectations.

Theorem: Suppose consistency holds, sequential exchangeability holds at every decision time, and positivity holds for the regime at histories generated under that regime. Assume \(E[|Y|]<\infty\) and that the conditional expectations in the recursion exist. Then, for histories \(h_t\) in the support of \(H_t^g\), \(q_t(h_t)=E[Y^g\mid H_t^g=h_t]\). In particular, \(E[Y^g]=E[q_0(L_0)]\).

Here, consistency means that a person's observed treatment and covariate sequence agrees with the corresponding potential sequence when the observed treatments follow it, and the observed outcome then equals the corresponding potential outcome. Sequential exchangeability means that, conditional on \(H_t\), treatment assignment at time \(t\) is independent of the potential covariate and outcome sequence under the regime. Positivity means that, at each relevant history, the conditional probability of receiving the regime-assigned treatment is positive. These conditions are needed for the observed conditional regressions to identify their intervention-world counterparts.

To justify the theorem, use backward induction. At the final time, conditional on \(H_K^g=h_K\), the regime sets \(A_K=g_K(h_K)\). Sequential exchangeability permits the conditional distribution of the potential outcome at that treatment to be represented by the observed outcome distribution among people with \(H_K=h_K\) and \(A_K=g_K(h_K)\). Consistency equates the observed outcome in that group to the corresponding potential outcome. Therefore \(q_K(h_K)=E[Y^g\mid H_K^g=h_K]\).

Now suppose the equality has been established at time \(t+1\). Under \(g\), given \(H_t^g=h_t\), the next treatment is \(g_t(h_t)\), and for \(t<K\) the next history also includes \(L_{t+1}^g\). Sequential exchangeability and consistency identify the conditional distribution of that next covariate with the observed distribution of \(L_{t+1}\) given \(H_t=h_t,A_t=g_t(h_t)\). Averaging the time-\(t+1\) conditional mean over this distribution gives

$$ E[Y^g\mid H_t^g=h_t] =E\!\left[q_{t+1}(H_{t+1})\mid H_t=h_t,\ A_t=g_t(h_t)\right] =q_t(h_t). $$

The induction yields the claimed identity. Taking expectations over the baseline distribution, which is unchanged by the intervention, gives \(E[Y^g]=E[q_0(L_0)]\). Positivity ensures that the observed conditional regressions used in the argument are defined on the intervention-relevant histories. \(\square\)

Three Practical Debugging Checks

1
Write the measurement order.
For every \(t\), record whether \(L_t\) is measured before or after \(A_t\). Do not infer this from a data-frame column order.
2
Write the history at each fit.
For a treatment or outcome regression, list the variables available at that decision or prediction time. Check each one against the measurement order.
3
Trace the regime through the recursion.
Confirm that \(g_t\) is applied to \(A_t\), then identify which next covariate distribution is averaged over before proceeding to \(t+1\).

Worked Example: A Post-Treatment Covariate Is Not Baseline Information

There are two decisions, \(A_0\) and \(A_1\), with timing \(L_0,A_0,L_1,A_1,Y\). Suppose \(L_1=L_0+2A_0+U\) and \(Y=L_1+5A_1+1\), where \(E[U\mid L_0,A_0]=0\). Consider regime \(g=(1,1)\) and a person with \(L_0=3\).

Under the regime, the conditional mean of \(L_1^g\) given \(L_0=3\) is \(3+2(1)+0=5\). At the second decision, the correct history includes this post-\(A_0\) covariate: \(H_1^g=(L_0,A_0^g,L_1^g)\). The time-1 regression under the regime is \(q_1(H_1)=E[Y\mid H_1,A_1=1]=L_1+5+1=L_1+6\).

At time 0, average that regression over the \(L_1\) distribution under \(A_0=1\): \(q_0(3)=E[L_1+6\mid L_0=3,A_0=1]=5+6=11\). Thus the regime-specific conditional mean at baseline is 11. A realized person with \(U=2\), however, has \(L_1=7\) and \(Y=7+5+1=13\). The conditional mean 11 and this individual's outcome 13 are different quantities, as they should be.

A model for \(A_0\) that conditions on \(L_1\) uses a covariate measured after \(A_0\). That is future-information leakage, even though \(L_1\) is a valid part of the history for the later \(A_1\) decision.

Worked Example: Detecting a Shifted Treatment Regime

Let \(L_0=0\), \(L_1=2A_0\), and \(Y=10+3A_0+5A_1+2L_1\). Suppose the intended regime is \(g=(0,1)\): no treatment at time 0 and treatment at time 1. The intended path has \(L_1=2(0)=0\), so its outcome is \(Y=10+3(0)+5(1)+2(0)=15\).

If code mistakenly applies the first regime value to \(A_1\) and the second to \(A_0\), it implements \((A_0,A_1)=(1,0)\). That path has \(L_1=2(1)=2\) and \(Y=10+3(1)+5(0)+2(2)=17\). The discrepancy is not a small numerical fluctuation: the code has evaluated a different intervention.

A direct audit is to print the values assigned to \((A_0,A_1)\) and recompute the induced covariate path and outcome by hand. Checking only that the regime contains one zero and one one would not detect the swap.

Worked Example: Separating Factual and Intervention Histories

Use the first example's model and consider a factual record with \(L_0=3,A_0=0,L_1=3,U=0,A_1=0,Y=4\). Under regime \(g=(1,1)\), the person would instead have \(A_0^g=1\), and with the same \(U=0\) would have \(L_1^g=3+2=5\), followed by \(A_1^g=1\) and \(Y^g=5+5+1=11\).

The factual second-decision history is \((L_0,A_0,L_1)=(3,0,3)\); the intervention-world history is \((L_0,A_0^g,L_1^g)=(3,1,5)\). They are not interchangeable. The identifying regression \(q_1(h_1)=E[Y\mid H_1=h_1,A_1=1]\) is evaluated at histories in the support of \(H_1^g\), not asserted to be \(E[Y^g\mid H_1=h_1]\) when \(h_1\) is treated as a factual history. The intervention-world statement is \(q_1(h_1)=E[Y^g\mid H_1^g=h_1]\) on the relevant support.

This distinction matters in code that predicts at later times: the history used for regime-specific predictions must reflect the earlier regime assignments and the covariate evolution following them, rather than simply carrying forward every factual treatment value.

Diagnostics, Alternatives, and Limitations

A time-index audit should be part of the data and model pipeline. A useful diagnostic is a compact table with one row per time point and columns for “measured before treatment,” “treatment assigned,” “measured after treatment,” and “outcome.” Compare that table with the variables passed to every nuisance fit and with the order in which the recursion is evaluated. Also inspect the generated regime paths: a treatment value intended for \(A_t\) should appear at that time and nowhere else by accidental indexing.

Several warning signs deserve investigation: a treatment model for \(A_t\) includes \(L_{t+1}\); a later regression uses an earlier treatment value after the regime has overwritten it; a covariate is carried forward from the factual record even though earlier intervention assignments would alter it; or changing a regime component \(g_t\) changes the simulated path at a different time. These can be caught with assertions on variable timestamps, small hand-calculated test cases, and path-level output for a few records. Correct timing does not guarantee adequate positivity or correct nuisance fits, but incorrect timing invalidates the intended interpretation regardless.

Longitudinal inverse-probability weighting and sequential regression use different computational representations of the same intervention problem. A weighting implementation makes the treatment probabilities and their time order especially visible; the sequential regression recursion makes the order of conditional outcome means and covariate transitions visible. Comparing their time-index maps can reveal a mismatch, but agreement between estimates does not prove that both implementations target the intended regime. Neither approach repairs a history containing future information or an intervention assigned to the wrong time.

This audit concerns indexing and treatment histories, not the construction of the clever covariate used in targeting. It also cannot establish the substantive assumptions of consistency, exchangeability, or positivity from code alone. Those assumptions require study-design knowledge and data diagnostics. A perfectly aligned implementation may still have weak support for a regime, misspecified nuisance fits, or measurement times that are not known precisely.

Common Mistake

Common mistake: “The regression conditions on \(H_t\), so its conditional mean is automatically \(E[Y^g\mid H_t=h_t]\).” This treats the observed history as if it were the intervention-generated history. The correct identified target is \(E[Y^g\mid H_t^g=h_t]\), for \(h_t\) in the support of \(H_t^g\). The observed regression identifies it through consistency, sequential exchangeability, positivity, and the regime-specific treatment restriction in the recursion.

A related mistake is to assume that a variable is safe to use at every time merely because it appears in the data set. Availability is time-specific: \(L_{t+1}\) can be part of the history for \(A_{t+1}\), but it cannot be treated as pre-treatment information for \(A_t\).

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. With timing \(L_0,A_0,L_1,A_1,Y\), can \(L_1\) be included in a model for \(A_0\)? Why?
  2. For the two-time regime \((g_0,g_1)=(0,1)\), which treatment is set to 1?
  3. State the correct intervention-world history for the time-1 conditional mean.
  4. In the model \(L_1=2A_0\), \(Y=10+3A_0+5A_1+2L_1\), calculate the outcome under regime \((0,1)\).

Solutions. 1. No. \(L_1\) is measured after \(A_0\), so it is future information for that decision, though it can be used before \(A_1\). 2. \(A_1\) is set to 1; \(A_0\) is set to 0. 3. It is \(H_1^g=(L_0,A_0^g,L_1^g)\), not the factual history by default. 4. \(L_1=2(0)=0\), and \(Y=10+3(0)+5(1)+2(0)=15\).

Clinical Biostats