Generating binomial random variates
To model the success or failure of several independent trials, one can generate samples from binomial distribution. In this recipe, we will discuss how to generate binomial random variates with R.
Getting ready
In this recipe, you need to prepare your environment with R installed.
How to do it…
Please perform the following steps to create a binomial distribution:
First, we can use
rbinom
to determine the frequency of drawing a six by rolling a dice 10 times:> set.seed(123) > rbinom(1, 10, 1/6) [1] 1
Next, we can simulate 100 gamblers rolling a dice 10 times, and observe how many times a six is drawn by each gambler:
> set.seed(123) > sim <- rbinom(100,10,1/6) > table(sim) sim 0 1 2 3 4 5 17 36 23 18 4 2
Additionally, we can simulate 1,000 people tossing a coin 10 times, and compute the number of heads at each tossing:
> set.seed(123) > sim2 <- rbinom(1000,10,1/2) > table(sim2) sim2 0 1 2 3 4 5 6 7...