5.4 The sample function

We have now carried out our first Monte Carlo experiment!! We will carry out others in the rest of this chapter and interpret their results. There is one function, the sample function which we will often use.

In the previous chapter we discussed how to simulate numbers distributed according to a specific random variable. One possible class of numbers we may want to simulate in some situations are integers. Suppose for example you want to simulate a game of dice: then we must be able to simulate in R one number from the set \(\{1,2,3,4,5,6\}\) where each has the same probability of appearing. We have not introduced yet a function that does this.

For this specific purpose there is the function sample. This takes four inputs:

  1. x: a vector of values we want to sample from.

  2. size: the size of the sample we want.

  3. replace: if TRUE sampling is done with replacement. That is if a value has been selected, it can be selected again. By default equal to FALSE

  4. prob: a vector of the same length of x giving the probabilities that the elements of x are selected. By defaul equal to a uniform probability.

So for instance if we wanted to simulate ten tosses of a fair dice we can write.

set.seed(2021)
sample(1:6, size = 10, replace = TRUE)
##  [1] 6 6 2 4 4 6 6 3 6 6

Notice that the vector x does not necessarily needs to be numeric. It could be a vector of characters. For instance, let’s simulate the toss of 5 coins, where the probability of heads is 2/3 and the probability of tails is 1/3.

set.seed(2021)
sample(c("heads","tails"), size = 5, replace = TRUE, prob = c(2/3,1/3))
## [1] "heads" "tails" "tails" "heads" "heads"