Learn R Programming

fifer (version 1.1)

mv.rnorm: Randomly Generate Multivariate Normal Data

Description

This function will randomly generate correlated multivariate normal data with specified means and covariances (or correlations). The user also has the flexibility to generate data with a randomly selected correlation matrix using the random.correlation function.

Usage

mv.rnorm(n = 1, vars = NULL, mu = NULL, Sigma = NULL, names = NULL)

Arguments

n
The sample size of the randomly generated dataset
vars
An integer indicating the number of variables. Ignored unless no Sigma is supplied.
mu
A vector of means that has the same length as the number of rows/columns of Sigma. Defaults to a vector of zeroes.
Sigma
A positive definate matrix. If NULL, the user must specify vars.
names
Optional. A vector of strings indicating the variable names.

Value

a nxp matrix of pseuodo-random values.

Details

mv.norm generates correlated multivariate normal data using a choleski decomposition. If the user does not specify Sigma, a random correlation matrix will be generated. Also, if means are not specified, the function will default to means of zero.

See Also

random.correlation cor2cov

Examples

Run this code
set.seed(2)
## generate data with correlation of .6
d = mv.rnorm(n=1000, Sigma=matrix(c(1, .6, .6, 1), 2), names=c("x", "y"))
head(d); cor(d)
## generate data with a random correlation
d = mv.rnorm(n=1000, vars=4, names=letters[1:4])
head(d); cor(d)
## generate non-scaled data
ms = c(100, 10, 5, 0) ### specify means
Sigma = matrix(c(1, .6, .5, .4,
			.6, 1, .3, .2,
			.5, .3, 1, .1,
			.4, .2, .1, 1), 4)
## convert Sigma to covariance matrix
Sigma = cor2cov(Sigma, sd=c(15, 3, 2, 1))
## generate the data
d = mv.rnorm(n=1000, mu=ms, Sigma=Sigma, names=letters[1:4])
head(d); cor(d)

Run the code above in your browser using DataLab