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. Seecleverly.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, andresult.simulationreports 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 underplugin_std_error.- Parameters:
- regimens
mapping,Regimen,DynamicRegimen,orsequenceofregimens 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 nodet >= 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_draws
int The simulated histories per unit behind the estimate.
- n_derivative_draws
intorNone The simulated histories per unit behind the parameter derivatives, at least two.
Nonetakes the larger ofn_drawsand ten.- covariate_types
mappingorNone 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_learner
objectorNone The template of every covariate model. The default is a main-terms
GLM. A Gaussian covariate needs a GLM.- outcome_learner
objectorNone The outcome model of an end-of-study outcome. The default is a main-terms
GLMof the outcome’s family.- hazard_learner
objectorNone The template of every hazard model of a survival outcome. The default is a GLM.
- pseudo_learner
objectorNone Refused when given: the estimator fits no sequential pseudo-outcome regression.
- **kwargs
Any The settings of
SequentialGComputationthat 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_binsandratio.n_foldsmust be1.msm,alpha,max_iterandtolare refused.
- regimens
See also
SequentialGComputationThe iterated conditional expectation form of the same parameter.
LTMLEThe targeted estimator of the same parameter.
gformulaThe 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'