cleverly.longitudinal.ParametricGFormula#

class cleverly.longitudinal.ParametricGFormula(regimens: Any, *, n_draws: int = 10, n_derivative_draws: int | None = None, covariate_types: Mapping[Any, str] | None = None, covariate_learner: Any | None = None, outcome_learner: Any | None = None, hazard_learner: Any | None = None, pseudo_learner: Any | None = None, **kwargs: Any)[source]#

The parametric g-formula of a treatment regimen over time, by forward simulation.

The estimator fits a conditional model for every time-varying covariate at nodes 2..T, for the outcome (or the discrete hazards of a survival outcome), and for the natural treatment where a plan reads it. It then simulates each unit’s history forward under each regimen and averages the simulated outcomes (Robins 1986; Taubman et al. 2009; McGrath et al. 2020). The estimate is consistent only when every fitted law is correctly specified, and the draw order within a node defines non-nested models. See cleverly.longitudinal.gformula.

With every model a GLM, the interval stacks the estimating equations of the models and the estimate (Stefanski and Boos 2002, Section 2). The simulation error is part of that interval, and result.simulation reports its share of the variance. Any other learner gives the "adaptive_baseline_plugin" status: the point estimate stands, the interval is withheld, and a diagnostic curve stays under plugin_std_error.

Parameters:
regimensmapping, Regimen, DynamicRegimen, or sequence of regimens

The regimens, as for LTMLE. Static, dynamic, known stochastic and modified treatment policies are accepted, and so is an odds tilt. A tilt, and a modified treatment policy at a node t >= 2, draw the natural treatment from a fitted model, or from the declared mechanism (treatment_probabilities=) of a node that has one. A fixed number at a continuous treatment node is a fixed dose, and a known rule (rule_kind="known") there sets the dose from the simulated history. Either reads the model of the dose and reports a support diagnostic.

n_drawsint

The simulated histories per unit behind the estimate.

n_derivative_drawsint or None

The simulated histories per unit behind the parameter derivatives, at least two. None takes the larger of n_draws and ten.

covariate_typesmapping or None

The type of each time-varying column at nodes 2..T, as "binary", "categorical" or "normal". A tuple of one-hot columns is one categorical variable. A column with values in {0, 1} is binary without a declaration, and any other undeclared column is refused.

covariate_learnerobject or None

The template of every covariate model. The default is a main-terms GLM. A Gaussian covariate needs a GLM.

outcome_learnerobject or None

The outcome model of an end-of-study outcome. The default is a main-terms GLM of the outcome’s family.

hazard_learnerobject or None

The template of every hazard model of a survival outcome. The default is a GLM.

pseudo_learnerobject or None

Refused when given: the estimator fits no sequential pseudo-outcome regression.

**kwargsAny

The settings of SequentialGComputation that apply: reference, horizons, treatment_learner (the natural-treatment models and the diagnostic mechanism), censoring_learner, competing_learner, g_bounds, q_bounds, alpha_sig, simultaneous, n_multiplier, multiplier_kind, band_scale, band_reference, random_state, run_id, n_jobs, n_bootstrap, bootstrap_resampling, density_bins and ratio. n_folds must be 1. msm, alpha, max_iter and tol are refused.

See also

SequentialGComputation

The iterated conditional expectation form of the same parameter.

LTMLE

The targeted estimator of the same parameter.

gformula

The one-call form of this estimator.

Notes

A repeated outcome is refused; LTMLE, SDR and IPSI fit one. The simulation draws come from a counter-based Philox generator keyed by random_state, so a fit repeats exactly.

A declared treatment mechanism at a tilt node, or at a modified-treatment-policy node t >= 2, replaces the natural-treatment model. The simulation reads the declared probabilities of each unit as that unit’s natural law at every simulated history, so the draw is the declared law only when the declaration does not depend on a covariate that the simulation redraws. That node has no learner and no score term. A unit with no declared value at such a node is refused.

A fit with a fixed dose or a rule-set dose reports no mechanism diagnostics for any regimen of the fit, because a dose has no density ratio. Fit the other regimens in a separate call to read their diagnostics.

Examples

>>> from cleverly.datasets import make_longitudinal
>>> from cleverly.longitudinal import ParametricGFormula
>>> frame, _ = make_longitudinal(n=300, seed=0)
>>> result = ParametricGFormula(
...     {"always": 1, "never": 0}, covariate_types={"L2": "normal"}, random_state=0
... ).fit(
...     frame,
...     outcome="Y",
...     treatment=["A1", "A2"],
...     baseline=["W1", "W2"],
...     time_varying=[[], ["L2"]],
...     censoring=["C1", "C2"],
... )
>>> sorted(result.estimates)
['ate_regimen[never vs always]', 'ey_regimen[always]', 'ey_regimen[never]']
>>> result.inference_status
'influence_curve'

Methods#

Attributes#