Add correlated data to existing data.table

addCorData(
  dtOld,
  idname,
  mu,
  sigma,
  corMatrix = NULL,
  rho,
  corstr = "ind",
  cnames = NULL
)

Arguments

dtOld

Data table that is the new columns will be appended to.

idname

Character name of id field, defaults to "id".

mu

A vector of means. The length of mu must be nvars.

sigma

Standard deviation of variables. If standard deviation differs for each variable, enter as a vector with the same length as the mean vector mu. If the standard deviation is constant across variables, as single value can be entered.

corMatrix

Correlation matrix can be entered directly. It must be symmetrical and positive semi-definite. It is not a required field; if a matrix is not provided, then a structure and correlation coefficient rho must be specified.

rho

Correlation coefficient, -1 <= rho <= 1. Use if corMatrix is not provided.

corstr

Correlation structure of the variance-covariance matrix defined by sigma and rho. Options include "ind" for an independence structure, "cs" for a compound symmetry structure, and "ar1" for an autoregressive structure.

cnames

Explicit column names. A single string with names separated by commas. If no string is provided, the default names will be V#, where # represents the column.

Value

The original data table with the additional correlated columns

Examples

def <- defData(varname = "xUni", dist = "uniform", formula = "10;20", id = "myID")
def <- defData(def,
  varname = "xNorm", formula = "xUni * 2", dist = "normal",
  variance = 8
)

dt <- genData(250, def)

mu <- c(3, 8, 15)
sigma <- c(1, 2, 3)

dtAdd <- addCorData(dt, "myID",
  mu = mu, sigma = sigma,
  rho = .7, corstr = "cs"
)
dtAdd
#> Key: <myID>
#>       myID     xUni    xNorm        V1       V2        V3
#>      <int>    <num>    <num>     <num>    <num>     <num>
#>   1:     1 18.15334 35.91999 3.5837828 9.361068 19.606262
#>   2:     2 16.81106 34.98541 0.6595679 3.418403  6.866132
#>   3:     3 16.41371 35.91866 2.9295731 7.656470 14.774557
#>   4:     4 16.31126 28.88397 2.7552943 9.282415 17.152096
#>   5:     5 15.42086 33.82735 2.7521645 7.752415 12.266413
#>  ---                                                     
#> 246:   246 18.58274 32.27800 4.5926123 7.067914 15.865338
#> 247:   247 11.57482 22.98997 2.8351522 5.202762 15.031914
#> 248:   248 14.99170 33.36374 4.3554301 5.638182 16.781637
#> 249:   249 10.81451 14.51719 3.4863076 8.957330 18.431615
#> 250:   250 11.94921 25.83222 4.0164770 9.961086 19.148114

round(var(dtAdd[, .(V1, V2, V3)]), 3)
#>       V1    V2     V3
#> V1 1.100 1.467  2.261
#> V2 1.467 4.216  4.544
#> V3 2.261 4.544 10.209
round(cor(dtAdd[, .(V1, V2, V3)]), 2)
#>      V1   V2   V3
#> V1 1.00 0.68 0.67
#> V2 0.68 1.00 0.69
#> V3 0.67 0.69 1.00

dtAdd <- addCorData(dt, "myID",
  mu = mu, sigma = sigma,
  rho = .7, corstr = "ar1"
)
round(cor(dtAdd[, .(V1, V2, V3)]), 2)
#>      V1   V2   V3
#> V1 1.00 0.71 0.48
#> V2 0.71 1.00 0.66
#> V3 0.48 0.66 1.00

corMat <- matrix(c(1, .2, .8, .2, 1, .6, .8, .6, 1), nrow = 3)

dtAdd <- addCorData(dt, "myID",
  mu = mu, sigma = sigma,
  corMatrix = corMat
)
round(cor(dtAdd[, .(V1, V2, V3)]), 2)
#>      V1   V2   V3
#> V1 1.00 0.23 0.80
#> V2 0.23 1.00 0.62
#> V3 0.80 0.62 1.00