user2007598
user2007598

Reputation: 163

confused in understanding an rbinom output

  p1 <- c(.25,.025,.025,.1,.2,.4)

  T <- sample(1:6,size=N,replace=TRUE, prob=someprobabilityvector)
  Y <- rbinom(N,1,p1[c(T)])

HI folks, I am new to R and programming in general and need some help with understanding sth basic. could someone explain to me one what is happening in vector Y above. I figure out what p1[c(T)] does above. But have no idea what vector Y is doing. All help is appreciated in advance.

Upvotes: 1

Views: 1872

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81713

The first line of your code creates a vector of six probabilities:

p1 <- c(.25,.025,.025,.1,.2,.4)

In the second line, you randomly choose N values from the numbers one to six (with replacement). The probability for each value is specified in someprobabilityvector. Hence, the function will return a vector of length N including values between 1 and 6

T <- sample(1:6,size=N,replace=TRUE, prob=someprobabilityvector)

In the third line, N random numbers from a binomial distribution with one trial and probablities specified in p1[c(T)] are generated. c(T) is the same as T: the vector including values from 1 to 6. The vector is used for indexing the vector p1. Hence, p1[c(T)] will return a vector including N values from vector p1.

Y <- rbinom(N,1,p1[c(T)])

Since the specified binomial distribution has one trial only, the vector Y will contain zeroes and ones.

Upvotes: 2

Related Questions