Skip to contents

Simulates a nested multilevel data structure. Lowest level can be used as observational error.

Usage

simulate_multilevel_data(
  n_levels = 3,
  n_units_perlevel = rep(25, n_levels),
  sigma_perlevel = rep(1, n_levels),
  mu_global = 0,
  add_data = TRUE,
  n_data = 10,
  sigma_y = 0.05,
  add_data1levelup = FALSE,
  seed = 12345
)

Arguments

n_levels

Number of hierarchical levels (default: 3).

n_units_perlevel

A vector specifying the number of units at each level (default: rep(25, n_levels)).

sigma_perlevel

A vector specifying the standard deviation of etas at each level (default: rep(1, n_levels)).

mu_global

Global mean for the outcome variable (default: 0).

add_data

Logical indicating whether to add observations (default: TRUE). If TRUE, adds column y.

n_data

Number of observations to add at the lowest level (default: 10).

sigma_y

Standard deviation of the added observations (default: 0.05)

add_data1levelup

Logical indicating whether to add observations one level up e.g. at national level if lowest level is subnational (default: FALSE).

seed

Random seed for reproducibility (default: 1234).

Value

A tibble with columns for each level, mu_global, etas, and their total labeled y