Draw multivariate random variables

draw_multivariate(formula, sep = "_")

Arguments

formula

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.

sep

Separator string between prefix and variable number. Only used when a single character string is provided and multiple variables created.

Value

tibble

Examples


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.4569785
#> Y_2 0.4569785 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  1.9483802  0.4971360
#> 2 -0.9623803 -0.2275308
#> 3  0.8766591 -0.5132523
#> 4 -0.5009920  0.6545170
#> 5 -1.0706870  0.3176628

# 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 -1.75838747 -0.305394817
#> 2   002  0.49512035  1.364335415
#> 3   003 -0.26760296 -0.598114346
#> 4   004 -1.34498504 -1.504375238
#> 5   005  0.13387904  0.773988012
#> 6   006  1.52543408  0.887352422
#> 7   007  1.69029022  1.866850669
#> 8   008  0.91083407  1.853377218
#> 9   009 -0.55658289 -1.082421951
#> 10  010 -0.10185072  0.111880891
#> 11  011 -0.03237983 -0.220238446
#> 12  012 -0.26994707  0.581064117
#> 13  013  0.98085810  0.646053885
#> 14  014 -0.93961733 -0.215240915
#> 15  015  0.83916804  1.302535620
#> 16  016 -1.48389204 -1.354082369
#> 17  017  0.43427180 -0.606213174
#> 18  018  0.61419985  0.001483948
#> 19  019  0.83439453  0.551064203
#> 20  020 -0.55097226 -0.979307935
#> 21  021  0.39053937  1.211186112
#> 22  022 -0.42529168 -0.222451027
#> 23  023 -0.14747518 -0.472360605
#> 24  024  0.40526767  0.158497638
#> 25  025  0.69828273 -0.268442961
#> 26  026 -1.29927217 -1.774079751
#> 27  027  2.58837956  1.255935721
#> 28  028 -0.78130885 -0.435619129
#> 29  029 -0.06987219 -1.064375070
#> 30  030 -1.54893087  0.774766171
#> 31  031  0.43337965  0.284874908
#> 32  032 -1.58868508 -0.375124661
#> 33  033  0.21523188  0.799686446
#> 34  034 -0.22498070  0.562488688
#> 35  035  1.01254563  2.730540080
#> 36  036  1.15168407  0.890724114
#> 37  037  1.19930025  0.966017966
#> 38  038 -0.34546300 -0.631989079
#> 39  039  0.21551936 -1.307912362
#> 40  040  0.47165985  1.048184397
#> 41  041 -1.03632864 -1.608002683
#> 42  042  0.37264174  1.433496345
#> 43  043  1.11651348  1.997850353
#> 44  044 -1.51880842  0.360049892
#> 45  045  0.32621180 -0.382636299
#> 46  046 -1.74147276 -1.745295633
#> 47  047 -0.12774006 -0.100337709
#> 48  048 -0.98998069  0.042970384
#> 49  049 -0.84593133 -0.411202822
#> 50  050 -0.43645846 -0.867263019
#> 51  051  0.16259443 -1.033140208
#> 52  052 -1.33917119  0.080492079
#> 53  053 -0.53586487  0.265402168
#> 54  054 -0.02204544  0.678204919
#> 55  055  1.67661240  0.447316770
#> 56  056  1.49903686  1.679946396
#> 57  057 -0.99501363  0.124265816
#> 58  058  0.33707337  0.736599701
#> 59  059  1.29982863 -0.160419216
#> 60  060 -0.44430964  0.103239064
#> 61  061  0.56635929  0.337858766
#> 62  062 -0.21509810 -0.051255177
#> 63  063  0.67783531 -0.286888430
#> 64  064 -0.17297777  0.481541834
#> 65  065 -1.30592831  0.362810338
#> 66  066  0.71233015 -0.726660355
#> 67  067 -0.74632616  0.603966350
#> 68  068  0.28283946 -0.351898374
#> 69  069 -0.29818469  0.102708007
#> 70  070  0.03009696  1.484306501
#> 71  071 -1.45906388 -1.559686917
#> 72  072  0.96260329  0.069022259
#> 73  073 -0.04769567 -1.953496739
#> 74  074 -0.44414372 -1.266562115
#> 75  075  2.10303292  2.701721250
#> 76  076 -1.27046947 -0.924007910
#> 77  077 -0.15026232  0.746869506
#> 78  078 -1.64809006 -0.925525523
#> 79  079  0.73775670  1.554536363
#> 80  080  0.40162060 -0.629337231
#> 81  081 -0.95618238 -0.718509329
#> 82  082 -0.12031536 -0.014843661
#> 83  083 -0.16779625 -0.421299868
#> 84  084  0.01639729  1.100302554
#> 85  085 -0.05787954 -0.561509907
#> 86  086 -0.36592027 -0.393480803
#> 87  087  0.02819025 -0.811578843
#> 88  088 -0.15246468 -0.397155020
#> 89  089  0.26419753 -0.804814993
#> 90  090 -2.03661020 -0.023147110
#> 91  091 -1.11839879  1.109248771
#> 92  092 -0.18735188 -0.074416212
#> 93  093  0.03640982 -0.320149922
#> 94  094  0.14930430 -0.754382219
#> 95  095  1.05762048 -0.100302843
#> 96  096 -0.09755452  0.014884445
#> 97  097  0.14358317 -1.424726414
#> 98  098  0.51396723  1.639998242
#> 99  099 -0.41867569 -1.201465103
#> 100 100 -1.69785721 -0.361777662

# 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.625563348  0.016467891
#> 2   002 -1.517829236  1.072828735
#> 3   003  1.219073753  0.551312640
#> 4   004  2.662330818  2.546616392
#> 5   005  0.864515677 -1.077347224
#> 6   006 -1.178485308 -1.402220628
#> 7   007  0.963679209  1.741132173
#> 8   008  0.523042625  0.257666565
#> 9   009 -1.586545786 -0.481157521
#> 10  010 -1.390667838 -0.869944877
#> 11  011  1.960222036  0.898188661
#> 12  012  0.762637885  0.258655483
#> 13  013  0.947011183 -0.755879813
#> 14  014 -1.158486355 -0.650286101
#> 15  015 -0.920031626  0.074744032
#> 16  016  0.291378464  0.957693367
#> 17  017 -0.781832013 -1.743293077
#> 18  018  0.061941855 -0.379614476
#> 19  019  1.191727506  0.028336940
#> 20  020  0.289159378  0.095851859
#> 21  021  0.138938706 -1.504462385
#> 22  022  1.084408755  1.620859839
#> 23  023 -0.336712507 -1.529159764
#> 24  024  1.525519839  1.104399458
#> 25  025  0.990527431  1.006541351
#> 26  026  0.442822320  0.061488474
#> 27  027 -0.763618994 -1.279826555
#> 28  028 -1.123112081 -0.066167013
#> 29  029 -1.509544237 -0.913966340
#> 30  030 -2.013655837 -0.187443873
#> 31  031  0.865343688  1.491999471
#> 32  032 -0.057851362  1.035657217
#> 33  033 -0.133584526 -1.895573855
#> 34  034  1.116988408  0.578477421
#> 35  035  0.078956352  0.513718114
#> 36  036 -0.027637389 -0.419349463
#> 37  037  0.052278926  0.501115043
#> 38  038 -0.846293295  0.402731787
#> 39  039  1.132347188  1.161166205
#> 40  040 -0.304337339 -1.011882660
#> 41  041 -1.740403572 -0.236330997
#> 42  042 -1.240382324  0.417922833
#> 43  043 -0.256879431  0.100137027
#> 44  044 -1.273354867 -1.134160505
#> 45  045 -2.046231558 -1.391196514
#> 46  046  1.254485586  0.223863315
#> 47  047 -0.364298247 -0.969278046
#> 48  048 -0.878747593  0.578893323
#> 49  049  2.320204747  0.659004140
#> 50  050  0.038354752  0.456332262
#> 51  051  0.417840727 -0.778248324
#> 52  052  0.026100165  0.893315023
#> 53  053 -1.098918011 -0.751233117
#> 54  054 -1.284410852 -0.977390281
#> 55  055 -1.025362566 -0.140833141
#> 56  056  1.584792803  0.863087005
#> 57  057 -0.305450712  0.184202677
#> 58  058 -1.713688139 -0.628286562
#> 59  059 -0.626779391 -0.487534353
#> 60  060 -0.928814305 -0.123136192
#> 61  061 -1.568939073 -2.244675779
#> 62  062  1.617951572  1.236480234
#> 63  063 -0.936064403 -0.002387546
#> 64  064  0.786278084 -0.331833554
#> 65  065 -0.830109435  0.334496634
#> 66  066  1.993157040  1.323527314
#> 67  067 -2.616959752 -0.678988069
#> 68  068 -0.185794500  1.427059391
#> 69  069 -0.260082914 -0.034627030
#> 70  070  1.843304742  0.692651490
#> 71  071 -1.132269846 -0.058975120
#> 72  072  0.441110698  0.345075776
#> 73  073  0.066080212 -1.101175151
#> 74  074 -0.653118698  1.465740606
#> 75  075 -2.021279759 -0.888441459
#> 76  076  0.128199636 -0.511403669
#> 77  077  0.323051036 -1.161673641
#> 78  078 -1.927962832  0.471923511
#> 79  079  0.637106739 -0.379229367
#> 80  080  0.320465730  0.531006244
#> 81  081 -1.043283114  0.568317506
#> 82  082  0.762188826  0.813255431
#> 83  083 -0.106344935  0.638090578
#> 84  084  0.458383721  1.722074965
#> 85  085 -0.082357003  0.168761589
#> 86  086  0.399432682  0.084116699
#> 87  087  0.831696862  2.253653556
#> 88  088 -0.047889172 -0.071434640
#> 89  089 -2.072020793  0.385817197
#> 90  090  1.427961557  0.898658169
#> 91  091  0.467355068  0.473640697
#> 92  092 -0.003988341  0.845583085
#> 93  093  1.167848822 -0.478686034
#> 94  094  0.299927591 -0.101372141
#> 95  095  1.253440936  0.579376220
#> 96  096  0.307669842 -2.839589558
#> 97  097  0.292034090 -0.791178084
#> 98  098 -0.901882504  0.436813853
#> 99  099 -1.600647091  0.496275784
#> 100 100 -0.188741364 -1.008424826