Skip to contents

Draw multivariate random variables

Usage

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.000000 0.484783
#> Y_2 0.484783 1.000000


# 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.12838360  0.9475012
#> 2 -0.59348400  0.5112166
#> 3 -1.65887980 -0.2388914
#> 4  0.26683396  0.7686479
#> 5 -0.05540859 -1.3477743

# 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.29829612  0.117807458
#> 2   002 -1.68656106  0.472614698
#> 3   003 -0.10364014 -0.521452787
#> 4   004 -0.63473544 -0.547080487
#> 5   005 -0.71250056 -1.256209626
#> 6   006 -0.42869826 -0.548565281
#> 7   007 -2.06862887 -0.268004011
#> 8   008 -0.16655503 -0.960802382
#> 9   009 -2.04515592 -2.405516476
#> 10  010 -0.02857333  0.602887319
#> 11  011  1.27221163 -0.391067725
#> 12  012 -0.18650345 -0.674508695
#> 13  013 -0.80748945 -0.828241667
#> 14  014 -1.50136795 -0.567496917
#> 15  015  1.28688336  1.054212480
#> 16  016 -0.02182392 -1.041453987
#> 17  017  0.26278357 -1.417544748
#> 18  018 -2.26007645  0.021318463
#> 19  019  1.23364114  0.191355838
#> 20  020  2.24893113  2.961238324
#> 21  021  0.35891780  1.579665784
#> 22  022 -2.61175903 -2.275740344
#> 23  023  1.54018605  1.261038115
#> 24  024  0.40677033  0.483192461
#> 25  025  0.61268283  0.258173801
#> 26  026 -1.35311046  0.079548088
#> 27  027 -0.96134064 -0.958669167
#> 28  028  0.44898349  0.792995997
#> 29  029  0.76664407 -0.063679306
#> 30  030 -1.53938707 -1.057271482
#> 31  031 -0.71531989 -1.403980258
#> 32  032  1.78106742  0.687707696
#> 33  033  0.04321225  0.884273228
#> 34  034 -1.14200176  0.312134480
#> 35  035  0.54518020  0.682420469
#> 36  036 -0.17224740  0.153297530
#> 37  037 -1.81789882 -0.594170796
#> 38  038  1.06787752  0.014563381
#> 39  039 -0.33064589  1.509686749
#> 40  040  3.06528657  0.833802291
#> 41  041 -1.29740121 -1.893245928
#> 42  042  0.03821898 -0.192253773
#> 43  043 -1.44469918  0.311616085
#> 44  044 -0.55922589 -0.083520322
#> 45  045  0.24273198  1.025376345
#> 46  046  0.71011326 -0.436132385
#> 47  047  0.39567496  1.345855639
#> 48  048 -0.08490850 -0.430136493
#> 49  049  1.03288291 -1.440668577
#> 50  050 -0.91604472  0.460219443
#> 51  051 -1.51610251  0.961042558
#> 52  052  2.34061705  1.003382848
#> 53  053 -2.90674590 -0.882590700
#> 54  054  0.17726471  1.036857887
#> 55  055 -0.69012233  0.038349018
#> 56  056 -0.56121029  1.141760844
#> 57  057  0.42063531 -0.929036660
#> 58  058 -0.03333108  0.052629896
#> 59  059  0.93540990  1.139616994
#> 60  060  0.08445617 -0.894594273
#> 61  061  0.49628544  1.090936240
#> 62  062 -1.13112473 -0.091807635
#> 63  063  1.32092331  0.042089027
#> 64  064  0.50629906  0.186320245
#> 65  065  0.38327927 -1.489549338
#> 66  066 -0.34281318 -0.251490528
#> 67  067 -1.32463940 -0.612275634
#> 68  068  0.55594877  0.445829272
#> 69  069  0.62381068 -0.427765135
#> 70  070  0.82693054 -0.064470803
#> 71  071 -1.42449178 -2.043629821
#> 72  072  0.77448184  1.029680887
#> 73  073 -0.65827436 -1.097590277
#> 74  074 -1.17124670  0.296902507
#> 75  075 -0.33835144  0.494154684
#> 76  076 -0.66201190 -1.575799310
#> 77  077 -1.36238530 -1.634980725
#> 78  078  0.60516535  1.356251901
#> 79  079  0.86582752  0.788971260
#> 80  080 -0.69190007 -0.544798543
#> 81  081  0.04769464 -0.273543564
#> 82  082  1.67499122 -0.028148852
#> 83  083  0.78823898  0.027200536
#> 84  084  0.66949102 -0.089534374
#> 85  085 -1.48814423 -1.195535579
#> 86  086 -0.70405024 -0.371813852
#> 87  087 -1.28319904 -1.515133813
#> 88  088  1.14702628  0.331219111
#> 89  089  1.52772798  1.743802775
#> 90  090 -0.18179319 -1.320658973
#> 91  091  0.54865379  0.688758802
#> 92  092  0.86317636  0.201042457
#> 93  093  0.53141915 -0.002972621
#> 94  094 -0.09552391 -1.508354790
#> 95  095  0.56878957  1.785993074
#> 96  096 -0.03635801  0.389173877
#> 97  097  1.17637572  1.055447105
#> 98  098 -0.51015438  0.141276343
#> 99  099  0.17712774  0.667333306
#> 100 100 -1.27725949 -0.572067033

# 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.290026159  1.10540174
#> 2   002  0.164329862 -0.47608270
#> 3   003  0.851791358  1.00645992
#> 4   004  0.588063635 -0.73099847
#> 5   005  1.208619892  0.64917565
#> 6   006  0.637540748 -0.64114296
#> 7   007 -0.663772535 -0.82562717
#> 8   008  1.254200228  0.04150217
#> 9   009  0.489104027  1.86044965
#> 10  010  0.305097695  0.38772540
#> 11  011  1.183941274 -0.08191250
#> 12  012 -2.203172397 -1.02889660
#> 13  013 -0.851093581 -0.15510191
#> 14  014  0.547306653  1.34737703
#> 15  015  0.039774424 -0.96242247
#> 16  016  0.930905433  0.25378977
#> 17  017 -0.953071518 -0.23210792
#> 18  018  0.928024106  1.88819225
#> 19  019 -0.474265252 -0.61993092
#> 20  020 -1.925035629 -0.41978009
#> 21  021 -0.003832519  0.67434789
#> 22  022 -0.705248552 -0.97080075
#> 23  023 -0.081650934 -0.53978347
#> 24  024 -1.377050841  0.36138069
#> 25  025  1.403377020  1.39027107
#> 26  026  0.835100907  0.04074755
#> 27  027 -0.106790210  0.14854318
#> 28  028  0.695177782 -0.18359987
#> 29  029  1.355677818  0.85329380
#> 30  030 -0.282035122  0.63619999
#> 31  031  0.369506492 -0.04604962
#> 32  032  0.863388457  0.36593433
#> 33  033  0.264000829  0.38698480
#> 34  034  0.848145083 -0.14594876
#> 35  035  0.043673141  0.52961100
#> 36  036  1.655757969  1.10471621
#> 37  037  0.397409296  0.56401012
#> 38  038 -1.095221408 -0.99965649
#> 39  039 -0.664019784  0.27334195
#> 40  040  0.092784755 -0.68580749
#> 41  041 -1.330897157 -1.67691212
#> 42  042  0.068683923  0.21461187
#> 43  043  0.598018899 -0.08135938
#> 44  044  0.459157039 -0.15454121
#> 45  045 -2.318753757 -0.11581861
#> 46  046 -0.022030804 -0.35080334
#> 47  047 -1.311222305  0.44055454
#> 48  048  1.944110200  0.97245846
#> 49  049 -0.649152093 -2.33869899
#> 50  050  0.521974253 -0.95941192
#> 51  051 -2.603303855 -0.17640907
#> 52  052  1.113083187  0.88462317
#> 53  053 -1.653344340 -0.54006082
#> 54  054  0.001914828 -0.43514902
#> 55  055 -1.099220111  0.16566450
#> 56  056 -1.247578419  0.18901931
#> 57  057  1.158939826  0.24027550
#> 58  058  2.133335658  0.21873315
#> 59  059  0.809344696  0.92431423
#> 60  060 -1.845244831 -0.33489671
#> 61  061 -1.203950776 -1.21300339
#> 62  062  0.350923904  0.21506716
#> 63  063 -0.322278684 -0.65852935
#> 64  064 -0.213619999  0.14417653
#> 65  065  0.702207162  1.04270574
#> 66  066 -2.738119311  0.35911139
#> 67  067 -0.052144738  0.62520800
#> 68  068  0.797409558  1.18340452
#> 69  069 -0.777177783 -1.61602041
#> 70  070 -0.465971880 -0.99599947
#> 71  071 -0.443936907 -2.73711899
#> 72  072 -0.023353356  1.83191029
#> 73  073  0.498734902 -0.12598006
#> 74  074  0.605186806  0.78105939
#> 75  075  1.302044594 -0.06374426
#> 76  076  0.398080912 -0.94743756
#> 77  077 -2.423960924 -1.47669565
#> 78  078  1.361453013  1.24571334
#> 79  079 -0.148290458 -0.57769495
#> 80  080 -0.220105145 -0.14047317
#> 81  081  0.384451508  1.39091550
#> 82  082 -2.704012006 -0.94949326
#> 83  083  0.562976501  0.44361519
#> 84  084 -0.451794199  1.19220443
#> 85  085  0.923717500 -0.38728026
#> 86  086 -0.483317144 -0.12061572
#> 87  087 -0.275476384  1.09418911
#> 88  088  0.782849187  0.43433679
#> 89  089 -1.064232526 -1.60970318
#> 90  090 -0.976506328 -0.99702601
#> 91  091  0.490549445  0.15169956
#> 92  092  0.123096992  0.30868909
#> 93  093  0.901299819  1.24716248
#> 94  094  0.074856304 -0.71847607
#> 95  095  1.161396945  0.41276533
#> 96  096  1.591968487 -0.25469816
#> 97  097  1.095937941 -0.46088647
#> 98  098  0.201527081  0.20388202
#> 99  099 -1.449260161  0.02094612
#> 100 100  1.076031925  0.29854743