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
).
Diese Übung ist Teil des Kurses
Generalized Linear Models in R
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Simulate 1 draw with a sample size of 100
binomial_sim <- ___