CommencerCommencer gratuitement

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 length n).
  • 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).

Cet exercice fait partie du cours

Generalized Linear Models in R

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Simulate 1 draw with a sample size of 100
binomial_sim <- ___
Modifier et exécuter le code