Binomial Distribution in R-Quick Guide

Binomial Distribution in R, Binomial distribution was invented by  James Bernoulli which was posthumously published in 1713.

Let n ( finite) Bernoulli trials be conducted with probability “p” of success and “q” of a failure.

The probability of x success out of n Bernoulli trials is given by

f(x)=(ncx)pxqn-x

where x=0, 1 , 2, …..,n. 0<=p<=1 and p+q=1

Important Features

1) If n=1, the binomial distribution reduces to Bernoulli distribution.

2) Binomial distribution has two parameters n and p.

3) The mean of the binomial distribution is np.

4) The variance of a binomial distribution is npq.

5) The moment generating function of a binomial distribution is (q+pet)n.

6) The characteristic function of b (n, p) is (q+peit)n

Naive Bayes Classification in R » Prediction Model »

Binomial Distribution in R

Let’s see how to plot binomial distribution in R.

First need to create a probability mass function while using dbinom(x, size, prob)

plot(x, y, type = ‘h’) to plot the probability mass function.

As discussed earlier we need to mention the number of trials and probability of success on a given trial in the dbinom() function.

Let’s take an example, binomial distribution with size = 50 and prob = 0.45,

success <- 0:50
plot(success, dbinom(success, size=50, prob=.45),type='h')

The x-axis indicates the number of successes and the y-axis displays the probability of obtaining that number of successes in 50 trials.

Principal component analysis (PCA) in R »

Let’s change the title and axis labels for better visualization.

success <- 0:50
plot(success,dbinom(success,size=50,prob=.45),
     type='h',
     main='Binomial Distribution (n=50, p=0.45)',
     ylab='Probability',
     xlab ='Successes',
     lwd=3)

For actual probabilities, you can make use of the below code.

options(scipen=999)
success <- 0:50
dbinom(success, size=50, prob=.45)
[1] 0.000000000000104264022198 0.000000000004265346362643 0.000000000085500806632979 0.000000001119283286831728
[5] 0.000000010760382507495824 0.000000080996333783695804 0.000000497022957309043696 0.000002556118066160793185
[9] 0.000011241110131866213424 0.000042920602321670976069 0.000143979111424515039339 0.000428367604238223827727
[13] 0.001139068402178920400014 0.002724205549267060596369 0.005890652259129421607076 0.011567098981563259108007
[17] 0.020702478290865976989776 0.033876782657780636631717 0.050815173986670875150296 0.070022823484025109586071
[21] 0.088801671600195419831181 0.103794161610618038138476 0.111943290001534315192266 0.111500826404690306370426
[25] 0.102631442486135343594711 0.087330027424566109006676 0.068703692903941820935287 0.049966322111957714446895
[29] 0.033581261938880557771370 0.020843541893098262163253 0.011937664902410870595983 0.006301406693354699890819
[33] 0.003061194728874021189075 0.001366153019497504247579 0.000558880780703519723998 0.000209035928367031056995
[37] 0.000071262248306942535775 0.000022061531416891290479 0.000006175117645397758965 0.000001554575071568665523
[41] 0.000000349779391102952633 0.000000069800765408571026 0.000000012237796532671604 0.000000001862835835840054
[45] 0.000000000242476565408934 0.000000000026451988953702 0.000000000002352449610507 0.000000000000163806936128
[49] 0.000000000000008376491052 0.000000000000000279734395 0.000000000000000004577472

Adding text labels to ggplot2 Bar Chart »

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen − twelve =