Formulaic Contrasts (version 1.0.0) is a Python library designed to build contrast vectors for statistical models defined using the `formulaic` library. It provides tools for conveniently constructing design matrices and specifying comparisons between categorical levels. The library released its 1.0.0 version, declaring the API as stable, and is part of the scverse project. Its release cadence is irregular but indicates movement towards stability.
pip install formulaic-contrastsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `FormulaicContrasts` with a pandas DataFrame and a formulaic design string. It then shows two ways to build contrast vectors: using the flexible `cond()` method for arbitrary conditions and arithmetic combinations, and the `contrast()` method for simple pairwise comparisons between levels of a single variable.
Consult the official documentation for `formulaic-contrasts` to ensure compatibility with the `v1.0.0` stable API. Update your usage patterns for `FormulaicContrasts`, `cond()`, and `contrast()` methods if necessary.
Understand that the output of `formulaic-contrasts` (contrast vectors) needs to be passed to a statistical package (e.g., `statsmodels`, `scanpy`'s `lm.ols`) that can interpret these vectors in the context of a fitted model to perform hypothesis testing or derive interpretations.
Use `model.cond(variable='level') - model.cond(variable='baseline_level')` for arbitrary or complex contrasts, and reserve `model.contrast(column='variable', baseline='baseline_level', group_to_compare='level')` for straightforward pairwise comparisons.
When upgrading `formulaic`, consult its changelog. Specifically, be cautious about how categorical factor column names might have changed, as this could impact the interpretation of `formulaic-contrasts` output. For example, `feature[T.A]` naming might have been modified.
Ensure the library is correctly installed using pip: `pip install formulaic-contrasts` or, if part of a larger scverse installation, via conda: `conda install -c conda-forge formulaic-contrasts`.
Simplify the formulaic design specification, particularly for categorical variables, to ensure that each factor level corresponds to a single, identifiable column in the design matrix, or adjust the approach to retrieving variables if complex designs are necessary.
Ensure all input data to `formulaic-contrasts` functions, especially those defining categorical levels or numerical values, are of the correct and consistent data types (e.g., pandas Series for factors, numerical arrays for continuous variables) and shapes.
Verify that all factor level names used in the contrast specification exactly match the levels present in the categorical data used to create the design matrix. Inspect the unique values of your categorical columns.