Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
With the correction for uniform marginals
>>> r, pval = circ_corrcc(x, y, correction_uniform=True)
>>> print(r, pval)
0.547 0.28585306869206784
"""
from scipy.stats import norm
x = np.asarray(x)
y = np.asarray(y)
# Check size
if x.size != y.size:
raise ValueError('x and y must have the same length.')
# Remove NA
x, y = remove_na(x, y, paired=True)
n = x.size
# Compute correlation coefficient
x_sin = np.sin(x - circmean(x))
y_sin = np.sin(y - circmean(y))
if not correction_uniform:
# Similar to np.corrcoef(x_sin, y_sin)[0][1]
r = np.sum(x_sin * y_sin) / np.sqrt(np.sum(x_sin**2) *
np.sum(y_sin**2))
else:
r_minus = np.abs(np.sum(np.exp((x - y) * 1j)))
r_plus = np.abs(np.sum(np.exp((x + y) * 1j)))
denom = 2 * np.sqrt(np.sum(x_sin ** 2) * np.sum(y_sin ** 2))
r = (r_minus - r_plus) / denom
from pingouin import (power_ttest, power_ttest2n, compute_effsize)
# Check tails
possible_tails = ['two-sided', 'one-sided', 'greater', 'less']
assert tail in possible_tails, 'Invalid tail argument.'
x = np.asarray(x)
y = np.asarray(y)
if x.size != y.size and paired:
warnings.warn("x and y have unequal sizes. Switching to "
"paired == False. Check your data.")
paired = False
# Remove rows with missing values
x, y = remove_na(x, y, paired=paired)
nx, ny = x.size, y.size
if ny == 1:
# Case one sample T-test
tval, pval = ttest_1samp(x, y)
dof = nx - 1
se = np.sqrt(x.var(ddof=1) / nx)
if ny > 1 and paired is True:
# Case paired two samples T-test
# Do not compute if two arrays are identical (avoid SciPy warning)
if np.array_equal(x, y):
warnings.warn("x and y are equals. Cannot compute T or p-value.")
tval, pval = np.nan, np.nan
else:
tval, pval = ttest_rel(x, y)
dof = nx - 1
>>> x = [0.785, 1.570, 3.141, 0.839, 5.934]
>>> y = [1.593, 1.291, -0.248, -2.892, 0.102]
>>> r, pval = circ_corrcl(x, y)
>>> print(r, pval)
0.109 0.9708899750629237
"""
from scipy.stats import pearsonr, chi2
x = np.asarray(x)
y = np.asarray(y)
# Check size
if x.size != y.size:
raise ValueError('x and y must have the same length.')
# Remove NA
x, y = remove_na(x, y, paired=True)
n = x.size
# Compute correlation coefficent for sin and cos independently
rxs = pearsonr(y, np.sin(x))[0]
rxc = pearsonr(y, np.cos(x))[0]
rcs = pearsonr(np.sin(x), np.cos(x))[0]
# Compute angular-linear correlation (equ. 27.47)
r = np.sqrt((rxc**2 + rxs**2 - 2 * rxc * rxs * rcs) / (1 - rcs**2))
# Compute p-value
pval = chi2.sf(n * r**2, 2)
pval = pval / 2 if tail == 'one-sided' else pval
return np.round(r, 3), pval
>>> np.median(np.array(x) - np.array(y))
-1.5
The median is negative, so Pingouin will test for the alternative
hypothesis that the median of the differences is negative (= less than 0).
>>> pg.wilcoxon(x, y, tail='one-sided') # Equivalent to tail = 'less'
W-val tail p-val RBC CLES
Wilcoxon 20.5 less 0.142883 -0.379 0.583
"""
x = np.asarray(x)
y = np.asarray(y)
# Remove NA
x, y = remove_na(x, y, paired=True)
# Check tails
possible_tails = ['two-sided', 'one-sided', 'greater', 'less']
assert tail in possible_tails, 'Invalid tail argument.'
if tail == 'one-sided':
# Detect the direction of the test based on the median
tail = 'less' if np.median(x - y) < 0 else 'greater'
# Compute test
wval, pval = scipy.stats.wilcoxon(x, y, zero_method='wilcox',
correction=True, alternative=tail)
# Effect size 1: common language effect size (McGraw and Wong 1992)
diff = x[:, None] - y
cles = max((diff < 0).sum(), (diff > 0).sum()) / diff.size
MWU 97.0 less 0.00278 0.515 0.758
Or simply leave it to Pingouin, using the `'one-sided'` argument, in which
case Pingouin will compare the medians of ``x`` and ``y`` and select the
most appropriate tail based on that:
>>> # Since np.median(x) < np.median(y), this is equivalent to tail='less'
>>> pg.mwu(x, y, tail='one-sided')
U-val tail p-val RBC CLES
MWU 97.0 less 0.00278 0.515 0.758
"""
x = np.asarray(x)
y = np.asarray(y)
# Remove NA
x, y = remove_na(x, y, paired=False)
# Check tails
possible_tails = ['two-sided', 'one-sided', 'greater', 'less']
assert tail in possible_tails, 'Invalid tail argument.'
if tail == 'one-sided':
# Detect the direction of the test based on the median
tail = 'less' if np.median(x) < np.median(y) else 'greater'
uval, pval = scipy.stats.mannwhitneyu(x, y, use_continuity=True,
alternative=tail)
# Effect size 1: common language effect size (McGraw and Wong 1992)
diff = x[:, None] - y
cles = max((diff < 0).sum(), (diff > 0).sum()) / diff.size
# Effect size 2: rank biserial correlation (Wendt 1972)