psyrgh
psyrgh

Reputation: 31

Creating conditional binomial random selections in R

I have a quick question. This is my function for vector x1:
x1 <- rbinom (100, 1, .5)

I need to have 3 more vectors, x2, x3, and x4 which look like this too:
x2 <- rbinom (100, 1, .5)
x3 <- rbinom (100, 1, .5)
x4 <- rbinom (100, 1, .5)

However, this gives me random distributions. What I want is to have these 4 separate vectors, but they're dependent on each other. I need to get something like this:

x1: 1 0 0 0 1 0 1 0 1 0
x2: 0 0 0 1 0 0 0 0 0 1
x3: 0 1 0 0 0 0 0 1 0 0
x4: 0 0 1 0 0 1 0 0 0 0 

In other words, if there's 1 in x1 on the first spot, there need to be 0s in the first spot in x1, x2, and x3.

Thank you very much!

Upvotes: 1

Views: 294

Answers (2)

Severin Pappadeux
Severin Pappadeux

Reputation: 20080

Frankly, it looks like Multinomial distribution, a.k.a. throwing K-sized dice multiple times. K in your case is equal to 4. Multinomial is natural extension to binomial

There is R function to sample from multinomial, for example, 10 samples with equal probability

set.seed(12345)
prob <- c(.25, .25, .25, .25)
q <- rmultinom(10, 1, prob)
print(q)

To compute density function you could use dmultinom. See https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Multinom.html for details.

Output

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    1    1    0    0    1    0    0    0     1
[2,]    1    0    0    0    1    0    0    0    0     0
[3,]    0    0    0    0    0    0    1    0    0     0
[4,]    0    0    0    1    0    0    0    1    1     0

and one more piece, probabilities check, should be close to 1/4 each

set.seed(12345)
q <- rmultinom(100000, 1, prob)
rowMeans(q)

produced

[1] 0.25075 0.24964 0.24871 0.25090

Upvotes: 1

MrFlick
MrFlick

Reputation: 206167

So you want to have 4 mutually exclusive options. Then sample from that directly, then you can turn them into essentially dummy variables later.

You can do

x <- sample(1:4, 100, replace=T)
x1 <- (x==1)+0
x2 <- (x==2)+0
x3 <- (x==3)+0
x4 <- (x==4)+0

This works if you want equal probability of being 1-4 at each draw. This is different than a rbinom(prob=.5) for each column.

Upvotes: 0

Related Questions