Calling function to simulate data

genData(n, dtDefs = NULL, id = "id", envir = parent.frame())

Arguments

n

the number of observations required in the data set.

dtDefs

name of definitions data.table/data.frame. If no definitions are provided a data set with ids only is generated.

id

The string defining the id of the record. Will override previously set id name with a warning (unless the old value is 'id'). If the id attribute in dtDefs is NULL will default to 'id'.

envir

Environment the data definitions are evaluated in. Defaults to base::parent.frame.

Value

A data.table that contains the simulated data.

Examples

genData(5)
#>    id
#> 1:  1
#> 2:  2
#> 3:  3
#> 4:  4
#> 5:  5
genData(5, id = "grpID")
#>    grpID
#> 1:     1
#> 2:     2
#> 3:     3
#> 4:     4
#> 5:     5

def <- defData(
  varname = "xNr", dist = "nonrandom", formula = 7,
  id = "idnum"
)
def <- defData(def,
  varname = "xUni", dist = "uniform",
  formula = "10;20"
)
def <- defData(def,
  varname = "xNorm", formula = "xNr + xUni * 2",
  dist = "normal", variance = 8
)
def <- defData(def,
  varname = "xPois", dist = "poisson",
  formula = "xNr - 0.2 * xUni", link = "log"
)
def <- defData(def,
  varname = "xCat", formula = "0.3;0.2;0.5",
  dist = "categorical"
)
def <- defData(def,
  varname = "xGamma", dist = "gamma", formula = "5+xCat",
  variance = 1, link = "log"
)
def <- defData(def,
  varname = "xBin", dist = "binary", formula = "-3 + xCat",
  link = "logit"
)
def
#>    varname          formula variance        dist     link
#> 1:     xNr                7        0   nonrandom identity
#> 2:    xUni            10;20        0     uniform identity
#> 3:   xNorm   xNr + xUni * 2        8      normal identity
#> 4:   xPois xNr - 0.2 * xUni        0     poisson      log
#> 5:    xCat      0.3;0.2;0.5        0 categorical identity
#> 6:  xGamma           5+xCat        1       gamma      log
#> 7:    xBin        -3 + xCat        0      binary    logit

genData(5, def)
#>    idnum xNr     xUni    xNorm xPois xCat   xGamma xBin
#> 1:     1   7 13.21221 31.87277    73    3 4779.621    1
#> 2:     2   7 11.63804 29.42522   127    3 1525.065    0
#> 3:     3   7 16.01978 36.30093    50    2 1155.946    0
#> 4:     4   7 19.61596 43.77713    18    3 2443.359    1
#> 5:     5   7 12.56710 33.65354   101    3 8068.865    1