Correlated data

Sometimes it is desirable to simulate correlated data from a correlation matrix directly. For example, a simulation might require two random effects (e.g. a random intercept and a random slope). Correlated data like this could be generated using the defData functionality, but it may be more natural to do this with genCorData or addCorData. Currently, simstudy can only generate multivariate normal using these functions. (In the future, additional distributions will be available.)

genCorData requires the user to specify a mean vector mu, a single standard deviation or a vector of standard deviations sigma, and either a correlation matrix corMatrix or a correlation coefficient rho and a correlation structure corsrt. It is easy to see how this can be used from a few different examples.

# specifying a specific correlation matrix C
C <- matrix(c(1, 0.7, 0.2, 0.7, 1, 0.8, 0.2, 0.8, 1), nrow = 3)
C
##      [,1] [,2] [,3]
## [1,]  1.0  0.7  0.2
## [2,]  0.7  1.0  0.8
## [3,]  0.2  0.8  1.0
# generate 3 correlated variables with different location and scale for each
# field
dt <- genCorData(1000, mu = c(4, 12, 3), sigma = c(1, 2, 3), corMatrix = C)
dt
##         id       V1        V2         V3
##    1:    1 3.353493  8.419955 -2.0197502
##    2:    2 4.594132 12.961478  2.7418187
##    3:    3 5.741102 15.973465  6.5740898
##    4:    4 3.102915 10.905038  2.0321700
##    5:    5 5.192009 14.053582  3.9280850
##   ---                                   
##  996:  996 4.268228 13.093005  4.3736941
##  997:  997 4.200137 13.191286  5.8786384
##  998:  998 4.116599 13.028362  5.5578596
##  999:  999 4.703264 11.488743  0.4973061
## 1000: 1000 2.737410 10.552865  3.5977527
# estimate correlation matrix
dt[, round(cor(cbind(V1, V2, V3)), 1)]
##     V1  V2  V3
## V1 1.0 0.7 0.2
## V2 0.7 1.0 0.8
## V3 0.2 0.8 1.0
# estimate standard deviation
dt[, round(sqrt(diag(var(cbind(V1, V2, V3)))), 1)]
## V1 V2 V3 
##  1  2  3
# generate 3 correlated variables with different location but same standard
# deviation and compound symmetry (cs) correlation matrix with correlation
# coefficient = 0.4.  Other correlation matrix structures are 'independent'
# ('ind') and 'auto-regressive' ('ar1').

dt <- genCorData(1000, mu = c(4, 12, 3), sigma = 3, rho = 0.4, corstr = "cs", 
    cnames = c("x0", "x1", "x2"))
dt
##         id       x0        x1       x2
##    1:    1 4.205299 17.226427 9.979683
##    2:    2 3.441720 10.525591 2.896634
##    3:    3 5.964983 12.622046 4.341463
##    4:    4 3.965103  9.400328 7.042382
##    5:    5 1.131549  9.433495 2.220678
##   ---                                 
##  996:  996 4.272364 13.739319 7.267494
##  997:  997 3.641261 12.095348 5.089953
##  998:  998 6.548894 11.493983 6.701292
##  999:  999 6.269624 16.349909 1.685508
## 1000: 1000 1.314632  6.045181 1.780967
# estimate correlation matrix
dt[, round(cor(cbind(x0, x1, x2)), 1)]
##     x0  x1  x2
## x0 1.0 0.5 0.4
## x1 0.5 1.0 0.4
## x2 0.4 0.4 1.0
# estimate standard deviation
dt[, round(sqrt(diag(var(cbind(x0, x1, x2)))), 1)]
##  x0  x1  x2 
## 3.1 3.0 3.0

The new data generated by genCorData can be merged with an existing data set. Alternatively, addCorData will do this directly:

# define and generate the original data set
def <- defData(varname = "x", dist = "normal", formula = 0, variance = 1, id = "cid")
dt <- genData(1000, def)

# add new correlate fields a0 and a1 to 'dt'
dt <- addCorData(dt, idname = "cid", mu = c(0, 0), sigma = c(2, 0.2), rho = -0.2, 
    corstr = "cs", cnames = c("a0", "a1"))

dt
##        cid           x          a0          a1
##    1:    1 -1.10508028  0.24628790  0.14851104
##    2:    2  0.43175070 -0.17744570 -0.12220814
##    3:    3  0.31609716  1.25730445  0.16216666
##    4:    4 -0.03396137  0.02068438  0.02102240
##    5:    5  0.63353377 -2.12564990 -0.18013305
##   ---                                         
##  996:  996 -2.83019666 -2.02030474  0.07102389
##  997:  997 -0.54260754 -0.98536175  0.24058085
##  998:  998 -0.74320507 -0.49324475  0.05507231
##  999:  999  0.26939441 -1.42964261 -0.06583784
## 1000: 1000  0.01567895  1.21694243  0.37164779
# estimate correlation matrix
dt[, round(cor(cbind(a0, a1)), 1)]
##      a0   a1
## a0  1.0 -0.2
## a1 -0.2  1.0
# estimate standard deviation
dt[, round(sqrt(diag(var(cbind(a0, a1)))), 1)]
##  a0  a1 
## 1.9 0.2

Correlated data: additional distributions

Two additional functions facilitate the generation of correlated data from binomial, poisson, gamma, and uniform distributions: genCorGen and addCorGen.

genCorGen is an extension of genCorData. In the first example, we are generating data from a multivariate Poisson distribution. We start by specifying the mean of the Poisson distribution for each new variable, and then we specify the correlation structure, just as we did with the normal distribution.

l <- c(8, 10, 12) # lambda for each new variable

dx <- genCorGen(1000, nvars = 3, params1 = l, dist = "poisson", rho = .3, corstr = "cs", wide = TRUE)
dx
##         id V1 V2 V3
##    1:    1  9 11 14
##    2:    2  8 12 12
##    3:    3 10 14  8
##    4:    4  4 12 16
##    5:    5  5  9  8
##   ---              
##  996:  996  7  7  6
##  997:  997  5  6 14
##  998:  998  4  5 12
##  999:  999  4 13 20
## 1000: 1000  9 13  9
round(cor(as.matrix(dx[, .(V1, V2, V3)])), 2)
##      V1   V2   V3
## V1 1.00 0.31 0.28
## V2 0.31 1.00 0.30
## V3 0.28 0.30 1.00

We can also generate correlated binary data by specifying the probabilities:

genCorGen(1000, nvars = 3, params1 = c(.3, .5, .7), dist = "binary", rho = .8, corstr = "cs", wide = TRUE)
##         id V1 V2 V3
##    1:    1  0  1  1
##    2:    2  0  0  1
##    3:    3  1  0  1
##    4:    4  1  1  1
##    5:    5  0  0  0
##   ---              
##  996:  996  0  0  1
##  997:  997  0  1  0
##  998:  998  1  1  1
##  999:  999  0  0  0
## 1000: 1000  0  1  1

The gamma distribution requires two parameters - the mean and dispersion. (These are converted into shape and rate parameters more commonly used.)

dx <- genCorGen(1000, nvars = 3, params1 = l, params2 = c(1,1,1), dist = "gamma", rho = .7, corstr = "cs", wide = TRUE, cnames="a, b, c")
dx
##         id          a         b         c
##    1:    1  4.3470914  4.469336  2.446167
##    2:    2  7.3506401  4.129969  1.428733
##    3:    3  3.9425059 16.149295  8.470703
##    4:    4  6.6694311  2.578483  4.441524
##    5:    5  9.4170093  8.558149  3.807168
##   ---                                    
##  996:  996  4.4074354 11.242832 25.784619
##  997:  997  0.8141126  2.664490  4.313770
##  998:  998 19.9621914 11.835678 26.788974
##  999:  999  4.3829124  5.997511  4.332851
## 1000: 1000  0.8544809  3.586636  6.598277
round(cor(as.matrix(dx[, .(a, b, c)])), 2)
##      a    b    c
## a 1.00 0.66 0.62
## b 0.66 1.00 0.65
## c 0.62 0.65 1.00

These data sets can be generated in either wide or long form. So far, we have generated wide form data, where there is one row per unique id. Now, we will generate data using the long form, where the correlated data are on different rows, so that there are repeated measurements for each id. An id will have multiple records (i.e. one id will appear on multiple rows):

dx <- genCorGen(1000, nvars = 3, params1 = l, params2 = c(1,1,1), dist = "gamma", rho = .7, corstr = "cs", wide = FALSE, cnames="NewCol")
dx
##         id period    NewCol
##    1:    1      0 2.1989145
##    2:    1      1 0.6165429
##    3:    1      2 1.4537038
##    4:    2      0 0.4029786
##    5:    2      1 1.3761356
##   ---                      
## 2996:  999      1 9.2763219
## 2997:  999      2 9.6776784
## 2998: 1000      0 4.0413617
## 2999: 1000      1 8.5108579
## 3000: 1000      2 3.9268922

addCorGen allows us to create correlated data from an existing data set, as one can already do using addCorData. In the case of addCorGen, the parameter(s) used to define the distribution are created as a field (or fields) in the dataset. The correlated data are added to the existing data set. In the example below, we are going to generate three sets (poisson, binary, and gamma) of correlated data with means that are a function of the variable xbase, which varies by id.

First we define the data and generate a data set:

def <- defData(varname = "xbase", formula = 5, variance = .2, dist = "gamma", id = "cid")
def <- defData(def, varname = "lambda", formula = ".5 + .1*xbase", dist="nonrandom", link = "log")
def <- defData(def, varname = "p", formula = "-2 + .3*xbase", dist="nonrandom", link = "logit")
def <- defData(def, varname = "gammaMu", formula = ".5 + .2*xbase", dist="nonrandom", link = "log")
def <- defData(def, varname = "gammaDis", formula = 1, dist="nonrandom")

dt <- genData(10000, def)
dt
##          cid    xbase   lambda         p   gammaMu gammaDis
##     1:     1 2.931810 2.210417 0.2459265  2.963473        1
##     2:     2 7.678095 3.553061 0.5752804  7.656990        1
##     3:     3 9.655786 4.330048 0.7102783 11.372035        1
##     4:     4 6.708888 3.224858 0.5031666  6.307741        1
##     5:     5 9.288025 4.173698 0.6870594 10.565617        1
##    ---                                                     
##  9996:  9996 3.447629 2.327426 0.2757362  3.285523        1
##  9997:  9997 4.704637 2.639168 0.3569541  4.224612        1
##  9998:  9998 5.316521 2.805697 0.4001012  4.774572        1
##  9999:  9999 7.436157 3.468130 0.5574568  7.295307        1
## 10000: 10000 5.201867 2.773713 0.3918744  4.666332        1

The Poisson distribution has a single parameter, lambda:

dtX1 <- addCorGen(dtOld = dt, idvar = "cid", nvars = 3, rho = .1, corstr = "cs",
                    dist = "poisson", param1 = "lambda", cnames = "a, b, c")
dtX1
##          cid    xbase   lambda         p   gammaMu gammaDis a b c
##     1:     1 2.931810 2.210417 0.2459265  2.963473        1 3 2 0
##     2:     2 7.678095 3.553061 0.5752804  7.656990        1 4 5 2
##     3:     3 9.655786 4.330048 0.7102783 11.372035        1 1 3 4
##     4:     4 6.708888 3.224858 0.5031666  6.307741        1 5 1 5
##     5:     5 9.288025 4.173698 0.6870594 10.565617        1 3 5 1
##    ---                                                           
##  9996:  9996 3.447629 2.327426 0.2757362  3.285523        1 4 4 5
##  9997:  9997 4.704637 2.639168 0.3569541  4.224612        1 3 2 1
##  9998:  9998 5.316521 2.805697 0.4001012  4.774572        1 2 2 3
##  9999:  9999 7.436157 3.468130 0.5574568  7.295307        1 2 5 6
## 10000: 10000 5.201867 2.773713 0.3918744  4.666332        1 0 0 1

The Bernoulli (binary) distribution has a single parameter, p:

dtX2 <- addCorGen(dtOld = dt, idvar = "cid", nvars = 4, rho = .4, corstr = "ar1",
                    dist = "binary", param1 = "p")
dtX2
##          cid    xbase   lambda         p   gammaMu gammaDis V1 V2 V3 V4
##     1:     1 2.931810 2.210417 0.2459265  2.963473        1  1  0  0  0
##     2:     2 7.678095 3.553061 0.5752804  7.656990        1  0  0  1  1
##     3:     3 9.655786 4.330048 0.7102783 11.372035        1  0  1  1  0
##     4:     4 6.708888 3.224858 0.5031666  6.307741        1  1  0  0  0
##     5:     5 9.288025 4.173698 0.6870594 10.565617        1  1  1  1  1
##    ---                                                                 
##  9996:  9996 3.447629 2.327426 0.2757362  3.285523        1  0  0  0  0
##  9997:  9997 4.704637 2.639168 0.3569541  4.224612        1  0  0  0  0
##  9998:  9998 5.316521 2.805697 0.4001012  4.774572        1  0  1  0  1
##  9999:  9999 7.436157 3.468130 0.5574568  7.295307        1  0  1  0  1
## 10000: 10000 5.201867 2.773713 0.3918744  4.666332        1  0  1  1  1

The Gamma distribution has two parameters - in simstudy the mean and dispersion are specified:

dtX3 <- addCorGen(dtOld = dt, idvar = "cid", nvars = 4, rho = .4, corstr = "cs",
                  dist = "gamma", param1 = "gammaMu", param2 = "gammaDis")
dtX3
##          cid    xbase   lambda         p   gammaMu gammaDis         V1
##     1:     1 2.931810 2.210417 0.2459265  2.963473        1  3.8137410
##     2:     2 7.678095 3.553061 0.5752804  7.656990        1  6.9510779
##     3:     3 9.655786 4.330048 0.7102783 11.372035        1  0.4831766
##     4:     4 6.708888 3.224858 0.5031666  6.307741        1  5.3421282
##     5:     5 9.288025 4.173698 0.6870594 10.565617        1  1.2884354
##    ---                                                                
##  9996:  9996 3.447629 2.327426 0.2757362  3.285523        1  0.9909068
##  9997:  9997 4.704637 2.639168 0.3569541  4.224612        1  2.3176190
##  9998:  9998 5.316521 2.805697 0.4001012  4.774572        1 15.4583810
##  9999:  9999 7.436157 3.468130 0.5574568  7.295307        1  1.3767475
## 10000: 10000 5.201867 2.773713 0.3918744  4.666332        1  2.5109895
##                V2         V3        V4
##     1:  4.3943664  5.5059636 4.8472005
##     2:  2.1925994  9.6898614 4.0615726
##     3: 21.5386109  6.1788638 9.4226073
##     4:  0.6523672  1.5033362 4.9449439
##     5: 10.3359076  3.0968809 4.9822934
##    ---                                
##  9996:  2.6538401  1.5724529 1.1865155
##  9997:  0.8596992  2.1715145 1.8861922
##  9998:  2.2746489  0.8784057 1.2459575
##  9999: 21.4411361 17.6925294 2.7063535
## 10000:  1.1749327  5.7689170 0.8522827

If we have data in long form (e.g. longitudinal data), the function will recognize the structure:

def <- defData(varname = "xbase", formula = 5, variance = .4, dist = "gamma", id = "cid")
def <- defData(def, "nperiods", formula = 3, dist = "noZeroPoisson")

def2 <- defDataAdd(varname = "lambda", formula = ".5+.5*period + .1*xbase", dist="nonrandom", link = "log")

dt <- genData(1000, def)

dtLong <- addPeriods(dt, idvars = "cid", nPeriods = 3)
dtLong <- addColumns(def2, dtLong)

dtLong
##        cid period    xbase nperiods timeID   lambda
##    1:    1      0 5.893503        3      1 2.972342
##    2:    1      1 5.893503        3      2 4.900564
##    3:    1      2 5.893503        3      3 8.079664
##    4:    2      0 7.582580        4      4 3.519285
##    5:    2      1 7.582580        4      5 5.802321
##   ---                                              
## 2996:  999      1 5.240540        3   2996 4.590799
## 2997:  999      2 5.240540        3   2997 7.568948
## 2998: 1000      0 6.761740        3   2998 3.241947
## 2999: 1000      1 6.761740        3   2999 5.345066
## 3000: 1000      2 6.761740        3   3000 8.812525
### Generate the data 

dtX3 <- addCorGen(dtOld = dtLong, idvar = "cid", nvars = 3, rho = .6, corstr = "cs",
                  dist = "poisson", param1 = "lambda", cnames = "NewPois")
dtX3
##        cid period    xbase nperiods timeID   lambda NewPois
##    1:    1      0 5.893503        3      1 2.972342       0
##    2:    1      1 5.893503        3      2 4.900564       3
##    3:    1      2 5.893503        3      3 8.079664       4
##    4:    2      0 7.582580        4      4 3.519285       2
##    5:    2      1 7.582580        4      5 5.802321       2
##   ---                                                      
## 2996:  999      1 5.240540        3   2996 4.590799      11
## 2997:  999      2 5.240540        3   2997 7.568948      12
## 2998: 1000      0 6.761740        3   2998 3.241947       8
## 2999: 1000      1 6.761740        3   2999 5.345066       7
## 3000: 1000      2 6.761740        3   3000 8.812525      16

We can fit a generalized estimating equation (GEE) model and examine the coefficients and the working correlation matrix. They match closely to the data generating parameters:

geefit <- gee(NewPois ~ period + xbase, data = dtX3, id = cid, family = poisson, corstr = "exchangeable")
## Beginning Cgee S-function, @(#) geeformula.q 4.13 98/01/27
## running glm to get initial regression estimate
## (Intercept)      period       xbase 
##   0.4840977   0.4942036   0.1015991
round(summary(geefit)$working.correlation, 2)
##      [,1] [,2] [,3]
## [1,] 1.00 0.59 0.59
## [2,] 0.59 1.00 0.59
## [3,] 0.59 0.59 1.00