Matthew Hui
Matthew Hui

Reputation: 664

How to sample according to a long array of probabilities of being True in R?

N <- 1000
arr_p_True <- runif(N)
arr_simulated <- sapply(arr_p_True, function(p) {
  sample(c(T, F), 1, prob = c(p, 1 - p))
})

arr_p_True is what I want to get, but with very large N this is very inefficient. sample() does not seem to be the right function to consider in this case, because it is vectorized over the probability of choosing each of the elements in x, but not vectorized over the probability of choosing the first element in x as required in my example.

I cannot find the right keyword for the purpose... I keep on being directed back to sample(). Any help is appreciated.

Upvotes: 2

Views: 151

Answers (2)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84519

I think you can do

arr_p_True <- runif(N)
as.logical(rbinom(N, size = 1, prob = arr_p_True))

But if arr_p_True is runif(N) in your real code, then this is equivalent to

as.logical(rbinom(N, size = 1, prob = 0.5))

Upvotes: 1

Ralf Stubner
Ralf Stubner

Reputation: 26823

You could generate a vector of random numbers from the unit interval. With probability p the value will be smaler than p:

N <- 1000
arr_p_True <- runif(N)
arr_simulated <- runif(N) < arr_p_True

Upvotes: 0

Related Questions