Create correlated data

genCorData(
  n,
  mu,
  sigma,
  corMatrix = NULL,
  rho,
  corstr = "ind",
  cnames = NULL,
  idname = "id"
)

Arguments

n

Number of observations

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.

idname

The name of the index id name. Defaults to "id."

Value

A data.table with n rows and the k + 1 columns, where k is the number of means in the vector mu.

Examples

mu <- c(3, 8, 15)
sigma <- c(1, 2, 3)

corMat <- matrix(c(1, .2, .8, .2, 1, .6, .8, .6, 1), nrow = 3)

dtcor1 <- genCorData(1000, mu = mu, sigma = sigma, rho = .7, corstr = "cs")
dtcor2 <- genCorData(1000, mu = mu, sigma = sigma, corMatrix = corMat)

dtcor1
#>         id       V1        V2       V3
#>    1:    1 3.187099 10.558281 15.65874
#>    2:    2 2.652683  7.793236 16.40572
#>    3:    3 3.921587 11.177416 19.46366
#>    4:    4 3.968115  9.618889 15.48945
#>    5:    5 3.129555  8.113490 13.11778
#>   ---                                 
#>  996:  996 1.553517  4.139324 10.54947
#>  997:  997 3.084290  7.966858 15.72537
#>  998:  998 4.278067  8.317735 19.44643
#>  999:  999 2.425239  6.008262 13.97734
#> 1000: 1000 3.901372  8.382401 14.45666
dtcor2
#>         id       V1       V2        V3
#>    1:    1 4.327344 5.057118 15.071292
#>    2:    2 2.441576 6.249117 12.912647
#>    3:    3 2.400894 7.907194 13.866811
#>    4:    4 1.683221 5.947965 11.040929
#>    5:    5 0.506367 4.634667  9.618313
#>   ---                                 
#>  996:  996 1.932159 7.930175 12.620801
#>  997:  997 3.048837 9.450562 15.362907
#>  998:  998 3.410602 5.333275 13.205202
#>  999:  999 3.479042 5.746235 15.073017
#> 1000: 1000 2.360636 7.973820 12.279591

round(var(dtcor1[, .(V1, V2, V3)]), 3)
#>       V1    V2    V3
#> V1 0.885 1.274 1.817
#> V2 1.274 3.898 3.844
#> V3 1.817 3.844 8.423
round(cor(dtcor1[, .(V1, V2, V3)]), 2)
#>      V1   V2   V3
#> V1 1.00 0.69 0.67
#> V2 0.69 1.00 0.67
#> V3 0.67 0.67 1.00

round(var(dtcor2[, .(V1, V2, V3)]), 3)
#>       V1    V2    V3
#> V1 0.956 0.383 2.237
#> V2 0.383 3.959 3.400
#> V3 2.237 3.400 8.214
round(cor(dtcor2[, .(V1, V2, V3)]), 2)
#>     V1  V2  V3
#> V1 1.0 0.2 0.8
#> V2 0.2 1.0 0.6
#> V3 0.8 0.6 1.0