Draw ordered categorical outcomes from a latent variable
Source:R/draw_categorical.R
draw_ordered.RdCuts a continuous latent variable at the supplied breaks to produce
ordered discrete categories. A natural companion to latent-variable
specifications of survey responses.
Usage
draw_ordered(
x = latent,
breaks,
labels = NULL,
N = length(x),
strict = FALSE,
latent = NULL,
link = "identity",
break_labels = NULL
)Arguments
- x
Latent continuous variable (numeric vector).
- breaks
Numeric vector of cut-points in ascending order. The vector should span the range of
x; values outside the range are placed in the outermost categories unlessstrict = TRUE. An infinite endpoint bounds the scale rather than cutting it, soc(-Inf, 0, Inf)gives the same two categories as0.- labels
Optional character vector of category labels, one per category. That is
length(breaks) + 1for interior cut-points, one fewer for each infinite endpoint, andlength(breaks) - 1whenstrict = TRUE. When supplied, returns an ordered factor.- N
Must equal
length(x)if it is given at all, and is an error otherwise. It cannot change how many values come back, which is alwayslength(x): unlikedraw_binary()and the rest of the family,draw_ordered()has never recycled toN, in either version. 1.x checksNagainst a recycling rule it does not apply.- strict
If
TRUE, observations outsidebreaksare codedNAinstead of being placed in the outermost category.- latent
Alias for
x(kept for compatibility).- link
Must be
"identity", the only link this function has:breakscut the latent variable on the scale it arrives. Anything else is an error, as it is in fabricatr 1.x.- break_labels
fabricatr 1.x's name for
labels. Accepted with a warning.
Value
An integer vector of category indices, one per element of
x, or an ordered factor with those labels when they are
supplied. With strict = TRUE, values outside breaks are
NA.
Examples
fabricate(N = 200, x = rnorm(N),
resp = draw_ordered(x, breaks = c(-1, 0, 1),
labels = c("disagree", "neutral",
"agree", "strongly agree")))
#> # A tibble: 200 × 3
#> ID x resp
#> <chr> <dbl> <ord>
#> 1 001 -0.819 neutral
#> 2 002 2.11 strongly agree
#> 3 003 1.93 strongly agree
#> 4 004 0.246 agree
#> 5 005 -0.555 neutral
#> 6 006 0.548 agree
#> 7 007 1.62 strongly agree
#> 8 008 0.142 agree
#> 9 009 0.189 agree
#> 10 010 1.05 strongly agree
#> # ℹ 190 more rows