Skip to content

chc.gmethods

Stability tier: evolving.

Robins' g-methods for time-varying treatment under time-varying confounding.

When a confounder L_t is itself affected by past treatment (A_{t-1} -> L_t -> Y and L_t -> A_t), ordinary regression adjustment is biased both ways: conditioning on L_t blocks the A_{t-1} -> L_t -> Y path (the earlier treatment's total effect is lost) and opens collider bias. The g-formula instead standardises over the confounder's post-treatment distribution. This implements the iterated-conditional-expectation (sequential-regression) g-computation estimator (Robins 1986; Bang & Robins 2005) of a treatment regime a = (a_0, ..., a_T) on the final outcome, with K-fold cross-fitting of the nuisance regressions (the Double-ML honesty).

A statistical estimator, NumPy float64 throughout (like chc.did / chc.scm) -- x64-flag independent. Continuous or binary treatments; ridge nuisances.

Only the columns a call names are read, each as float64 where it holds real numbers: a boolean, an integer or a floating dtype, or bool, int, float, Decimal or Fraction values. A column of text, dates, times, durations, complex numbers or other objects is refused, and so is a number finite in its own type that is an infinity in float64, naming the column and the row; a float64 cast read the text "0.5" as 0.5 and a date as its count of days. A nan or an infinity is read as it is, and the effect reads nan.

sequential_g_formula(data, *, treatments, confounders, outcome, regime, baseline, ridge=0.001, folds=2, seed=0)

Effect E[Y^regime] - E[Y^baseline] of a treatment regime under time-varying confounding.

treatments is time-ordered and confounders[t] names the covariates measured before treatment t (same length as treatments). regime / baseline set each treatment's value under the two interventions. Iterated conditional expectation: regress the running pseudo-outcome on the history through time t, set A_t to the regime value, then recurse to t = 0, standardising over each confounder's realised post-treatment distribution rather than conditioning on it. The nuisance regressions are cross-fitted over folds folds, each row predicted by the fit on the others, so there are at least two and at most one a row. ridge is their Tikhonov term, on each coefficient but the intercept's in units of its column's variance, so the effect reads the same in any units and from any origin of the treatments and the confounders, the regime and the baseline taken in the treatments' own.

A treatment may be a confounder of a later one, measured before it; one set at its own step and also among the confounders measured before that step would be held at its logged value where it is set.

Raises:

Type Description
ValueError

when the treatments, confounders, regime and baseline differ in length, folds is not a whole number from 2 to the rows, the outcome is named among the treatments or the confounders, a treatment is named twice, or a treatment is among the confounders of its own step or an earlier one; or when a column it reads does not hold real numbers, or is a masked array that masks a cell, naming the column and the row.

naive_pooled_effect(data, *, treatments, confounders, outcome)

The biased baseline: one pooled regression of outcome on all treatments and confounders, summing the treatment coefficients. Wrong under time-varying confounding -- it conditions on the post-treatment confounders that the g-formula standardises over.

Raises:

Type Description
ValueError

when the outcome is named among the treatments or the confounders, a treatment is named twice, or a treatment is among the confounders: pooled, its coefficient would be split with its own copy; or when a column it reads does not hold real numbers, or is a masked array that masks a cell, naming the column and the row.