5.7 The rbinom function

The basis of our simulation is R’s rbinom function, which allows us to sample from a binomial distribution. rbinom takes three arguments:

  • n: how many times we’re drawing from the distribution
  • size: the size of the population we’re sampling from (i.e. N)
  • p: the success probability (i.e. allele frequency)

Every generation, we’ll draw once to produce the number of individuals carrying the A allele in the next generation.

Let’s once again look at a population of size 100, and an A allele currently at AF = 0.5. We use rbinom to get the number of individuals in the next generation who will have A:

rbinom(n = 1, size = 100, prob = 0.5)
## [1] 45

Change the rbinom code so that it returns the allele frequency (instead of the number of individuals).
# divide by the population size to get AF
rbinom(n = 1, size = 100, prob = 0.5) / 100
## [1] 0.51


Why do we get a different number every time we run rbinom?

rbinom generates a random number between 0 and 100. Because it’s random, the number it draws will be different every time we run it.