Skip to main content

A Python package for stepwise estimation of latent class models with measurement and structural components. The package can also be used to fit mixture models with various observed random variables.

Project description

StepMix

PyPI version Build Documentation Status Code style: black Downloads Downloads arXiv

For StepMixR, please refer to this repository.

A Python package following the scikit-learn API for model-based clustering and generalized mixture modeling (latent class/profile analysis) of continuous and categorical data. StepMix handles missing values through Full Information Maximum Likelihood (FIML) and provides multiple stepwise Expectation-Maximization (EM) estimation methods based on pseudolikelihood theory. Additional features include support for covariates and distal outcomes, various simulation utilities, and non-parametric bootstrapping, which allows inference in semi-supervised and unsupervised settings.

Reference

If you find StepMix useful, please consider citing our arXiv preprint:

@article{morin2023stepmix,
  title={StepMix: A Python Package for Pseudo-Likelihood Estimation of Generalized Mixture Models with External Variables},
  author={Morin, Sacha and Legault, Robin and Bakk, Zsuzsa and Gigu{\`e}re, Charles-{\'E}douard and de la Sablonni{\`e}re, Roxane and Lacourse, {\'E}ric},
  journal={arXiv preprint arXiv:2304.03853},
  year={2023}
}

Install

You can install StepMix with pip, preferably in a virtual environment:

pip install stepmix

Quickstart

A simple StepMix mixture using the continuous variables of the Iris Dataset:

import pandas as pd
from sklearn.datasets import load_iris
from sklearn.metrics import rand_score

from stepmix.stepmix import StepMix

# Load dataset in a Dataframe
data_continuous, target = load_iris(return_X_y=True, as_frame=True)

# Continuous StepMix Model with 3 latent classes
model = StepMix(n_components=3, measurement="continuous", verbose=0, random_state=123)

# Fit model and predict clusters
model.fit(data_continuous)
pred_continuous = model.predict(data_continuous)

# A Rand score close to 1 indicates good alignment between clusters and flower types
print(rand_score(pred_continuous, target))

StepMix also provides support for categorical mixtures:

# Create categorical data based on the Iris Dataset quantiles
data_categorical = data_continuous.copy()
for col in data_categorical:
   data_categorical[col] = pd.qcut(data_continuous[col], q=3).cat.codes

# Categorical StepMix Model with 3 latent classes
model = StepMix(n_components=3, measurement="categorical", verbose=0, random_state=123)

# Fit model and predict clusters
model.fit(data_categorical)
pred_categorical = model.predict(data_categorical)

# A Rand score close to 1 indicates good alignment between clusters and flower types
print(rand_score(pred_categorical, target))

Please refer to the StepMix tutorials to learn how to handle missing values and combine continuous and categorical data in the same model.

Tutorials

Detailed tutorials are available in notebooks:

  1. Generalized Mixture Models with StepMix: an in-depth look at how latent class models can be defined with StepMix. The tutorial uses the Iris Dataset as an example and covers:
    1. Continuous LCA models (latent profile analysis/gaussian mixture model);
    2. Binary LCA models;
    3. Categorical LCA models;
    4. Mixed variables mixture models (continuous and categorical data);
    5. Missing Values through Full-Information Maximum Likelihood.
  2. Stepwise Estimation with StepMix: a tutorial demonstrating how to define measurement and structural models. The tutorial discusses:
    1. LCA models with distal outcomes;
    2. LCA models with covariates;
    3. 1-step, 2-step and 3-step estimation;
    4. Corrections (BCH or ML) and other options for 3-step estimation.
  3. Model Selection: a short tutorial discussing:
    1. Selecting the number of components in a mixture model (n_components);
    2. Comparing models with fit indices: AIC and BIC.
  4. Parameters, Bootstrapping and CI: a tutorial discussing how to:
    1. Access StepMix parameters;
    2. Bootstrap StepMix estimators;
    3. Quickly plot confidence intervals.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stepmix-1.1.1.tar.gz (47.8 kB view details)

Uploaded Source

Built Distribution

stepmix-1.1.1-py3-none-any.whl (38.0 kB view details)

Uploaded Python 3

File details

Details for the file stepmix-1.1.1.tar.gz.

File metadata

  • Download URL: stepmix-1.1.1.tar.gz
  • Upload date:
  • Size: 47.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for stepmix-1.1.1.tar.gz
Algorithm Hash digest
SHA256 16586a40146375ce86888b45925b3fbf49b4d8b9c87240a8e972f3d7ab084221
MD5 a24cc34d7f40148b018cadc1d8730cb9
BLAKE2b-256 9d1228ef4c588025315f6373915400f602f9351bcb67f42ba7bf587fd68bd799

See more details on using hashes here.

File details

Details for the file stepmix-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: stepmix-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 38.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for stepmix-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2aac5f2d296ff10464ff3df2417b89d7c86c15b71ec9e2a466fed2cbc53f81b2
MD5 8c99cf8b975131cac582383642a654a2
BLAKE2b-256 a338899721710afa8c07130a40ad93f8788968e78bad229de20e91285b970b61

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page