Hi Carrie,
Use the first approach:
n <- 5
p <- c(0.2, 0.9, 0.15, 0.8, 0.75)
rbinom(n, 1, p)
# [1] 0 0 0 1 1
rbinom(n, 1, p)
# [1] 1 1 0 1 1
To check, replicate the analysis 5000 times and then estimate the
probability for each subject:
rowMeans(replicate(5000, rbinom(n, 1, p)))
# 0.2002 0.9026
Carrie Li wrote:
Dear R-helpers,
I would like to generate a variable that takes 0 or 1, and each subject has
different probabilities of taking the draw.
So, which of the following code I should use ?
I don't think either.
Try this:
probs <- seq(0,1, by = .1)
sapply(probs, function(x) samp
Dear R-helpers,
I would like to generate a variable that takes 0 or 1, and each subject has
different probabilities of taking the draw.
So, which of the following code I should use ?
suppose there are 5 subjects, and their probabilities of this Bernoulli
variable is p=c(0.2, 0.9, 0.15, 0.8, 0.7
3 matches
Mail list logo