A thin wrapper around simulate_correlated() that supplies defaults for
means and sds, so simulate_correlation(n) generates a single standard
normal variable. structure is forwarded only when it is supplied, which
keeps simulate_correlated()'s own default resolution intact: "custom"
when correlation is given, "exchangeable" when a non-zero rho is
given, and "independent" otherwise.
Usage
simulate_correlation(
n,
means = 0,
sds = 1,
rho = 0,
structure = c("independent", "exchangeable", "ar1", "custom"),
correlation = NULL,
variable_names = NULL,
seed = NULL,
batch = NULL
)Arguments
- n
Number of observations.
- means, sds, rho, structure, correlation, variable_names, seed
Arguments passed to
simulate_correlated(). Unlike that function,meansdefaults to0. One variable is generated per element ofmeans;sdsmust be a single value, which is recycled, or one value per mean.- 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
an id column followed by one column per variable. The parameters,
correlation and covariance components hold the requested means and
standard deviations and the tidy correlation and covariance matrices; use
components() to list them.
Examples
result <- simulate_correlation(n = 200, means = c(0, 0), sds = c(1, 1), rho = 0.6,
seed = 1)
head(result)
#> <simulab_sim:correlated> 6 rows x 3 columns
#> id V1 V2
#> 1 1 -0.46484204 0.1902906
#> 2 2 0.70828798 1.6602790
#> 3 3 -0.29102359 1.2409210
#> 4 4 1.40877402 0.1905454
#> 5 5 -0.41005641 -2.0637653
#> 6 6 0.01146529 2.1100350
#>
#> Truth (parameters):
#> variable mean sd
#> 1 V1 0 1
#> 2 V2 0 1
#>
#> Other tables: correlation, covariance. Read one with as.data.frame(x, what = "correlation").
as.data.frame(result, what = "correlation")
#> row column correlation
#> 1 V1 V1 1.0
#> 2 V2 V1 0.6
#> 3 V1 V2 0.6
#> 4 V2 V2 1.0