# Poisson Probability Density Function
# P(X = 4, lambda = 3)
plot(dpois(0:20, 3))
dpois(x = 4, lambda = 3)
dpois(4, 3)
# Poisson Cumulative Distribution Function
# P(X <= 4, lambda = 3)
ppois(4, 3)
# Poisson Cumulative Quantile Function
# Choose k so that P(X <= k) >= .95
qpois(.95, lambda = 3)
qpois(.95, 3)
# You perform RNA-seq deep sequencing of an experimental and a control tissue.
# You obtain fold enrichment values of genes in the experimental sample over control.
# The reference genome has ~15,000 genes.
# 3,000 out of 15,000 genes are enriched above a certain cut-off in your sample of interest compared to control.
# In a ChIP-chip experiment, 400 genes are enriched by ChIP-chip.
# Of the 400 ChIP-chip genes, 100 genes are in the group of 3,000 enriched RNA-Seq transcripts.
# What is the probability that my 100 ChIP-chip genes would be enriched by RNA-Seq by chance alone?
# The p-value you want is the probability of getting 100 or more white balls in a sample of size 400 from an urn
# with 3000 white balls and 12000 black balls. Here are four ways to calculate it.
plot(dhyper(0:149, 3000, 12000, 400))
sum(dhyper(100:400, 3000, 12000, 400))
1 - sum(dhyper(0:99, 3000, 12000, 400))
phyper(99, 3000, 12000, 400, lower.tail=FALSE)
1-phyper(99, 3000, 12000, 400)