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
LTMLEreports only when the model holds.- Parameters:
- regimens
mapping, sequence,orNone Static regimens, as for
LTMLE.Nonereports the blips and the reference mean only. A rule, a modified treatment policy or an incremental policy is refused.- reference
strorNone, default=None The regimen every contrast is taken against.
Nonetakes the first.- blip_features
mappingorNone, 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_referenceonly.- outcome_learner
learnerorNone, default=None The learner of \(q_t\).
Nonefits a Super Learner.- treatment_learner
learnerorNone, default=None The learner of the treatment-side nuisances \(m_{j,t}\). A target in {0, 1} takes this learner as a classifier.
Nonefits a Super Learner.- regression_learner
learnerorNone, 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.
Nonetakesoutcome_learner. A binary outcome with a dose node or a continuous feature needs this setting whenoutcome_learneris a classifier.- n_folds
int, default=10 The outer folds, drawn over trajectories.
1fits in sample.- learner_folds
int, default=5 The inner folds of a default Super Learner.
- positivity_margin
float, default=0.01 The distance from 0 or 1 that the diagnostic share of extreme class probabilities counts. It truncates nothing.
- min_eigenvalue
float, default=1e-8 The floor of the smallest standardized eigenvalue of each diagonal Jacobian block. A smaller block raises.
- alpha_sig
float, default=0.05 The significance level of the intervals.
- simultaneousbool, default=True
Whether to report simultaneous bands.
- n_multiplier
int, default=10_000 Multiplier draws of the bands.
- multiplier_kind
str, default=”mammen” The multiplier law.
- band_scale
str, default=”logit” The scale of the band of a bounded estimate (
simultaneous_bands()).- band_reference
str, default=”normal” "t"bands each estimate on its own Student t reference (simultaneous_bands()).- random_state
intorNone, default=None The seed of the folds, the default learners and the bands.
- run_id
strorNone, default=None A label for the provenance record.
- n_jobs
int, default=1 Workers for the nuisance fits.
- msm
None Refused. A working model over regimens is declared on
LTMLEorSDR.- pseudo_learner
None Refused. The estimator builds no pseudo-outcome.
- censoring_learner
None Refused. The estimator fits an end-of-study outcome with no censoring.
- horizons
None Refused. The outcome is one end-of-study value.
- **refused
Any A point-treatment keyword, refused by name.
- regimens
- Raises:
CapabilityErrorWhen a refused setting is passed, with the reason code of the refusal.
See also
snmmThe one-call form.
cleverly.longitudinal.LTMLEThe nonparametric estimator of static regimen values.
StructuralNestedMeanResult.check_modelThe 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:
- data
DataFrameorLongitudinalData One wide frame, or a container that has already read the columns.
- outcome
strorNone The end-of-study outcome column.
- treatment
str, sequenceofstr,orNone The treatment column of each node, in time order.
- baselinesequence
ofstrorNone The covariates measured before any treatment.
- time_varyingsequence
ofsequenceofstrorNone The covariates measured before the treatment of each node.
- censoringsequence
ofstrorNone Refused when it names a column.
- id
strorNone A cluster column.
- weights
strorNone A column of known observation weights.
- weights_type
str How to read
weights.- weights_estimatedbool
Declare that the weights came out of a fitted model. The fit then takes the
estimated_weight_pluginstatus.- family{“auto”, “binomial”, “gaussian”}
The outcome family.
- continuous_treatmentsequence
ofstr The treatment columns that hold a dose. A dose enters the blip linearly, and the reference dose 0 must lie in the observed range.
- **refused
Any A point-treatment keyword, refused by name.
- data
- Returns:
StructuralNestedMeanResultThe 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:
- data
LongitudinalData The prepared container to fit, usually
result.datawith one array changed.- random_state
intorNone Seed of the refit’s split and default learners.
Nonekeeps this estimator’s own.- frozen_foldssequence
ofFoldsorNone The realized outer split, for the rows of
data. The estimator draws one split, so the sequence holds one entry.Nonedraws the split as usual.- frozen_seedssequence
ofintorNone 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’sStructuralConfig.fit_seed, a refit on data with the same row count draws the original split and the original learner seeds.
- data
- Returns:
StructuralNestedMeanResultThe result of the whole fit on
data.
- Raises:
ValueErrorWhen
frozen_foldsorfrozen_seedsholds other than one entry, or a frozen split labels another number of rows thandataholds.