Skip to main content
v2026.11,610 entries · CC-BY 4.0

Marginal Structural Models: Solving Time-Varying Confounding with IPTW

A time-varying confounder that is also affected by prior treatment breaks standard regression adjustment. Marginal structural models, fit via inverse probability of treatment weighting (IPTW), solve it — with a worked, reproducible numeric example.

Ask about Marginal Structural Models: Solving Time-Varying Confounding with IPTW

Answers are drawn from this guide and the rest of the CASRAI corpus, with a link to every source.

Answers are AI-generated from CASRAI’s own published pages and can be wrong, so check the linked sources before relying on one; your question is logged without personal data — never sold, never used to train a third-party model — to show us what CASRAI is missing, so please do not type personal or confidential details. How we use this

Written and maintained by CASRAI Editorial Board

Last updated

A marginal structural model (MSM) is a causal-inference tool for a specific, common trap in longitudinal data: a covariate that is measured repeatedly over time, predicts who gets treated next, predicts the outcome — and is itself changed by the treatment given earlier. Ordinary regression adjustment, the tool researchers reach for by default to handle confounding, breaks down on exactly this variable. Inverse probability of treatment weighting (IPTW) is the mechanism most commonly used to fit an MSM. This guide explains why the standard approach fails, how IPTW-weighted MSMs fix it, and works through the arithmetic on a simulated example so the bias is visible in numbers, not just asserted.

The problem: a confounder that treatment itself creates

Confounding in a single cross-sectional comparison has a well-worn fix: measure the confounder, adjust for it (via regression, matching, or propensity-score weighting), and the treatment-outcome association that’s left over is the causal effect. That logic depends on one assumption holding — the confounder was fixed before treatment was assigned, so adjusting for it can only remove bias, not causal signal.

Longitudinal studies with repeated treatment decisions routinely violate that assumption. The canonical example, from the epidemiologic literature on HIV treatment, is CD4 count in patients on antiretroviral therapy: a low CD4 count predicts that a clinician will start or continue treatment (so it confounds the treatment-outcome relationship, the ordinary role of a confounder) and predicts mortality directly (so it looks like exactly the kind of variable you’re supposed to adjust for) — but CD4 count itself is also raised by earlier antiretroviral treatment. The same variable is simultaneously a confounder of the next treatment decision and a mediator on the causal pathway from earlier treatment to the outcome. Robins, Hernán, and Brumback formalized this structure and named it treatment-confounder feedback in their foundational 2000 paper introducing marginal structural models to a broad epidemiology audience (Epidemiology, 2000), with a companion paper by Hernán, Brumback, and Robins applying the method to exactly this zidovudine/CD4/survival problem the same year.

Outside clinical research, the same structure shows up anywhere treatment or exposure is repeated and a covariate sits between two rounds of it: a caseworker’s assessment score that both responds to prior intervention and predicts the next one, a firm’s credit rating that both reflects a past policy intervention and predicts the next round of eligibility, a student’s test score that responds to an earlier tutoring assignment and predicts whether tutoring continues.

Why standard covariate adjustment fails here

Once a covariate plays this dual role, there is no single choice that avoids bias in an ordinary regression or matching framework:

  • Adjust for it, and you block part of the true effect. Including the time-varying confounder as an ordinary covariate in a regression of the outcome on treatment history conditions on a variable that sits on the causal pathway from earlier treatment to the outcome. Part of treatment’s real effect — the part that runs through changing the confounder — gets absorbed into the confounder’s own coefficient instead of counted as an effect of treatment. The regression no longer estimates the total effect of the treatment strategy; it estimates something closer to the effect holding the mediator artificially fixed, which is not the causal question a treatment-strategy comparison is usually asking.
  • Leave it out, and the later treatment decision stays confounded. Drop the time-varying variable from the model and the second round of treatment is now confounded by exactly the covariate you needed to control for — you’re back to comparing treated and untreated patients who differ systematically on their disease severity at that point in time.

This is not a minor estimation nuance; it is a structural property of the data. No amount of covariate selection inside a standard regression framework resolves it, because the same variable needs to be controlled for the second treatment decision and left alone for the outcome. That is the specific problem MSMs, fit via IPTW, are built to solve.

How IPTW-weighted MSMs break the loop

The fix is to stop conditioning on the time-varying confounder in the outcome model at all, and instead use it only to build a weight. At each time point, fit a model for the probability of the treatment a person actually received, given their treatment and covariate history up to that point (the denominator model). Weight each observation by the inverse of that predicted probability, and — standard practice, and what “stabilized” refers to — multiply by a numerator model that predicts the same treatment using treatment history alone, deliberately excluding the time-varying confounder. Multiplying the per-period weights together across all time points gives each person a single cumulative weight.

Reweighting the sample this way creates a pseudo-population in which, by construction, treatment at each time point is no longer statistically associated with the time-varying confounder — the confounder’s influence on who got treated has been removed by the weight, rather than by conditioning on it in the outcome model. In that pseudo-population, a simple regression of the outcome on treatment history alone — with no confounder term — is unbiased for the total causal effect. That outcome-stage regression is the “marginal” structural model: marginal because it doesn’t condition on the confounder (the model is marginal over it, not conditional on it); structural because its coefficients are interpreted as parameters of the underlying causal (counterfactual) process, not just an associational fit.

The single-timepoint mechanics of weight construction — stabilization, the extreme-weight problem, and the diagnostics that tell you whether the weighted estimate can be trusted — are exactly the same mechanics used for a single treatment decision; see Inverse Probability Weighting: When It Beats Propensity Score Matching for that mechanical detail. What’s different for an MSM is only that the process repeats at every time point, and that the numerator/denominator split matters even more, because it’s the numerator model’s deliberate omission of the time-varying confounder that keeps the weighting step itself from re-introducing the mediator-conditioning problem it exists to avoid.

Worked example: quantifying the bias

This section uses a simulated dataset, not real patient or institutional data. It is a deterministic, fixed-seed computer simulation built specifically for this page to make the size and direction of the bias concrete — every number below was computed by generating synthetic data from a known process and fitting both estimators to it, and is exactly reproducible by rerunning the same generating code. It illustrates the mechanism; it is not an estimate of any real treatment effect.

The simulated setup mirrors the CD4/treatment structure above with two decision points. A baseline covariate L0 influences the first treatment decision A0. A time-varying covariate L1 is generated so that it is caused by the prior treatment A0 (the treatment-confounder feedback), and L1 in turn predicts the second treatment decision A1 and the outcome Y directly. By construction, being treated at a given time point reduces the outcome by a fixed amount, and the true total causal effect of a full two-round treatment strategy versus no treatment at either round is known exactly, because it was set by the simulation and independently confirmed by simulating both counterfactual regimes directly (a 2,000,000-subject Monte Carlo comparison of “treat at both rounds” against “treat at neither”).

Estimator Estimated total effect (both rounds) Bias vs. the known true effect
True causal effect (by construction, confirmed via simulation) −2.740
Standard regression, adjusting for L1 as an ordinary covariate −4.007 −1.267 (overstates the treatment benefit by roughly 46%)
IPTW-weighted marginal structural model (no L1 term in the outcome model) −2.750 −0.010 (within simulation noise)

The direction of the bias here is intuitive once the mechanism is visible: treatment lowers the outcome directly, but treatment also raises the time-varying confounder, which itself raises the outcome — a partial offsetting pathway. The standard regression conditions on the confounder and, in doing so, strips out that offsetting pathway along with the confounding it was meant to remove, so it reports a treatment effect nearly 50% larger in magnitude than the true total effect. The IPTW-weighted model, which never puts the confounder in the outcome regression, recovers the true total effect to within Monte Carlo noise (300,000 simulated subjects, fixed-seed pseudo-random generator, denominator and numerator propensity models fit by logistic regression on the simulated data rather than assumed from the generating parameters). The simulated weights themselves stayed well-behaved for this example — mean 1.00, ranging roughly 0.27 to 5.74 — illustrating that even a “clean” IPTW fit still needs the weight-distribution check the linked IPTW guide describes; real data with sparser treatment histories routinely produces far more extreme tails than this constructed example does.

Building IPTW weights for a marginal structural model, step by step

  1. Lay out the treatment and confounder history. For each time point, identify what was measured before that period’s treatment decision — this determines what belongs in that period’s denominator model.
  2. Fit the denominator models. For each time point, model the probability of the treatment actually received, conditional on the full treatment and covariate history up to that point, including the time-varying confounder.
  3. Fit the numerator models for stabilization. Model the same treatment, but conditional only on treatment history (and, if relevant, baseline covariates measured before any treatment) — deliberately excluding the time-varying confounder. This is what keeps the weighted outcome model unconfounded once the confounder itself is dropped from it.
  4. Multiply the per-period weights. Each subject’s cumulative stabilized weight is the product of their period-by-period numerator/denominator ratios across every time point up to the outcome.
  5. Fit the marginal outcome model. Regress the outcome on treatment history alone — no time-varying confounder term — weighted by the cumulative stabilized weight. For a survival outcome this is typically a weighted (pooled) logistic or Cox model rather than linear regression, but the weighting logic is identical.
  6. Check the weights before trusting the estimate. Extreme weights signal positivity violations — treatment histories that are nearly deterministic given the covariate history — and can dominate the weighted estimate the same way they can in a single-timepoint IPTW fit; see the diagnostics section of the linked IPTW guide rather than skipping this step because the model “ran.”

What an MSM still assumes — and when you don’t need one

Fitting the weights correctly does not remove the need for the same causal assumptions any confounding-adjustment method requires: consistency (the treatment version received matches the one being compared), positivity (every treatment history observed has a nonzero probability of either treatment value, given the covariate history — the same requirement that produces the extreme-weight problem when it’s nearly violated), and sequential exchangeability (no unmeasured confounder at any time point). An MSM does not solve unmeasured confounding; it solves a specific, structural problem — that a measured, correctly-identified confounder is also a mediator — that ordinary adjustment cannot solve regardless of how completely the confounders were measured.

None of this machinery is needed for a single treatment decision measured once. If treatment doesn’t change over the course of the study, or no covariate measured after baseline both predicts later treatment and is affected by earlier treatment, a single-timepoint approach — IPTW or propensity score matching — is the right, simpler tool; reach for a marginal structural model specifically when treatment is repeated and at least one covariate sits between two rounds of it.

Reporting standards for a marginal structural model

  • State explicitly which covariates were treated as time-varying versus fixed at baseline, and at which time point each was measured relative to the treatment decisions.
  • Report both the denominator and numerator model specifications for every time point’s weight, not just the final combined weight.
  • Report the weight distribution (mean, and the extremes — not just the mean) and whether any truncation or trimming was applied.
  • State the causal estimand being targeted (e.g., the effect of a sustained treatment strategy versus no treatment) as explicitly as the treatment history itself, since “the effect of treatment” is ambiguous once treatment can change over time.

Frequently asked questions

Is a marginal structural model the same thing as IPTW?

No — they’re closely linked but distinct. IPTW is the weighting mechanism; the marginal structural model is the outcome-stage model (typically a simple regression of outcome on treatment history) that the weights make unbiased to fit. IPTW can also be used to fit models other than MSMs, and MSMs can in principle be fit with methods other than IPTW, such as the parametric g-formula.

Is g-computation an alternative to IPTW for this problem?

Yes. The parametric g-formula (also from Robins’ work) models the time-varying confounder and outcome directly and simulates forward under different treatment strategies, rather than reweighting. It targets the same estimand and handles treatment-confounder feedback for the same structural reason IPTW does; the practical trade-off is that g-computation requires correctly specifying models for the confounders themselves, while IPTW requires correctly specifying the treatment (propensity) models — analysts often prefer whichever set of models they trust more for a given dataset, and doubly robust estimators exist specifically to hedge between the two.

Do I need an MSM if I only have two time points?

Yes, if a covariate measured between the two treatment decisions is both affected by the first and predictive of the second and the outcome — that’s exactly the structure the worked example above uses, and two time points is enough for the standard-adjustment bias to appear. The framework scales to any number of time points; two is simply the minimum needed to demonstrate it.

What software fits an MSM?

There’s no single required tool — an MSM is fit in two ordinary steps any standard statistical package can do: a logistic (or similar) regression for each period’s denominator and numerator weight models, then a weighted regression (linear, logistic, or Cox depending on the outcome) for the marginal outcome model, using the product of the per-period weights as the observation weight. Purpose-built packages exist to automate the weight-construction step specifically (for example, R’s ipw package), but the underlying computation is the same two-stage logic regardless of tool.

Related CASRAI resources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Regulatory Radar

Stop finding out after the fact

$29/month, cancel anytime. Daily digest updates from our analysis, a dashboard holding the same items, and a cited assistant for everything they raise.

  • Federal Register, Federal Register+, Grants.gov, Regulations.gov, NSF News, UKRI, plus CASRAI’s own published content.
  • 44,322 indexed passages, and every answer cites the ones it drew on.