Skip to content

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

Open
wants to merge 4 commits into
base: master
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
19 changes: 14 additions & 5 deletions Statistics/Correlation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)


----------------------------------------------------------------
Expand All @@ -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)

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.

Copy link
Collaborator

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.

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 #-}


Expand All @@ -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
Expand All @@ -56,7 +65,7 @@ spearman :: ( Ord a
, G.Vector v (Int, b)
)
=> v (a, b)
-> Double
-> (Double, PValue Double)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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)
Expand Down