R/add_correlated_data.R
addCorGen.Rd
Create multivariate (correlated) data - for general distributions
addCorGen(
dtOld,
nvars = NULL,
idvar = "id",
rho = NULL,
corstr = NULL,
corMatrix = NULL,
dist,
param1,
param2 = NULL,
cnames = NULL,
method = "copula",
...
)
The data set that will be augmented. If the data set includes a single record per id, the new data table will be created as a "wide" data set. If the original data set includes multiple records per id, the new data set will be in "long" format.
The number of new variables to create for each id. This is only applicable when the data are generated from a data set that includes one record per id.
String variable name of column represents individual level id for correlated data.
Correlation coefficient, -1 <= rho <= 1. Use if corMatrix is not provided.
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.
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.
A string indicating "normal", "binary", "poisson" or "gamma".
A string that represents the column in dtOld that contains the parameter for the mean of the distribution. In the case of the uniform distribution the column specifies the minimum.
A string that represents the column in dtOld that contains a possible second parameter for the distribution. For the normal distribution, this will be the variance; for the gamma distribution, this will be the dispersion; and for the uniform distribution, this will be the maximum.
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.
Two methods are available to generate correlated data. (1) "copula" uses the multivariate Gaussian copula method that is applied to all other distributions; this applies to all available distributions. (2) "ep" uses an algorithm developed by Emrich and Piedmonte (1991).
May include additional arguments that have been deprecated and are no longer used.
Original data.table with added column(s) of correlated data
Emrich LJ, Piedmonte MR. A Method for Generating High-Dimensional Multivariate Binary Variates. The American Statistician 1991;45:302-4.
# Wide example
def <- defData(varname = "xbase", formula = 5, variance = .4, dist = "gamma", id = "cid")
def <- defData(def, varname = "lambda", formula = ".5 + .1*xbase", dist = "nonrandom", link = "log")
dt <- genData(100, def)
addCorGen(
dtOld = dt, idvar = "cid", nvars = 3, rho = .7, corstr = "cs",
dist = "poisson", param1 = "lambda"
)
#> cid xbase lambda V1 V2 V3
#> 1: 1 9.6292829 4.318587 2 3 2
#> 2: 2 3.6321483 2.370770 1 2 1
#> 3: 3 12.9489627 6.018850 10 8 10
#> 4: 4 2.3522736 2.085956 4 4 3
#> 5: 5 4.4012583 2.560304 2 2 2
#> 6: 6 1.7852378 1.970966 3 1 3
#> 7: 7 0.5918623 1.749248 2 1 1
#> 8: 8 9.9127846 4.442772 2 2 2
#> 9: 9 10.4406319 4.683582 6 8 6
#> 10: 10 11.0867856 4.996205 7 10 6
#> 11: 11 6.6164892 3.195198 2 1 2
#> 12: 12 1.8512361 1.984017 0 1 1
#> 13: 13 1.1231912 1.844705 1 1 3
#> 14: 14 12.8615307 5.966456 10 6 7
#> 15: 15 10.5756391 4.747242 5 1 3
#> 16: 16 2.3306674 2.081454 3 3 2
#> 17: 17 7.6383593 3.538971 4 0 2
#> 18: 18 4.8757185 2.684708 3 2 4
#> 19: 19 2.7802567 2.177170 3 4 1
#> 20: 20 1.8537667 1.984519 2 3 2
#> 21: 21 1.2809387 1.874035 2 1 1
#> 22: 22 0.8172840 1.789128 1 1 1
#> 23: 23 5.0782549 2.739637 4 3 2
#> 24: 24 15.3144200 7.625074 8 5 8
#> 25: 25 5.5558979 2.873670 7 4 5
#> 26: 26 4.2572214 2.523690 1 2 2
#> 27: 27 7.0486319 3.336303 4 2 5
#> 28: 28 1.8224889 1.978322 1 1 3
#> 29: 29 5.6565204 2.902731 3 4 3
#> 30: 30 4.3130389 2.537816 2 3 2
#> 31: 31 0.9545000 1.813847 2 1 3
#> 32: 32 0.7921273 1.784633 5 5 7
#> 33: 33 4.0537847 2.472868 3 4 3
#> 34: 34 3.5446621 2.350120 6 6 3
#> 35: 35 9.8699108 4.423765 2 2 2
#> 36: 36 8.9268542 4.025646 1 4 4
#> 37: 37 5.1115820 2.748783 1 3 0
#> 38: 38 1.4311614 1.902400 3 3 4
#> 39: 39 3.4802980 2.335042 2 2 2
#> 40: 40 10.1337863 4.542051 5 6 9
#> 41: 41 4.2338954 2.517810 2 2 4
#> 42: 42 11.0587317 4.982208 2 3 3
#> 43: 43 6.3556266 3.112925 4 4 4
#> 44: 44 0.8162432 1.788942 1 1 1
#> 45: 45 1.0209911 1.825948 3 4 4
#> 46: 46 3.4074225 2.318087 1 2 1
#> 47: 47 7.2841327 3.415805 1 1 2
#> 48: 48 9.8041693 4.394778 4 6 5
#> 49: 49 3.8437226 2.421464 2 3 2
#> 50: 50 4.8900339 2.688554 3 5 5
#> 51: 51 7.7424120 3.575987 3 4 4
#> 52: 52 2.5148381 2.120144 4 5 3
#> 53: 53 3.0595500 2.238834 1 1 1
#> 54: 54 11.9154848 5.427879 6 5 6
#> 55: 55 1.7907319 1.972049 0 1 1
#> 56: 56 11.6112649 5.265239 1 1 2
#> 57: 57 5.0384952 2.728766 6 6 4
#> 58: 58 1.8713754 1.988017 1 3 1
#> 59: 59 5.5370579 2.868261 0 0 1
#> 60: 60 5.8957533 2.973011 2 1 3
#> 61: 61 4.4540587 2.573858 1 1 2
#> 62: 62 1.8741056 1.988560 2 1 1
#> 63: 63 5.1555747 2.760902 5 5 6
#> 64: 64 1.0865404 1.837956 0 0 1
#> 65: 65 2.1162690 2.037303 4 5 6
#> 66: 66 3.0413396 2.234760 0 1 0
#> 67: 67 3.7954359 2.409800 3 2 4
#> 68: 68 2.4326257 2.102785 1 2 2
#> 69: 69 10.8606807 4.884506 5 3 6
#> 70: 70 8.0515802 3.688272 1 2 4
#> 71: 71 4.7993863 2.664293 2 1 2
#> 72: 72 7.4702037 3.479959 2 3 4
#> 73: 73 6.3352036 3.106574 2 4 2
#> 74: 74 3.8343222 2.419189 4 5 4
#> 75: 75 2.7756507 2.176167 2 4 3
#> 76: 76 4.2449092 2.520585 2 1 4
#> 77: 77 5.0469626 2.731078 3 2 2
#> 78: 78 6.8285181 3.263668 2 3 1
#> 79: 79 5.0488720 2.731599 2 3 3
#> 80: 80 2.1562288 2.045460 3 2 2
#> 81: 81 10.3766671 4.653719 3 3 2
#> 82: 82 6.9792761 3.313243 2 0 1
#> 83: 83 1.4729663 1.910369 0 0 0
#> 84: 84 4.8587565 2.680158 4 3 3
#> 85: 85 5.5950195 2.884934 0 2 2
#> 86: 86 3.6563823 2.376522 3 2 3
#> 87: 87 15.2472444 7.574024 7 11 9
#> 88: 88 6.4822398 3.152589 3 1 2
#> 89: 89 5.8501291 2.959478 2 2 1
#> 90: 90 4.7771525 2.658376 2 2 1
#> 91: 91 11.3057877 5.106830 5 5 6
#> 92: 92 4.4891017 2.582893 1 1 2
#> 93: 93 6.8237604 3.262116 3 2 3
#> 94: 94 9.3424364 4.196470 5 8 8
#> 95: 95 3.6647984 2.378523 3 3 2
#> 96: 96 5.1178049 2.750494 5 7 7
#> 97: 97 8.1047784 3.707945 4 4 3
#> 98: 98 2.8276292 2.187508 4 3 2
#> 99: 99 11.2362302 5.071431 2 4 2
#> 100: 100 2.6063244 2.139629 3 3 2
#> cid xbase lambda V1 V2 V3
# Long example
def <- defData(varname = "xbase", formula = 5, variance = .4, dist = "gamma", id = "cid")
def2 <- defDataAdd(
varname = "p", formula = "-3+.2*period + .3*xbase",
dist = "nonrandom", link = "logit"
)
dt <- genData(100, def)
dtLong <- addPeriods(dt, idvars = "cid", nPeriods = 3)
dtLong <- addColumns(def2, dtLong)
addCorGen(
dtOld = dtLong, idvar = "cid", nvars = NULL, rho = .7, corstr = "cs",
dist = "binary", param1 = "p"
)
#> cid period xbase timeID p X
#> 1: 1 0 8.120489 1 0.36265632 0
#> 2: 1 1 8.120489 2 0.41002708 1
#> 3: 1 2 8.120489 3 0.45912805 1
#> 4: 2 0 11.441717 4 0.60647407 0
#> 5: 2 1 11.441717 5 0.65305952 0
#> ---
#> 296: 99 1 4.586292 296 0.19401770 0
#> 297: 99 2 4.586292 297 0.22721355 0
#> 298: 100 0 1.628284 298 0.07505488 0
#> 299: 100 1 1.628284 299 0.09017379 0
#> 300: 100 2 1.628284 300 0.10798267 0