Sample covariates from multivariate normal distributions
sample_covariates_mvtnorm.RdSamples from a multivariate normal distribution either derived from observed
data or specified directly via means plus a covariance matrix (sigma)
or standard deviations (sd).
Arguments
- data
data.frame (n x p) containing the original, observed, time-invariant covariates (ID should not be included) that will be used to inform the imputation. Can be
NULLwhenmeansand eithersigmaorsdare supplied directly. Ignored with a warning whenmeansis also provided.- means
named numeric vector of means for each covariate. When supplied, the distribution is specified directly and
datais ignored. Must be supplied together with eithersigmaorsdwhendataisNULL.- sigma
numeric matrix (p x p) giving the full covariance matrix. Takes precedence over
sdwhen both are provided. If bothsigmaandmeansare named, the matrix is reordered to match the order ofmeans; names must refer to the same set of variables.- sd
numeric vector of standard deviations. Used to construct a diagonal covariance matrix (
diag(sd^2)) whensigmais not provided. If bothsdandmeansare named,sdis reordered to match the order ofmeans; names must refer to the same set of variables.- cat_covs
character vector containing the names of the categorical covariates in orgCovs.
- n_subjects
number of simulated subjects. Defaults to
nrow(data)whendatais provided; required (no default) whendataisNULL.- exponential
sample from exponential distribution? Default
FALSE. Only applies when means/covariance are derived fromdata.- conditional
description...
- seed
integer random seed passed to
set.seed()for reproducibility. DefaultNULLdoes not set a seed.- ...
additional arguments passed to
mvrnorm()function
Details
The conditional argumentcan be specified only when data is used as input (and not withmeansandsigma or sd``). If conditional is used in conjuction with `data, it will first downsample the provided dataset to
include only patients that match the conditional. After that, it will get
the covariance matrix from the downsampled dataset, and draw samples from
it. It is therefore possible that, in contrast to e.g. bootstrap sampling,
samples are drawn that are outside of the min/max given in the conditional.
Examples
sample_covariates_mvtnorm(
means = c(WT = 70, HT = 170, AGE = 50),
sd = c(10, 20, 5),
n_subjects = 100
)
#> WT HT AGE
#> 1 55.99956 175.1063 37.81368
#> 2 69.94429 182.4311 55.74206
#> 3 51.78182 165.0535 48.77900
#> 4 67.17295 158.9260 53.14491
#> 5 90.65025 137.3802 52.56213
#> 6 51.36989 159.5597 49.73699
#> 7 75.42996 151.7185 52.34077
#> 8 73.62951 143.9091 53.68888
#> 9 88.88505 168.0511 45.32076
#> 10 69.84050 153.4642 42.43800
#> 11 79.35363 173.5298 51.21843
#> 12 86.23549 172.2408 49.33001
#> 13 50.89913 164.4153 48.43277
#> 14 80.67308 171.4007 46.80438
#> 15 69.50035 164.9703 52.22399
#> 16 97.55418 170.9306 52.88855
#> 17 71.18195 131.7656 54.31043
#> 18 67.56763 165.8783 50.09589
#> 19 70.29561 180.9966 38.62943
#> 20 96.82557 162.7756 51.06678
#> 21 80.74346 156.6982 55.56976
#> 22 67.54104 146.4487 45.12075
#> 23 80.65057 172.6334 52.44314
#> 24 53.00549 140.5853 51.42075
#> 25 83.37320 174.7339 56.59147
#> 26 75.23910 182.1350 49.45032
#> 27 71.72182 168.1935 59.62172
#> 28 82.98393 184.9758 52.78112
#> 29 64.51743 192.2107 36.93833
#> 30 68.44306 178.6778 48.09024
#> 31 74.24188 191.2620 55.24356
#> 32 69.61897 179.7230 58.36441
#> 33 66.45639 188.9270 56.58413
#> 34 67.03360 162.2557 46.07284
#> 35 59.43263 154.0892 41.21862
#> 36 63.09462 158.8292 47.31668
#> 37 72.27127 189.5691 48.95559
#> 38 56.00590 175.1707 47.79100
#> 39 75.68600 212.5370 52.12429
#> 40 53.15718 174.9880 55.36419
#> 41 90.39369 178.9891 56.95907
#> 42 74.26567 172.1517 50.11147
#> 43 76.03611 164.7470 47.35868
#> 44 71.92149 147.0760 54.23092
#> 45 70.81720 143.8977 45.27544
#> 46 74.54342 152.8959 48.56552
#> 47 78.94962 171.3461 49.18662
#> 48 61.72690 207.5301 53.83220
#> 49 79.79957 196.4356 44.40145
#> 50 75.14600 139.8180 57.66371
#> 51 74.29147 172.4421 44.30994
#> 52 64.41985 191.0508 53.38842
#> 53 70.38500 162.8724 53.91422
#> 54 78.04412 131.9988 54.67892
#> 55 66.90948 175.2613 41.04704
#> 56 62.11741 147.3396 51.81826
#> 57 67.14112 180.3534 49.48546
#> 58 60.25930 195.4134 54.80432
#> 59 77.68721 190.7186 47.63056
#> 60 57.24665 163.8876 61.05885
#> 61 59.58332 147.0695 41.62336
#> 62 85.25939 181.0837 59.96555
#> 63 68.45879 221.2882 55.31000
#> 64 81.42695 192.4768 48.01499
#> 65 61.76739 158.4223 58.81895
#> 66 71.32992 177.5300 55.69354
#> 67 82.41263 182.2418 47.85310
#> 68 83.60461 168.5829 48.63923
#> 69 45.53320 171.3097 44.50746
#> 70 63.66822 128.7269 63.24466
#> 71 58.46602 163.1872 53.93181
#> 72 57.29487 180.8428 50.37553
#> 73 75.58514 178.3081 42.73850
#> 74 79.41206 163.2213 49.62213
#> 75 70.40204 172.4860 45.00784
#> 76 82.33390 176.8085 47.63649
#> 77 77.08753 139.4208 51.18713
#> 78 56.87186 184.9406 42.18741
#> 79 70.71053 157.2093 45.77402
#> 80 76.75245 193.0675 41.56748
#> 81 60.97185 196.3527 55.50095
#> 82 82.03768 141.3746 56.91455
#> 83 70.03126 168.4423 52.20714
#> 84 71.28923 153.3957 47.48204
#> 85 58.06359 154.9655 57.27921
#> 86 61.71396 175.7955 47.59973
#> 87 63.95171 199.2022 50.74840
#> 88 55.66679 169.7939 48.93882
#> 89 60.93660 127.9570 59.46680
#> 90 60.31874 167.9479 51.19980
#> 91 70.60899 126.4485 49.41070
#> 92 71.12295 170.1577 59.38872
#> 93 91.58757 184.1943 53.83492
#> 94 66.91789 190.2400 45.40474
#> 95 75.63380 176.4497 51.83337
#> 96 81.29835 151.1700 51.08919
#> 97 84.15412 162.3253 49.12957
#> 98 67.78255 149.8094 52.40363
#> 99 86.04407 139.6995 42.91988
#> 100 78.76777 182.4826 60.56139
sample_covariates_mvtnorm(
means = c(WT = 70, HT = 170, AGE = 50),
sigma = matrix(
c(100, 20, 12,
20, 400, 10,
12, 10, 25), nrow = 3, ncol = 3),
n_subjects = 100
)
#> WT HT AGE
#> 1 66.60820 148.9017 54.61554
#> 2 81.56729 174.5902 49.03818
#> 3 78.91733 210.5774 60.51544
#> 4 52.91860 157.7187 42.78257
#> 5 74.67488 134.1570 51.68558
#> 6 70.99246 193.5062 49.21758
#> 7 60.13750 173.5251 52.44320
#> 8 74.45021 199.7830 51.18864
#> 9 90.50250 142.0521 53.12625
#> 10 84.62019 178.3669 54.46062
#> 11 80.11782 131.5899 56.02622
#> 12 69.51097 176.2669 56.21206
#> 13 58.04930 146.6899 49.21384
#> 14 79.97847 166.6721 60.85643
#> 15 67.07741 141.6554 43.75934
#> 16 86.55749 169.3551 49.16396
#> 17 68.87429 185.4191 47.35528
#> 18 70.26975 181.0856 58.61895
#> 19 85.10549 201.1492 49.83225
#> 20 87.12734 159.8511 46.97221
#> 21 67.92188 191.3253 54.13510
#> 22 88.64639 169.4610 56.22119
#> 23 53.27982 152.6629 53.74666
#> 24 67.83681 173.1934 46.49271
#> 25 59.53694 149.7447 47.20319
#> 26 81.35687 178.6753 48.74664
#> 27 69.74495 194.5549 52.00049
#> 28 66.51013 167.8152 49.85937
#> 29 73.86050 170.6733 50.35038
#> 30 79.07505 157.0559 52.13577
#> 31 67.96951 189.1009 59.08883
#> 32 64.60214 170.4804 59.45956
#> 33 57.25645 192.2025 48.38064
#> 34 66.71443 204.1266 46.20134
#> 35 59.92275 189.1329 42.26316
#> 36 60.28976 182.6156 59.64701
#> 37 62.81113 136.9230 48.84150
#> 38 62.65094 156.0048 46.87058
#> 39 83.94996 183.7862 53.81665
#> 40 53.34344 186.7913 50.05411
#> 41 76.24584 167.1104 54.03866
#> 42 58.71239 110.8801 51.63261
#> 43 61.87068 131.7145 49.10313
#> 44 78.33314 179.3223 46.67674
#> 45 75.79276 193.1124 47.16357
#> 46 63.98693 146.1913 46.80844
#> 47 68.87149 134.2048 42.48916
#> 48 75.94652 145.5276 48.27503
#> 49 73.36671 163.3622 43.90597
#> 50 61.02478 199.7140 51.46997
#> 51 87.22859 178.5485 47.03097
#> 52 52.53773 166.4838 38.45239
#> 53 70.70718 163.3284 55.51034
#> 54 68.26154 190.1029 48.91459
#> 55 85.25798 155.2693 46.46787
#> 56 66.77212 157.9127 48.95448
#> 57 81.65969 183.3483 48.07708
#> 58 59.68508 151.4454 38.35622
#> 59 55.53043 159.0176 45.86199
#> 60 67.60280 179.0953 43.48922
#> 61 68.41531 183.1509 53.72752
#> 62 79.37717 200.1036 48.02937
#> 63 77.33426 186.0006 60.04336
#> 64 49.34147 169.8333 47.09156
#> 65 64.22170 141.3192 46.61677
#> 66 66.88869 148.2729 51.15655
#> 67 66.99639 196.7647 46.75136
#> 68 76.93470 176.3295 51.60849
#> 69 58.54478 168.6736 52.41547
#> 70 78.57193 190.7142 56.56717
#> 71 59.29983 222.1845 49.25907
#> 72 64.94993 165.4159 45.18522
#> 73 78.05247 171.7078 44.54830
#> 74 79.57128 155.9976 49.59635
#> 75 76.05706 181.5535 53.33311
#> 76 61.25850 169.6448 35.67834
#> 77 59.88610 153.5106 45.71234
#> 78 76.10970 160.3775 51.63269
#> 79 75.61150 185.0107 53.66654
#> 80 64.89059 174.4340 55.14797
#> 81 89.06397 170.0387 58.13355
#> 82 63.07609 164.9974 46.05620
#> 83 66.46891 182.0659 52.95196
#> 84 61.81369 153.5181 44.46050
#> 85 88.26692 178.2530 52.82485
#> 86 61.78915 171.4544 47.76123
#> 87 86.37152 158.4351 53.94188
#> 88 68.50878 136.2540 56.27424
#> 89 42.44961 147.3556 44.03437
#> 90 63.13558 180.1685 48.09401
#> 91 60.11055 142.0573 44.79640
#> 92 64.46589 186.0049 51.29976
#> 93 87.35000 153.3040 50.85911
#> 94 64.89139 159.1275 48.95893
#> 95 45.56543 171.8265 49.81703
#> 96 85.68761 186.6950 57.17147
#> 97 69.49983 160.1977 57.17672
#> 98 66.25189 168.4967 46.94607
#> 99 62.08543 167.1177 46.61001
#> 100 70.84848 184.3605 40.99945