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

BUG: fixed calculation of ranks in spearman, added option to use midr… #192

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions permute/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import warnings
from scipy.optimize import brentq, fsolve
from scipy.stats import ttest_ind, ttest_1samp
from scipy.stats import ttest_ind, ttest_1samp, rankdata
from fractions import Fraction

from .utils import get_prng, potential_outcomes, permute
Expand Down Expand Up @@ -52,7 +52,7 @@ def corr(x, y, alternative='greater', reps=10**4, seed=None, plus1=True):
return tst, pvalue, sims


def spearman_corr(x, y, alternative='greater', reps=10**4, seed=None, plus1=True):
def spearman_corr(x, y, alternative='greater', reps=10**4, seed=None, plus1=True, method='average'):
r"""
Simulate permutation p-value for Spearman correlation coefficient

Expand All @@ -72,15 +72,20 @@ def spearman_corr(x, y, alternative='greater', reps=10**4, seed=None, plus1=True
flag for whether to add 1 to the numerator and denominator of the
p-value based on the empirical permutation distribution.
Default is True.
method : {‘average’, ‘min’, ‘max’, ‘dense’, ‘ordinal’}
passed to scipy.stats.rankdata. 'average' uses midranks.
'ordinal' uses distinct ranks, with ties broken by order
of appearance in the original list.
Default is 'average'.

Returns
-------
tuple
Returns test statistic, p-value, simulated distribution
"""

xnew = np.argsort(x)+1
ynew = np.argsort(y)+1
xnew = rankdata(x, method=method)
ynew = rankdata(y, method=method)
return corr(xnew, ynew, alternative=alternative, reps=reps, seed=seed)


Expand Down