mv.rnorm: Randomly Generate Multivariate Normal Data

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/mvrnorm.R

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

1
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.

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.

Value

a nxp matrix of pseuodo-random values.

Author(s)

Dustin Fife

See Also

random.correlation cor2cov

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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)

dustinfife/fifer documentation built on Oct. 31, 2020, 3:36 p.m.