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

Description Usage Arguments Value Examples

View source: R/addCorFlex.R

Description

Create multivariate (correlated) data - for general distributions

Usage

1
2
addCorFlex(dt, defs, rho = 0, tau = NULL, corstr = "cs",
  corMatrix = NULL)

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.

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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)]))
}

# Check global correlations - should not be as correlated
cor(di[, list(A, B, C, D)])

kgoldfeld/simstudy documentation built on Nov. 8, 2018, 7:41 p.m.