Simulating binary data
A Bernoulli distribution is a special case of a binomial.
Next, you will see how to simulate both in R and then examine the outputs to see how they are similar.
Both distributions can be simulated with the random binomial function: rbinom()
.
rbinom()
requires 3 arguments:
n
, which is the number of draws or random numbers (i.e., an output vector of lengthn
).size
, which is the number of samples per draw (i.e., the maximum value for each random number).prob
, which is the probability for the simulation.
To sample with a Bernoulli, you simply use size = 1
.
If we take a single random draw (n = 1
) from a binomial distribution with a large number of samples per draw (e.g. size = 100
), we should get similar results as a taking a many samples (e.g. n = 100
) with 1 sample per draw (size = 1
).
This exercise is part of the course
Generalized Linear Models in R
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Simulate 1 draw with a sample size of 100
binomial_sim <- ___