Tutorials › Marginal Structural Models › Coding Past Treatments in Sequential Models

Inverse Probability of Treatment Weights · Tutorial 88 of 501

Coding Past Treatments in Sequential Models

Learn to represent treatment history in later treatment-probability models and recognize when a coding choice cannot distinguish clinically relevant past treatment patterns.

Advanced 12 min read

What You'll Learn

  • Define the treatment history available as a predictor at each follow-up time
  • Code prior treatment using lagged indicators, history indicators, and selected summaries
  • Distinguish initiation probabilities from continuation probabilities
  • Represent prior switching patterns without confusing them with the current treatment outcome
  • Identify information lost when distinct histories share one summary code
  • Check history coding for unsupported patterns and implausible predicted probabilities

Why Prior Treatment Needs an Explicit Code

A sequential treatment model estimates the probability of the treatment assigned at a given time from information available before that assignment. When treatment can persist, stop, restart, or switch, a later treatment probability may depend on how the person arrived at the current point, not just on the most recent covariates. The model therefore needs predictors that represent relevant past assignments in a deliberate way.

Let \(A_t\in\{0,1\}\) denote treatment at time \(t\), for \(t=1,\ldots,T\). The prior treatment history at time \(t\) is \(H_t^A=(A_1,\ldots,A_{t-1})\); at \(t=1\), this history is empty. A treatment model may condition on \(H_t^A\) and the measured covariate history. This tutorial focuses on how to encode \(H_t^A\), not on how to construct covariate histories or choose the timing of covariate measurements.

Definition: A prior-treatment predictor is a variable computed from treatment assignments strictly before the treatment being modeled. Examples include the most recent assignment \(A_{t-1}\), a cumulative count \(\sum_{j=1}^{t-1}A_j\), an ever-treated indicator \(I(\sum_{j=1}^{t-1}A_j>0)\), or indicators for particular treatment histories. These codes are not interchangeable: each retains different information about the past.

In a pooled logistic model, for example, one might write

$$ \operatorname{logit}P(A_t=1\mid H_t^A,\bar L_t) =\alpha+\beta A_{t-1}+\gamma^\top g(H_t^A,\bar L_t), $$

where \(\bar L_t\) denotes the covariates included in the model and \(g\) represents other chosen predictors. At the first time point, \(A_{t-1}\) is not observed; it must not be silently set to a meaningful treatment value. One can instead model the first time separately, define the baseline treatment convention explicitly, or use a coding scheme that has a designated first-period value.

Common Ways to Encode Past Assignments

The simplest code is the lagged treatment \(A_{t-1}\). It distinguishes whether treatment was assigned at the immediately preceding time, but ignores earlier assignments once the lag is known. A cumulative count distinguishes the number of prior treated occasions, but generally ignores their order. An ever-treated indicator distinguishes people with any previous treatment from those never treated, but ignores both the number and timing of treated occasions.

A more detailed code uses indicators for particular histories. With binary treatment and \(t=3\), the prior histories are \(00,01,10,11\), where the first digit is \(A_1\) and the second is \(A_2\). Four history indicators can represent a different treatment probability for each pattern. This may be useful when history-specific differences matter, but the number of patterns grows as \(2^{t-1}\), so late-time histories may be sparse.

Worked Example: A Lagged Assignment and the Later Treatment Probability

Suppose a model for treatment at time 2 is \(\operatorname{logit}P(A_2=1\mid A_1)= -1+1.4A_1\). Calculate the probabilities and the odds ratio comparing \(A_1=1\) with \(A_1=0\).

For a person untreated at time 1, \(A_1=0\), so the linear predictor is \(-1+1.4(0)=-1\). Thus \(P(A_2=1\mid A_1=0)=\operatorname{expit}(-1)=1/(1+\exp(1))\approx0.2689\).

For a person treated at time 1, \(A_1=1\), the predictor is \(-1+1.4(1)=0.4\). Thus \(P(A_2=1\mid A_1=1)=\operatorname{expit}(0.4)\approx0.5987\).

The model odds are exactly \(\exp(-1)\approx0.3679\) and \(\exp(0.4)\approx1.4918\). Their ratio is \(\exp(0.4-(-1))=\exp(1.4)\approx4.0552\). This checks the coefficient interpretation: in this model, prior treatment multiplies the odds of treatment at time 2 by \(\exp(1.4)\). It does not multiply the probability by that amount.

Initiation and Continuation

For a binary treatment that can be initiated and continued, \(A_{t-1}\) has a direct transition interpretation. Among people with \(A_{t-1}=0\), \(A_t=1\) is initiation. Among people with \(A_{t-1}=1\), \(A_t=1\) is continuation. A model containing \(A_{t-1}\) can give these two groups different treatment probabilities. Whether this is a useful model depends on the application: initiation and continuation may have distinct determinants, and a single linear-logit effect of \(A_{t-1}\) imposes a particular relationship between them.

Worked Example: Interpreting Initiation and Continuation

Suppose a pooled model uses \(\operatorname{logit}P(A_t=1\mid A_{t-1})=-1.2+2A_{t-1}\), for \(t>1\). Interpret the two probabilities represented by this model.

If \(A_{t-1}=0\), the person was untreated at the previous occasion. The model gives the initiation probability \(\operatorname{expit}(-1.2)=1/(1+\exp(1.2))\approx0.2315\).

If \(A_{t-1}=1\), the person was treated at the previous occasion. The linear predictor is \(-1.2+2=0.8\), so the continuation probability is \(\operatorname{expit}(0.8)=1/(1+\exp(-0.8))\approx0.6900\).

The model therefore assigns a higher probability to remaining treated than to initiating treatment. The coefficient \(2\) is the difference in log odds for these two prior-treatment groups; the odds ratio is \(\exp(2)\approx7.389\). This pooled specification does not automatically imply that all other determinants act identically for initiation and continuation. That is a separate modeling restriction to assess in context.

If treatment is truly absorbing after initiation, then once \(A_{t-1}=1\), the next assignment is known to be 1 by the treatment definition. In that case a continuation probability model is not estimating an uncertain decision for those records; the appropriate risk set and treatment process must reflect the absorbing rule. Do not use an ordinary transition model that estimates positive probabilities of stopping when stopping is impossible by design.

Switching and the Information in History

When treatment can switch on and off, the lag \(A_{t-1}\) identifies the previous state, but not necessarily whether that state was reached by remaining in the same state or by switching. One possible additional predictor is the previous switch indicator \(S_{t-1}=I(A_{t-1}\ne A_{t-2})\), when both past assignments exist. This is a summary of prior treatment history, not the treatment outcome at time \(t\). Other choices include a count of prior switches or indicators for exact treatment sequences. The choice should follow the substantive process being modeled.

Worked Example: Distinguishing Switching Histories

At time 3, consider the model \(\operatorname{logit}P(A_3=1\mid A_1,A_2)=-1+1.1A_2+0.6S_2\), where \(S_2=I(A_2\ne A_1)\). Calculate the probability for each prior history.

For history \(00\), \(A_2=0\) and \(S_2=0\), so the predictor is \(-1\) and the probability is \(\operatorname{expit}(-1)\approx0.2689\).

For history \(01\), \(A_2=1\) and \(S_2=1\), so the predictor is \(-1+1.1+0.6=0.7\) and the probability is \(\operatorname{expit}(0.7)\approx0.6682\).

For history \(10\), \(A_2=0\) and \(S_2=1\), so the predictor is \(-1+0+0.6=-0.4\) and the probability is \(\operatorname{expit}(-0.4)\approx0.4013\).

For history \(11\), \(A_2=1\) and \(S_2=0\), so the predictor is \(-1+1.1=0.1\) and the probability is \(\operatorname{expit}(0.1)\approx0.5250\).

The model distinguishes \(01\) from \(11\), even though both have \(A_2=1\), because the former has a prior switch and the latter does not. It also distinguishes \(00\) from \(10\), both with \(A_2=0\). These probabilities are consequences of the stated model; they do not establish that a prior switch truly has the same effect regardless of the direction of the switch.

What Full History Coding Can and Cannot Guarantee

Theorem: At a fixed time \(t\), a logistic model with one indicator for each possible binary prior-treatment history can represent any collection of history-specific probabilities \(q_h\) strictly between 0 and 1. Specifically, if \(I(H_t^A=h)\) is included for every history \(h\), then setting its coefficient to \(\operatorname{logit}(q_h)\) yields \(P(A_t=1\mid H_t^A=h)=q_h\).

Proof. Write the model as \(\operatorname{logit}P(A_t=1\mid H_t^A)=\sum_h \eta_h I(H_t^A=h)\). For any observed history \(h^\ast\), exactly one history indicator equals 1: \(I(H_t^A=h^\ast)=1\), while all other indicators equal 0. The linear predictor is therefore \(\eta_{h^\ast}\). Set \(\eta_h=\operatorname{logit}(q_h)=\log(q_h/(1-q_h))\). Applying the inverse logit gives the fitted probability \(q_h\) for each history \(h\), as required. The result assumes each history can be represented in the model; it does not ensure that every history occurs in the sample or that its probability can be estimated precisely. \(\square\)

Full history indicators are flexible but costly in data: at time \(t\), there are \(2^{t-1}\) binary histories. If some histories are absent, their probabilities cannot be estimated from that time-specific data without additional restrictions. If histories are rare, estimates may be highly uncertain or produce near-zero or near-one predicted probabilities.

Result: A history summary that assigns the same code to two distinct histories cannot represent every possible history-specific treatment probability function.

Derivation. Suppose a summary \(C(H_t^A)\) gives the same value \(c\) for distinct histories \(h_1\) and \(h_2\). Any model whose treatment probability depends on history only through \(C\) must assign the same probability \(r(c)\) to both histories. But a valid history-specific probability function could assign \(q_{h_1}=0.2\) and \(q_{h_2}=0.8\). Since \(0.2\ne0.8\), no single value \(r(c)\) can equal both. Thus the summary cannot represent every possible function of the full history. It may still be appropriate if substantive knowledge or a modeling assumption justifies treating those histories as equivalent.

Choosing and Checking a Coding

There is no universally best history code. A compact code such as \(A_{t-1}\), ever treated, or cumulative treated occasions uses fewer parameters and may be more stable, but it makes distinct histories equivalent unless other predictors separate them. Exact-history indicators avoid that particular restriction but can be impractical when histories are numerous or sparse. Separate models by time can expose history-specific patterns; pooled models can share information across times but impose common features unless time variation is otherwise represented. These are alternative parameterizations, not automatic substitutes for a substantive decision about which past assignments matter.

Before fitting, make a small table of the observed histories and the values of each proposed code. Check whether histories that should have different implications collapse to the same value. After fitting, inspect the number of observations and treatment outcomes within relevant history groups, the predicted treatment probabilities by history, and warnings of separation or unstable estimates. Extreme predicted probabilities can lead to extreme inverse probability weights; they may signal sparse histories, a misspecified treatment model, or limited empirical support. A plausible coding does not by itself guarantee positivity or correct model specification.

A frequent mistake is to use only \(A_{t-1}\) and then claim that the model adjusts for the full treatment history. That conclusion is false whenever earlier assignments can matter after conditioning on the lag. For instance, at \(t=3\), histories \(01\) and \(11\) both have \(A_2=1\), but differ in \(A_1\). The correction is not to include every possible history automatically; it is to state the restriction implied by the chosen code, consider whether it is defensible, and check whether the data support a more detailed representation.

Key takeaway: Code past treatment to match the treatment process being modeled. A lag captures the prior state; a switch indicator captures a feature of the transition; a cumulative summary captures selected totals; and full-history indicators distinguish every sequence. Each choice makes a different set of histories equivalent.

Check Your Understanding

Answer each question, then compare with the solutions.

  1. At time 4, what does \(A_3\) distinguish, and what information about earlier assignments does it omit?
  2. For a treatment that can only be initiated once and never stopped, should the post-initiation rows be treated as uncertain continuation decisions? Explain.
  3. At time 3, the cumulative count code assigns the same value to histories \(10\) and \(01\). What assumption does a model using only that count make about their treatment probabilities?
  4. How many possible binary prior-treatment histories are there at time \(t=5\)?
  5. Why can a full-history indicator model be difficult to estimate even though it can represent any set of history-specific probabilities?

Solutions. (1) \(A_3\) distinguishes whether treatment was assigned at the immediately preceding occasion. By itself, it omits assignments at times 1 and 2. (2) No. If initiation is absorbing by definition, post-initiation treatment is determined rather than an uncertain continuation decision, so the modeled risk set must reflect that rule. (3) Both histories have one prior treated occasion, so a model depending only on the count assigns them the same probability; that is a restriction, not a mathematical consequence of the data. (4) There are \(2^{5-1}=16\) possible histories. (5) The number of history patterns grows exponentially with time, and some may be rare or absent; this creates imprecision or makes particular probabilities unidentifiable from the observed data.

Clinical Biostats