Why the Confounder Must Be Averaged Over
In a longitudinal study, a covariate measured between treatment periods can be affected by earlier treatment and still influence both later treatment and the outcome. That creates a calculation problem: the covariate distribution under a treatment plan may differ from the distribution observed among people who happened to follow that plan. The g-formula addresses this by combining conditional outcome risks with the covariate distribution that would arise under the specified intervention.
This tutorial works through a deliberately simple setting: two binary treatments, one binary time-varying confounder measured between treatments, and a binary outcome. The target is the risk under a sustained regime that assigns the same treatment at both periods. The example has no baseline covariates, so the calculation can be shown as a short sum over the two possible confounder values.
Setup and the Binary G-Formula
Let \(A_1\) be treatment at the first period. Let \(L_2\) be a binary covariate measured after \(A_1\) and before \(A_2\), the treatment at the second period. Let \(Y\) be a binary outcome measured after the second treatment. The ordering is therefore \(A_1 \rightarrow L_2 \rightarrow A_2 \rightarrow Y\), with earlier variables also potentially affecting later ones. Here \(L_2\) is a time-varying confounder because it can be affected by \(A_1\) and can influence both \(A_2\) and \(Y\).
The sustained “treat at both periods” regime is \((1,1)\); the sustained “do not treat at either period” regime is \((0,0)\). The g-formula calculates either risk by averaging the relevant conditional outcome risk over the distribution of \(L_2\) that would arise under the first treatment in the regime.
The two factors have different roles. The outcome factor uses the treatment values specified by the regime and the covariate value \(l\). The second factor supplies the probability of that covariate value under the first treatment specified by the regime. It is this averaging step that incorporates how earlier treatment changes the distribution of the later confounder.
Identification Conditions and a Short Justification
The formula identifies a causal risk from observed conditional probabilities under standard longitudinal assumptions. In this simple setting, these conditions can be stated directly:
- Consistency: An individual who receives \(A_1=a_1\) and \(A_2=a_2\) has the outcome \(Y^{a_1,a_2}\), and the observed \(L_2\) after \(A_1=a_1\) agrees with the covariate value under that first treatment. Treatment versions must be sufficiently well defined for this correspondence to make sense.
- Sequential exchangeability: At the first treatment decision, treatment assignment is independent of the relevant future counterfactuals (unconditionally here, because there are no baseline covariates). At the second decision, conditional on \(A_1\) and \(L_2\), \(A_2\) is independent of the relevant future counterfactual outcome.
- Positivity: Each treatment value required by the regime occurs with positive probability at the relevant histories. For regime \((a_1,a_2)\), this requires \(P(A_1=a_1)>0\) and \(P(A_2=a_2\mid A_1=a_1,L_2=l)>0\) for each \(l\) with positive probability under \(A_1=a_1\).
A brief derivation shows why the covariate distribution belongs in the formula. Under regime \((a_1,a_2)\), first partition the risk according to the value of the intermediate covariate that would occur after setting the first treatment to \(a_1\):
Consistency and sequential exchangeability identify the covariate distribution in this expression with \(P(L_2=l\mid A_1=a_1)\). They identify the conditional counterfactual outcome risk, under the specified second treatment and covariate history, with \(P(Y=1\mid A_1=a_1,L_2=l,A_2=a_2)\). Substituting these observed-data quantities gives the binary g-formula. Positivity ensures the required conditional probabilities can be learned from the observed data rather than requiring unsupported extrapolation.
These assumptions are not established merely by performing the sum. In an observational analysis, exchangeability depends on having measured and appropriately conditioned on the relevant confounders. A numerical calculation can illustrate the formula, but it cannot verify that unmeasured confounding is absent.
Calculating Risk Under Sustained Treatment
Worked Example: Treat at Both Periods
Suppose a hypothetical study has no baseline covariates, and its design supports sequential exchangeability: first-period treatment is randomized, and second-period treatment is randomized within the observed \(A_1,L_2\) histories. Both treatment options have positive probability at each relevant history. The estimated probabilities are:
- \(P(L_2=1\mid A_1=1)=0.40\), so \(P(L_2=0\mid A_1=1)=1-0.40=0.60\).
- \(P(Y=1\mid A_1=1,L_2=0,A_2=1)=0.10\).
- \(P(Y=1\mid A_1=1,L_2=1,A_2=1)=0.30\).
We want the risk if everyone were assigned treatment at both periods, \((1,1)\). The g-formula requires the distribution of \(L_2\) under \(A_1=1\), not a single covariate value for everyone:
The estimated sustained-treatment risk is 0.18, or 18%. In this hypothetical population, the formula combines a 10% conditional risk for \(L_2=0\) with a 30% conditional risk for \(L_2=1\), weighted by the 60% and 40% covariate distribution expected under first-period treatment.
The assumptions are reflected in how the quantities are obtained. Randomization at the first decision supports exchangeability for \(A_1\); randomization within \(A_1,L_2\) histories supports exchangeability for \(A_2\). The stated positive assignment probabilities support positivity for the treatment path used here. Consistency still requires that the hypothetical treatment assignments correspond to the intervention of interest.
Working From Hypothetical Counts
The same calculation can begin with counts rather than probabilities. The relevant covariate distribution is estimated among those with the regime’s first treatment, while the conditional outcome risks come from the cells matching the specified treatment history and covariate value. The resulting quantities are then multiplied and summed.
Worked Example: Turning Counts Into the Treated-Regime Risk
Consider a second hypothetical data summary. Among 1,000 people with \(A_1=1\), 600 have \(L_2=0\) and 400 have \(L_2=1\). Among those with \(A_2=1\), the observed outcome counts are as follows:
| \(L_2\) | Number with \(A_2=1\) | Number with \(Y=1\) |
|---|---|---|
| 0 | 300 | 30 |
| 1 | 200 | 60 |
The covariate probabilities under \(A_1=1\) are \(600/1000=0.60\) for \(L_2=0\) and \(400/1000=0.40\) for \(L_2=1\). The conditional outcome risks in the \(A_1=1,A_2=1\) cells are \(30/300=0.10\) when \(L_2=0\) and \(60/200=0.30\) when \(L_2=1\). Substitution gives:
Thus the estimated risk is 18%. This calculation uses the covariate distribution among all individuals with \(A_1=1\), not the distribution of \(L_2\) only among those who subsequently received \(A_2=1\). The g-formula’s factors represent distinct parts of the calculation: the distribution generated by the first intervention and the outcome risks conditional on the full specified treatment history.
For a causal interpretation, the hypothetical design must also support sequential exchangeability and positivity. In particular, each \(L_2\) value with positive probability under \(A_1=1\) must have some individuals with \(A_2=1\) from whom its conditional outcome risk can be estimated. The nonzero cell counts shown here support that requirement in this example; in a real analysis, sparse cells would make the estimate unstable even if formal positivity held.
Calculating Risk Under Sustained No Treatment
The covariate distribution changes when the first treatment in the regime changes. Therefore, the untreated regime requires its own \(L_2\) distribution and its own conditional outcome risks. Reusing the \(L_2\) distribution from the treated regime would generally calculate the wrong quantity.
Worked Example: Do Not Treat at Either Period
In another hypothetical setting, suppose \(P(L_2=1\mid A_1=0)=0.25\), so \(P(L_2=0\mid A_1=0)=0.75\). Suppose the conditional risks under \(A_1=0,A_2=0\) are 0.08 for \(L_2=0\) and 0.20 for \(L_2=1\). Assume consistency, sequential exchangeability, and positivity hold for this regime.
For the sustained regime \((0,0)\), use the covariate distribution under \(A_1=0\):
The estimated risk under sustained no treatment is 0.11, or 11%. The larger conditional risk for \(L_2=1\) contributes one quarter of the total weighted risk because \(L_2=1\) has probability 0.25 under the first-period intervention \(A_1=0\). The result is a population risk under the specified regime, not the outcome risk for a person whose covariate value is known to be 0 or 1.
A Practical Calculation Sequence
For a binary covariate, the calculation has only two terms. Keeping the histories and weights explicit helps prevent mixing quantities from incompatible treatment paths.
Write the assigned treatment values \((a_1,a_2)\) and the target outcome risk \(P(Y^{a_1,a_2}=1)\).
Obtain \(P(L_2=0\mid A_1=a_1)\) and \(P(L_2=1\mid A_1=a_1)\), which must sum to 1.
For each \(l\), use \(P(Y=1\mid A_1=a_1,L_2=l,A_2=a_2)\).
Multiply each conditional risk by the corresponding covariate probability, then sum the two products.
The result must lie between 0 and 1 because it is a weighted average of conditional risks, with nonnegative weights summing to 1. This is a useful arithmetic check, but it does not establish that the assumptions needed for a causal interpretation are satisfied.
Common Mistake: Holding the Confounder Fixed
A tempting but incorrect shortcut is to calculate the outcome risk for one value of \(L_2\) and report it as the risk under the regime, or to average the two conditional risks with equal weights. Neither calculation generally targets the population risk. The regime changes treatment; it does not set \(L_2\) to a chosen value, and the two covariate values need not be equally common.
Check Your Understanding
Use the formula and assumptions above to answer these questions.
- For a regime \((1,0)\), which first-period covariate distribution and which second-period treatment value appear in the g-formula?
- If \(P(L_2=1\mid A_1=1)=0.30\), what is \(P(L_2=0\mid A_1=1)\)?
- Suppose the two conditional risks under a regime are 0.12 for \(L_2=0\) and 0.32 for \(L_2=1\), with respective covariate probabilities 0.70 and 0.30. Calculate the regime-specific risk.
- Why does positivity for a sustained regime require support for the second treatment at the relevant \(A_1,L_2\) histories?
- What is wrong with using the same distribution of \(L_2\) for both sustained regimes \((1,1)\) and \((0,0)\) when earlier treatment can affect \(L_2\)?