pylimma.contrasts_fit
- pylimma.contrasts_fit(data, contrasts=None, coefficients=None, key='pylimma')[source]
Apply contrast matrix to a fitted model.
Transforms coefficients and standard errors to reflect contrasts of interest rather than the original model parameterisation.
- Parameters:
data (AnnData or dict) – Either an AnnData object with fit results in adata.uns[key], or a dict returned by lm_fit().
contrasts (ndarray or DataFrame, optional) – Contrast matrix of shape (n_original_coefs, n_contrasts). Each column defines a contrast. If DataFrame, column names are preserved as contrast names.
coefficients (int, str, or list, optional) –
Alternative to contrasts. Specifies which coefficients to keep in the revised fit object. Can be indices (int), names (str), or a list of either. This is a simpler way to subset coefficients without defining a full contrast matrix.
Warning
Integer indices are 0-based (Python convention). R’s
contrasts.fit(fit, coefficients=c(2, 3))uses 1-based indices; the equivalent pylimma call iscontrasts_fit(fit, coefficients=[1, 2]). Prefer string names when porting R code to avoid silent off-by-one errors.key (str, default "pylimma") – Key for fit results in adata.uns (AnnData input only).
- Returns:
If input is dict, returns updated dict with transformed coefficients. If input is AnnData, updates adata.uns[key] in place and returns None.
- Return type:
dict or None
Notes
Exactly one of contrasts or coefficients must be provided.
The coefficients parameter provides a simpler way to specify the contrasts matrix when the desired contrasts are just a subset of the original coefficients.
The transformation preserves the relationship between coefficients and their standard errors. For orthogonal designs, the standard errors transform simply. For non-orthogonal designs, the correlation structure is accounted for.
Any previous test statistics (t, p-values, etc.) are removed since they are no longer valid after the transformation.
References
Smyth, G. K. (2004). Linear models and empirical Bayes methods for assessing differential expression in microarray experiments. Statistical Applications in Genetics and Molecular Biology, 3(1), Article 3.