| simulate_multilevel | R Documentation |
Draws a random intercept (and optionally a random slope) per cluster, draws the predictors independently for every unit, and forms the outcome as the fixed part plus the cluster random effects plus normal residual noise.
simulate_multilevel(
clusters,
cluster_size,
intercept = 0,
slopes = numeric(0L),
predictor_means = 0,
predictor_sds = 1,
random_intercept_sd = 1,
random_slope_sd = 0,
random_effect_correlation = 0,
residual_sd = 1,
seed = NULL,
batch = NULL
)
clusters |
Number of clusters. A single whole number of at least 2. |
cluster_size |
Units per cluster: a single whole number recycled across clusters, or one whole number per cluster, so clusters may be unbalanced. |
intercept |
Fixed intercept. A single number, defaulting to |
slopes |
Named fixed-effect slopes; the names become the predictor
columns of the returned data. Defaults to |
predictor_means, predictor_sds |
Mean and standard deviation of each
predictor, a scalar recycled across predictors (the defaults, |
random_intercept_sd |
Random-intercept standard deviation. A single
non-negative number, defaulting to |
random_slope_sd |
Random-slope standard deviation for the first
predictor only; the remaining predictors have fixed slopes. A single
non-negative number, defaulting to |
random_effect_correlation |
Correlation between random intercept and
slope. A single number in |
residual_sd |
Residual standard deviation. A single positive number,
defaulting to |
seed |
Optional random seed. |
batch |
Optional single positive whole number. When given, the
simulator runs |
A simulab_sim base data.frame with one row per unit
(sum(cluster_size) rows) and columns id, cluster, one column per
name of slopes, and outcome. Components: fixed_effects (one row per
fixed term, columns term and coefficient), variance_components (one
row for each of random_intercept, random_slope and residual,
columns component and variance – the squared standard deviations
supplied as arguments), and random_effects (one row per cluster,
columns cluster, random_intercept and random_slope; the
random_slope column is present but zero when random_slope_sd = 0).
# `slopes` must be named; the names become the predictor columns.
result <- simulate_multilevel(
clusters = 30,
cluster_size = 10,
intercept = 1,
slopes = c(x1 = 0.5),
random_intercept_sd = 0.8,
seed = 1
)
head(result)
as.data.frame(result, what = "fixed_effects")
as.data.frame(result, what = "variance_components")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.