def calc_spearman_t(r,n,tails='two-tailed',eps=1e-10): """Calculate the t value, and probability for Spearman correlation Equation from Wikipedia article on Spearman rank correlation: http://en.wikipedia.org/wiki/Spearman's_rank_correlation_coefficient TODO: verify with additional / published sources. TODO: want to find the correct handling for the case of perfect correlation """ #Right now I subtract an arbitrarily tiny #epsilon if r is exactly 1.0 to prevent divide-by-zero #errors. if r == 1.0: r = r-eps t = r*(((n-2)/(1.0-r**2))**0.5) if tails == 'two-tailed': prob = tprob(t,n-2) elif tails == 'high': prob = t_high(t,n-2) elif tails == 'low': prob = t_low(t,n-2) else: raise RuntimeError("Valid prob. methods are 'two-tailed','high',and 'low'") return prob
def calc_spearman_t(r, n, tails='two-tailed', eps=1e-10): """Calculate the t value, and probability for Spearman correlation Equation from Wikipedia article on Spearman rank correlation: http://en.wikipedia.org/wiki/Spearman's_rank_correlation_coefficient TODO: verify with additional / published sources. TODO: want to find the correct handling for the case of perfect correlation """ #Right now I subtract an arbitrarily tiny #epsilon if r is exactly 1.0 to prevent divide-by-zero #errors. if r == 1.0: r = r - eps t = r * (((n - 2) / (1.0 - r**2))**0.5) if tails == 'two-tailed': prob = tprob(t, n - 2) elif tails == 'high': prob = t_high(t, n - 2) elif tails == 'low': prob = t_low(t, n - 2) else: raise RuntimeError( "Valid prob. methods are 'two-tailed','high',and 'low'") return prob
def t_tailed_prob(t, df, tails): """Return appropriate p-value for given t and df, depending on tails.""" if tails == 'high': return t_high(t, df) elif tails == 'low': return t_low(t, df) else: return tprob(t,df)
def test_t_high(self): """t_high should match R's pt(lower.tail=FALSE) function""" negprobs = { 1: [ 0.5000000, 0.5031830, 0.5317255, 0.6475836, 0.7500000, 0.8524164, 0.9371670, 0.9682745, 0.9840977, 0.9893936, 0.9936347, 0.9984085, ], 10: [ 0.5000000, 0.5038910, 0.5388396, 0.6860532, 0.8295534, 0.9633060, 0.9997313, 0.9999992, 1.0000000, 1.0000000, 1.0000000, 1.0000000, ], 100:[ 0.5000000, 0.5039794, 0.5397277, 0.6909132, 0.8401379, 0.9758939, 0.9999988, 1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000, ], } probs = { 1: [ 0.500000000, 0.496817007, 0.468274483, 0.352416382, 0.250000000, 0.147583618, 0.062832958, 0.031725517, 0.015902251, 0.010606402, 0.006365349, 0.001591536, ], 10: [ 5.000000e-01, 4.961090e-01, 4.611604e-01, 3.139468e-01, 1.704466e-01, 3.669402e-02, 2.686668e-04, 7.947766e-07, 1.073031e-09, 1.980896e-11, 1.237155e-13, 1.200254e-19, ], 100:[ 5.000000e-01, 4.960206e-01, 4.602723e-01, 3.090868e-01, 1.598621e-01, 2.410609e-02, 1.225087e-06, 4.950844e-17, 4.997134e-37, 4.190166e-52, 7.236082e-73, 2.774197e-132, ], } for df in self.df: for x, p in zip(self.values, probs[df]): self.assertFloatEqualRel(t_high(x, df), p, eps=1e-4) for x, p in zip(self.negvalues, negprobs[df]): self.assertFloatEqualRel(t_high(x, df), p, eps=1e-4)