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
#> myID xUni xNorm V1 V2 V3 #> 1: 1 18.41861 34.82973 3.858941 9.666185 16.874089 #> 2: 2 16.31256 34.99261 1.788267 6.828155 12.247735 #> 3: 3 17.54575 35.26970 3.852962 8.479367 12.087658 #> 4: 4 13.84560 27.35040 2.482529 4.960985 14.710103 #> 5: 5 16.24744 37.07076 2.785640 10.243463 13.505925 #> --- #> 246: 246 17.73700 31.85579 1.737855 7.566850 10.015151 #> 247: 247 11.05785 20.00066 1.692345 6.484659 9.764174 #> 248: 248 14.69369 31.05177 0.761934 5.351151 8.705052 #> 249: 249 19.07693 37.18574 3.287041 10.947499 22.196264 #> 250: 250 15.58474 31.25266 2.132821 7.096336 15.578519
round(var(dtAdd[, .(V1, V2, V3)]), 3)
#> V1 V2 V3 #> V1 0.844 1.198 1.786 #> V2 1.198 3.651 3.822 #> V3 1.786 3.822 8.631
round(cor(dtAdd[, .(V1, V2, V3)]), 2)
#> V1 V2 V3 #> V1 1.00 0.68 0.66 #> V2 0.68 1.00 0.68 #> V3 0.66 0.68 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.65 0.46 #> V2 0.65 1.00 0.71 #> V3 0.46 0.71 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.21 0.8 #> V2 0.21 1.00 0.6 #> V3 0.80 0.60 1.0