3
votes

I could not find answer for this question in R. I would like to generate a random sample of 0 to 1's 'RandomSample'. For each sample I would like to have a specific number of values 'numval' which is derived from the length of the vector 'Prob'. 'Prob' is giving me probability value that each individual point will be 0 or 1. So in this instance first number will have prob value of 0.9 being 1, and 0.1 being 0. And so on. Then, I would like to repeat random sample generation 1000 times. I have a script (below) generating random 0 and 1's but I am missing component on giving the probability values. Help will be much appreciated - I am fairly new to R.

Prob <- c(0.9, 0.3, 0.6, 0.8, 0.23, 0.45, 0.1, 0.3, 0.5, 0.03)
RandomSample <- list()
zeroones <- c(0,1)
rep = 1000
numval <- length(Prob)

for (i in 1:rep) RandomSample[[i]] <- c(sample(zeroones,numval,replace = TRUE))
t(sapply(RandomSample, unlist, simplify = TRUE))
2
try this for (i in 1:rep) RandomSample[[i]] <- c(sample(zeroones,numval,prob = Prob[i],replace = TRUE))Sangram
before you run to stackoverflow you might try a simple "?sample" in your prompt. it gives you the documentation that clearly defines how to set the probability of your random variables.Jason

2 Answers

10
votes

You can use rbinom() to generate random samples from a binomial distribution.

Try this:

prob <- c(0.9, 0.3, 0.6, 0.8, 0.23, 0.45, 0.1, 0.3, 0.5, 0.03)
rbinom(length(prob), size = 1, prob=prob)

 [1] 1 1 1 0 0 0 0 1 0 0

To demonstrate that the probabilities are in fact what you are after, try using replicate() to repeatedly draw samples using your probabilities:

x <- t(replicate(100, rbinom(length(prob), size = 1, prob=prob)))
head(x)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    0    1    1    1    1    0    0    1     0
[2,]    1    1    1    1    0    1    0    1    0     0
[3,]    1    0    1    1    0    0    0    1    0     0
[4,]    1    0    1    0    0    1    0    0    1     0
[5,]    1    1    1    1    0    0    0    0    0     0
[6,]    1    0    0    0    0    0    0    0    0     0

Now you can use colMeans() to compare the actual achieved probability against your specification:

colMeans(x)
 [1] 0.93 0.28 0.61 0.67 0.25 0.43 0.11 0.29 0.40 0.01
1
votes

You could use rbinom():

Prob <- c(0.9, 0.3, 0.6, 0.8, 0.23, 0.45, 0.1, 0.3, 0.5, 0.03) #specify vector of probabilities
niter<- 1000 #number of iterations
randomSample<-rbinom(niter,1,prob=rep(Prob,niter)) #randomly sample from binomial with vector of probabilities.