Marginal Structural Models

A 3-Day Remote Seminar Taught by
Daniel Westreich, Ph.D.

To see a sample of the course materials, click here.

This seminar is an introduction to marginal structural models (MSMs) from both a theoretical and applied standpoint. Marginal structural models (typically fit with inverse probability weights) are increasingly used for causal inference in observational data.

Such approaches are essential to the analysis of longitudinal data with time-varying treatments and confounders. They can yield correct estimates even when traditional methods (such as a time-dependent Cox proportional hazards model) would produce a biased result. The class format will be a mix of didactic lecture, interactive “journal club”, and in-class programming exercises.

Starting March 18, we are offering this seminar as a 3-day synchronous*, remote workshop for the first time. Each day will consist of a 4-hour live lecture held via the free video-conferencing software Zoom. You are encouraged to join the lecture live, but will have the opportunity to view the recorded session later in the day if you are unable to attend at the scheduled time.

Each lecture session will conclude with a hands-on exercise reviewing the content covered, to be completed on your own. An additional lab session will be held Thursday and Friday afternoons, where you can review the exercise results with the instructor and ask any questions.

*We understand that scheduling is difficult during this unpredictable time. If you prefer, you may take all or part of the course asynchronously. The video recordings will be made available within 24 hours of each session and will be accessible for two weeks after the seminar, meaning that you will get all of the class content and discussions even if you cannot participate synchronously.


Marginal structural models are most usefully applied to longitudinal data when three conditions hold:

  1. treatments vary with time
  2. potential confounders vary with time
  3. confounders may also act as mediators.

In this situation, standard methods almost always yield biased results. 

Consider a simple causal scenario, illustrated by a directed acyclic graph (DAG):

The goal is to estimate the total effect of Treatment at times 0 and 1 on the Outcome. The variable Z is a mediator of the causal effect of Treatment0 on the Outcome (Treatment0 → Z → Outcome), but also a confounder of the causal effect of Treatment1 on the Outcome (Treatment1 ← Z → Outcome).

Because Z is a confounder, we must control for Z. If we do that using typical regression approaches, we will hold Z constant at each possible level of Z. In a DAG, this is illustrated by placing a box around Z, and that has the effect of removing all arrows out of Z (if Z is held constant, it cannot affect other variables). Here’s the new DAG:

Unfortunately, this control for Z has removed the effect of Treatment0 on the Outcome, because (again) Z was a mediator of that causal effect. Thus, regression control for Z does not allow us to assess the total effect of both Treatment0 and Treatment1 on the Outcome. But lack of regression control for Z would lead to a biased assessment of the effect of Treatment1 on the Outcome.

This “damned if you do, damned if you don’t” scenario is resolved if we can analytically remove the arrow from Z to Treatment1, as in the third DAG, below. In this DAG, we can assess both the effect of Treatment1 on the Outcome (which is now unconfounded by Z) while still assessing the effect of Treatment0 on the Outcome (since the pathway Treatment0 → Z → Outcome remains present).

Now the question is, how can we remove the Z → Treatment1 arrow? While there are multiple ways to approach this problem, marginal structural models based on inverse probability weights (which are closely related to propensity scores) are perhaps the most natural and intuitive, as well as the most widely known.

This seminar will start by reviewing the fundamental ideas of causal inference in observational studies, including potential outcomes, counterfactual thinking, causal identification conditions, DAGs, and alternative approaches to causal thinking. We will then introduce time-fixed marginal structural models, including introductions to inverse probability weights and the g-formula. We will review and discuss relevant literature and work through examples of these ideas in code.

After this we will introduce time-varying MSM with inverse probability weights, including discrete time hazard models and data structures essential to understanding those models. We’ll also work through both literature and code examples of these time-varying MSMs. Finally, we will discuss several advanced topics, including some basics of the time-varying parametric

Overall, this seminar will prepare you to both recognize situations in which MSM should be applied and provide you with worked examples of code to enable you to apply these methods in your own work. See the computing section below for more details.

Before coming to the seminar, you should read:

Petersen et al. (2006) Assessing the effectiveness of antiretroviral adherence interventions: Using marginal structural models to replicate the findings of randomized controlled trials.

Robins et al. (2000) Marginal structural models and causal inference in epidemiology.


The exercises and lab work for this seminar will primarily (but not exclusively) use SAS. However, equivalent R code with commentary will also be available.  To participate in the exercises, you should have access to a computer with SAS 9.4 or a recent version of R installed.

Who should Register? 

You should be familiar with regression modeling (including both coursework and application). Some familiarity with ideas in causal inference such as causal diagrams and potential outcomes would be helpful but is not required. If you wish to gain that familiarity, you could read Chapter 3 of the instructor’s textbook Epidemiology By Design or Part I of Causal Inference: What If? by Hernán and Robins (available free on the web). The class is appropriate for epidemiologists, biostatisticians, data scientists, and social scientists generally. However, it will primarily be taught through the lens and vocabulary of epidemiology.


Day 1: Causal inference and introduction to time-fixed MSM.

1. Causal inference in epidemiology

  • Counterfactuals/potential outcomes
  • Well-defined interventions
  • Causal identification conditions
  • DAGs & confounding

2. Introduction to time-fixed marginal structural models

  • Randomization
  • Inverse probability weights
  • The g-formula

3. Implementation of time-fixed MSM with the g-formula

Day 2: Time-fixed MSM and introduction to time-varying MSM

4. Time-fixed MSM theory
5. Implementation of time-fixed MSM with inverse probability weights
6. Introduction to time-varying MSM

  • DAG examples
  • Data structures
  • Discrete time hazard models
  • More on inverse probability weights

Day 3: Time-varying MSM and additional topics

7. Time-varying MSM theory
8. Implementation of time-varying MSM with inverse probability weights
9. Selected topics

  • Timescale
  • Time-varying parametric g-formula