smvrnorm() simulates data from a multivariate normal distribution.

smvrnorm(
  n = 1,
  mu,
  sigma,
  tol = 1e-06,
  empirical = FALSE,
  eispack = FALSE,
  seed
)

Arguments

n

the number of observations to simulate

mu

a vector of means

sigma

a positive-definite symmetric matrix specifying the covariance matrix of the variables.

tol

tolerance (relative to largest variance) for numerical lack of positive-definiteness in sigma.

empirical

logical. If true, mu and sigma specify the empirical not population mean and covariance matrix.

eispack

logical. values other than FALSE result in an error

seed

set an optional seed

Value

The function returns simulated data from a multivariate normal distribution.

Details

This is a simple port and rename of mvrnorm() from the MASS package. I elect to plagiarize/port it because the MASS package conflicts with a lot of things in my workflow, especially select(). This is useful for "informal Bayes" approaches to generating quantities of interest from a regression model.

References

B. D. Ripley (1987) Stochastic Simulation. Wiley. Page 98.

Examples

M1 <- lm(mpg ~ disp + cyl, mtcars) smvrnorm(100, coef(M1), vcov(M1))
#> (Intercept) disp cyl #> [1,] 31.31195 -0.029118551 -0.83814761 #> [2,] 38.60937 -0.026992253 -1.90387374 #> [3,] 40.99990 0.000976433 -3.43414173 #> [4,] 28.48359 -0.033666680 -0.14469984 #> [5,] 30.20752 -0.034598362 -0.38059611 #> [6,] 38.01195 -0.010269545 -2.56953700 #> [7,] 33.30378 -0.024795802 -1.22966773 #> [8,] 36.57855 -0.021049000 -1.88252148 #> [9,] 27.95876 -0.026589592 -0.47204836 #> [10,] 36.32702 -0.023394274 -1.64412634 #> [11,] 30.52653 -0.020885002 -0.93836777 #> [12,] 41.09084 0.002544177 -3.39920489 #> [13,] 37.06990 -0.014911457 -2.13213903 #> [14,] 27.56352 -0.029701245 -0.33020590 #> [15,] 37.66006 -0.018357718 -2.21093982 #> [16,] 36.45667 -0.009922074 -2.28613860 #> [17,] 28.60326 -0.045715851 0.34335295 #> [18,] 31.62777 -0.021329236 -1.12687893 #> [19,] 29.99317 -0.044504022 0.12125811 #> [20,] 34.54677 -0.017677400 -1.79770677 #> [21,] 33.87942 -0.033329440 -0.98607811 #> [22,] 41.18466 -0.011230016 -2.93768190 #> [23,] 31.56178 -0.020418952 -1.08734510 #> [24,] 33.92132 -0.024528363 -1.39863227 #> [25,] 35.16884 -0.014233597 -2.07936856 #> [26,] 35.53921 -0.028659800 -1.49890126 #> [27,] 35.59803 -0.012477551 -1.98505201 #> [28,] 36.68386 -0.018285695 -1.96615832 #> [29,] 36.71898 -0.008817405 -2.32637793 #> [30,] 39.94172 -0.007168770 -2.87651263 #> [31,] 34.97916 -0.017453815 -1.68178376 #> [32,] 31.92084 -0.035062762 -0.63602071 #> [33,] 29.62006 -0.042715282 -0.05923353 #> [34,] 34.83529 -0.006378292 -2.12210647 #> [35,] 34.45467 -0.016315725 -1.67576197 #> [36,] 33.08406 -0.020467856 -1.29769780 #> [37,] 34.79163 -0.026632146 -1.39849752 #> [38,] 37.56160 -0.018513820 -2.23121534 #> [39,] 34.69617 -0.007341424 -2.01710084 #> [40,] 34.46382 -0.021177489 -1.61480062 #> [41,] 36.09671 -0.021896766 -1.81581854 #> [42,] 31.76464 -0.027507268 -0.90343193 #> [43,] 33.23828 -0.024981624 -1.26271439 #> [44,] 37.04350 -0.015493022 -2.18812032 #> [45,] 34.78892 -0.034119989 -0.98392735 #> [46,] 37.15601 -0.017751090 -2.08350041 #> [47,] 38.40107 -0.011998566 -2.52123951 #> [48,] 34.05663 -0.021965988 -1.33748930 #> [49,] 34.93979 -0.020690789 -1.67526513 #> [50,] 35.90926 -0.022055777 -1.73001615 #> [51,] 33.75420 -0.028114387 -1.26849898 #> [52,] 32.78031 -0.023853990 -1.22232115 #> [53,] 33.58576 -0.010131987 -1.89921825 #> [54,] 35.02440 -0.023900104 -1.35673964 #> [55,] 35.78827 -0.015085305 -1.89296654 #> [56,] 34.59310 -0.036729910 -1.00424253 #> [57,] 33.47848 -0.034458588 -0.86862296 #> [58,] 34.84807 -0.032277341 -1.26861915 #> [59,] 32.09024 -0.016165488 -1.37131589 #> [60,] 33.86291 -0.026025330 -1.14298019 #> [61,] 32.71275 -0.039110687 -0.35356630 #> [62,] 35.50936 -0.021467326 -1.67279887 #> [63,] 30.13142 -0.048942618 0.11910239 #> [64,] 34.07559 -0.010991752 -1.87408733 #> [65,] 30.43592 -0.029185267 -0.66189290 #> [66,] 32.98894 -0.021922119 -1.28730972 #> [67,] 35.83698 -0.013260916 -2.01926778 #> [68,] 34.89535 -0.022845405 -1.50334743 #> [69,] 35.17625 -0.025086540 -1.59730974 #> [70,] 33.79347 -0.030839078 -1.08872486 #> [71,] 36.98113 -0.022268469 -1.96441581 #> [72,] 37.41765 -0.019813942 -2.09702402 #> [73,] 34.64266 -0.005951515 -2.16653371 #> [74,] 37.24913 -0.024452082 -1.81182963 #> [75,] 31.56502 -0.024442617 -1.00969351 #> [76,] 36.28808 -0.023964492 -1.61677373 #> [77,] 35.56202 -0.021467283 -1.57394897 #> [78,] 35.82075 -0.020834208 -1.68482605 #> [79,] 30.37089 -0.032418418 -0.55555612 #> [80,] 36.63187 -0.018991060 -1.85064485 #> [81,] 33.54885 -0.030629085 -1.07916811 #> [82,] 34.99039 -0.024650266 -1.48952623 #> [83,] 32.69577 -0.036704046 -0.52629587 #> [84,] 35.26932 0.001560977 -2.46173284 #> [85,] 35.44280 -0.008901363 -2.07680301 #> [86,] 36.64995 -0.013754358 -2.28509144 #> [87,] 36.53738 -0.021464306 -1.58318750 #> [88,] 32.80064 -0.035339856 -0.83387346 #> [89,] 35.09901 -0.017235420 -1.75412312 #> [90,] 32.13571 -0.023990291 -0.98596091 #> [91,] 34.95887 -0.027877919 -1.42226747 #> [92,] 30.59559 -0.015689298 -1.05185122 #> [93,] 32.68103 -0.023042899 -1.23579633 #> [94,] 32.81399 -0.028305369 -0.91630765 #> [95,] 36.13617 -0.009710292 -2.43068148 #> [96,] 33.34523 -0.020116986 -1.47035170 #> [97,] 32.66358 -0.026009055 -1.12309193 #> [98,] 34.96208 -0.025062300 -1.57416583 #> [99,] 36.51065 -0.031971494 -1.52154802 #> [100,] 32.35252 -0.035139914 -0.69546033