gfoRmula: Parametric G-Formula

Victoria Lin, Sean McGrath, Zilu Zhang, Roger Logan, Lucia C. Petito, Jing Li, Jessica G. Young, Miguel A. HernánOpenalex Unknown 2019 · OpenAlex

TL;DR

The gfoRmula R package provides a robust implementation of the parametric g-formula, a powerful tool for causal inference in longitudinal studies. It allows researchers to estimate counterfactual risks and means under complex, time-varying treatment strategies, effectively handling the "g-null paradox" and time-varying confounding that standard regression methods cannot.

What Problem Does This Paper Address?

In longitudinal studies, researchers often face the challenge of time-varying confounding. This occurs when a variable (like blood pressure) influences future treatment decisions but is also influenced by past treatments. Standard statistical methods, such as Cox proportional hazards models or standard logistic regression, produce biased results in these scenarios because they cannot simultaneously treat a variable as both a confounder and an intermediate step on the causal pathway.

The g-formula, originally proposed by James Robins in 1986, is a "G-method" designed specifically to handle this complexity. However, implementing it from scratch is computationally intensive and technically demanding. gfoRmula addresses this gap by providing a standardized, flexible, and user-friendly interface for applying the parametric g-formula to real-world data.

Methods

The package implements the Non-Iterative Conditional Expectation (NICE) algorithm. This approach involves a multi-step simulation process:

  1. Modeling: The user specifies parametric models (e.g., GLMs) for each time-varying covariate and the outcome of interest based on the observed data.
  2. Simulation: The package simulates a large "counterfactual" population. In this simulation, the treatment variables are set according to a user-defined hypothetical strategy (intervention), while other covariates are predicted based on the models fit in step one.
  3. Estimation: The counterfactual mean or risk is calculated from this simulated population.

Key Features: * Flexible Interventions: Supports static interventions (e.g., "always treat"), threshold-based interventions (e.g., "treat if a lab value exceeds X"), and joint interventions on multiple treatments. * Outcome Diversity: Handles failure time (survival) outcomes, binary outcomes at the end of follow-up, and continuous outcomes. * Complex Data Realities: Includes built-in logic for handling competing events, censoring (loss to follow-up), and random measurement/visit processes via a "carry forward" mechanism. * Inference: Uses bootstrapping to provide standard errors and 95% confidence intervals for the estimated causal effects.

Datasets

The package includes several synthetic datasets designed to demonstrate its capabilities across different study designs:

  • basicdata & basicdata_nocomp: Example datasets for survival outcomes with and without censoring.
  • binary_eofdata: Designed for binary outcomes measured only at the end of a fixed follow-up period.
  • continuous_eofdata & continuous_eofdata_pb: Datasets for continuous outcomes at the end of follow-up, including versions with pre-baseline data.
  • censor_data: A larger dataset (118,725 observations) specifically for testing censoring indicators.

Key Insights and Ideas

  • The "Carry Forward" Logic: One of the most practical additions is the carry_forward function. In real-world clinical data, patients don't visit the doctor at every possible time point. This package allows researchers to model the visit process and carry forward covariate values when data is not recorded, mimicking actual clinical decision-making.
  • Handling Competing Risks: For survival analysis, the package offers two distinct ways to handle competing events, allowing researchers to choose the approach that best fits their causal question (e.g., treating the competing event as a censoring event vs. a distinct failure type).
  • Customizability: While it provides standard model types (binary, normal, truncated normal, etc.), it allows users to define custom covariate types and fit functions, making it adaptable to highly specific data distributions.

Key Results

As a software documentation manual, the "results" are the successful implementation of these complex algorithms. The package enables:

  • Risk Estimation: Calculation of risk, risk differences, and risk ratios over time for survival outcomes.
  • Mean Estimation: Calculation of mean outcomes, mean differences, and mean ratios for end-of-follow-up data.
  • Visualization: Built-in plotting functions (plot.gformula_survival, etc.) that automatically compare simulated "natural course" values against observed data to help users assess model fit and misspecification.

Limitations and Future Work

  • Parametric Assumptions: The accuracy of the g-formula estimates relies heavily on the correct specification of the underlying parametric models. If the models for the covariates or the outcome are wrong, the causal estimates will be biased.
  • Computational Intensity: Because the package relies on simulation and bootstrapping for inference, it can be computationally demanding for very large datasets or complex model structures, though it does support parallel processing (ncores argument).
  • Exchangeability: Like all causal inference methods, it assumes "no unmeasured confounding" (exchangeability). The package can only adjust for the confounders provided in the dataset.
  • Future Directions: While not explicitly detailed as "future work" in the manual, the ongoing development (version 1.1.1) suggests continued refinement of custom model support and performance optimizations for high-dimensional longitudinal data.

Summary updated

Abstract

Implements the non-iterative conditional expectation (NICE) algorithm of the g-formula algorithm (Robins (1986) &lt;<a href="https://doi.org/10.1016%2F0270-0255%2886%2990088-6" target="_top">doi:10.1016/0270-0255(86)90088-6</a>&gt;, Hernán and Robins (2024, ISBN:9781420076165)). The g-formula can estimate an outcome's counterfactual mean or risk under hypothetical treatment strategies (interventions) when there is sufficient information on time-varying treatments and confounders. This package can be used for discrete or continuous time-varying treatments and for failure time outcomes or continuous/binary end of follow-up outcomes. The package can handle a random measurement/visit process and a priori knowledge of the data structure, as well as censoring (e.g., by loss to follow-up) and two options for handling competing events for failure time outcomes. Interventions can be flexibly specified, both as interventions on a single treatment or as joint interventions on multiple treatments. See McGrath et al. (2020) &lt;<a href="https://doi.org/10.1016%2Fj.patter.2020.100008" target="_top">doi:10.1016/j.patter.2020.100008</a>&gt; for a guide on how to use the package.
BibTeX
@inproceedings{lin2019gformula,
  author = {Victoria Lin and Sean McGrath and Zilu Zhang and Roger Logan and Lucia C. Petito and Jing Li and Jessica G. Young and Miguel A. Hernán and Jessica G. Young and Miguel A. Hernán},
  title = {{gfoRmula: Parametric G-Formula}},
  booktitle = {Openalex Unknown},
  year = {2019},
  doi = {10.32614/cran.package.gformula},
}

Loading related content...

Figure