Create multivariate (correlated) data - for general distributions

addCorFlex(
  dt,
  defs,
  rho = 0,
  tau = NULL,
  corstr = "cs",
  corMatrix = NULL,
  envir = parent.frame()
)

Arguments

dt

Data table that will be updated.

defs

Field definition table created by function defDataAdd.

rho

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

tau

Correlation based on Kendall's tau. If tau is specified, then it is used as the correlation even if rho is specified. If tau is NULL, then the specified value of rho is used, or rho defaults to 0.

corstr

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

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.

envir

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

Value

data.table with added column(s) of correlated data

Examples

defC <- defData(
  varname = "nInds", formula = 50, dist = "noZeroPoisson",
  id = "idClust"
)

dc <- genData(10, defC)
#### Normal only

dc <- addCorData(dc,
  mu = c(0, 0, 0, 0), sigma = c(2, 2, 2, 2), rho = .2,
  corstr = "cs", cnames = c("a", "b", "c", "d"),
  idname = "idClust"
)

di <- genCluster(dc, "idClust", "nInds", "id")

defI <- defDataAdd(
  varname = "A", formula = "-1 + a", variance = 3,
  dist = "normal"
)
defI <- defDataAdd(defI,
  varname = "B", formula = "4.5 + b", variance = .5,
  dist = "normal"
)
defI <- defDataAdd(defI,
  varname = "C", formula = "5*c", variance = 3,
  dist = "normal"
)
defI <- defDataAdd(defI,
  varname = "D", formula = "1.6 + d", variance = 1,
  dist = "normal"
)

#### Generate new data

di <- addCorFlex(di, defI, rho = 0.4, corstr = "cs")

# Check correlations by cluster

for (i in 1:nrow(dc)) {
  print(cor(di[idClust == i, list(A, B, C, D)]))
}
#>           A         B         C         D
#> A 1.0000000 0.5171581 0.3817322 0.3639781
#> B 0.5171581 1.0000000 0.4279778 0.3355316
#> C 0.3817322 0.4279778 1.0000000 0.3496021
#> D 0.3639781 0.3355316 0.3496021 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.3903512 0.4026135 0.3443305
#> B 0.3903512 1.0000000 0.4678636 0.2624362
#> C 0.4026135 0.4678636 1.0000000 0.4798214
#> D 0.3443305 0.2624362 0.4798214 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.2688254 0.2069256 0.3289418
#> B 0.2688254 1.0000000 0.3550006 0.3234738
#> C 0.2069256 0.3550006 1.0000000 0.5482005
#> D 0.3289418 0.3234738 0.5482005 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.5396512 0.4555515 0.5354672
#> B 0.5396512 1.0000000 0.4296432 0.5985278
#> C 0.4555515 0.4296432 1.0000000 0.2208391
#> D 0.5354672 0.5985278 0.2208391 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.2944865 0.5018583 0.5175711
#> B 0.2944865 1.0000000 0.4086113 0.3634259
#> C 0.5018583 0.4086113 1.0000000 0.4454087
#> D 0.5175711 0.3634259 0.4454087 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.4349546 0.3922987 0.2806613
#> B 0.4349546 1.0000000 0.2117363 0.2471127
#> C 0.3922987 0.2117363 1.0000000 0.4359644
#> D 0.2806613 0.2471127 0.4359644 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.4331744 0.3321799 0.4254004
#> B 0.4331744 1.0000000 0.5181145 0.3315383
#> C 0.3321799 0.5181145 1.0000000 0.3274795
#> D 0.4254004 0.3315383 0.3274795 1.0000000
#>           A         B          C          D
#> A 1.0000000 0.4043722 0.17278349 0.37770673
#> B 0.4043722 1.0000000 0.21162067 0.38832563
#> C 0.1727835 0.2116207 1.00000000 0.07326301
#> D 0.3777067 0.3883256 0.07326301 1.00000000
#>           A         B         C         D
#> A 1.0000000 0.3978527 0.3366249 0.5245554
#> B 0.3978527 1.0000000 0.4912680 0.4185149
#> C 0.3366249 0.4912680 1.0000000 0.3857595
#> D 0.5245554 0.4185149 0.3857595 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.3218660 0.3005669 0.5492002
#> B 0.3218660 1.0000000 0.5250258 0.4707207
#> C 0.3005669 0.5250258 1.0000000 0.3635217
#> D 0.5492002 0.4707207 0.3635217 1.0000000

# Check global correlations - should not be as correlated
cor(di[, list(A, B, C, D)])
#>           A         B         C         D
#> A 1.0000000 0.2782913 0.5128389 0.1945679
#> B 0.2782913 1.0000000 0.1428397 0.3048219
#> C 0.5128389 0.1428397 1.0000000 0.3241647
#> D 0.1945679 0.3048219 0.3241647 1.0000000