Generating Multivariate Normal Distribution in R
Install Package "MASS"
Create a vector mu. mu is a vector of means.
mu=c(2,3)
Create a matrix sigma that is variance-covariance matrix of variables.
This matrix is a positive definite symmetric matrix.
sigma=matrix(c(9,6,6,16),2,2) #A 2x2 matrix
Now we can generate two variables having correlation=0.5, variance(1)=9,
Variance(2)=16, Covariance=6.
(No. of variables is order of sigma matrix i.e 2 here)
variables=mvrnorm(1000,mu,sigma)
produces 1000 observations of 2 normally distributed variables with predefined
mu and sigma
Ещё видео!