-
Notifications
You must be signed in to change notification settings - Fork 71
Added Spearman p value #125
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,12 @@ module Statistics.Correlation | |
|
||
import qualified Data.Vector.Generic as G | ||
import qualified Data.Vector.Unboxed as U | ||
import Statistics.Distribution | ||
import Statistics.Distribution.StudentT | ||
import Statistics.Matrix | ||
import Statistics.Sample | ||
import Statistics.Test.Internal (rankUnsorted) | ||
import Statistics.Types (mkPValue, PValue) | ||
|
||
|
||
---------------------------------------------------------------- | ||
|
@@ -26,15 +29,20 @@ import Statistics.Test.Internal (rankUnsorted) | |
-- | Pearson correlation for sample of pairs. Exactly same as | ||
-- 'Statistics.Sample.correlation' | ||
pearson :: (G.Vector v (Double, Double), G.Vector v Double) | ||
=> v (Double, Double) -> Double | ||
pearson = correlation | ||
=> v (Double, Double) -> (Double, PValue Double) | ||
pearson xy = (coeff, p) | ||
where | ||
coeff = correlation xy | ||
n = fromIntegral . G.length $ xy | ||
stat = coeff * ((sqrt (n - 2)) / (1 - (coeff ** 2))) | ||
p = mkPValue $ 2 * (complCumulative (studentT (n - 2)) . abs $ stat) | ||
{-# INLINE pearson #-} | ||
|
||
-- | Compute pairwise pearson correlation between rows of a matrix | ||
pearsonMatByRow :: Matrix -> Matrix | ||
pearsonMatByRow m | ||
= generateSym (rows m) | ||
(\i j -> pearson $ row m i `U.zip` row m j) | ||
(\i j -> fst . pearson $ row m i `U.zip` row m j) | ||
{-# INLINE pearsonMatByRow #-} | ||
|
||
|
||
|
@@ -43,7 +51,8 @@ pearsonMatByRow m | |
-- Spearman | ||
---------------------------------------------------------------- | ||
|
||
-- | compute spearman correlation between two samples | ||
-- | Compute spearman correlation between two samples with p value. P value is | ||
-- calculated using Student's /t/ distribution with /n - 2/ degrees of freedom | ||
spearman :: ( Ord a | ||
, Ord b | ||
, G.Vector v a | ||
|
@@ -56,7 +65,7 @@ spearman :: ( Ord a | |
, G.Vector v (Int, b) | ||
) | ||
=> v (a, b) | ||
-> Double | ||
-> (Double, PValue Double) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's especially important for Spearman correlation. What is meaning of p-value here? Is it described anywhere? I'm not sure that student's distribution will arise for ranks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wikipedia sources the following for this test: Press; Vettering; Teukolsky; Flannery (1992). Numerical Recipes in C: The Art of Scientific Computing (2nd ed.). p. 640. The equation is 14.6.2. Whether this approximation is optimal, I do not know, but I'm sure there are better methods out there for p-values for Spearman's correlation coefficient, but I used the Student's t distribution as a simple solution. |
||
spearman xy | ||
= pearson | ||
$ G.zip (rankUnsorted x) (rankUnsorted y) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should probably document the return value? that's not clear to an amateur like me at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's absolutely necessary to document meaning of p-value and what hypothesis is being tested.