# Correlated Data

## 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 2.694803  9.851947 0.5627331
##    2:    2 2.978733 10.087729 1.2934916
##    3:    3 5.477223 14.459296 5.2893416
##    4:    4 5.363631 15.787044 9.4356220
##    5:    5 3.686199 11.425404 3.7244193
##   ---
##  996:  996 3.987098 12.418247 3.2024848
##  997:  997 4.811493 14.042753 5.8988445
##  998:  998 4.235882 13.916894 6.7397738
##  999:  999 2.261872  9.129413 0.6543015
## 1000: 1000 3.299655  9.462865 0.2574918
# 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.236340 13.797557  2.244675
##    2:    2  2.001765 12.675495 -5.458968
##    3:    3  4.147352  8.261134  3.737179
##    4:    4  5.956926 15.359351  6.742574
##    5:    5  7.852580 14.148470  2.201871
##   ---
##  996:  996 -0.502010 11.000300  5.147081
##  997:  997  2.232273  9.307345  2.234368
##  998:  998  1.350443 13.341055 -1.390604
##  999:  999  5.860382 14.521098  6.041564
## 1000: 1000  1.256840 11.024335 -0.432417
# estimate correlation matrix
dt[, round(cor(cbind(x0, x1, x2)), 1)]
##     x0  x1  x2
## x0 1.0 0.4 0.4
## x1 0.4 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.0 2.9 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  0.719015349  1.4041582 -0.29334660
##    2:    2  0.183204937 -0.6565829  0.03225875
##    3:    3  1.472204042  1.3380213  0.26060221
##    4:    4  0.523511314 -2.2775250  0.20958652
##    5:    5 -1.190532001 -0.7852328  0.02286039
##   ---
##  996:  996  0.460710898 -0.1784745 -0.02460386
##  997:  997  1.307029729  1.2440737  0.16575144
##  998:  998  2.660968818 -0.3282456  0.31661370
##  999:  999  0.003777091 -0.8673943 -0.02052644
## 1000: 1000  0.774878914  1.5500980 -0.04238378
# 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
## 2.0 0.2

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  7  8  7
##    2:    2  9 13 10
##    3:    3  6 12  7
##    4:    4  9 17 13
##    5:    5 16 12 13
##   ---
##  996:  996  4  4  7
##  997:  997 13 10 11
##  998:  998 11  4  9
##  999:  999  4  8 11
## 1000: 1000  7 11 16
round(cor(as.matrix(dx[, .(V1, V2, V3)])), 2)
##      V1   V2   V3
## V1 1.00 0.32 0.33
## V2 0.32 1.00 0.31
## V3 0.33 0.31 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  1  1  1
##    2:    2  1  1  1
##    3:    3  1  1  1
##    4:    4  0  0  0
##    5:    5  0  0  1
##   ---
##  996:  996  0  1  1
##  997:  997  0  1  1
##  998:  998  1  1  1
##  999:  999  0  0  0
## 1000: 1000  0  0  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 11.2408250 23.8020758 21.99045775
##    2:    2  3.0993850  9.3361212  3.14981669
##    3:    3  1.2340109  5.1418105  4.77130477
##    4:    4  2.1169889  4.5321690  8.64008365
##    5:    5 16.0649270 13.0907019 49.27097791
##   ---
##  996:  996  4.5714983  9.0729693  3.67288884
##  997:  997  0.3885833  2.2577864  8.15757975
##  998:  998 11.7581651 32.4952120 48.69427761
##  999:  999  0.4120833  0.8752991  0.09811203
## 1000: 1000  0.3239261  0.6563278  2.01134725
round(cor(as.matrix(dx[, .(a, b, c)])), 2)
##      a    b    c
## a 1.00 0.68 0.69
## b 0.68 1.00 0.67
## c 0.69 0.67 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  3.1030541
##    2:    1      1  2.2742335
##    3:    1      2 15.9322077
##    4:    2      0 13.2224016
##    5:    2      1  9.4743518
##   ---
## 2996:  999      1 16.8264266
## 2997:  999      2 13.3899282
## 2998: 1000      0  1.2673701
## 2999: 1000      1  0.2554448
## 3000: 1000      2  3.0599486

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.742832 2.169037 0.2355649 2.853557        1
##     2:     2 3.908402 2.437176 0.3041783 3.602688        1
##     3:     3 2.089307 2.031818 0.2021021 2.503930        1
##     4:     4 6.116369 3.039329 0.4588211 5.602841        1
##     5:     5 3.453132 2.328707 0.2760660 3.289141        1
##    ---
##  9996:  9996 0.832190 1.791797 0.1480039 1.947289        1
##  9997:  9997 6.316734 3.100841 0.4737791 5.831923        1
##  9998:  9998 3.488477 2.336952 0.2781901 3.312474        1
##  9999:  9999 6.035385 3.014815 0.4527949 5.512824        1
## 10000: 10000 6.292627 3.093375 0.4719764 5.803873        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.742832 2.169037 0.2355649 2.853557        1 2 3 2
##     2:     2 3.908402 2.437176 0.3041783 3.602688        1 4 5 2
##     3:     3 2.089307 2.031818 0.2021021 2.503930        1 4 0 1
##     4:     4 6.116369 3.039329 0.4588211 5.602841        1 2 3 3
##     5:     5 3.453132 2.328707 0.2760660 3.289141        1 0 2 1
##    ---
##  9996:  9996 0.832190 1.791797 0.1480039 1.947289        1 2 2 2
##  9997:  9997 6.316734 3.100841 0.4737791 5.831923        1 2 4 2
##  9998:  9998 3.488477 2.336952 0.2781901 3.312474        1 3 3 3
##  9999:  9999 6.035385 3.014815 0.4527949 5.512824        1 3 2 1
## 10000: 10000 6.292627 3.093375 0.4719764 5.803873        1 4 1 2

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.742832 2.169037 0.2355649 2.853557        1  0  0  0  0
##     2:     2 3.908402 2.437176 0.3041783 3.602688        1  0  1  0  0
##     3:     3 2.089307 2.031818 0.2021021 2.503930        1  1  1  0  1
##     4:     4 6.116369 3.039329 0.4588211 5.602841        1  1  1  0  0
##     5:     5 3.453132 2.328707 0.2760660 3.289141        1  1  1  1  0
##    ---
##  9996:  9996 0.832190 1.791797 0.1480039 1.947289        1  0  1  0  0
##  9997:  9997 6.316734 3.100841 0.4737791 5.831923        1  0  1  0  1
##  9998:  9998 3.488477 2.336952 0.2781901 3.312474        1  0  0  0  0
##  9999:  9999 6.035385 3.014815 0.4527949 5.512824        1  0  1  1  0
## 10000: 10000 6.292627 3.093375 0.4719764 5.803873        1  1  0  0  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        V2
##     1:     1 2.742832 2.169037 0.2355649 2.853557        1 1.3564107 4.1633484
##     2:     2 3.908402 2.437176 0.3041783 3.602688        1 1.2774642 0.4145486
##     3:     3 2.089307 2.031818 0.2021021 2.503930        1 0.6751280 3.8242693
##     4:     4 6.116369 3.039329 0.4588211 5.602841        1 5.1754002 0.4363970
##     5:     5 3.453132 2.328707 0.2760660 3.289141        1 0.3919126 2.7579778
##    ---
##  9996:  9996 0.832190 1.791797 0.1480039 1.947289        1 2.6459003 2.8611367
##  9997:  9997 6.316734 3.100841 0.4737791 5.831923        1 5.9286527 2.3367648
##  9998:  9998 3.488477 2.336952 0.2781901 3.312474        1 0.8821161 1.6748387
##  9999:  9999 6.035385 3.014815 0.4527949 5.512824        1 2.2358986 4.5055024
## 10000: 10000 6.292627 3.093375 0.4719764 5.803873        1 0.5093749 9.0076124
##                V3        V4
##     1: 2.16437688 6.5380864
##     2: 5.01549409 1.1651384
##     3: 0.31839873 1.4304311
##     4: 4.42401215 5.7046481
##     5: 0.73926609 0.9977570
##    ---
##  9996: 3.18989340 1.9985841
##  9997: 5.57781700 4.7727476
##  9998: 0.09912782 0.2731202
##  9999: 4.47864065 2.6299499
## 10000: 1.58486272 0.1846041

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
##        cid period    xbase nperiods timeID    lambda
##    1:    1      0 8.047496        5      1  3.686766
##    2:    1      1 8.047496        5      2  6.078449
##    3:    1      2 8.047496        5      3 10.021669
##    4:    2      0 6.516184        2      4  3.163308
##    5:    2      1 6.516184        2      5  5.215413
##   ---
## 2996:  999      1 4.022269        5   2996  4.064240
## 2997:  999      2 4.022269        5   2997  6.700800
## 2998: 1000      0 7.459545        5   2998  3.476251
## 2999: 1000      1 7.459545        5   2999  5.731369
## 3000: 1000      2 7.459545        5   3000  9.449431
### 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 8.047496        5      1  3.686766       3
##    2:    1      1 8.047496        5      2  6.078449       3
##    3:    1      2 8.047496        5      3 10.021669       7
##    4:    2      0 6.516184        2      4  3.163308       2
##    5:    2      1 6.516184        2      5  5.215413       3
##   ---
## 2996:  999      1 4.022269        5   2996  4.064240       5
## 2997:  999      2 4.022269        5   2997  6.700800       6
## 2998: 1000      0 7.459545        5   2998  3.476251       6
## 2999: 1000      1 7.459545        5   2999  5.731369       7
## 3000: 1000      2 7.459545        5   3000  9.449431      12

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.4797709   0.4974509   0.1023700
round(summary(geefit)\$working.correlation, 2)
##      [,1] [,2] [,3]
## [1,] 1.00 0.55 0.55
## [2,] 0.55 1.00 0.55
## [3,] 0.55 0.55 1.00