cleverly.longitudinal.StructuralNestedMean#

class cleverly.longitudinal.StructuralNestedMean(regimens: Any = None, *, reference: str | None = None, blip_features: Mapping[Any, Any] | None = None, outcome_learner: Any | None = None, treatment_learner: Any | None = None, regression_learner: Any | None = None, n_folds: int = 10, learner_folds: int = 5, positivity_margin: float = 0.01, min_eigenvalue: float = 1e-08, alpha_sig: float = 0.05, simultaneous: bool = True, n_multiplier: int = 10000, multiplier_kind: str = 'mammen', band_scale: str = 'logit', band_reference: str = 'normal', random_state: int | None = None, run_id: str | None = None, n_jobs: int = 1, msm: Any = None, pseudo_learner: Any = None, censoring_learner: Any = None, horizons: Any = None, **refused: Any)[source]#

G-estimation of a linear structural nested mean model over treatment nodes.

The estimator reports the blip coefficients of each node, the mean outcome with every treatment at its reference level, and, for declared static regimens, their means and contrasts. The coefficients are the parameters of a restricted model. A level or a contrast is the same estimand as the one LTMLE reports only when the model holds.

Parameters:
regimensmapping, sequence, or None

Static regimens, as for LTMLE. None reports the blips and the reference mean only. A rule, a modified treatment policy or an incremental policy is refused.

referencestr or None, default=None

The regimen every contrast is taken against. None takes the first.

blip_featuresmapping or None, default=None

The features \(\phi_t\) of each node. A key is a node counted from one or the treatment column. A value lists terms. A term is a column name, or a tuple of names for a product. A blip may read baseline columns, earlier treatments and the time-varying covariates through its own node. The intercept is always included. A feature that reads a time-varying covariate allows the blips and ey_reference only.

outcome_learnerlearner or None, default=None

The learner of \(q_t\). None fits a Super Learner.

treatment_learnerlearner or None, default=None

The learner of the treatment-side nuisances \(m_{j,t}\). A target in {0, 1} takes this learner as a classifier. None fits a Super Learner.

regression_learnerlearner or None, default=None

The learner of every target outside {0, 1}: a continuous outcome, a dose, a product with a dose or with a continuous feature. None takes outcome_learner. A binary outcome with a dose node or a continuous feature needs this setting when outcome_learner is a classifier.

n_foldsint, default=10

The outer folds, drawn over trajectories. 1 fits in sample.

learner_foldsint, default=5

The inner folds of a default Super Learner.

positivity_marginfloat, default=0.01

The distance from 0 or 1 that the diagnostic share of extreme class probabilities counts. It truncates nothing.

min_eigenvaluefloat, default=1e-8

The floor of the smallest standardized eigenvalue of each diagonal Jacobian block. A smaller block raises.

alpha_sigfloat, default=0.05

The significance level of the intervals.

simultaneousbool, default=True

Whether to report simultaneous bands.

n_multiplierint, default=10_000

Multiplier draws of the bands.

multiplier_kindstr, default=”mammen”

The multiplier law.

band_scalestr, default=”logit”

The scale of the band of a bounded estimate (simultaneous_bands()).

band_referencestr, default=”normal”

"t" bands each estimate on its own Student t reference (simultaneous_bands()).

random_stateint or None, default=None

The seed of the folds, the default learners and the bands.

run_idstr or None, default=None

A label for the provenance record.

n_jobsint, default=1

Workers for the nuisance fits.

msmNone

Refused. A working model over regimens is declared on LTMLE or SDR.

pseudo_learnerNone

Refused. The estimator builds no pseudo-outcome.

censoring_learnerNone

Refused. The estimator fits an end-of-study outcome with no censoring.

horizonsNone

Refused. The outcome is one end-of-study value.

**refusedAny

A point-treatment keyword, refused by name.

Raises:
CapabilityError

When a refused setting is passed, with the reason code of the refusal.

See also

snmm

The one-call form.

cleverly.longitudinal.LTMLE

The nonparametric estimator of static regimen values.

StructuralNestedMeanResult.check_model

The comparison with LTMLE.

Examples

>>> from cleverly.datasets import make_longitudinal
>>> from cleverly.learners import GLM
>>> from cleverly.longitudinal import StructuralNestedMean
>>> frame, _ = make_longitudinal(n=300, seed=0, censoring=False)
>>> result = StructuralNestedMean(
...     {"always": 1, "never": 0},
...     outcome_learner=GLM(family="binomial"),
...     treatment_learner=GLM(family="binomial"),
...     n_folds=2,
...     random_state=0,
... ).fit(
...     frame,
...     outcome="Y",
...     treatment=["A1", "A2"],
...     baseline=["W1", "W2"],
...     time_varying=[[], ["L2"]],
... )
>>> sorted(result)
['ate_regimen[never vs always]', 'blip[A1:1.0:intercept]', 'blip[A2:1.0:intercept]',
 'ey_reference', 'ey_regimen[always]', 'ey_regimen[never]']

Methods#

StructuralNestedMean.fit(data: Any, *, outcome: str | Sequence[str] | Mapping[str, Sequence[str]] | Repeated | None = None, treatment: str | Sequence[str] | None = None, baseline: Sequence[str] | None = None, time_varying: Sequence[Sequence[str]] | None = None, censoring: Sequence[str] | None = None, id: str | None = None, weights: str | None = None, weights_type: str = 'probability', weights_estimated: bool = False, family: str = 'auto', continuous_treatment: Sequence[str] = (), **refused: Any) → StructuralNestedMeanResult[source]#

Fit on a wide dataframe, or on an already-built LongitudinalData.

The columns are read as cleverly.longitudinal.LTMLE.fit() reads them. The design must hold one end-of-study outcome and no censoring. weights= names known observation weights: every nuisance is fitted with them, every mean is weighted, and the curve carries the weight of its row. id= names a cluster column: the folds hold whole clusters and the variance sums the curve within a cluster.

Parameters:
dataDataFrame or LongitudinalData

One wide frame, or a container that has already read the columns.

outcomestr or None

The end-of-study outcome column.

treatmentstr, sequence of str, or None

The treatment column of each node, in time order.

baselinesequence of str or None

The covariates measured before any treatment.

time_varyingsequence of sequence of str or None

The covariates measured before the treatment of each node.

censoringsequence of str or None

Refused when it names a column.

idstr or None

A cluster column.

weightsstr or None

A column of known observation weights.

weights_typestr

How to read weights.

weights_estimatedbool

Declare that the weights came out of a fitted model. The fit then takes the estimated_weight_plugin status.

family{“auto”, “binomial”, “gaussian”}

The outcome family.

continuous_treatmentsequence of str

The treatment columns that hold a dose. A dose enters the blip linearly, and the reference dose 0 must lie in the observed range.

**refusedAny

A point-treatment keyword, refused by name.

Returns:
StructuralNestedMeanResult

The blips, the reference mean, and the declared regimen means and contrasts.

StructuralNestedMean.refit(data: LongitudinalData, *, random_state: int | None = None, frozen_folds: Sequence[Folds] | None = None, frozen_seeds: Sequence[int | None] | None = None) → StructuralNestedMeanResult[source]#

Run the whole fit again, nuisances included, on a prepared container.

cleverly.validation.refute() calls this on a structural result. It changes a column, draws units, or appends a covariate, and then refits. The estimator is not modified: every setting applies to a copy, and the copy builds no simultaneous band.

Parameters:
dataLongitudinalData

The prepared container to fit, usually result.data with one array changed.

random_stateint or None

Seed of the refit’s split and default learners. None keeps this estimator’s own.

frozen_foldssequence of Folds or None

The realized outer split, for the rows of data. The estimator draws one split, so the sequence holds one entry. None draws the split as usual.

frozen_seedssequence of int or None

One seed, in place of random_state, for the outer split and for every learner the estimator seeds. The sequence holds one entry. Given the original fit’s StructuralConfig.fit_seed, a refit on data with the same row count draws the original split and the original learner seeds.

Returns:
StructuralNestedMeanResult

The result of the whole fit on data.

Raises:
ValueError

When frozen_folds or frozen_seeds holds other than one entry, or a frozen split labels another number of rows than data holds.