InfluentialPoints.com
Biology, images, analysis, design...
 Use/Abuse Principles How To Related
"It has long been an axiom of mine that the little things are infinitely the most important" (Sherlock Holmes)

## Binomial and Poisson distribution functions using R

### Binomial mass probability function

R's dbinom function calculates the proportion of samples (each comprising n observations) expected to contain a given number of 'successes' (y) - assuming those samples are randomly selected from an infinite population, of which a known proportion (P) equal one, and the remainder equal zero.
Or, equivalently, this function gives the probability of obtaining f 'successes' in a sample of n successes & failures, where P is the probability a randomly selected observation will be a success.

For example:

Gave:

 [1] 0.05739563

Note:
• Because we have not told R to put the results into variables, R has merely printed them out.

• The frequency and sample size must be whole-numbers and cannot be negative - and P cannot be less than zero, or greater than one.

• For consistency with its other distribution functions (such as the normal ones) this mass probability function is called dbinom rather than mbinom.

• If y contained a set of binary values, coded as ones & zeroes (or as TRUE & FALSE) the following code gives the same result as that above.

• If however, y contains a series of observed frequencies, (but the same sample size) the following code would give the probability of observing each frequency.

• The following code gives the probability for each possible outcome (in ascending order, from 0 to n) as a bargraph.

### Cumulative binomial probability function

Whereas R's dbinom function yields the binomial probability of observing a frequency (f) in a sample of n values, the pbinom function returns the probability of observing f values or less.

For example:

Gave:

 [1] 0.6496107

Note:
• Being a cumulative probability, the instruction pbinom(3, 10, .3) produces the same result as sum(dbinom(0:3, 10, .3))

• Very often you want to estimate the probability of observing each of a series of frequencies. For instance, pbinom(c(0,1,2,3,4), 4, .3) would give the probability of observing every possible frequency of ones in a sample of (n=) 4 binary observations, assuming a 0.3 probability of selecting a one. It is equivalent to using cumsum(dbinom(0:4,4,0.3))

• Of course, pbinom(0, n, P) always yields the same result as dbinom(0, n, P), irrespective of n or P, and pbinom(n, n, P) should always yield 1.

### Inverse binomial probability function

R's qbinom function does the opposite to its cumulative binomial function, pbinom. In other words, whereas pbinom yields the probability (p) of observing a binomial quantile less than equal to the given value, qbinom gives the quantile at or below which a given proportion of that binomial population lies.

For example:

Gave:

 [1] 2

Note:
• Since frequencies are discrete whole-number values, and any cumulative binomial distribution is therefore a 'step function', different values of p do not necessarily yield different results. For instance both qbinom(.4,10,.3) and qbinom(.6,10,.3) yield 3.

• For the same reason, unlike normally distributed quantiles the proportion of binomially distributed frequencies which equal or exceed a given quantile (f) is not 1 - qbinom(f, n, .P) but is 1 - qbinom(f,n,.P) + dbinom(f,n,.P)

### Random binomial function

The rbinom function returns one or more frequencies, randomly selected from an infinite binomially-distributed population of frequencies, of a specified sample size and probability of obtaining a 'success' (P).

For example, to select one frequency at random:

Gave us:

 [1] 3

Note:
• This frequency being randomly selected, repeating these instructions is liable to yield a different outcome. Given P=0.5 the result of executing the above instructions is equivalent to tossing an unbiased coin 5 times, and recording the observed number of 'heads'.

• If you require a number of such random frequencies, for instance (N=) 50, you could enter rbinom(50, 5, 0.5)

• This sort of instruction is useful in simulation models. It is equivalent to supplying a set of uniformly-distributed random numbers, between 0 and 1, to the inverse cumulative function.
For example as qbinom(runif(10), 5, 0.5)

• If you obtain many such random frequencies (say N=100000) their distribution can be expected to approach the predicted (binomial) frequency distribution.

### Poisson mass probability function

R's dpois function returns the probability of observing f 'successes', as predicted by the Poisson model, given a known expected mean frequency (F).

For example:

Gave:

 [1] 1.500157e-05

Note:
• When needed, R prints out, and will accept numbers in exponential format.
For example 10000*1e-05 yields 0.1, and 10000000000 yields 1e+10.
In other words, in this context, e is an exponent to the base ten. So this e has nothing to do with the base of natural logs, 2.71828 - which is the exponent used to obtain the antilog of natural logs, where x = exp[ln(x)] = eln[x]

• For graphical purposes it may be useful to give the probability of obtaining a sample containing 0, 1, 2, 3, or 4 ones.

For example:

• Since the Poisson model assumes the (binomial) sample is extremely large, the expected frequency (F) is not the proportion of ones in the population being sampled.

### Cumulative Poisson probability function

ppois returns the probability of observing a frequency of f or fewer 'successes', as predicted by the Poisson model given a specified mean success rate (F).

For example:

Gave:

 [1] 0.916082

Note:
• This instruction produces the same result as adding up the probability of observing a frequency of f or less.
• If p is the probability of observing f or fewer successes, the probability of obtaining a frequency of greater than f is 1-p.
If you need to find the probability of observing f or more, given you expect F, use 1 - ppois(f, F) + dpois(f, F)

### Inverse Poisson probability function

R's inverse (quantile) Poisson function does the opposite to the cumulative Poisson function.

For example:

Gave:

 [1] 5

Note:
• Like the other distribution functions on this page, the inverse Poisson function can return the quantiles corresponding to a set of (lower-tailed) probabilities and, if need be, a matching set of expected frequencies - neither of which have to be given in ascending order.

### Random Poisson function

R's rpois function enables you to obtain a set of (N) frequencies, randomly selected, from a single Poisson distribution (of expected frequency, F).

For example:

Gave:

 [1] 14 8 8 7 11

Note:
• rpois(N, F) is equivalent to these instructions:
p=runif(N) qpois(p, F)
Where p has a random uniform distribution.