Question

Hi,I saw someone calculate venn diagram overlap p-value as in the following example. They use hypergeometric distribution and R. When I apply their function in R, I just cannot get the same results. Can anyone help me about this?

The sample I saw in someone else's publication:

From 15220 genes, set A is 1850+195 genes, set B is 195+596 genes, overlap is 195 genes. Their p value is 2e-26.

Their method is: given a total of N genes, if gene sets A and B contain m and n genes, respectively, and k of them are in common, then the p-value of enrichment is calculated by:

p = Σ (m,i)(N-m,n-i)/(N,n)

for i from k to min(m,n), where "(m,i)" represent a binomial form.

And the way I'm using R is:

sum(choose(596+195,195:(195+596))*choose(15220-596-195,(1850+195)-195:(195+596)))/choose(15220,1850+195).

I got NaN.

Or using: phyper(195,1850+195,15220-1850-195,596+195), I got 1.

I also refer from link http://www.pangloss.com/wiki/VennSignificance but when I caculate

1 - phyper(448,1000,13800,2872) in R, I got 0 instead 1.906314e-81 of the link.

I am completely new to R and statistic, sorry for post many mistakes here.

Was it helpful?

Solution

Using package gmp, and replacing choose with chooseZ, we can implement you p-value as:

require(gmp)

enrich_pvalue <- function(N, A, B, k)
{
    m <- A + k
    n <- B + k
    i <- k:min(m,n)

    as.numeric( sum(chooseZ(m,i)*chooseZ(N-m,n-i))/chooseZ(N,n) )
}

Result:

> enrich_pvalue(15220, 1850, 596, 195)
[1] 1.91221e-18

Using the example in your pangloss link (with your notation), we get:

> enrich_pvalue(N=14800, A=1000-448, B=2872-448, k=448)
[1] 7.289388e-81
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top