flip.coin() in the animation package provides a
simulation to the process of flipping coins and computes the frequencies for
Coin flipping is a well-known Bernoulli trial. When
you flip a coin, there are two possible outcomes:
tail. A fair coin has the probability
head by definition.
We toss a fair coin 100 times below.
library(animation) ani.options(nmax = 100, interval = 0.3) par(mar = c(2, 4, 2, 2)) flip.coin(bg = "yellow")
Note the outcome is random, so if you run the code above again, you are likely to see different results, but on average you should get 50 heads and 50 tails in the long run.
The coin here does not have to mean a coin literally. We can generalize it to an object that can
produce $n$ possible outcomes. For example, three outcomes
Stand (a coin may stand on the
Tail with probabilities 0.45, 0.1 and 0.45 respectively:
ani.options(nmax = 100, interval = 0.3) par(mar = c(2, 4, 2, 2)) flip.coin(faces = c("Head", "Stand", "Tail"), type = "n", prob = c(0.45, 0.1, 0.45), col = c(1, 2, 4))