Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RBC calculation for Wilcoxon signed-rank test to be dependent on the alternative #457

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/pingouin/nonparametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,37 +410,37 @@ def wilcoxon(x, y=None, alternative="two-sided", **kwargs):
>>> y = np.array([38, 37, 33, 29, 14, 12, 20, 22, 17, 25, 26, 16])
>>> pg.wilcoxon(x, y, alternative='two-sided')
W_val alternative p_val RBC CLES
Wilcoxon 20.5 two-sided 0.285765 -0.378788 0.395833
Wilcoxon 20.5 two-sided 0.288086 -0.378788 0.395833

Same but using pre-computed differences. However, the CLES effect size
cannot be computed as it requires the raw data.

>>> pg.wilcoxon(x - y)
W_val alternative p_val RBC CLES
Wilcoxon 20.5 two-sided 0.285765 -0.378788 NaN
Wilcoxon 20.5 two-sided 0.288086 -0.378788 NaN

Compare with SciPy

>>> import scipy
>>> scipy.stats.wilcoxon(x, y)
WilcoxonResult(statistic=20.5, pvalue=0.2661660677806492)
WilcoxonResult(statistic=20.5, pvalue=0.2880859375)

The p-value is not exactly similar to Pingouin. This is because Pingouin automatically applies
a continuity correction. Disabling it gives the same p-value as scipy:

>>> pg.wilcoxon(x, y, alternative='two-sided', correction=False)
W_val alternative p_val RBC CLES
Wilcoxon 20.5 two-sided 0.266166 -0.378788 0.395833
Wilcoxon 20.5 two-sided 0.288086 -0.378788 0.395833

One-sided test

>>> pg.wilcoxon(x, y, alternative='greater')
W_val alternative p_val RBC CLES
Wilcoxon 20.5 greater 0.876244 -0.378788 0.395833
Wilcoxon 20.5 greater 0.865723 -0.378788 0.395833

>>> pg.wilcoxon(x, y, alternative='less')
W_val alternative p_val RBC CLES
Wilcoxon 20.5 less 0.142883 -0.378788 0.604167
Wilcoxon 20.5 less 0.144043 0.378788 0.604167
"""
x = np.asarray(x)
if y is not None:
Expand Down Expand Up @@ -490,7 +490,9 @@ def wilcoxon(x, y=None, alternative="two-sided", **kwargs):
rsum = r.sum()
r_plus = np.sum((d > 0) * r)
r_minus = np.sum((d < 0) * r)
rbc = r_plus / rsum - r_minus / rsum
rbc = (
r_minus / rsum - r_plus / rsum if alternative == "less" else r_plus / rsum - r_minus / rsum
)

# Fill output DataFrame
stats = pd.DataFrame(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_nonparametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_wilcoxon(self):
# Note that the RBC value are compared to JASP in test_pairwise.py
# The RBC values in JASP does not change according to the tail.
assert round(wc_pg.at["Wilcoxon", "RBC"], 3) == -0.379
assert round(wc_pg_less.at["Wilcoxon", "RBC"], 3) == -0.379
assert round(wc_pg_less.at["Wilcoxon", "RBC"], 3) == 0.379
assert round(wc_pg_greater.at["Wilcoxon", "RBC"], 3) == -0.379
# CLES is compared to:
# https://janhove.github.io/reporting/2016/11/16/common-language-effect-sizes
Expand Down
Loading