Simulate data set that is one level down in a multilevel data context. The level "2" data set must contain a field that specifies the number of individual records in a particular cluster.

genCluster(dtClust, cLevelVar, numIndsVar, level1ID, allLevel2 = TRUE)

Arguments

dtClust

Name of existing data set that contains the level "2" data

cLevelVar

Variable name (string) of cluster id in dtClust

numIndsVar

Variable name (string) of number of observations per cluster in dtClust. Can also be a single integer value that will be used for all clusters.

level1ID

Name of id field in new level "1" data set

allLevel2

Indicator: if set to TRUE (default), the returned data set includes all of the Level 2 data columns. If FALSE, the returned data set only includes the Levels 1 and 2 ids.

Value

A simulated data table with level "1" data

Examples

gen.school <- defData(
  varname = "s0", dist = "normal",
  formula = 0, variance = 3, id = "idSchool"
)
gen.school <- defData(gen.school,
  varname = "nClasses",
  dist = "noZeroPoisson", formula = 3
)

dtSchool <- genData(3, gen.school) #'
dtSchool
#> Key: <idSchool>
#>    idSchool          s0 nClasses
#>       <int>       <num>    <num>
#> 1:        1 -2.47606482        3
#> 2:        2 -0.68211482        3
#> 3:        3  0.02383165        4

dtClass <- genCluster(dtSchool,
  cLevelVar = "idSchool",
  numIndsVar = "nClasses", level1ID = "idClass"
)
dtClass
#> Key: <idClass>
#>     idSchool          s0 nClasses idClass
#>        <int>       <num>    <num>   <int>
#>  1:        1 -2.47606482        3       1
#>  2:        1 -2.47606482        3       2
#>  3:        1 -2.47606482        3       3
#>  4:        2 -0.68211482        3       4
#>  5:        2 -0.68211482        3       5
#>  6:        2 -0.68211482        3       6
#>  7:        3  0.02383165        4       7
#>  8:        3  0.02383165        4       8
#>  9:        3  0.02383165        4       9
#> 10:        3  0.02383165        4      10

dtClass <- genCluster(dtSchool,
  cLevelVar = "idSchool",
  numIndsVar = 3, level1ID = "idClass"
)
dtClass
#> Key: <idClass>
#>    idSchool          s0 nClasses idClass
#>       <int>       <num>    <num>   <int>
#> 1:        1 -2.47606482        3       1
#> 2:        1 -2.47606482        3       2
#> 3:        1 -2.47606482        3       3
#> 4:        2 -0.68211482        3       4
#> 5:        2 -0.68211482        3       5
#> 6:        2 -0.68211482        3       6
#> 7:        3  0.02383165        4       7
#> 8:        3  0.02383165        4       8
#> 9:        3  0.02383165        4       9