Chapter 3 page 114
# Testing the outcome of heads or tails
outcomes <- c("heads", "tails")
sample(outcomes, size = 1, replace = TRUE)
## [1] "heads"
# Testing outcome of heads or tails at 100 tosses
sim_fair_coin <- sample(outcomes, size = 100, replace = TRUE)
sim_fair_coin
## [1] "heads" "tails" "tails" "heads" "heads" "tails" "tails" "tails" "heads"
## [10] "tails" "tails" "tails" "heads" "tails" "heads" "tails" "heads" "heads"
## [19] "tails" "tails" "tails" "heads" "tails" "tails" "heads" "heads" "tails"
## [28] "heads" "tails" "heads" "heads" "tails" "heads" "heads" "heads" "heads"
## [37] "heads" "heads" "heads" "tails" "tails" "tails" "heads" "heads" "heads"
## [46] "tails" "heads" "heads" "tails" "tails" "heads" "tails" "tails" "tails"
## [55] "heads" "heads" "heads" "heads" "heads" "tails" "heads" "heads" "heads"
## [64] "heads" "tails" "tails" "heads" "heads" "heads" "heads" "tails" "heads"
## [73] "tails" "heads" "heads" "tails" "heads" "heads" "tails" "tails" "tails"
## [82] "heads" "tails" "heads" "heads" "tails" "heads" "tails" "tails" "heads"
## [91] "heads" "heads" "tails" "tails" "heads" "heads" "heads" "tails" "heads"
## [100] "tails"
# Total outcome of heads or tails at 100 tosses
table(sim_fair_coin)
## sim_fair_coin
## heads tails
## 56 44
# 100 tosses of a fair coin, the number of heads that turns up lies between 35 and 65
pbinom(35, size = 100, prob = 0.5) - pbinom(65, size = 100, prob = 0.5)
## [1] -0.9973462
# 100 tosses of a fair coin, the number of heads that turns up lies between 40 and 60
pbinom(40, size = 100, prob = 0.5) - pbinom(60, size = 100, prob = 0.5)
## [1] -0.9539559
# 100 tosses of a fair coin, the number of heads that turns up lies between 45 and 55
pbinom(45, size = 100, prob = 0.5) - pbinom(55, size = 100, prob = 0.5)
## [1] -0.6802727
Reference: https://rc2e.com/index.html#the-recipes