Conducting a Simulation
For example, say we want to simulate the probability of getting “heads” exactly 4 times in 10 flips of a fair coin.
One way to generate a flip of the coin is to create a vector in R with all of the possible outcomes and then randomly select one of those outcomes. The sample function takes a vector of elements (in this case heads or tails) and chooses a random sample of size elements.
coin <- c("heads","tails") sample(coin, size = 1, replace = TRUE)
We can do this with or without replacement. Since we are interested in the number of heads in 10 flips of a coin, we need to do this with replacement. Recall that sampling with replacement means that if I get some outcome (e.g., “tails”) I can get that outcome again on a subsequent trial (coin flip).
sample(coin, size = 10, replace = TRUE)
Another approach that will work for any simulation is as follows. In order to conduct a simulation, we need to (1) describe all possible outcomes, (2) connect these outcomes to a random variable(s), (3) choose a source of random numbers, (4) generate a number and note the outcome, (5) repeat step 4 until the generated numbers show a stable pattern, (6) analyze the simulated outcomes.
In the coin flip example, (1) there are two possible outcomes: heads or tails. (2) We are interested in heads, so we will let a 1 represent “heads” and a 0 represent “tails”. (3) For a source of random numbers, we will use R to generate draws from the appropriate distribution. Since there are exactly two outcomes and “heads” (or 1) occurs with 50% probability, we know that we are working with a bernoulli distribution. For repeated flips of the coin, this is a binomial distribution. Finally, (4) we can generate a single flip of the coin using
rbinom(n=1, size=1, prob=0.5)
where n is the number of observations (or experimental repetitions) we want to generate and size is the number of trials in our binomial experiment (see ?rbinom for more information). To generate the number of 1s (heads) in 10 flips of the coin (1 experimental repetition), we write
rbinom(n=1, size=10, prob=0.5)
Note: if we wanted to generate 10 flips and count the number of 1s ourselves, we could write
rbinom(n=10, size=1, prob=0.5)
If in doubt, test with a small number like 5 or 10!
Our goal is to simulate the probability of getting “heads” exactly 4 times in 10 flips of a fair coin. In (1), you asked R to flip a coin 10 times and record the number of heads… and then to repeat this process 1000 times. In order to count the number of times there were exactly 4 heads, we need to figure out how often nheads = 4.
Internally, R stores TRUE and FALSE as 1 and 0, respectively. This means that we can tally up the number of times that something happens by asking if it happens and then summing over all of the TRUE/FALSE values.
sum(nheads == 4)
Using RStudio
R code with comments
---
#set a random seed
set.seed(123)
#Generate the number of heads in 10 flips of the coin
#for 1000 experimental repetitions. Store these values as
nheads.
nheads<- rbinom(n=1000, size=10, prob=0.5)
# count the number of times there were exactly 4 heads
count<- sum(nheads==4)
#the probability that exactly 4 in 10 coin flips result in
heads.
prob<- count/1000
sprintf('the estimated probability that exactly 4 in 10 coin flips
result in heads is %.4f',prob)
---
get this
Get Answers For Free
Most questions answered within 1 hours.