contrast / pairs¶
Linear contrasts and pairwise comparisons on estimated marginal means.
contrast() applies a set of linear contrasts to an emmeans result.
pairs() is a convenience wrapper for all pairwise differences with Tukey HSD adjustment,
matching R’s pairs() ergonomics.
- contrast(emm, method='pairwise', adjust='none')[source]¶
Apply linear contrasts to estimated marginal means.
- Parameters:
emm – An
EmmResultreturned byemmeans().method – How to form the contrasts:
'pairwise'All pairwise differences
i - jfori < j(alphabetical level order). Producesn*(n-1)//2rows.'trt.vs.ctrl'Each non-first level minus the first (control) level. Produces
n-1rows.- list of np.ndarray
Each array is a contrast vector of length
n(number of EMM rows). Generic namescontrast1,contrast2, … are used.- dict mapping str → np.ndarray
Same as a list but uses the dict keys as contrast names.
adjust – Multiple-comparison p-value adjustment:
'none'(default),'bonferroni','holm','fdr', or'tukey'.
- Return type:
Any- Returns:
pandas.DataFrame – Columns:
["contrast", "estimate", "SE", "df", "t.ratio", "p.value"].- Raises:
ValueError – If method is a string other than
'pairwise'or'trt.vs.ctrl', or if adjust is not a recognised method.- Parameters:
emm (EmmResult)
method (str | list[ndarray] | dict[str, ndarray])
adjust (str)
Examples
>>> emm = interlace.emmeans(result, specs="treatment") >>> interlace.contrast(emm, method="pairwise")
- pairs(emm, adjust='tukey')[source]¶
All pairwise comparisons of estimated marginal means.
Convenience wrapper for
contrast(emm, method='pairwise', adjust=adjust). Matches R’spairs()ergonomics with Tukey HSD adjustment by default.- Parameters:
emm – An
EmmResultreturned byemmeans().adjust – Multiple-comparison adjustment (default
'tukey'). Any value accepted bycontrast()is valid.
- Return type:
Any- Returns:
pandas.DataFrame – Columns:
["contrast", "estimate", "SE", "df", "t.ratio", "p.value"].- Parameters:
emm (EmmResult)
adjust (str)
Examples
>>> emm = interlace.emmeans(result, specs="treatment") >>> interlace.pairs(emm)
Built-in contrast methods¶
|
Description |
Rows produced |
|---|---|---|
|
All pairwise differences |
|
|
Each non-first level minus the first (control) level |
|
list of |
Custom contrast vectors of length |
one per vector |
dict |
Same as list, using the dict keys as contrast names |
one per entry |
p-value adjustment methods¶
|
Method |
|---|---|
|
No adjustment (raw p-values) |
|
Bonferroni correction ( |
|
Step-down Bonferroni (Holm 1979) |
|
Benjamini-Hochberg false discovery rate |
|
Tukey HSD via the studentized range distribution |
Returned columns¶
Column |
Description |
|---|---|
|
Name of the contrast (e.g. |
|
Contrast estimate |
|
Standard error |
|
Satterthwaite denominator degrees of freedom |
|
t-statistic ( |
|
Adjusted p-value |
Examples¶
Pairwise comparisons (no adjustment)¶
import interlace
result = interlace.fit("rt ~ condition", data=df, groups=["subject", "item"])
emm = interlace.emmeans(result, specs="condition")
pw = interlace.contrast(emm, method="pairwise")
print(pw)
# contrast estimate SE df t.ratio p.value
# 0 high - control 46.4 8.3 41.2 5.59 0.0001
All pairwise with Tukey HSD via pairs()¶
pw_tukey = interlace.pairs(emm) # adjust='tukey' by default
pw_bonf = interlace.pairs(emm, adjust="bonferroni")
Treatment vs. control¶
# First level (alphabetically) is treated as control
tvc = interlace.contrast(emm, method="trt.vs.ctrl", adjust="holm")
Custom contrast vectors¶
import numpy as np
# 3-level factor: [control, low, high]
# Custom: average of treatments vs. control
custom = interlace.contrast(emm, method={
"avg(low,high) - control": np.array([-1.0, 0.5, 0.5]),
})