Formulaic Contrasts
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.
Warnings
- breaking When upgrading from `formulaic-contrasts` versions `v0.x.x` to `v1.0.0`, be aware that the API is now considered stable. While explicit breaking changes from previous versions were not extensively documented for `formulaic-contrasts` itself, it's prudent to review your code for compatibility with the stabilized API.
- gotcha `formulaic-contrasts` is solely a tool for constructing contrast vectors for models defined with `formulaic`. It does not implement any statistical tests or perform model fitting itself. Users must integrate the generated contrast vectors with a separate statistical modeling library to perform analysis.
- gotcha The behavior of `FormulaicContrasts.cond()` and `FormulaicContrasts.contrast()` differs. `cond()` is a general method for defining specific conditions which can then be combined arithmetically to form complex contrasts. `contrast()` is a convenience method for simple pairwise comparisons between two levels of a single categorical variable. For more complex comparisons, `cond()` should be preferred.
- breaking As `formulaic-contrasts` depends on `formulaic`, be aware of breaking changes introduced in `formulaic`'s `v1.0.x` series. These include changes to canonical formatting of Python tokens, removal of previously deprecated methods, and potential alterations to column names associated with categorical factors in the generated design matrix. Such changes in the underlying `formulaic` library can indirectly affect how contrast vectors are interpreted or how `formulaic-contrasts` interacts with the design matrix.
Install
-
pip install formulaic-contrasts
Imports
- FormulaicContrasts
from formulaic_contrasts import FormulaicContrasts
Quickstart
import pandas as pd
from formulaic_contrasts import FormulaicContrasts
from formulaic_contrasts.datasets import treatment_response
# Load example data
df = treatment_response()
# Initialize FormulaicContrasts with data and a formulaic design string
model = FormulaicContrasts(df, '~ response + treatment')
# Build a contrast for 'responder' vs 'non_responder'
# using the more general .cond() method
contrast_vector = model.cond(response='responder') - model.cond(response='non_responder')
print("Contrast Vector (responder vs non_responder):")
print(contrast_vector)
# Build a simple pairwise contrast using .contrast()
simple_contrast = model.contrast(
column='treatment',
baseline='drugA',
group_to_compare='drugB'
)
print("\nSimple Contrast Vector (drugB vs drugA):")
print(simple_contrast)