Simulating Random Variables for Individual Events

Flipping Coins

# Generate reproducible numbers
set.seed(1)
# Create a sample, wherein a binomial outcome (head, tail) is simulated. We repeat this process 10,000 times
coins <- sample(c("head","tail"), size = 10000, replace = TRUE)
# Results
table(coins)
## coins
## head tail 
## 4984 5016
# Barplot
barplot(table(coins), ylab = "Counts")

Rolling Dice

set.seed(1)
# Create a sample, wherein six outcomes (1,2,3,4,5,6) have equal probability. We repeat this process 10,000 times
dice <- sample(c(1:6), size = 10000, replace = TRUE)
# Results
table(dice)
## dice
##    1    2    3    4    5    6 
## 1686 1622 1605 1701 1727 1659
# Barplot
barplot(table(dice), ylab = "Counts")

Outcome with Unequal Probabilities

set.seed(1)
# Create a sample, wherein four outcomes (sunny, rainy, foggy, windy) have unequal probability. We repeat this process 10,000 times
weather <- sample(c("Sunny", "Rainy", "Foggy", "Windy"), size = 10000, replace = TRUE, prob = c(.4, .15, .1, .35))
# Results
table(weather)
## weather
## Foggy Rainy Sunny Windy 
##  1044  1533  4031  3392
# Barplot
barplot(table(weather), ylab = "Counts")

Discrete Distribution Probability

Binomial Distribution

rbinom is the R function that generates binomial distribution
help(rbinom)
# This represents the process of taking 15 samples, each with 1 trial, where the probability of success in each trial is 0.8, and the outcomes are the number of successes in each sample:
rbinom(15, size = 1, prob = .8)
##  [1] 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1
# Plot:
barplot(table(rbinom(15, size = 1, prob = .8)), ylab = "Counts")

dbinom is the R function that calculates the probability function of binomial distribution
Question: Suppose widgits produced at Acme Widgit Works have probability 0.005 of being defective. Suppose widgits are shipped in cartons containing 25 widgits. What is the probability that a randomly chosen carton contains exactly one defective widgit?
Question Rephrased: What is P(X = 1) when X has the Bin(25, 0.005) distribution?
dbinom(1, size = 25, prob = .005)
## [1] 0.1108317
Question: Suppose widgits produced at Acme Widgit Works have probability 0.005 of being defective. Suppose widgits are shipped in cartons containing 25 widgits. What is the probability that a randomly chosen carton contains no more than one defective widgit?
Question Rephrased: What is P(X <= 1) when X has the Bin(25, 0.005) distribution?
pbinom(1, size = 25, prob = .005)
## [1] 0.9930519

Poisson Distribution

help(rpois)
rpois(15, lambda = 20)
##  [1] 18 16 20 17 21 26 26 22 18 13 23 27 17 11 20
Question: Data from the maternity ward in a certain hospital shows that there is a historical average of 4.5 babies born in this hospital every day. What is the probability that 6 babies will be born in this hospital tomorrow?
dpois(6, lambda = 4.5)
## [1] 0.1281201
Question: Data from the maternity ward in a certain hospital shows that there is a historical average of 4.5 babies born in this hospital every day. What is the probability that 6 or less babies will be born in this hospital tomorrow?
ppois(6, lambda = 4.5)
## [1] 0.8310506