genCorFlex: Create multivariate (correlated) data - for general...

Description Usage Arguments Value Examples

View source: R/generate_correlated_data.R

Description

Create multivariate (correlated) data - for general distributions

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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"])
cor(dt[, -"id"], method = "kendall")
var(dt[, -"id"])
apply(dt[, -"id"], 2, mean)

Example output

Loading required package: data.table
            xNorm   xGamma1      xBin    xUnif1     xPois    xUnif2    xUnif3
xNorm   1.0000000 0.4098348 0.3183649 0.4398508 0.4441573 0.4289617 0.4639305
xGamma1 0.4098348 1.0000000 0.2885350 0.3983408 0.3829825 0.3447793 0.3865606
xBin    0.3183649 0.2885350 1.0000000 0.3585087 0.3341653 0.3199520 0.3432859
xUnif1  0.4398508 0.3983408 0.3585087 1.0000000 0.4206811 0.4034433 0.4395420
xPois   0.4441573 0.3829825 0.3341653 0.4206811 1.0000000 0.4314393 0.4654842
xUnif2  0.4289617 0.3447793 0.3199520 0.4034433 0.4314393 1.0000000 0.4263075
xUnif3  0.4639305 0.3865606 0.3432859 0.4395420 0.4654842 0.4263075 1.0000000
xGamma2 0.4464916 0.3904979 0.3184090 0.4494799 0.3907215 0.4071315 0.4545403
xNegBin 0.3936877 0.3963505 0.2947030 0.4104054 0.4042050 0.3768469 0.4302988
          xGamma2   xNegBin
xNorm   0.4464916 0.3936877
xGamma1 0.3904979 0.3963505
xBin    0.3184090 0.2947030
xUnif1  0.4494799 0.4104054
xPois   0.3907215 0.4042050
xUnif2  0.4071315 0.3768469
xUnif3  0.4545403 0.4302988
xGamma2 1.0000000 0.4085452
xNegBin 0.4085452 1.0000000
            xNorm   xGamma1      xBin    xUnif1     xPois    xUnif2    xUnif3
xNorm   1.0000000 0.2804004 0.2507557 0.2866867 0.2987118 0.2827788 0.3159279
xGamma1 0.2804004 1.0000000 0.2703498 0.2978579 0.2947518 0.2500420 0.2998879
xBin    0.2507557 0.2703498 1.0000000 0.2919889 0.2754337 0.2614450 0.2804160
xUnif1  0.2866867 0.2978579 0.2919889 1.0000000 0.2963191 0.2759319 0.3020100
xPois   0.2987118 0.2947518 0.2754337 0.2963191 1.0000000 0.3070362 0.3293956
xUnif2  0.2827788 0.2500420 0.2614450 0.2759319 0.3070362 1.0000000 0.2924484
xUnif3  0.3159279 0.2998879 0.2804160 0.3020100 0.3293956 0.2924484 1.0000000
xGamma2 0.2935976 0.2966406 0.2556670 0.3096617 0.2716504 0.2777017 0.3063504
xNegBin 0.3016939 0.3043916 0.2750526 0.3134790 0.2980118 0.2864139 0.3233049
          xGamma2   xNegBin
xNorm   0.2935976 0.3016939
xGamma1 0.2966406 0.3043916
xBin    0.2556670 0.2750526
xUnif1  0.3096617 0.3134790
xPois   0.2716504 0.2980118
xUnif2  0.2777017 0.2864139
xUnif3  0.3063504 0.3233049
xGamma2 1.0000000 0.3073788
xNegBin 0.3073788 1.0000000
             xNorm    xGamma1      xBin     xUnif1      xPois    xUnif2
xNorm    3.9734791  15.973549 0.3171864  2.5302118  3.2421004 1.2065847
xGamma1 15.9735487 382.308654 2.8197422 22.4764558 27.4214318 9.5126631
xBin     0.3171864   2.819742 0.2498088  0.5170941  0.6116026 0.2256537
xUnif1   2.5302118  22.476456 0.5170941  8.3278328  4.4455258 1.6428669
xPois    3.2421004  27.421432 0.6116026  4.4455258 13.4093684 2.2293471
xUnif2   1.2065847   9.512663 0.2256537  1.6428669  2.2293471 1.9911670
xUnif3  13.4600676 110.010341 2.4972889 18.4618520 24.8094966 8.7555960
xGamma2  7.2728235  62.392163 1.3004495 10.5993826 11.6916495 4.6945381
xNegBin  4.1474369  40.957080 0.7784505  6.2592394  7.8225506 2.8103556
            xUnif3   xGamma2    xNegBin
xNorm    13.460068  7.272823  4.1474369
xGamma1 110.010341 62.392163 40.9570801
xBin      2.497289  1.300449  0.7784505
xUnif1   18.461852 10.599383  6.2592394
xPois    24.809497 11.691649  7.8225506
xUnif2    8.755596  4.694538  2.8103556
xUnif3  211.844778 54.061189 33.0995092
xGamma2  54.061189 66.774180 17.6435895
xNegBin  33.099509 17.643589 27.9309069
       xNorm      xGamma1         xBin       xUnif1        xPois       xUnif2 
  0.03270592  14.20552408   0.52100000   5.11438498  14.92900000  25.53741416 
      xUnif3      xGamma2      xNegBin 
124.94801154 150.05544042   4.96800000 

simstudy documentation built on Oct. 23, 2020, 6:55 p.m.