cleverly.msm.working_model_means#

cleverly.msm.working_model_means(result: Any, *, alpha: float | None = None) → WorkingModelMeans[source]#

The working model’s fitted mean at each level, with a pointwise interval.

A point-treatment marginal structural model fit reports the coefficients \(\hat\beta\) of the working model \(m(a) = g^{-1}(\varphi(a)^\top\beta)\). This accessor reads the fitted model back at each declared treatment level, so a plot can set the model against the per-level counterfactual means. The interval is built on the link scale: the linear predictor \(\varphi(a)^\top\hat\beta\) has variance \(\varphi(a)^\top\hat\Sigma\varphi(a)\), with \(\hat\Sigma\) the covariance of the coefficients from their influence curves (summed within clusters on a clustered fit, with the t reference of a few-cluster fit), and both limits are mapped through the inverse link. On an unclustered fit of a continuous outcome, whose coefficients read the skew-widened t, the linear predictor reads it too, from its own influence curve \(\varphi(a)^\top \mathrm{IC}\). Nothing is refitted.

Parameters:
resultTMLEResult

A point-treatment fit of MSMProjection on a treatment with a finite set of levels.

alphafloat or None, default=None

Two-sided level of the interval; the coefficients’ own level when omitted.

Returns:
WorkingModelMeans

The fitted means and their intervals at each level.

Raises:
CapabilityError

When the fit has no working model, the treatment is continuous, the design depends on covariates (a model with modifier terms has one curve per modifier value, which the fit does not store), or the fit reports the median of several cross-fitting draws, which has no joint covariance.

See also

cleverly.estimators.TMLEResult.coefficients

The coefficients this reads back as means.

cleverly.plotting.curve

Draws these means beside the per-level means.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from sklearn.linear_model import LinearRegression, LogisticRegression
>>> from cleverly import CausalStudy, MSMProjection, PointTreatment
>>> from cleverly.msm import MSM, working_model_means
>>> rng = np.random.default_rng(0)
>>> a = rng.integers(0, 3, 300).astype(float)
>>> frame = pd.DataFrame({"A": a, "W": rng.normal(size=300)})
>>> frame["Y"] = 0.5 * a + 0.3 * frame["W"] + rng.normal(size=300)
>>> study = CausalStudy(
...     frame, design=PointTreatment(outcome="Y", treatment="A", adjustment=("W",))
... )
>>> fit = study.identify(MSMProjection(MSM.linear())).estimate(
...     outcome_learner=LinearRegression(),
...     treatment_learner=LogisticRegression(max_iter=1000),
...     cross_fit=False,
...     random_state=0,
... )
>>> fitted = working_model_means(fit)
>>> fitted.levels, fitted.estimate.shape
((0.0, 1.0, 2.0), (3,))