Andrew Ellis
12/09/2013
use the sample() function to sample from a vector of ones (heads) and zeros (tails).
?sample
sample.space <- c(0,1)
theta <- 0.5 # this is a fair coin
N <- 20 # we want to flip a coin 20 times
flips <- sample(sample.space,
size = N,
replace = TRUE,
prob = c(theta, 1 - theta))
flips
[1] 0 0 1 1 1 1 0 0 0 1 0 0 1 0 0 1 0 0 1 1
use the function rbinom() to draw a number from a Bernoulli distribution:
?rbinom
theta <- 0.5 # this is a fair coin
N <- 20
flips <- rbinom(n = N,
size = 1,
prob = theta)
flips
[1] 0 1 0 0 1 0 0 1 1 1 0 1 0 0 1 1 1 1 1 0
use the function rbinom() to draw numbers from a binomial distribution:
theta <- 0.5 # this is a fair coin
N <- 20
flips <- rbinom(n = 1,
size = N,
prob = theta)
flips
[1] 10