1 Let’s toss a coin

To illustrate the concepts behind object-oriented programming in R, we are going to consider a classic chance process (or chance experiment) of flipping a coin.

In this chapter you will learn how to implement code in R that simulates tossing a coin one or more times.

1.1 Coin object

To toss a coin using R, we first need an object that plays the role of a coin. How do you create such a coin? Perhaps the simplest way to create a coin with two sides, `"heads"` and `"tails"`, is with a character vector via the combine function `c()`:

``````# a (virtual) coin object
coin

You can also create a numeric coin that shows `1` and `0` instead of `"heads"` and `"tails"`:

``````num_coin <- c(1, 0)
num_coin
#> [1] 1 0``````

Likewise, you can also create a logical coin that shows `TRUE` and `FALSE` instead of `"heads"` and `"tails"`:

``````log_coin <- c(TRUE, FALSE)
log_coin
#> [1]  TRUE FALSE``````

1.2 Tossing a coin

Once you have an object that represents the coin, the next step involves learning how to simulate tossing the coin.

Tossing a coin is a random experiment: you either get heads or tails. One way to simulate the action of tossing a coin in R is with the function `sample()` which lets you draw random samples, with or without replacement, of the elements of an input vector.

Here’s how to simulate a coin toss using `sample()` to take a random sample of size 1 of the elements in `coin`:

``````# toss a coin

sample(coin, size = 1)

You use the argument `size = 1` to specify that you want to take a sample of size 1 from the input vector `coin`.

1.2.1 Random Samples

By default, `sample()` takes a sample of the specified `size` without replacement. If `size = 1`, it does not really matter whether the sample is done with or without replacement.

To draw two elements WITHOUT replacement, use `sample()` like this:

``````# draw 2 elements without replacement
sample(coin, size = 2)

What if you try to toss the coin three or four times?

``````# trying to toss coin 3 times
sample(coin, size = 3)
#> Error in sample.int(length(x), size, replace, prob): cannot take a sample larger than the population when 'replace = FALSE'``````

Notice that R produced an error message. This is because the default behavior of `sample()` cannot draw more elements than the length of the input vector.

To be able to draw more elements, you need to sample with replacement, which is done by specifying the argument `replace = TRUE`, like this:

``````# draw 4 elements with replacement
sample(coin, size = 4, replace = TRUE)
#> [1] "tails" "tails" "tails" "tails"``````

1.3 The Random Seed

The way `sample()` works is by taking a random sample from the input vector. This means that every time you invoke `sample()` you will likely get a different output.

``````# five tosses
sample(coin, size = 5, replace = TRUE)
``````# another five tosses
sample(coin, size = 5, replace = TRUE)

In order to make the examples replicable (so you can get the same output as mine), you need to specify what is called a random seed. This is done with the function `set.seed()`. By setting a seed, every time you use one of the random generator functions, like `sample()`, you will get the same values.

``````# set random seed
set.seed(1257)

# toss a coin with replacement
sample(coin, size = 4, replace = TRUE)

1.4 Sampling with different probabilities

Last but not least, `sample()` comes with the argument `prob` which allows you to provide specific probabilities for each element in the input vector.

By default, `prob = NULL`, which means that every element has the same probability of being drawn. In the example of tossing a coin, the command `sample(coin)` is equivalent to `sample(coin, prob = c(0.5, 0.5))`. In the latter case we explicitly specify a probability of 50% chance of heads, and 50% chance of tails:

``````# tossing a fair coin

sample(coin)

# equivalent
sample(coin, prob = c(0.5, 0.5))

However, you can provide different probabilities for each of the elements in the input vector. For instance, to simulate a loaded coin with chance of heads 20%, and chance of tails 80%, set `prob = c(0.2, 0.8)` like so:

``````# tossing a loaded coin (20% heads, 80% tails)
sample(coin, size = 5, replace = TRUE, prob = c(0.2, 0.8))
#> [1] "heads" "tails" "tails" "tails" "tails"``````

1.4.1 Simulating tossing a coin

Now that we have all the elements to toss a coin with R, let’s simulate flipping a coin 100 times, and then use the function `table()` to count the resulting number of `"heads"` and `"tails"`:

``````# number of flips
num_flips <- 100

# flips simulation
flips <- sample(coin, size = num_flips, replace = TRUE)

# number of heads and tails
freqs <- table(flips)
freqs
#> flips
#>    56    44``````

In my case, I got 56 heads and 44 tails. Your results will probably be different than mine. Sometimes you will get more `"heads"`, sometimes you will get more `"tails"`, and sometimes you will get exactly 50 `"heads"` and 50 `"tails"`.

Let’s run another series of 100 flips, and find the frequency of `"heads"` and `"tails"` with the help of the `table()` function:

``````# one more 100 flips
flips <- sample(coin, size = num_flips, replace = TRUE)
freqs <- table(flips)
freqs
#> flips
#>    47    53``````

To make things more interesting, let’s consider how the frequency of `heads` evolves over a series of n tosses (in this case n = `num_flips`).

``heads_freq <- cumsum(flips == 'heads') / 1:num_flips``

With the vector `heads_freq`, we can graph the (cumulative) relative frequencies with a line-plot:

``````plot(heads_freq,      # vector
type = 'l',      # line type
lwd = 2,         # width of line
col = 'tomato',  # color of line
las = 1,         # orientation of tick-mark labels
ylim = c(0, 1),  # range of y-axis
ylab = "relative frequency")  # y-axis label
abline(h = 0.5, col = 'gray50')``````

Make a donation

If you find this resource useful, please consider making a one-time donation in any amount. Your support really matters.