Skip to content

statspai.imputation

imputation

Missing data handling and multiple imputation.

Provides MICE (Multiple Imputation by Chained Equations), EM imputation, and analysis tools for multiply-imputed data.

MICEResult

Bases: ResultProtocolMixin

Results from MICE imputation.

Examples:

>>> import statspai as sp
>>> import numpy as np, pandas as pd
>>> rng = np.random.default_rng(7)
>>> x1 = rng.normal(size=120)
>>> x2 = 0.5 * x1 + rng.normal(size=120)
>>> y = 1.0 + x1 + x2 + rng.normal(size=120)
>>> df = pd.DataFrame({"y": y, "x1": x1, "x2": x2})
>>> df.loc[rng.choice(120, size=15, replace=False), "x2"] = np.nan
>>> res = sp.mice(df, m=3, method="pmm", seed=0)
>>> type(res).__name__
'MICEResult'
>>> res.n_imputations
3
>>> bool(res.complete(0)["x2"].notna().all())
True

complete

complete(m: int = 0) -> DataFrame

Return the m-th completed dataset (0-indexed).

combine

combine(estimates: List[Dict[str, Any]]) -> Dict[str, Any]

Apply Rubin's rules to combine estimates across imputations.

mi_estimate

mi_estimate(mice_result: MICEResult, estimator: Any, **kwargs: Any) -> Dict[str, Any]

Run an estimator on each imputed dataset and combine using Rubin's rules.

Parameters:

Name Type Description Default
mice_result MICEResult

Result from mice().

required
estimator callable

Estimation function that returns an EconometricResults object.

required
**kwargs Any

Arguments passed to the estimator.

{}

Returns:

Type Description
dict

Combined estimates (Rubin's rules).

Examples:

>>> import statspai as sp
>>> import numpy as np, pandas as pd
>>> rng = np.random.default_rng(7)
>>> x1 = rng.normal(size=120)
>>> x2 = 0.5 * x1 + rng.normal(size=120)
>>> y = 1.0 + x1 + x2 + rng.normal(size=120)
>>> df = pd.DataFrame({"y": y, "x1": x1, "x2": x2})
>>> df.loc[rng.choice(120, size=15, replace=False), "x2"] = np.nan
>>> mice_res = sp.mice(df, m=3, seed=0)
>>> combined = sp.mi_estimate(mice_res, sp.regress, formula="y ~ x1 + x2")
>>> combined['n_imputations']
3
>>> combined['var_names']
['Intercept', 'x1', 'x2']