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. Field definition table created by function defDataAdd. Correlation coefficient, -1 <= rho <= 1. Use if corMatrix is not provided. 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. 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". 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. 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

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")

varname = "A", formula = "-1 + a", variance = 3,
dist = "normal"
)
varname = "B", formula = "4.5 + b", variance = .5,
dist = "normal"
)
varname = "C", formula = "5*c", variance = 3,
dist = "normal"
)
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.3062024 0.4470299 0.3869610
#> B 0.3062024 1.0000000 0.3060284 0.4224323
#> C 0.4470299 0.3060284 1.0000000 0.6152698
#> D 0.3869610 0.4224323 0.6152698 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.4502677 0.4758974 0.2090688
#> B 0.4502677 1.0000000 0.6720441 0.3670891
#> C 0.4758974 0.6720441 1.0000000 0.3709190
#> D 0.2090688 0.3670891 0.3709190 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.3151178 0.2666300 0.2319625
#> B 0.3151178 1.0000000 0.3670170 0.3673458
#> C 0.2666300 0.3670170 1.0000000 0.3757361
#> D 0.2319625 0.3673458 0.3757361 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.4702654 0.4228648 0.4269063
#> B 0.4702654 1.0000000 0.6511097 0.4654823
#> C 0.4228648 0.6511097 1.0000000 0.5140927
#> D 0.4269063 0.4654823 0.5140927 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.3025460 0.3653579 0.2985908
#> B 0.3025460 1.0000000 0.3719276 0.2709871
#> C 0.3653579 0.3719276 1.0000000 0.4309840
#> D 0.2985908 0.2709871 0.4309840 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.4164247 0.3527294 0.1922481
#> B 0.4164247 1.0000000 0.5163798 0.4001157
#> C 0.3527294 0.5163798 1.0000000 0.3981343
#> D 0.1922481 0.4001157 0.3981343 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.5267601 0.4256210 0.4790293
#> B 0.5267601 1.0000000 0.4849352 0.4944706
#> C 0.4256210 0.4849352 1.0000000 0.3851054
#> D 0.4790293 0.4944706 0.3851054 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.2726211 0.2500102 0.3504211
#> B 0.2726211 1.0000000 0.3343043 0.3675255
#> C 0.2500102 0.3343043 1.0000000 0.4198390
#> D 0.3504211 0.3675255 0.4198390 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.3400145 0.2629416 0.3527861
#> B 0.3400145 1.0000000 0.4845887 0.3238015
#> C 0.2629416 0.4845887 1.0000000 0.4974922
#> D 0.3527861 0.3238015 0.4974922 1.0000000
#>           A         B         C         D
#> A 1.0000000 0.3139947 0.5068051 0.4542754
#> B 0.3139947 1.0000000 0.5209270 0.3794740
#> C 0.5068051 0.5209270 1.0000000 0.4710229
#> D 0.4542754 0.3794740 0.4710229 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.51793243 0.4445728 0.38325573
#> B 0.5179324 1.00000000 0.1232242 0.09727062
#> C 0.4445728 0.12322415 1.0000000 0.70672855
#> D 0.3832557 0.09727062 0.7067286 1.00000000