Why Implementation Details Change the Estimand
A parametric g-computation analysis can use correctly fitted models and still answer the wrong question if its implementation does not follow the intervention being evaluated. The most consequential errors often occur after model fitting: observed treatment values are inadvertently retained, post-treatment covariates are not regenerated, predictions are averaged over the wrong population, or an event is assigned to the wrong interval.
The central audit question is simple: for every simulated person and time point, does the computation represent the specified intervention and the history that would occur under it? The formulas below make that question precise. They are not a substitute for the identification assumptions or model diagnostics; they help ensure that the calculation implements the intended g-formula.
The Computation Being Implemented
Let \(L_0\) denote baseline covariates. At times \(t=0,\ldots,K-1\), let \(A_t\) be treatment and \(L_{t+1}\) the covariate history measured after that treatment and before the next treatment decision. Let \(Y\) be an outcome measured after the final treatment. Write \(a=(a_0,\ldots,a_{K-1})\) for a fixed treatment regime. The target is the mean outcome had everyone followed that regime, averaged over a specified baseline population.
Here \(\bar l_t=(l_0,\ldots,l_t)\) and \(\bar a_t=(a_0,\ldots,a_t)\). For discrete covariates, the integrals are sums. The formula presumes consistency, sequential exchangeability, and positivity for the histories and treatment levels needed by the regime. Its parametric implementation also requires adequate models for the covariate transitions and outcome regression. These assumptions concern identification and model validity; a coding check cannot establish them.
The computational order follows the factorization: draw or enumerate baseline covariates from \(F_0^*\), set treatment to the regime value, generate each next covariate from its transition model conditional on the generated history, and finally predict the outcome. The treatment values are set, not resampled from the observed treatment mechanism. The covariates are generated, not frozen at their observed values.
Worked Example: Observed Treatment Values Sneak Into a Regime
Suppose \(X\) is binary with \(P(X=1)=0.5\), treatment \(A\) is binary, and a fitted outcome model gives \(P(Y=1\mid A,X)=0.10+0.40A+0.20X\). Consider the intervention \(A=1\) for everyone. The predicted risks are \(0.50\) for \(X=0\) and \(0.70\) for \(X=1\), so the target-population risk is
Now suppose the implementation predicts using each person's observed \(A\), rather than replacing it with 1. If half the sample has \(A=1\), and \(A\) is independent of \(X\) in this example, the average prediction is \(0.10+0.40(0.5)+0.20(0.5)=0.40\). That is an average over the observed treatment mix, not the risk under \(A=1\). The discrepancy, \(0.60-0.40=0.20\), is not Monte Carlo error: it is a different calculation.
Result 1: Why Covariates Must Be Propagated Under the Regime
Derivation. Under consistency, the observed outcome and covariates equal their counterfactual counterparts for people whose observed treatment history follows \(a\). Sequential exchangeability allows the conditional distribution of each next covariate, given the history and the treatment at that time, to identify its counterfactual transition under that treatment. Positivity ensures these conditional distributions are supported by the observed data. Applying the chain rule to the counterfactual covariate history under the fixed regime therefore gives
The counterfactual mean is the expectation of the outcome conditional mean over this history distribution. Integrating that conditional mean against the displayed factorization gives the g-formula. In particular, the distribution of \(L_{t+1}\) depends on the treatment and covariate history leading to it. Holding \(L_{t+1}\) at its observed value instead averages the outcome regression over a different distribution, so it does not in general compute \(\psi(a)\). \(\square\)
Worked Example: Freezing a Post-Treatment Covariate
Consider two treatment decisions. Set \(A_0=1\) and \(A_1=0\). Suppose the binary post-\(A_0\) covariate \(L_1\) satisfies \(P(L_1=1\mid A_0=1)=0.80\) and \(P(L_1=1\mid A_0=0)=0.20\). Suppose the final outcome risk under \(A_1=0\) is \(0.10\) when \(L_1=0\) and \(0.50\) when \(L_1=1\).
Under the specified regime, \(L_1\) must be generated from its distribution under \(A_0=1\). Thus
If the code instead freezes \(L_1\) at a distribution from the \(A_0=0\) history, it obtains \((1-0.20)(0.10)+0.20(0.50)=0.08+0.10=0.18\). The error is not that the outcome model uses \(L_1\); it should. The error is that \(L_1\) was not propagated under the intervention. A related bug is to simulate \(L_1\) correctly but then use an earlier value, or a value from another simulated person, in the outcome prediction.
Baseline Averaging: Use the Intended Population
The baseline distribution is part of the estimand, not a harmless choice of averaging weights. It may be the empirical distribution in the analysis sample, a weighted distribution representing a target population, or a specified distribution for a subgroup. The outcome predictions must be averaged over that stated population. Changing the baseline weights changes the answer even when every conditional model and regime-specific prediction is unchanged.
Worked Example: Averaging Over the Analysis Sample Instead of the Target
Let \(X=1\) identify a high-risk baseline group. Under a treatment regime, suppose the model predicts risk \(0.20\) for \(X=0\) and \(0.80\) for \(X=1\). The analysis sample is 25% high-risk, but the target population is 60% high-risk.
The analysis-sample average is \(0.75(0.20)+0.25(0.80)=0.15+0.20=0.35\). The target-population average is \(0.40(0.20)+0.60(0.80)=0.08+0.48=0.56\).
Thus \(0.35\) is not a computational approximation to the target risk \(0.56\); it answers a different population question. A practical safeguard is to calculate and report the baseline group proportions used in the averaging step, and to verify that they match the intended target.
Event Timing and Risk Accumulation
In longitudinal event analyses, a time index must specify when treatment is assigned, when covariates are measured, and which interval an event hazard describes. Let \(D_{t+1}=1\) indicate an event during the interval from \(t\) to \(t+1\), and let \(R_t=1\) indicate survival and being event-free through time \(t\). Define the interval hazard \(h_{t+1}\) conditional on being at risk at time \(t\) and on the history available before that interval.
Derivation. By the definition of conditional probability, \(P(R_{t+1}=1\mid R_t=1,\text{history at }t)=1-h_{t+1}\). The event-free indicator at \(K\) requires being event-free at every preceding time. Applying the chain rule successively gives
and therefore \(P(D\text{ by }K\mid\text{history})=1-\prod_{t=0}^{K-1}(1-h_{t+1})\). With changing covariate histories, the hazards and histories must be generated and accumulated in the correct time order; the formula is applied along each simulated history before averaging across histories. \(\square\)
Worked Example: Assigning an Event to the Wrong Interval
Suppose a regime-specific event hazard is \(0.20\) in interval 1 and \(0.50\) in interval 2, conditional on being event-free at the start of each interval. The probability of no event in both intervals is \((1-0.20)(1-0.50)=0.80(0.50)=0.40\). Thus the two-interval cumulative risk is \(1-0.40=0.60\).
Adding the hazards gives \(0.20+0.50=0.70\), which is wrong because people who experience the first event cannot also experience a later first event. Another timing error would be to use a covariate measured after interval 1 to predict the event during interval 1. That covariate is not available at the start of the interval and may itself be observed only among survivors. The implementation should document, for each variable, whether it is measured before treatment, after treatment, or after the interval's event process.
Audit Checks, Alternatives, and Limitations
Several checks can expose implementation problems before interpreting the estimate:
- Treatment-setting check: In a deterministic regime, verify that every simulated \(A_t\) equals the specified \(a_t\), regardless of the person's observed treatment.
- History check: Confirm that each generated \(L_{t+1}\) is drawn or integrated from the transition model conditional on the generated past and the regime treatment. Compare simulated covariate distributions across regimes; they may differ when treatment affects covariates.
- Population check: Print the baseline weights or stratum proportions used in the final average. Check that they represent the declared target, not an unintended complete-case subset or a resampled distribution.
- Timing check: Write a time-line showing when each \(A_t\), \(L_t\), and \(D_{t+1}\) is defined. Verify that no prediction uses future information and that events remove individuals from later risk sets.
- Numerical check: For risks, verify that estimates lie in \([0,1]\), and compare analytic integration with Monte Carlo integration when both are feasible. Increasing simulation size should reduce Monte Carlo error, but cannot correct a wrong treatment assignment, transition, or target distribution.
Analytic integration and Monte Carlo integration are alternative ways to evaluate the same g-formula when they use the same models, regime, and target population. Inverse-probability weighting is a different g-method: it builds a weighted pseudo-population rather than integrating outcome predictions over generated covariate histories. Agreement across approaches can be reassuring, but disagreement may reflect different modeling assumptions, target populations, or implementation defects; it is not by itself proof that one approach is correct.
G-computation remains sensitive to misspecified transition and outcome models, and positivity problems can force extrapolation even when code is flawless. Diagnostics can reveal implausible predictions, unsupported histories, or implementation inconsistencies, but cannot establish untestable causal assumptions. Treating an audit as a guarantee of causal validity is itself a mistake.
Common Mistake: Treating Prediction as Intervention
Incorrect reasoning: “The fitted model includes treatment, so predicting each person's outcome from their observed row estimates the outcome if everyone received the intervention.” It does not: observed treatment values preserve the natural treatment mix. Likewise, retaining observed post-treatment covariates preserves histories generated under that mix.
Correction: For each regime, explicitly set treatment to its prescribed value, propagate covariates forward from the corresponding transition distributions, predict the outcome for those histories, and average over the intended baseline population. Each of these steps has a separate role, and a correct outcome regression alone cannot replace them.
Check Your Understanding
Answer each question, then compare with the solutions.
- A regime sets \(A=1\). The code uses \(A\) as recorded for each person when predicting outcomes. What is the implementation error?
- Under a regime, \(P(L_1=1)=0.75\). The outcome risks are \(0.20\) for \(L_1=0\) and \(0.60\) for \(L_1=1\). Compute the g-computation average over \(L_1\).
- The analysis sample is 40% in a subgroup with predicted risk \(0.70\); the rest have risk \(0.10\). The target population is 25% in that subgroup. Compute the target average and explain the error in using the analysis-sample proportion.
- Two consecutive interval hazards are \(0.10\) and \(0.30\). Compute the two-interval risk and explain why adding the hazards is incorrect.
- Name one diagnostic that can identify a timing error and one limitation that remains even after the implementation passes its checks.
Solutions. (1) The code averages predictions over observed treatment rather than setting \(A=1\), so it does not implement the regime. (2) The average is \(0.25(0.20)+0.75(0.60)=0.05+0.45=0.50\). (3) The target average is \(0.25(0.70)+0.75(0.10)=0.175+0.075=0.25\). Using the sample proportion gives \(0.40(0.70)+0.60(0.10)=0.28+0.06=0.34\), which targets the sample composition instead. (4) The risk is \(1-(1-0.10)(1-0.30)=1-(0.90)(0.70)=1-0.63=0.37\). Adding hazards double-counts people who already experienced the event in the first interval. (5) A time-line can check that covariates measured after an interval are not used to predict an event during it. Correct implementation does not eliminate bias from misspecified models, lack of positivity, or unverified causal assumptions.