13.4 Indicator (Bernoulli) Variables
A special case of a categorical variable is an indicator variable, sometimes referred to as a binary or dummy variable. The underlying distribution of an indicator variable is called a Bernoulli distribution.
Suppose we are interested in evaluating the whether a flip of a coin would be a head or a tail. Here we could define Head as the variable of interest.
\[ Head = \left\{ \begin{array}{ll} 1 & \mbox{If Flip a Head} \\ 0 & \mbox{If Flip a Tail} \end{array} \right. \]
If you are more interested in evaluating Tails, you could define the random variable as:
\[ Tail = \left\{ \begin{array}{ll} 1 & \mbox{If Flip a Tail} \\ 0 & \mbox{If Flip a Head} \end{array} \right. \]
We can simulate this random variable using a Binomial distribution. (Technically, the Bernoulli distribution is a special case of a Binomial.) We need to set values for n = , size = , and prob = , where n is the number of values you want to simulate, size in this case is 1 (as we want to simulate an indicator variable), and prob, is the probability that you will flip a head (or tail, depending on your random variable).
Simulating indicator variables is completed using the rbinom function. Here, we simulate 5 values of heads with a probability of 1/2 of getting a head on each flip (or a fair coin).
rbinom(n = 6, size=1, prob=0.5)
If you are simulating 6 coin flips using a fair coin, how many heads do you expect? What did you get?
Re-run the above code several times to see how the sequence of 6 coin flips varies.
Note: this is one way of simulating what is known as a Bernoulli random variable. You can also use a function called rbernoulli that is part of the purrr package.