Skip to contents

smvrnorm() simulates data from a multivariate normal distribution.

Usage

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,]    34.44180 -0.016093590 -1.6668364
#>   [2,]    32.03398 -0.018701730 -1.2757296
#>   [3,]    37.36271 -0.020331480 -2.0825009
#>   [4,]    36.71988 -0.012678111 -2.3042494
#>   [5,]    34.98588 -0.030837751 -1.2148439
#>   [6,]    35.75183 -0.031857450 -1.3338967
#>   [7,]    36.17367 -0.006791696 -2.3472860
#>   [8,]    32.91489 -0.026496439 -1.0494384
#>   [9,]    36.29545 -0.026003203 -1.5875640
#>  [10,]    34.96627 -0.016327081 -1.7916997
#>  [11,]    30.70833 -0.034137268 -0.4632021
#>  [12,]    35.25495 -0.024852183 -1.5871001
#>  [13,]    29.67764 -0.045307263  0.1279449
#>  [14,]    33.27457 -0.024805958 -1.2678062
#>  [15,]    35.90700 -0.028802646 -1.3244104
#>  [16,]    32.70040 -0.028711966 -0.8978397
#>  [17,]    35.64675 -0.007246586 -2.2356900
#>  [18,]    35.50407 -0.023854934 -1.6782045
#>  [19,]    33.96691 -0.030668985 -1.2353990
#>  [20,]    37.86235 -0.004940957 -2.7436385
#>  [21,]    33.28914 -0.012421275 -1.7446521
#>  [22,]    34.30010 -0.028712418 -1.3577551
#>  [23,]    36.06222 -0.021116329 -1.8633183
#>  [24,]    37.46621 -0.023343183 -1.8936744
#>  [25,]    36.07328 -0.021921595 -1.8317841
#>  [26,]    36.56082 -0.019457823 -1.8880890
#>  [27,]    31.39368 -0.040165534 -0.4286164
#>  [28,]    29.33698 -0.032083182 -0.4472746
#>  [29,]    34.28392 -0.013035185 -1.8310718
#>  [30,]    33.58597 -0.023962357 -1.3969869
#>  [31,]    28.97209 -0.030077306 -0.3020914
#>  [32,]    34.96147 -0.001392552 -2.3925628
#>  [33,]    34.29185 -0.027030451 -1.3731181
#>  [34,]    30.42862 -0.020690303 -0.8481803
#>  [35,]    36.42266 -0.017422488 -1.9164967
#>  [36,]    33.56155 -0.043038754 -0.5721694
#>  [37,]    31.87355 -0.019477538 -1.2724780
#>  [38,]    36.74445 -0.016871084 -1.9450795
#>  [39,]    32.78354 -0.016504146 -1.5442419
#>  [40,]    35.39997 -0.019395674 -1.8751268
#>  [41,]    35.60957 -0.020148263 -1.6666933
#>  [42,]    37.99107  0.007172362 -3.3032306
#>  [43,]    40.85089 -0.010726402 -2.9515661
#>  [44,]    37.95101 -0.028593076 -1.7685981
#>  [45,]    33.67591 -0.022775762 -1.3301353
#>  [46,]    37.97592 -0.015515732 -2.4204318
#>  [47,]    33.96554 -0.019441511 -1.6081523
#>  [48,]    34.18454 -0.019333806 -1.4407924
#>  [49,]    34.55656 -0.025640883 -1.3469341
#>  [50,]    33.92344 -0.014969830 -1.6643016
#>  [51,]    34.34051 -0.020552987 -1.5917340
#>  [52,]    34.10286 -0.032555955 -1.0019450
#>  [53,]    35.99606 -0.016518191 -1.8187662
#>  [54,]    31.72513 -0.029475953 -0.7907692
#>  [55,]    32.81959 -0.022554931 -1.2138406
#>  [56,]    35.99504 -0.028737140 -1.5838799
#>  [57,]    33.62026 -0.012538926 -1.6514403
#>  [58,]    37.26592 -0.022992995 -1.8182772
#>  [59,]    36.14499 -0.017645647 -1.9345679
#>  [60,]    35.23342 -0.004586844 -2.2137780
#>  [61,]    31.81256 -0.043409009 -0.2694654
#>  [62,]    36.43215 -0.022729756 -1.8196314
#>  [63,]    39.28006 -0.004245869 -2.9370557
#>  [64,]    34.91233 -0.032243438 -1.1931419
#>  [65,]    31.46309 -0.015660756 -1.1197044
#>  [66,]    34.22477 -0.025875124 -1.3415898
#>  [67,]    37.68134 -0.027979933 -1.7807292
#>  [68,]    33.35864 -0.017707105 -1.4843595
#>  [69,]    33.76621 -0.021178795 -1.5660815
#>  [70,]    38.65470 -0.020472447 -2.1995093
#>  [71,]    30.48207 -0.028090660 -0.6645297
#>  [72,]    32.25134 -0.034734893 -0.6216782
#>  [73,]    30.90073 -0.028385834 -0.7434711
#>  [74,]    33.09523 -0.033242923 -0.8108385
#>  [75,]    35.14176 -0.019699942 -1.5724585
#>  [76,]    37.28685 -0.012467155 -2.4016195
#>  [77,]    36.72132 -0.008554207 -2.4195983
#>  [78,]    36.02494 -0.024415962 -1.8528437
#>  [79,]    34.11541 -0.031099366 -1.2083065
#>  [80,]    34.19654 -0.020677674 -1.4154843
#>  [81,]    30.07842 -0.025284376 -0.6767066
#>  [82,]    37.29116 -0.018444192 -2.3045275
#>  [83,]    38.45071 -0.010092197 -2.5877892
#>  [84,]    33.82277 -0.023651190 -1.3520588
#>  [85,]    34.66278 -0.014667477 -1.7998157
#>  [86,]    31.40191 -0.032412551 -0.7281680
#>  [87,]    42.25891 -0.009559488 -3.1434194
#>  [88,]    34.35595 -0.006480486 -2.1190544
#>  [89,]    36.11058 -0.010001272 -2.2122046
#>  [90,]    33.28499 -0.012173669 -1.6930537
#>  [91,]    34.92931 -0.018505094 -1.5643630
#>  [92,]    35.37642 -0.025342419 -1.5472113
#>  [93,]    35.41801 -0.013244018 -1.9558777
#>  [94,]    36.00575 -0.022021632 -1.7105708
#>  [95,]    33.56557 -0.031543846 -0.9619895
#>  [96,]    34.27518 -0.026714387 -1.2335824
#>  [97,]    36.98794 -0.003431031 -2.5075174
#>  [98,]    38.92558 -0.018050955 -2.3751259
#>  [99,]    37.27147 -0.015998570 -2.1360520
#> [100,]    30.62294 -0.033973310 -0.4645830