Create multivariate (correlated) data - for general distributions

genCorFlex(n, defs, rho = 0, tau = NULL, corstr = "cs", corMatrix = NULL)

Arguments

n

Number of observations

defs

Field definition table created by function `defData`. All definitions must be scalar. Definition specifies distribution, mean, and variance, with all caveats for each of the distributions. (See defData).

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. This is only used if tau is not specified.

Value

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

Examples

def <- defData(varname = "xNorm", formula = 0, variance = 4, dist = "normal") def <- defData(def, varname = "xGamma1", formula = 15, variance = 2, dist = "gamma") def <- defData(def, varname = "xBin", formula = 0.5, dist = "binary") def <- defData(def, varname = "xUnif1", formula = "0;10", dist = "uniform") def <- defData(def, varname = "xPois", formula = 15, dist = "poisson") def <- defData(def, varname = "xUnif2", formula = "23;28", dist = "uniform") def <- defData(def, varname = "xUnif3", formula = "100;150", dist = "uniform") def <- defData(def, varname = "xGamma2", formula = 150, variance = 0.003, dist = "gamma") def <- defData(def, varname = "xNegBin", formula = 5, variance = .8, dist = "negBinomial") dt <- genCorFlex(1000, def, tau = 0.3, corstr = "cs") cor(dt[, -"id"])
#> xNorm xGamma1 xBin xUnif1 xPois xUnif2 xUnif3 #> xNorm 1.0000000 0.3635256 0.3502807 0.4348943 0.4406840 0.4334729 0.4736992 #> xGamma1 0.3635256 1.0000000 0.2703809 0.3329087 0.3614445 0.3549100 0.3569301 #> xBin 0.3502807 0.2703809 1.0000000 0.3396448 0.3529756 0.3123344 0.3482656 #> xUnif1 0.4348943 0.3329087 0.3396448 1.0000000 0.4203605 0.4541368 0.4536201 #> xPois 0.4406840 0.3614445 0.3529756 0.4203605 1.0000000 0.3985913 0.4262241 #> xUnif2 0.4334729 0.3549100 0.3123344 0.4541368 0.3985913 1.0000000 0.4377402 #> xUnif3 0.4736992 0.3569301 0.3482656 0.4536201 0.4262241 0.4377402 1.0000000 #> xGamma2 0.4442560 0.3925799 0.3843497 0.4485447 0.4534048 0.4318113 0.4327878 #> xNegBin 0.3691570 0.3840732 0.2925915 0.3724766 0.4006984 0.3944300 0.3963623 #> xGamma2 xNegBin #> xNorm 0.4442560 0.3691570 #> xGamma1 0.3925799 0.3840732 #> xBin 0.3843497 0.2925915 #> xUnif1 0.4485447 0.3724766 #> xPois 0.4534048 0.4006984 #> xUnif2 0.4318113 0.3944300 #> xUnif3 0.4327878 0.3963623 #> xGamma2 1.0000000 0.4169939 #> xNegBin 0.4169939 1.0000000
cor(dt[, -"id"], method = "kendall")
#> xNorm xGamma1 xBin xUnif1 xPois xUnif2 xUnif3 #> xNorm 1.0000000 0.3242362 0.2873684 0.2963604 0.2956935 0.2904905 0.3192192 #> xGamma1 0.3242362 1.0000000 0.2812001 0.3005405 0.2907424 0.2985946 0.3004324 #> xBin 0.2873684 0.2812001 1.0000000 0.2770822 0.2903532 0.2549408 0.2844570 #> xUnif1 0.2963604 0.3005405 0.2770822 1.0000000 0.2929309 0.3142022 0.3121642 #> xPois 0.2956935 0.2907424 0.2903532 0.2929309 1.0000000 0.2769917 0.2974992 #> xUnif2 0.2904905 0.2985946 0.2549408 0.3142022 0.2769917 1.0000000 0.3002563 #> xUnif3 0.3192192 0.3004324 0.2844570 0.3121642 0.2974992 0.3002563 1.0000000 #> xGamma2 0.2876476 0.3199600 0.3074198 0.3055375 0.3054708 0.2859900 0.2905105 #> xNegBin 0.2711682 0.2894342 0.2912141 0.2823726 0.3055129 0.2982413 0.3088064 #> xGamma2 xNegBin #> xNorm 0.2876476 0.2711682 #> xGamma1 0.3199600 0.2894342 #> xBin 0.3074198 0.2912141 #> xUnif1 0.3055375 0.2823726 #> xPois 0.3054708 0.3055129 #> xUnif2 0.2859900 0.2982413 #> xUnif3 0.2905105 0.3088064 #> xGamma2 1.0000000 0.3065857 #> xNegBin 0.3065857 1.0000000
var(dt[, -"id"])
#> xNorm xGamma1 xBin xUnif1 xPois xUnif2 #> xNorm 4.0367045 15.923185 0.3517784 2.5255407 3.4066156 1.2386201 #> xGamma1 15.9231852 475.294680 2.9464367 20.9779896 30.3183438 11.0042976 #> xBin 0.3517784 2.946437 0.2498498 0.4907064 0.6788388 0.2220353 #> xUnif1 2.5255407 20.977990 0.4907064 8.3543838 4.6747839 1.8668378 #> xPois 3.4066156 30.318344 0.6788388 4.6747839 14.8035145 2.1810865 #> xUnif2 1.2386201 11.004298 0.2220353 1.8668378 2.1810865 2.0226714 #> xUnif3 13.9146847 113.768460 2.5451148 19.1693214 23.9760467 9.1019894 #> xGamma2 7.2968627 69.967912 1.5705636 10.5986933 14.2612609 5.0204900 #> xNegBin 3.7313742 42.124974 0.7357758 5.4162710 7.7561151 2.8221286 #> xUnif3 xGamma2 xNegBin #> xNorm 13.914685 7.296863 3.7313742 #> xGamma1 113.768460 69.967912 42.1249745 #> xBin 2.545115 1.570564 0.7357758 #> xUnif1 19.169321 10.598693 5.4162710 #> xPois 23.976047 14.261261 7.7561151 #> xUnif2 9.101989 5.020490 2.8221286 #> xUnif3 213.754068 51.727513 29.1537033 #> xGamma2 51.727513 66.831134 17.1499675 #> xNegBin 29.153703 17.149968 25.3097808
apply(dt[, -"id"], 2, mean)
#> xNorm xGamma1 xBin xUnif1 xPois xUnif2 #> -0.04650213 14.30170908 0.48000000 4.99250585 14.98300000 25.40058719 #> xUnif3 xGamma2 xNegBin #> 124.21462961 149.86789726 4.72700000