## Binomial Random Numbers Generation in R

We will learn how to generate Bernoulli or Binomial Random Numbers (Binomial distribution) in R with the example of a flip of a coin. This tutorial is based on how to generate random numbers according to different statistical probability distributions in R. Our focus is on binomial random numbers generation in R.

### Binomial Random Numbers in R

We know that in Bernoulli distribution, either something will happen or not such as a coin flip has two outcomes head or tail (either head will occur or head will not occur i.e. tail will occur). For an unbiased coin, there will be a 50% chance that the head or tail will occur in the long run. To generate a random number that is binomial in R, use the rbinom(n, size, prob) command.

rbinom(n, size, prob) #command has three parameters, namey

where
‘$n$’ is the number of observations
‘$size$’ is the number of trials (it may be zero or more)
‘$prob$’ is the probability of success on each trial for example 1/2

### Examples of Generation Binomial Random Numbers

• One coin is tossed 10 times with a probability of success=0.5
the coin will be fair (unbiased coin as p=1/2)
rbinom(n=10, size=1, prob=1/2)
OUTPUT: 1 1 0 0 1 1 1 1 0 1
• Two coins are tossed 10 times with a probability of success=0.5
• rbinom(n=10, size=2, prob=1/2)
OUTPUT: 2 1 2 1 2 0 1 0 0 1
• One coin is tossed one hundred thousand times with a probability of success=0.5
rbinom(n=100,000, size=1, prob=1/2)
• store simulation results in $x$ vector
x <- rbinom(n=100000, size=5, prob=1/2)
count 1’s in x vector
sum(x)
find the frequency distribution
table(x)
creates a frequency distribution table with frequency
t = (table(x)/n *100)
plot frequency distribution table
plot(table(x),ylab = "Probability",main = "size=5,prob=0.5")

View the Video tutorial on rbinom command

## Probability Distributions in R: A Comprehensive Tutorial

The article is a discussion about Probability Distributions in R Language.

We often make probabilistic statements when working with statistical Probability Distributions. We want to know four things:

• The density (PDF) at a particular value,
• The distribution (CDF) at a particular probability,
• The quantile value corresponding to a particular probability, and
• A random draw of values from a particular distribution.

### Probability Distributions in R Language

R language has plenty of functions for obtaining density, distribution, quantile, and random numbers and variables.

Consider a random variable $X$ which is $N(\mu = 2, \sigma^2 = 16)$. We want to:

1) Calculate the value of PDF at $x=3$ (that is, the height of the curve at $x=3$)

dnorm(x = 3, mean = 2, sd = sqrt(16) )

dnorm(x = 3, mean = 2, sd = 4)
dnorm(x = 3, 2, 4)

2) Calculate the value of the CDF at $x=3$ (that is, $P(X\le 3)$)

pnorm(q = 3, m = 2, sd = 4)

3) Calculate the quantile for probability 0.975

qnorm(p = 0.975, m = 2, sd = 4)

4) Generate a random sample of size $n = 10$

rnorm(n = 10, m = 2, sd = 5)

There are many probability distributions available in the R Language. I will list only a few.

Observe that a prefix (d, q, p, and r) is added for each distribution.

### Drawing the Density Function

The density function dnorm() can be used to draw a graph of normal (or any distribution). Let us compare two normal distributions both with mean = 20, one with sd = 6, and the other with sd = 3.

For this purpose, we need $x$-axis values, such as $\overline{x} \pm 3SD \Rightarrow 20 + \pm 3\times 6$.

xaxis <- seq(0, 40, 0.5)
y1 <- dnorm(xaxis, 20, 6)
y2 <- dnorm(xaxis, 20, 3)

plot(xaxis, y2, type = "l", main = "comparing two normal distributions", col = "blue")

points(xaxis, y1, type="l", col = "red")

### Finding Probabilities in R

Probabilities in R language can be computed using pnorm() function for normal distribution.

#Left Tailed Probability
pnorm(1.96)

#Area between two Z-scores
pnorm(1.96) - pnorm(-1.96)

Finding Right-Tailed Probabilities

1 - pnorm(1.96)

Solving Real Problem

Suppose, you took a standardized test that has a mean of 500 and a standard deviation of 100. You took 720 marks (score). You are interested in the approximate percentile on this test.

To solve this problem, you have to find the Z-score of 720 and then use the pnorm( ) to find the percentile of your score.

zscore <- scale(x = 720,  500,  100)

pnorm(2.2)
pnorm(zscore[1,1])
pnorm(zscore[1])
pnorm(zscore[1, ])

MCQs in Statistics