rvbern
generates a random vector where each simulation comes from a
Bernoulli sampling distribution.
rvbern(n = 1, prob, logical = FALSE)
number of random scalars to draw
probability of "success"; may be a random vector itself
logical; return a logical random variable instead
A random vector (an rv object) of length n
.
rvbern
is a special case of rvbinom
with the argument size=1.
If logical
is TRUE
, the function returns a logical random
variable which has TRUE for 1, FALSE for 0. (The printed summary of this
object is slightly different from a regular continuous numeric random
variable.)
The resulting vector will not be independent and identically
distributed Bernoulli unless prob
is a fixed number.
Kerman, J. and Gelman, A. (2007). Manipulating and Summarizing Posterior Simulations Using Random Variable Objects. Statistics and Computing 17:3, 235-244.
See also vignette("rv")
.
rvbern(2, prob=0.5)
#> mean sd 1% 2.5% 25% 50% 75% 97.5% 99% sims
#> [1] 0.51 0.5 0 0 0 1 1 1 1 200
#> [2] 0.44 0.5 0 0 0 0 1 1 1 200
rvbinom(2, size=1, prob=0.5) # Equivalent
#> mean sd 1% 2.5% 25% 50% 75% 97.5% 99% sims
#> [1] 0.53 0.5 0 0 0 1 1 1 1 200
#> [2] 0.55 0.5 0 0 0 1 1 1 1 200
print(rvbern(1, 0.5, logical=TRUE)) # won't show the quantiles
#> mean sd 1% 2.5% 25% 50% 75% 97.5% 99% sims
#> [1] 0.49 0.5 0 0 0 0 1 1 1 200
print(as.logical(rvbern(1, 0.5))) # equivalent
#> mean sd 1% 2.5% 25% 50% 75% 97.5% 99% sims
#> [1] 0.48 0.5 0 0 0 0 1 1 1 200