Draw multivariate random variables
draw_multivariate(formula, sep = "_")
Formula describing the multivariate draw. The lefthand side is the names or prefix and the right-hand side is the multivariate draw function call, such as mvrnorm from the MASS library or rmnom from the extraDistr library.
Separator string between prefix and variable number. Only used when a single character string is provided and multiple variables created.
tibble
library(MASS)
# draw from multivariate normal distribution
dat <-
draw_multivariate(c(Y_1, Y_2) ~ mvrnorm(
n = 500,
mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
))
cor(dat)
#> Y_1 Y_2
#> Y_1 1.0000000 0.4805853
#> Y_2 0.4805853 1.0000000
# equivalently, you can provide a prefix for the variable names
# (easier if you have many variables)
draw_multivariate(Y ~ mvrnorm(
n = 5,
mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
))
#> Y_1 Y_2
#> 1 -0.72331399 -1.5682141
#> 2 0.52411755 0.5585430
#> 3 0.12146162 -0.9052185
#> 4 -0.75028876 -1.0880032
#> 5 0.07067974 0.4937584
# within fabricate
fabricate(
N = 100,
draw_multivariate(c(Y_1, Y_2) ~ mvrnorm(
n = N,
mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
))
)
#> ID Y_1 Y_2
#> 1 001 -0.249238581 -0.24196422
#> 2 002 0.578720114 -1.04120080
#> 3 003 -1.371198381 -0.60474142
#> 4 004 -1.269118059 -0.28781907
#> 5 005 0.440045174 1.11212945
#> 6 006 0.699424236 1.40818850
#> 7 007 -0.309095216 -2.69926979
#> 8 008 -1.493219352 -1.27414771
#> 9 009 -0.814588539 1.55212810
#> 10 010 -1.468918081 0.48589555
#> 11 011 -0.246223787 -0.98449919
#> 12 012 1.985123731 1.34708755
#> 13 013 -0.867045316 -0.83585185
#> 14 014 0.031555901 -0.09859618
#> 15 015 0.787302518 -0.05220057
#> 16 016 -1.990977798 -2.06061045
#> 17 017 -0.825751694 0.34067834
#> 18 018 -2.466401144 -0.66853192
#> 19 019 1.928615175 1.63768440
#> 20 020 0.244632236 0.20537374
#> 21 021 0.376456014 0.29989440
#> 22 022 1.085530304 1.32226989
#> 23 023 -1.160763916 -0.65483113
#> 24 024 0.561170571 0.86506282
#> 25 025 0.555772490 2.21893075
#> 26 026 0.177111668 0.89225210
#> 27 027 -0.096489954 -0.35199373
#> 28 028 -0.128134498 -0.31028925
#> 29 029 1.435230353 0.86127214
#> 30 030 -1.511234135 -1.22593465
#> 31 031 -0.774534408 0.21781907
#> 32 032 -1.265771878 0.65634510
#> 33 033 0.567691529 -0.62201564
#> 34 034 0.124651891 0.52409181
#> 35 035 -0.435936707 -1.84307634
#> 36 036 -0.559674671 -1.04672408
#> 37 037 1.359914014 -0.07032413
#> 38 038 0.199656642 -0.01074987
#> 39 039 -0.282109775 0.48917331
#> 40 040 -0.447583176 0.09542241
#> 41 041 -0.075217025 -0.30547076
#> 42 042 -0.922701328 -0.84931922
#> 43 043 -0.163025497 -0.52317774
#> 44 044 -1.233698410 -0.62457480
#> 45 045 -0.355084881 -0.04922422
#> 46 046 0.119954236 -0.32831699
#> 47 047 0.106492225 0.80725691
#> 48 048 -0.311080263 -1.01430733
#> 49 049 -0.797784434 0.06185148
#> 50 050 0.086976992 0.47024383
#> 51 051 -0.333723360 -1.24847564
#> 52 052 0.592179556 -0.34452216
#> 53 053 0.006278424 -0.19304856
#> 54 054 -0.196972882 -0.05685144
#> 55 055 0.277743438 0.67306560
#> 56 056 -1.107866342 -0.11731010
#> 57 057 0.914035780 0.74876986
#> 58 058 -0.898181467 -0.51548974
#> 59 059 1.764604744 -0.50159866
#> 60 060 -1.653065963 -0.87083272
#> 61 061 -1.200565568 -2.38824705
#> 62 062 0.219259161 -0.45873470
#> 63 063 -1.498428045 -2.65398910
#> 64 064 -0.662000401 -0.68281406
#> 65 065 0.915309507 -0.37015950
#> 66 066 -1.017321130 -0.30933001
#> 67 067 -0.033773833 0.48179605
#> 68 068 0.961746120 0.08911022
#> 69 069 0.137592290 -0.03992734
#> 70 070 0.411111822 -0.83051560
#> 71 071 1.192368801 -1.16809026
#> 72 072 -0.135224542 -0.76942083
#> 73 073 0.423912304 -1.49758388
#> 74 074 -0.740020454 -1.10304793
#> 75 075 0.622724777 1.68750272
#> 76 076 0.273515380 0.88751637
#> 77 077 1.083920431 0.06133327
#> 78 078 0.462855160 0.58308859
#> 79 079 0.484028749 -0.06065791
#> 80 080 1.344138229 -0.84940344
#> 81 081 -0.207648740 -2.09186858
#> 82 082 0.237185543 -0.26548254
#> 83 083 -1.083001209 -1.96749736
#> 84 084 1.605498588 0.12415789
#> 85 085 0.739526807 0.15280437
#> 86 086 -2.628897800 -1.81094021
#> 87 087 -0.161452878 -0.83020827
#> 88 088 0.157299671 -0.87352446
#> 89 089 -0.075122675 -0.63555152
#> 90 090 1.007556443 1.00737949
#> 91 091 -2.070048344 -1.58165978
#> 92 092 -0.289964486 -0.80932314
#> 93 093 -1.398694529 0.14475979
#> 94 094 -0.418040438 -0.16203912
#> 95 095 0.665419418 0.46313397
#> 96 096 -0.693673765 -0.94828116
#> 97 097 0.691600437 -0.12981326
#> 98 098 1.045031057 -0.02245676
#> 99 099 -1.968203961 1.87173976
#> 100 100 0.963264288 0.80182437
# You can also write the following, which works but gives less control over the names
fabricate(N = 100,
Y = mvrnorm(
n = N,
mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
))
#> ID Y.1 Y.2
#> 1 001 -0.456439968 0.292448736
#> 2 002 1.026346665 1.393055819
#> 3 003 -0.848696483 -0.291614381
#> 4 004 1.613899073 1.320767589
#> 5 005 -0.236357334 0.095876282
#> 6 006 0.629772504 0.610259468
#> 7 007 1.139005767 -0.568400067
#> 8 008 -0.414897821 -0.969379070
#> 9 009 0.254428538 -0.636479335
#> 10 010 0.072115141 -0.243650184
#> 11 011 -0.808927675 -1.231389269
#> 12 012 -0.319798247 0.072823990
#> 13 013 -1.054774226 -1.275237109
#> 14 014 0.995060025 -0.247695557
#> 15 015 0.857168238 0.726374510
#> 16 016 0.285269005 0.195857715
#> 17 017 -0.745074439 -1.479426943
#> 18 018 2.043693673 2.151755602
#> 19 019 -0.240263268 -0.658542703
#> 20 020 0.958976611 0.373788335
#> 21 021 -2.771782629 -1.866014568
#> 22 022 0.609711090 0.411865428
#> 23 023 0.505365393 -1.057848374
#> 24 024 -0.553194338 -0.808380110
#> 25 025 -0.086336348 -0.777329474
#> 26 026 0.480979422 -0.217622496
#> 27 027 0.221868842 0.921622062
#> 28 028 0.259093054 -0.211896047
#> 29 029 1.788737805 0.856123150
#> 30 030 -0.717994080 -0.033090088
#> 31 031 1.926064357 1.922675998
#> 32 032 0.082303546 0.471195457
#> 33 033 1.557349768 -0.201892024
#> 34 034 -0.630582391 -1.177045789
#> 35 035 -1.828638237 -1.135572085
#> 36 036 -0.756508973 0.083440301
#> 37 037 -0.598167504 -0.253944692
#> 38 038 -1.089309812 -0.256724986
#> 39 039 -1.730106517 -0.098922908
#> 40 040 -0.328800184 -0.441311084
#> 41 041 1.035937096 1.173661090
#> 42 042 -1.347658048 -0.198535201
#> 43 043 -0.341438322 0.073944010
#> 44 044 -0.842825188 -0.107488494
#> 45 045 1.268284358 0.700108413
#> 46 046 -0.102930046 0.090884632
#> 47 047 -1.179909019 -1.340843948
#> 48 048 0.433538236 1.281192724
#> 49 049 0.577530014 1.379800680
#> 50 050 -0.428045760 0.337777316
#> 51 051 -0.259273259 0.110962086
#> 52 052 0.005567369 0.464667688
#> 53 053 -1.186581820 -2.037232164
#> 54 054 1.538192132 0.773406863
#> 55 055 -0.968563098 -1.238138198
#> 56 056 1.956249440 0.427170133
#> 57 057 0.348867921 -0.436352216
#> 58 058 -1.910918560 -1.008978808
#> 59 059 -2.055647724 0.232183357
#> 60 060 -0.814847552 -0.622692841
#> 61 061 -0.635812604 -0.826156811
#> 62 062 0.752022464 0.683905312
#> 63 063 1.495177252 2.756046342
#> 64 064 0.384176030 0.640617364
#> 65 065 -0.138774055 -0.283400173
#> 66 066 1.183253265 -0.975024591
#> 67 067 0.810443773 1.872351572
#> 68 068 -0.078927117 1.182220877
#> 69 069 0.710214775 -0.239504545
#> 70 070 0.318628262 1.623181593
#> 71 071 -0.145980487 -0.307371110
#> 72 072 0.175535269 -0.529878272
#> 73 073 0.826912069 -0.177048186
#> 74 074 -0.055079549 1.097783477
#> 75 075 1.857052967 2.095582877
#> 76 076 -0.281906699 -0.165978888
#> 77 077 -0.886819607 -0.008036618
#> 78 078 0.723901664 1.240723352
#> 79 079 -1.133198633 -1.248977716
#> 80 080 -1.124634518 0.049682193
#> 81 081 0.754387863 -0.748214392
#> 82 082 1.159118598 0.520389874
#> 83 083 -0.562077939 -1.090956503
#> 84 084 2.831918834 0.965816769
#> 85 085 2.332515873 0.723561245
#> 86 086 -0.433314099 -0.768417190
#> 87 087 0.564411732 0.037035945
#> 88 088 0.583756445 -0.442675191
#> 89 089 0.166405040 0.201148847
#> 90 090 -0.592363276 -0.249651285
#> 91 091 -0.562395441 0.288419096
#> 92 092 -0.197257807 0.441301738
#> 93 093 2.420337490 1.255701517
#> 94 094 0.810721100 1.225850507
#> 95 095 0.147942861 -0.719595367
#> 96 096 -0.809554282 0.648645673
#> 97 097 0.548021209 0.930638649
#> 98 098 0.379931857 1.164330507
#> 99 099 1.193341127 1.008366588
#> 100 100 0.212102432 -0.912422550