Description Usage Arguments Details Value Examples
View source: R/simulate_data.R
Generate univariate (for now) linear 3 level data with n3
groups,
n2
subgroups per group and n1
individuals per subgroups.
1 2 3 4 5 6 7 8 9 10 11 12 | simulate_data(
n1,
n2,
n3,
fixed_slope,
fixed_intercept,
lvl2_corr_mat,
lvl3_corr_mat,
sigma2,
separable_in_x = TRUE,
distance_factor = 2
)
|
n1 |
number of individuals at level 1. Must be strictly positive. |
n2 |
number of subgroups at level 2. Must be strictly positive. |
n3 |
number of groups at level 3. Must be strictly positive. |
fixed_slope |
Fixed effect slope. Must be finite. |
fixed_intercept |
Fixed effect intercept. Must be finite. |
lvl2_corr_mat |
Level 2 random effect correlation matrix. Must be of type matrix with size of 2x2 |
lvl3_corr_mat |
Level 3 random effect correlation matrix. Must be of type matrix with size of 2x2 |
sigma2 |
variance of the residuals. Must be strictly positive. |
separable_in_x |
logical; if TRUE, the data will be separable in x axis. |
distance_factor |
numeric; When separable_in_x == TRUE, indicates the distances in x between level 3 groups. Must be strictly positive. |
The generated data follows the 3 levels lmm equations given below :
Y_{ijk} = β_{0jk} + β_{1jk} X_{ijk} + ε_{ijk}
Level 2 :
β_{0jk} = γ_{00k} + u_{0jk}
β_{1jk} = γ_{10k} + u_{1jk}
Level 3 :
γ_{00k} = δ_{000} + u_{00k}
γ_{10k} = δ_{100} + u_{10k}
With :
δ_{000} and δ_{100} : fixed effects intercept and slope.
u_{00k} and u_{10k} : level 3 random effects intercept and slope for each level 3 group.
u_{0jk} and u_{0jk} : level 2 random effects intercept and slope for each level 2 group within level 3 group.
ε_{ijk} : residual noise.
data frame containing the generated data containing the following list of columns :
x : explicative variable
y : target variable
lvl2 : factors indicating level 2 groups
lvl3 : factors indicating level 3 groups
lvl3_intercepts : numeric indicating level 3 random intercepts
lvl3_slopes : numeric indicating level 3 random slopes
lvl2_intercepts : numeric indicating level 2 random intercepts
lvl2_slopes : numeric indicating level 2 random slopes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ## define correlation matrices for random effects slopes and intercepts
lvl2_corr_mat = matrix(c(1, 0,
0, 1), 2, 2)
lvl3_corr_mat = matrix(c(10, 0,
0, 10), 2, 2)
## to generate 3 levels data with 3 groups that contains each 10 subgroups,
## and each subgroup contains 30 individuals
data = simulate_data(n1 = 30,
n2 = 10,
n3 = 3,
fixed_slope = -2,
fixed_intercept = 2,
lvl2_corr_mat = lvl2_corr_mat,
lvl3_corr_mat = lvl3_corr_mat,
sigma2 = 1
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.