Written and maintained by CASRAI Editorial Board
Last updated
G-computation — also called the parametric g-formula — is a method for estimating the causal effect of a treatment strategy sustained over time, when at least one confounder is measured repeatedly and is itself affected by earlier treatment. Rather than reweighting the observed sample the way inverse-probability-weighted marginal structural models (MSMs) do, g-computation fits a model for the outcome (and for the time-varying confounder itself), then simulates each subject forward under a hypothetical treatment history — “what would this person’s confounder and outcome trajectory have looked like if they had always been treated?” — and averages the simulated outcomes across the sample. It targets the same estimand as an IPTW-weighted MSM and handles the same structural problem, through a completely different mechanism: standardization by simulation rather than reweighting.
The problem: a confounder that treatment itself creates
Ordinary regression adjustment assumes a confounder was fixed before treatment was assigned. Longitudinal studies with repeated treatment decisions routinely break that assumption: a covariate measured partway through follow-up can predict the next treatment decision (an ordinary confounding role) while also having been changed by the treatment already given (a mediating role on the causal pathway). CASRAI’s marginal structural models guide covers this structure — treatment-confounder feedback, named by Robins, Hernán, and Brumback in their 2000 paper introducing MSMs — in full, including why conditioning on the confounder in an ordinary regression blocks part of the true effect, and why dropping it leaves the later treatment decision confounded. G-computation exists to solve exactly that same bind, and predates the MSM/IPTW solution: James Robins introduced the parametric g-formula in 1986, in work on controlling for the healthy worker survivor effect in occupational cohort studies, more than a decade before the 2000 MSM paper formalized the reweighting alternative.
The mechanism: standardization by forward simulation
Where IPTW removes the confounder’s influence by building a weight from treatment-assignment (propensity) models, g-computation removes it by modeling the confounder and outcome processes directly and simulating what would have happened under a treatment strategy that never actually occurred in the data:
- Fit a model for each time-varying confounder, as a function of the treatment and covariate history up to that point — in a two-timepoint setup, a model for the confounder measured between the two treatment decisions, given baseline covariates and the first treatment.
- Fit a model for the outcome, as a function of the full treatment and covariate history, including the time-varying confounder.
- Simulate forward under a fixed treatment regime. For each subject (starting from their own baseline covariates), set treatment to the regime being evaluated (e.g., “always treat”), draw the time-varying confounder from the fitted confounder model, then draw the outcome from the fitted outcome model given that simulated confounder and the regime’s treatment values.
- Repeat for the comparison regime (e.g., “never treat”) using the same subjects’ baseline covariates, so both simulated populations are standardized over the same baseline distribution.
- Average and difference. The mean simulated outcome under each regime, differenced, is the g-computation estimate of the total causal effect of that treatment strategy.
Nothing in this sequence involves a treatment-assignment (propensity) model at all — g-computation never asks “how likely was this person to be treated.” IPTW never asks the reverse question — it never models the confounder or outcome process. That asymmetry is the whole basis for choosing between them, and for the doubly robust estimators (e.g., augmented IPTW, targeted maximum likelihood estimation) built specifically to hedge between the two: g-computation needs the confounder and outcome models correctly specified; IPTW needs the treatment (propensity) models correctly specified. A doubly robust estimator combines both machineries so the estimate stays consistent if either side is right, not only if both are.
Worked example: two different mechanisms, the same answer
This section uses a simulated dataset, not real patient or institutional data. It is an independent, deterministic, fixed-seed computer simulation built specifically for this page — with the same treatment-confounder-feedback structure used to illustrate the MSM guide’s own worked example, generated fresh with its own seed and parameters, not copied from it — so every number below is exactly reproducible by rerunning the same generating code. It illustrates the mechanism; it is not an estimate of any real treatment effect.
The setup: a baseline covariate L0 influences the first treatment decision A0. A time-varying confounder L1 is caused by A0 (and L0) — the treatment-confounder feedback — and L1 in turn predicts the second treatment decision A1 and the outcome Y directly. Treatment lowers the outcome directly at each round, but also raises the time-varying confounder, which itself raises the outcome — a partial offsetting pathway, the same structure that makes naive adjustment misleading. The true total effect of a sustained “treat at both rounds” strategy versus “treat at neither” is known exactly because it was set by the simulation, and independently confirmed via a separate 2,000,000-subject Monte Carlo comparison of the two counterfactual regimes simulated directly from the generating process (N=300,000 for the fitted-sample results below).
| Estimator | Mechanism | Estimated total effect | Bias vs. the known true effect |
|---|---|---|---|
| True causal effect (by construction, confirmed via Monte Carlo) | — | −1.239 | — |
Standard regression, adjusting for L1 as an ordinary covariate |
Conditions on the confounder | −2.507 | −1.268 (overstates the true effect by roughly 102%) |
| G-computation (simulated forward from fitted confounder + outcome models) | Standardization by simulation | −1.247 | −0.009 (within about 1% of the true effect) |
| IPTW-weighted marginal structural model, same dataset | Reweighting via propensity models | −1.240 | −0.001 (within simulation noise) |
Both g-computation and the IPTW-weighted MSM recover the true effect closely, despite using no overlapping machinery: g-computation never fits a treatment model, and the IPTW fit never fits an outcome or confounder model. The naive regression’s own coefficients on A0 and A1 are individually close to the true per-round direct effects — the bias is not in the coefficients themselves, but in treating their sum as the total effect of a sustained treatment strategy, which silently ignores the indirect pathway running through the confounder that both g-computation’s simulation step and IPTW’s reweighting step correctly restore. The IPTW fit on this dataset produced stabilized weights averaging 1.00 and ranging roughly 0.16 to 26.4 — a wider spread than a gentler example would show, consistent with the point made in the linked IPTW guide that weight extremity depends on how strongly the confounder predicts the next treatment decision, and is worth checking regardless of which estimator a given analysis ultimately reports.
What g-computation still assumes
G-computation removes the treatment-confounder feedback problem, not the underlying causal assumptions every confounding-adjustment method needs: consistency (the treatment version received matches the one specified in the simulated regime), positivity (every treatment-and-covariate history observed has a nonzero chance of continuing under either treatment option — the same requirement that produces IPTW’s extreme-weight problem shows up here as extrapolation risk, since the simulation has to predict outcomes for confounder values a purely deterministic regime may push outside the range actually observed), and sequential exchangeability (no unmeasured confounder at any time point). It also adds a requirement IPTW does not have in the same form: the confounder and outcome models’ functional forms have to be specified correctly, not just the treatment models — which is precisely the tradeoff the doubly robust framing above is about.
None of this machinery is needed for a single treatment decision measured once. If treatment doesn’t change over the course of the study, a single-timepoint approach — IPTW or propensity score matching — is the simpler, right tool; reach for g-computation (or an IPTW-weighted MSM) specifically when treatment is repeated and at least one covariate sits between two rounds of it.
Reporting standards for a g-computation analysis
- State the exact treatment regimes being compared (e.g., “always treat” versus “never treat,” or a more realistic dynamic strategy) — g-computation can simulate regimes an MSM cannot easily target, so the estimand needs to be as explicit as the models.
- Report the specification of every confounder model and the outcome model, including which covariates and interactions each includes.
- Report the number of Monte Carlo simulation draws used and confirm the estimate is stable across a change in simulation seed — a small number of draws adds simulation noise on top of sampling uncertainty.
- Report how confidence intervals were obtained (the nonparametric bootstrap, refitting all models within each bootstrap sample, is the standard approach — the standard error of the point estimate alone does not capture the models’ own uncertainty).
- State explicitly whether positivity held across the simulated covariate range, not only within the range actually observed.
Frequently asked questions
Is g-computation the same thing as standardization?
Yes — g-computation is standardization carried out by simulation rather than by a closed-form formula. The “g” in g-formula and g-computation refers to Robins’ general (hence “g”) framework for identifying causal effects under time-varying treatment and confounding, of which standardization, IPTW, and g-estimation of structural nested models are all members.
Is g-computation the same as doubly robust estimation?
No. G-computation relies on the confounder and outcome models being correctly specified; it has no built-in protection if they aren’t. Doubly robust estimators (augmented IPTW, targeted maximum likelihood estimation) combine g-computation’s outcome-modeling machinery with IPTW’s treatment-modeling machinery specifically so the estimate stays consistent even if one of the two model sets is wrong — g-computation is a building block those methods use, not a doubly robust method on its own.
Should I use g-computation or an IPTW-weighted MSM?
Both target the same estimand and, correctly specified, converge on the same answer, as the worked example above shows. The practical choice usually comes down to which set of models an analyst trusts more for a given dataset: g-computation if the outcome and confounder processes are easier to model credibly than the treatment-assignment process, IPTW if the reverse is true — and a doubly robust estimator when there’s no strong reason to prefer one over the other.
Do I need g-computation 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. The framework scales to any number of time points; two is simply the minimum needed to demonstrate the mechanism.
What software fits a g-computation model?
There’s no single required tool — the confounder and outcome models are ordinary regressions any standard statistical package fits, and the simulation step is a loop over subjects and Monte Carlo draws. Purpose-built packages exist to automate the bookkeeping specifically (for example, R’s gfoRmula package), but the underlying computation is the same fit-then-simulate logic regardless of tool.
Related CASRAI resources
- Marginal Structural Models: Solving Time-Varying Confounding with IPTW — the reweighting alternative this guide compares against directly.
- Inverse Probability Weighting: When It Beats Propensity Score Matching — the single-timepoint weighting mechanics IPTW-weighted MSMs build on.
- Propensity Score Matching: How It Works and What It Cannot Fix — the matching alternative for a single treatment decision.
- Endogeneity: The Three Sources, and the Remedy That Matches Each — the broader family of problems standard adjustment can’t solve on its own.
- Confounding Variable — the baseline definition this guide extends to the time-varying, simulation-based case.
- Research Methods & Statistics — the full cluster hub for quantitative and qualitative analysis methods.








