Random generation for the multivariate normal (also called Gaussian) distribution.

rmvnorm(n, mean=rep(0,d), cov=diag(d), sd, rho, d=2)

Arguments

n

sample size – number of random vectors of length d to return (as rows in a matrix).

cov

covariance or correlation matrix with d rows and columns.

d

dimension of the multivariate normal.

mean

vector of length d, or matrix with n rows and d columns.

rho

scalar, vector, or bdVector of length n, containing correlations for bivariate data. This is ignored if cov is supplied.

sd

vector of length d, or matrix with n rows and d columns, containing standard deviations. If supplied, the rows and columns of cov are multiplied by sd. In particular, if cov is a correlation matrix and sd is a vector of standard deviations, the result is a covariance matrix. If sd is a matrix then one row is used for each observation.

Value

random sample ( rmvnorm) for the multivariate normal distribution.

See also

Examples

## 5 rows and 2 independent columns 
rmvnorm(5)
#>            [,1]      [,2]
#> [1,] -0.4417995 0.2494018
#> [2,]  0.5685999 1.0728383
#> [3,]  2.1268505 2.0393693
#> [4,]  0.4248584 0.4494538
#> [5,] -1.6842815 1.3918140

## 5 rows and 3 independent columns 
rmvnorm(5, mean=c(9,3,1))
#>          [,1]     [,2]        [,3]
#> [1,] 9.426567 2.471736 -0.30511701
#> [2,] 9.107584 3.192149  0.05508794
#> [3,] 9.022295 1.853800  1.45434159
#> [4,] 9.603611 3.846185  0.14479750
#> [5,] 8.737349 3.081720  0.71310478

## 2 columns, std. dev. 1, correlation .9 
rmvnorm(5, rho=.9)
#>             [,1]       [,2]
#> [1,]  0.89496163  1.1395490
#> [2,]  0.06730444  0.4877272
#> [3,] -0.16267634  0.4297423
#> [4,] -0.82731017 -1.2326498
#> [5,]  1.87650562  1.9131639

## specify variable means and covariance matrix 
rmvnorm(5, mean=c(9,3), cov=matrix(c(4,1,1,2), 2))
#>           [,1]     [,2]
#> [1,] 11.660363 4.862553
#> [2,]  6.532060 0.543099
#> [3,]  8.493573 1.866525
#> [4,]  8.781541 2.857035
#> [5,] 11.037224 4.329606