Drawing discrete data based on probabilities or latent traits is a common task that can be cumbersome. draw_binary is an alias for draw_discrete(type = "binary") that allows you to draw binary outcomes more easily.

draw_discrete(x, N = length(x), type = "binary", link = "identity",
  breaks = c(-Inf, 0, Inf), break_labels = FALSE, k = 1)

draw_binary(x, N = length(x), link = "identity")

Arguments

x

vector representing either the latent variable used to draw the count outcome (if link is "logit" or "probit") or the probability for the count outcome (if link is "identity"). For cartegorical distributions x is a matrix with as many columns as possible outcomes.

N

number of units to draw. Defaults to the length of the vector x

type

type of discrete outcome to draw, one of 'binary' (or 'bernoulli'), 'binomial', 'categorical', 'ordered' or 'count'

link

link function between the latent variable and the probability of a postiive outcome, i.e. "logit", "probit", or "identity". For the "identity" link, the latent variable must be a probability.

breaks

vector of breaks to cut an ordered latent outcome

break_labels

vector of labels for the breaks for an ordered latent outcome (must be the same length as breaks)

k

the number of trials (zero or more)

Examples

fabricate(N = 3, p = c(0, .5, 1), binary = draw_discrete(p))
#> ID p binary #> 1 1 0.0 0 #> 2 2 0.5 0 #> 3 3 1.0 1
fabricate(N = 3, p = c(0, .5, 1), binary = draw_discrete(p, type = "bernoulli"))
#> ID p binary #> 1 1 0.0 0 #> 2 2 0.5 1 #> 3 3 1.0 1
fabricate(N = 3, x = 10*rnorm(N), binary = draw_discrete(x, type = "bernoulli", link = "probit"))
#> ID x binary #> 1 1 2.55317055 1 #> 2 2 -24.37263611 0 #> 3 3 -0.05571287 1
fabricate(N = 3, p = c(0, .5, 1), binomial = draw_discrete(p, type = "binomial", k = 10))
#> ID p binomial #> 1 1 0.0 0 #> 2 2 0.5 4 #> 3 3 1.0 10
fabricate(N = 3, x = 5*rnorm(N), ordered = draw_discrete(x, type = "ordered", breaks = c(-Inf, -1, 1, Inf)))
#> ID x ordered #> 1 1 -9.109088 1 #> 2 2 -1.236627 1 #> 3 3 -1.220998 1
fabricate(N = 3, x = c(0,5,100), count = draw_discrete(x, type = "count"))
#> ID x count #> 1 1 0 0 #> 2 2 5 4 #> 3 3 100 119
# Categorical fabricate(N = 6, p1 = runif(N), p2 = runif(N), p3 = runif(N), cat = draw_discrete(cbind(p1, p2, p3), type = "categorical"))
#> ID p1 p2 p3 cat #> 1 1 0.67838043 0.53021246 0.6364656 1 #> 2 2 0.73531960 0.69582388 0.4790245 2 #> 3 3 0.19595673 0.68855600 0.4321713 2 #> 4 4 0.98053967 0.03123033 0.7064338 3 #> 5 5 0.74152153 0.22556253 0.9485766 1 #> 6 6 0.05144628 0.30083081 0.1803388 2