Sampling from a Binomial(K, p) with unexpected result
distribution, r
Solution
Argument recycling of the size argument is the prime cause.
Because `n` is 1000, `0:1` is recycled until you get 500 `0`'s and 500 `1`'s (alternating).
All the 0-size ones give `0`:
> rbinom(10,size=0,prob=0.2)
[1] 0 0 0 0 0 0 0 0 0 0
Resulting in 500 `0`'s + 500 Bernoulli trials with p=0.2, resulting in about 100 `1`'s out of 1000 elements.
[Your results didn't seem surprising to me, but argument recycling can bite if you're not looking for it, and - while there are reasons why the number of successes in 0 Bernoulli trials should be defined as 0 - it may not seem obvious at first either.]
Problem
In the help files for `rbinom`, size argument is a number of trials (incl. a zero) but it doesn't say if this can also be a vector. The correct way of using this function is ``` table(rbinom(n = 1000, size = 1, prob = 0.2)) 0 1 809 191 ``` But what is happening here? ``` table(rbinom(n = 1000, size = 0:1, prob = 0.2)) 0 1 894 106 ```