Marginal Structural Models

Python · from scratch IPTW  ·  R · ipw::ipwtm  ·  simulated longitudinal study, known counterfactuals

When the Confounder Is Also the Mechanism

Every adjustment strategy in this arc has assumed treatment happens once. In practice it usually does not: a drug is prescribed repeatedly, a benefit is renewed each period, a policy is reapplied. And when treatment is repeated, the confounders stop holding still.

The structure that breaks everything is treatment–confounder feedback. A clinical marker LtL_t determines whether treatment is given at time tt, so it confounds. But past treatment changed LtL_t, so it is also a mediator of the treatment's own benefit. It is simultaneously a variable you must adjust for and a variable you must not.

On a simulation where the truth is known, treatment is strongly protective — counterfactual survival of 0.841 under always-treat against 0.450 under never-treat, a gap of +0.390 — and it works largely by improving the marker. So the marker carries the benefit and creates the confounding at once.

Both standard analyses fail, in opposite directions, which is what makes this more than a technicality:

analysiscoefficient on cumulative treatmentwhat went wrong
naive death ~ cumA−0.232confounded — understates the benefit
adjusted death ~ cumA + L−0.065blocks the A → L → Y pathway
MSM, IPTW-weighted−0.342reweighted, not conditioned

Negative is protective. True counterfactual survival: 0.841 always-treat against 0.450 never-treat.

The naive regression is confounded and understates the benefit. The adjusted regression — the thing every methods course teaches — is worse, and biased toward the null, because conditioning on the marker blocks the ALYA \rightarrow L \rightarrow Y pathway through which the treatment does most of its work. “Adjust for confounders” is not merely insufficient here; applied literally it removes most of the effect. There is no specification of that regression that fixes it, because the variable's two roles cannot be separated by including or excluding it.

Reweighting Instead of Conditioning

Marginal structural models resolve it by changing the operation from conditioning to reweighting. Each subject gets an inverse-probability-of-treatment weight at every time point, building a pseudo-population in which treatment at each period is unconfounded by the marker — while the ALYA \rightarrow L \rightarrow Y pathway is left completely intact, because nothing was conditioned on. The structural model is then fit in that pseudo-population.

The weighted coefficient is −0.342, recovering the strong protective effect that the naive analysis understated and the adjusted analysis nearly erased, with a mean stabilized weight of 0.95 — well-behaved, which matters because IPTW's failure mode is a handful of enormous weights carrying the estimate.

Be exact about what it recovers, though. Translated back to counterfactual survival, the MSM gives a gap of +0.31 against a true +0.39 — it captures 79% and leaves 0.08 on the table. That shortfall is not confounding; the weights removed that. It is the structural model's own approximation error: the model here is a pooled logistic in cumulative treatment and time, and the counterfactual curve it implies is only as good as that functional form.

Which is the caveat worth carrying out of the g-methods literature. Inverse-probability weighting buys you an unconfounded pseudo-population. It does not excuse you from specifying the model you then fit inside it, and a misspecified structural model biases the answer with no diagnostic that separates it from the real thing. The naive and adjusted estimators here are wrong for reasons no sample size fixes; the MSM is approximately right for a reason more careful modelling could improve.

Where this sits

This is the dynamic generalisation of the collider and mediator problem: the marker is a confounder and a mediator at once, so the graphical rules that told you which variables to adjust for now give contradictory instructions and reweighting is the way out. The weights are the same construction as propensity weighting and as the IPTW in causal survival analysis, applied at every time point rather than once. And the failure of “adjust for everything” is the same lesson the survey-data page found when adjusting for BMI deleted most of an exercise effect — here compounded over time.

Notebooks

References