"sample" and "rbinom" functions in R

9.1k views Asked by At

I guess it has been asked before, but I'm still a bit rusty about "sample" and "rbinom" functions in R, and would like to ask the following two simple questions:

a) Let's say we have:

        rbinom(n = 5, size = 1, prob = c(0.9,0.2,0.3))

So "n" = 5 but "prob" is only indicated for three of them. What values R assigns for these two n's?

b) Let's say we have:

        sample(x = 1:3, size = 1, prob = c(.5,0.2,0.9))

According to R-help (?sample):

     The optional prob argument can be used to give a vector of weights    
     for obtaining the elements of the vector being sampled.  
     They need not sum to one, but they should be non-negative and not all zero.

The question would be: why "prob" does not need sum to one?

Any answers would be very appreciated: thank you!

1

There are 1 answers

0
Tim Biegeleisen On BEST ANSWER

From the documentation for rbinom:

The numerical arguments other than n are recycled to the length of the result.

This means that in your example the prob vector you pass in will be recycled until it reaches the required length (presumably 5). So the vector which will be used is:

c(0.9, 0.2, 0.3, 0.9, 0.2)

As for the sample function, as @thelatemail pointed out the probabilities do not have to sum to 1. It appears that the prob vector gets normalized to 1 internally.