Generating random numbers (0 and 1) given specific probability values in R

梦想的初衷 提交于 2019-12-01 07:17:05

问题


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))

回答1:


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



回答2:


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. 


来源:https://stackoverflow.com/questions/28541777/generating-random-numbers-0-and-1-given-specific-probability-values-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!