Simulate correlated ordinal variables
Usage
simulate_ordinal(
n,
probabilities,
n_variables = 1L,
rho = 0,
structure = c("independent", "exchangeable", "ar1", "custom"),
correlation = NULL,
labels = NULL,
variable_names = NULL,
seed = NULL,
batch = NULL
)Arguments
- n
Number of observations.
- probabilities
A numeric vector used for every variable or a matrix with one row per variable and one column per category.
- n_variables
Number of variables when
probabilitiesis a vector.- rho, structure, correlation
Correlation arguments for the latent normal variables.
structurefollows the same resolution rule assimulate_correlated(): leaving it unset selects"exchangeable"when a non-zerorhois given. Passed to the latent Gaussian generator.- labels
Optional category labels.
- variable_names
Optional variable names.
- seed
Optional random seed. A single number makes the call reproducible and restores the session's random-number state afterwards;
NULL(the default) draws from the session's stream and advances it.- batch
Optional single positive whole number. When given, the simulator runs
batchtimes and returns a plainlistofbatchresults, each exactly what the same call withoutbatchreturns. With aseed, every dataset gets its own seed drawn fromseed, so the whole batch is reproducible; without one the datasets are consecutive draws from the session's random-number stream. The defaultNULLreturns a single result.
Value
A simulab_sim base data.frame with one row per observation and
columns id and one column per variable, each holding a category label.
Two tidy tables come from as.data.frame(): what = "probabilities" has
one row per variable and category with columns variable, category and
probability; what = "correlation" has one row per latent
correlation-matrix cell with columns row, column and correlation.
That correlation is the latent Gaussian one, not the correlation of the
realized categories, which is attenuated by the thresholding.
References
Olsson, U. (1979). Maximum likelihood estimation of the polychoric correlation coefficient. Psychometrika, 44(4), 443–460. doi:10.1007/BF02296207
Examples
result <- simulate_ordinal(
n = 200, probabilities = c(0.2, 0.5, 0.3), n_variables = 2, rho = 0.4, seed = 1
)
head(result)
#> <simulab_sim:ordinal> 6 rows x 3 columns
#> id V1 V2
#> 1 1 2 2
#> 2 2 3 3
#> 3 3 2 3
#> 4 4 3 2
#> 5 5 2 1
#> 6 6 2 3
#>
#> Truth (probabilities):
#> variable category probability
#> 1 V1 1 0.2
#> 2 V1 2 0.5
#> 3 V1 3 0.3
#> 4 V2 1 0.2
#> 5 V2 2 0.5
#> 6 V2 3 0.3
#>
#> Other tables: correlation. Read one with as.data.frame(x, what = "correlation").
as.data.frame(result, what = "probabilities")
#> variable category probability
#> 1 V1 1 0.2
#> 2 V1 2 0.5
#> 3 V1 3 0.3
#> 4 V2 1 0.2
#> 5 V2 2 0.5
#> 6 V2 3 0.3