Learn R Programming

ldt (version 0.5.3)

rand.mnormal: Generate Random Samples from a Multivariate Normal Distribution

Description

Use this function to get random samples from a multivariate normal distribution.

Usage

rand.mnormal(n, mu = NULL, sigma = NULL, p = NULL, byRow = TRUE)

Value

A list containing the generated sample (p x n), mu, and sigma.

Arguments

n

The number of samples to generate.

mu

The mean vector of the distribution. If NULL, it defaults to a zero vector of length p. If NA, it is set to a random vector.

sigma

The covariance matrix of the distribution. If NULL, it defaults to an identity matrix of size p x p. If NA, it is set to a random positive definite matrix.

p

The dimension of the distribution, if both mu and sigma are NA or NULL.

byRow

If TRUE, generated samples are stored in the rows. Otherwise, they are stored in the columns.

Examples

Run this code
s1 <- rand.mnormal(10, mu = c(0, 0), sigma = matrix(c(1, 0.5, 0.5, 1), ncol = 2))
s2 <- rand.mnormal(10, mu = c(1,1), sigma = NA, p = 2)
s3 <- rand.mnormal(10, p = 2, byRow = FALSE) #standard normal

Run the code above in your browser using DataLab