What does it mean when there are more than two values in "prob" of the rbinom function in R?

239 Views Asked by At

There is an example say that a king presents two daggers in a box which could be golden or silver. I understand the first part is to simulate all three possible results in the box.

set.seed(20202020)
    boxes <- sample(c('GG','SS','GS'),
                size=10^5,replace=T,prob=c(1/3,1/3,1/3))
    table(boxes)/10^5`
## boxes
##      GG      GS      SS 
## 0.33333 0.33413 0.33254

But I am confused with the second part. I understand that as.numeric(as.factor(boxes))turns the result of "GG" "GS" "SS" into numeric 1,2,3; I don't understand what the purpose of this line: prob=c(1,.5,0)[as.numeric(as.factor(boxes))]. It seems to produce three values in the probabilities, but this is a binomial simulation so I can't figure it out.

dagger <- rbinom(10^5,size=1,prob=c(1,.5,0)[as.numeric(as.factor(boxes))])
1

There are 1 best solutions below

1
user3124634 On

So, as you point out, prob is usually a single number between 0 and 1. But, if you give it a vector instead, it applies the vector sequentially until it reaches the end, and then applies it from the beginning again.

For example, the code rbinom(n=3, size=10, prob = c(1,0)) would give results c(10,0,10). So, let's look at what c(1,.5,0)[as.numeric(as.factor(boxes))] does. If x is a vector, then x[c(1,2,1)] returns the first element of x, then the second element, and then the first again. So what c(1,.5,0)[as.numeric(as.factor(boxes))] does is replace every instance, in the vector boxes by the following rule: GG is replaced by 1, GS with 0.5, and SS with 0.

In other words, what the code does as a whole is first create a list of boxes. Then, based on the type of the box, it adjusts the probability of success to match. If it is a GG box, then it guarantees there will be a positive result. If it is a GS box, then it only guarantees a positive result 50% of the time. And if you get an SS box, then you are guaranteed to get nothing. Dagger lists 1 every time you got a dagger, and 0 for the times you didn't.