Add multi-factorial data

addMultiFac(dtOld, nFactors, levels = 2, coding = "dummy", colNames = NULL)

Arguments

dtOld

data.table that is to be modified

nFactors

Number of factors (columns) to generate.

levels

Vector or scalar. If a vector is specified, it must be the same length as nFatctors. Each value of the vector represents the number of levels of each corresponding factor. If a scalar is specified, each factor will have the same number of levels. The default is 2 levels for each factor.

coding

String value to specify if "dummy" or "effect" coding is used. Defaults to "dummy".

colNames

A vector of strings, with a length of nFactors. The strings represent the name for each factor.

Value

A data.table that contains the added simulated data. Each new column contains an integer.

Examples

defD <- defData(varname = "x", formula = 0, variance = 1)

DT <- genData(360, defD)
DT <- addMultiFac(DT, nFactors = 3, levels = c(2, 3, 3), colNames = c("A", "B", "C"))
DT
#> Key: <id>
#>         id          x     A     B     C
#>      <int>      <num> <int> <int> <int>
#>   1:     1  0.4456463     1     3     2
#>   2:     2 -1.3738458     1     3     2
#>   3:     3  0.7336057     2     2     3
#>   4:     4  0.5728606     1     2     1
#>   5:     5 -0.1538775     1     1     3
#>  ---                                   
#> 356:   356  0.6066500     1     1     1
#> 357:   357  0.5467441     2     1     3
#> 358:   358  0.6698458     2     3     1
#> 359:   359 -0.4435307     1     1     1
#> 360:   360 -0.4333330     2     1     3
DT[, .N, keyby = .(A, B, C)]
#> Key: <A, B, C>
#>         A     B     C     N
#>     <int> <int> <int> <int>
#>  1:     1     1     1    20
#>  2:     1     1     2    20
#>  3:     1     1     3    20
#>  4:     1     2     1    20
#>  5:     1     2     2    20
#>  6:     1     2     3    20
#>  7:     1     3     1    20
#>  8:     1     3     2    20
#>  9:     1     3     3    20
#> 10:     2     1     1    20
#> 11:     2     1     2    20
#> 12:     2     1     3    20
#> 13:     2     2     1    20
#> 14:     2     2     2    20
#> 15:     2     2     3    20
#> 16:     2     3     1    20
#> 17:     2     3     2    20
#> 18:     2     3     3    20

DT <- genData(300, defD)
DT <- addMultiFac(DT, nFactors = 3, levels = 2)
DT[, .N, keyby = .(Var1, Var2, Var3)]
#> Key: <Var1, Var2, Var3>
#>     Var1  Var2  Var3     N
#>    <num> <num> <num> <int>
#> 1:     0     0     0    37
#> 2:     0     0     1    38
#> 3:     0     1     0    38
#> 4:     0     1     1    37
#> 5:     1     0     0    37
#> 6:     1     0     1    38
#> 7:     1     1     0    37
#> 8:     1     1     1    38