Skip to content

statspai.msm

msm

Marginal Structural Models (MSM) for time-varying treatments.

Implements Robins' inverse-probability-of-treatment-weighted (IPTW) estimator for longitudinal data with time-varying confounding.

MarginalStructuralModel

Class wrapper around :func:msm for programmatic access.

stabilized_weights

stabilized_weights(data: DataFrame, treat: str, id: str, time: str, time_varying: List[str], baseline: Optional[List[str]] = None, treat_type: str = 'auto', trim_per_period: float = 0.0) -> ndarray

Compute stabilized IPTW weights for time-varying treatments.

Returns one weight per row (long-format). For treatment :math:A_t with history :math:\bar{A}_{t-1}, time-varying confounders :math:\bar{L}_t (must be pre-treatment), and baseline V:

  • Numerator: conditional density/probability of :math:A_t given :math:\bar{A}_{t-1} and V (no L).
  • Denominator: conditional density/probability of :math:A_t given :math:\bar{A}_{t-1}, :math:\bar{L}_t, and V.

The row weight is the cumulative product of per-period ratios up to and including period t.

Parameters:

Name Type Description Default
data DataFrame

Sorted long-format panel (one row per unit-time).

required
treat str

Treatment column (binary 0/1 or continuous).

required
id str

Unit and time identifiers.

required
time str

Unit and time identifiers.

required
time_varying list of str

Time-varying confounders (already lagged to pre-treatment).

required
baseline list of str

Baseline covariates.

None
treat_type (auto, binary, continuous)

Overrides auto-detection.

'auto'
trim_per_period float

If > 0, trim each period's density ratio at the symmetric quantile [trim_per_period, 1 - trim_per_period] before taking the cumulative product. A common value is 0.01. Set to 0 to disable per-period trimming (the default).

0.0

Returns:

Type Description
ndarray

Stabilized weight for each row.