Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate multivariate normal data in R?

Tags:

r

I'm completing an assignment, in which I have to generate a sample X = (X1, X2) from a bivariate normal in which each marginal is N(0,1) and the correlation between X1 and X2 is 0.5.

I think the way to approach this is to use the mvrnorm function, but I'm not quite sure how to proceed after that. Any advice? Thanks in advance!

like image 839
Aspire Avatar asked Jan 31 '20 04:01

Aspire


People also ask

How do you sample a multivariate normal distribution?

Sampling from a multivariate normal It is then possible to sample from N ( μ Y , Σ Y ) by sampling and applying the affine transform on the samples. This transform is Y = L X + u where we know from the previous section that the covariance of will be Σ Y = L Σ X L ⊤ .

What is R Dmvnorm?

Description. Calculates the probability density function of the multivariate normal distribution.

What is multivariate normal distribution in statistics?

The multivariate normal distribution is a generalization of the univariate normal distribution to two or more variables. It is a distribution for random vectors of correlated variables, where each vector element has a univariate normal distribution.


2 Answers

Indeed, the mvrnorm function from the MASS package is probably your best bet. This function can generate pseudo-random data from multivariate normal distributions.

Examining the help page for this function (??mvrnorm) shows that there are three key arguments that you would need to simulate your data based your given parameters, ie:

  • n - the number of samples required (an integer);
  • mu - a vector giving the means of the variables - here, your distributions are standard normal so it will be a vector of zeros; and
  • Sigma - a positive-definite symmetric matrix specifying the covariance matrix of the variables - ie, in your case, a matrix with variance on the diagonal of ones and covariance on the off-diagonals of 0.5.

Have a look at the examples in this help page, which should help you put these ideas together!

like image 142
Spherical Avatar answered Sep 22 '22 23:09

Spherical


Here are some options:

1) mvtnorm::rmvnorm and MASS::mvrnorm work the same way, although the mvtnorm::rmvnorm function does not require that you specify the means (i.e., the default is 0). Giving names to the mu vector will specify the names of the simulated variables.

n <- 100
R <- matrix(c(1, 0.5,
              0.5, 1), 
            nrow = 2, ncol = 2)

mu <- c(X = 0, Y = 0)
mvtnorm::rmvnorm(n, mean = mu, sigma = R)
MASS::mvrnorm(n, mu = mu, Sigma = R)

2) simstandard::sim_standardized will make standardized data only, but will do so with less typing:

simstandard::sim_standardized("X ~~ 0.5 * Y", n = 100)
like image 41
Joel Schneider Avatar answered Sep 21 '22 23:09

Joel Schneider